import unittest
import distutils.version

# Local
import utils

keys = [('', '_method_foo_method'),
        ('', '_signal_foo_signal'),
        ('10.0', '_method_added_method_in10'),
        ('10.0', '_signal_added_signal_in10'),
        ('', '_method_baz_method'),
        ('', '_signal_baz_signal'),
        ('', '_method_bar_method'),
        ('', '_signal_bar_signal'),
        ('2.0', '_method_new_method_in2'),
        ('2.0', '_signal_new_signal_in2'),
       ]

sortedKeys = [('', '_method_bar_method'),
              ('', '_method_baz_method'),
              ('', '_method_foo_method'),
              ('', '_signal_bar_signal'),
              ('', '_signal_baz_signal'),
              ('', '_signal_foo_signal'),
              ('2.0', '_method_new_method_in2'),
              ('2.0', '_signal_new_signal_in2'),
              ('10.0', '_method_added_method_in10'),
              ('10.0', '_signal_added_signal_in10'),
             ]

# Old sorting compare function for reference.
def old_version_cmp(a, b):
    if len(a[0]) > 0 and len(b[0]) > 0:
        va = distutils.version.LooseVersion(a[0])
        vb = distutils.version.LooseVersion(b[0])
        ret = va.__cmp__(vb)
    else:
        ret = cmp(a[0], b[0])
    if ret != 0:
        return ret
    return cmp(a[1], b[1])


class Test(unittest.TestCase):
    def test_sorting(self):
        copiedKeys = sorted(keys[:], key=utils.version_cmp_key)
        self.assertEqual(sortedKeys, copiedKeys)


if __name__ == '__main__':
    unittest.main()
