#!/www/python/bin/python
"""
$URL: svn+ssh://svn.mems-exchange.org/repos/trunk/dulcinea/lib/test/test_category.py $
$Id: test_category.py 22652 2003-09-30 14:11:54Z dbinger $

Test for dulcinea.category.
"""

from sancho.unittest import TestScenario, parse_args, run_scenarios
from dulcinea.category import Category, Grouped

tested_modules = [ "dulcinea.category" ]

class CategoryTest (TestScenario):

    def check_basic (self):
        self.test_stmt('self.category = Category()')
        self.test_val('str(self.category)', '*unnamed*')
        self.test_false('self.category.check_name("ab%")')
        self.test_false('self.category.check_name("[ab]")')
        self.test_false('self.category.check_name("a\b")')
        self.test_false('self.category.check_name("a b")')
        self.test_false('self.category.check_name("a/b")')
        self.test_false('self.category.check_name(" a/b")')
        self.test_true('self.category.check_name("__")')
        self.test_true('self.category.check_name("34")')
        self.test_true('self.category.check_name("a8")')
        self.test_true('self.category.check_name("_a8")')
        self.test_exc('self.category.set_name(" ")', ValueError)
        self.test_stmt('self.category.set_name("boo")')
        self.test_val('self.category.get_name()', "boo")
        self.test_stmt('self.category.set_label("Boo")')
        self.test_val('self.category.get_label()', "Boo")
        self.test_stmt('self.category.set_description("casual")')
        self.test_val('self.category.get_description()', "casual")

    def check_grouped (self):
        self.test_stmt('self.grouped = Grouped()')
        self.test_stmt('self.category1 = Category()')
        self.test_stmt('self.category2 = Category()')
        self.test_exc('self.grouped.add_group(None)', TypeError)
        self.test_exc('self.grouped.add_group(42)', TypeError)
        self.test_stmt('self.grouped.add_group(self.category1)')
        self.test_true('self.grouped.in_group(self.category1)')
        self.test_false('self.grouped.in_group(None)')
        self.test_true('self.grouped.in_groups([self.category1])')
        self.test_set('self.grouped.get_groups()', [self.category1])
        self.test_set('self.grouped.get_descendants_of(self.category1)',
                      [self.category1])
        self.test_set('self.grouped.get_descendants_of(self.category2)',
                      [])
        self.test_false(
            'self.grouped.in_groups([self.category1, self.category2])')
        
    def check_ancestry (self):
        self.test_stmt('self.a = Category("a")')
        self.test_stmt('self.b = Category("b")')
        self.test_stmt('self.c = Category("c")')
        self.test_stmt('self.d = Category("d")')
        self.test_set('self.a.get_children()', [])
        self.test_set('self.b.get_parents()', [])
        self.test_stmt('self.a.add_child(self.b)')
        self.test_set('self.a.get_children()', [self.b])
        self.test_set('self.b.get_parents()', [self.a])
        self.test_set('self.a.get_children()', [self.b])
        self.test_set('self.b.get_parents()', [self.a])
        self.test_exc('self.b.add_child(self.a)', ValueError)
        self.test_stmt('self.b.add_child(self.c)')
        self.test_set('self.c.get_children()', [])
        self.test_set('self.c.get_parents()', [self.b])
        # Now we have a -> b -> c.
        self.test_set('self.a.get_descendants()',[self.b, self.c])
        self.test_set('self.b.get_descendants()',[self.c])
        self.test_set('self.c.get_descendants()',[])
        self.test_true('self.c.is_descendant_of(self.a)')
        self.test_true('self.c.is_descendant_of(self.b)')
        self.test_true('self.c.is_descendant_of(self.c)')
        self.test_true('self.c.is_ancestor_of(self.c)')
        self.test_false('self.d.is_ancestor_of(self.c)')
        self.test_stmt('self.a.set_children([self.b, self.c])')
        # a -> b, a-> c, b-> c, d-> b, d-> c
        self.test_true('self.c.is_descendant_of(self.a)')
        self.test_true('self.c.is_descendant_of(self.b)')
        self.test_true('self.c.is_descendant_of(self.c)')
        self.test_exc('self.a.set_parents([self.b, self.c])', ValueError)
        self.test_true('self.c.is_descendant_of(self.a)')
        self.test_true('self.c.is_descendant_of(self.b)')
        self.test_true('self.c.is_descendant_of(self.c)')
        self.test_stmt('self.a.set_parents([self.d])')
        self.test_true('self.c.is_descendant_of(self.d)')
        self.test_stmt('self.c.set_parents(list(self.c.get_parents()))')
        self.test_set('list(self.a.expand())', [self.a, self.b, self.c])
        self.test_set('list(self.b.expand())', [self.b, self.c])
        self.test_set('list(self.c.expand())', [self.c])
        self.test_set('list(self.d.expand())',
                      [self.a, self.b, self.c, self.d])
        self.test_stmt('self.a.remove_child(self.c)')
        self.test_true('self.a.is_ancestor_of(self.c)')
        self.test_stmt('self.b.remove_child(self.c)')
        self.test_false('self.a.is_ancestor_of(self.c)')
        self.test_set('list(self.b.expand())', [self.b])
        self.test_set('list(self.a.expand())', [self.a, self.b])
        self.test_stmt('self.a.get_leaves()')

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

