#!/www/python/bin/python

"""$URL: svn+ssh://svn.mems-exchange.org/repos/trunk/dulcinea/lib/test/test_sort.py $
$Id: test_sort.py 23444 2004-02-06 15:59:49Z dbinger $

Test for dulcinea.sort.
"""

from __future__ import generators

from sancho.unittest import TestScenario, parse_args, run_scenarios
from dulcinea.sort import sort, str_sort, lexical_sort, \
     attr_sort, lex_attr_sort, \
     method_sort, function_sort, natural_sort

tested_modules = [ "dulcinea.sort" ]

class Thing:
    counter = 0

    def __init__(self, name):
        self.name = name
        Thing.counter += 1
        self.id = "T-%03d" % Thing.counter

    def __repr__(self):
        return "<Thing: %s>" % self.id

    def __str__(self):
        return self.name

    def __cmp__(self, other):
        return cmp(self.id, other.id)

    def get_id(self):
        return self.id


def gen(*args):
    for arg in args:
        yield arg


class SortTest (TestScenario):

    def check_sort(self):
        words = ["foo", "BAR", "aBc-123", "blah!", "123-abc"]
        sorted = ["123-abc", "BAR", "aBc-123", "blah!", "foo"]
        self.test_val("sort(gen(*words))", sorted)
        words2 = words[:]
        self.test_val("sort(words2)", sorted)
        self.test_val("sort(words)", sorted)
        self.test_val("words", sorted)

    def check_instances(self):
        thing1 = Thing("bob")
        thing2 = Thing("Tim")
        thing3 = Thing("123-abc")
        things = [thing2, thing1, thing3]
        self.test_val("sort(gen(thing2, thing1, thing3))",
                      [thing1, thing2, thing3])
        self.test_val("str_sort(things)",
                      [thing3, thing2, thing1])
        self.test_val("lexical_sort(gen(thing1, thing2, thing3))",
                      [thing3, thing1, thing2])
        self.test_val("attr_sort(things, 'id')",
                      [thing1, thing2, thing3])
        self.test_val("attr_sort(things, 'name')",
                      [thing3, thing2, thing1])
        self.test_val("lex_attr_sort(things, 'name')",
                      [thing3, thing1, thing2])
        self.test_val("method_sort(gen(thing2, thing1, thing3), 'get_id')",
                      [thing1, thing2, thing3])
        def fun_get_id(x):
            return x.get_id()
        self.test_val("function_sort(gen(thing2, thing1, thing3), fun_get_id)",
                      [thing1, thing2, thing3])

    def check_natural(self):
        nums = [1, 5, 9, 10, 15, 105]
        self.test_val("str_sort(gen(*nums))", [1, 10, 105, 15, 5, 9])
        nums = map(str, nums)
        self.test_val("sort(nums[:])", ["1", "10", "105", "15", "5", "9"])
        self.test_val("natural_sort(nums)", nums)

        # Check that the promises made in the docstring hold.
        l = ["a1b", "a1", "a20", "a", "a0", "a1a", "a10", "a2"]
        self.test_val("natural_sort(l)",
                      ["a", "a0", "a1", "a1a", "a1b", "a2", "a10", "a20"])
        l = ["x2-y7", "x8-y8", "x2-g8", "x2-y08"]
        self.test_val("natural_sort(l)",
                      ["x2-g8", "x2-y7", "x2-y08", "x8-y8"])
        l = ["1.02", "1.001", "1.3", "1.002", "1.1", "1.010"]
        self.test_val("natural_sort(l)",
                      ["1.001", "1.002", "1.010", "1.02", "1.1", "1.3"])


if __name__ == "__main__":
    (scenarios, options) = parse_args()
    run_scenarios(scenarios, options)
