"""
$URL: svn+ssh://svn.mems-exchange.org/repos/trunk/dulcinea/lib/test/utest_category.py $
$Id: utest_category.py 29576 2007-03-12 17:48:18Z dbinger $

"""
from sancho.utest import UTest, raises
from dulcinea.category import Category, Categorized, remove_ancestors
from qp.lib.spec import get_spec_problems, Specified

class CategoryTest (UTest):

    def check_basic (self):
        self.category = Category()
        assert str(self.category) == '*unnamed*'
        assert not self.category.name_is("ab%")
        assert not self.category.name_is("[ab]")
        assert not self.category.name_is("a\b")
        assert not self.category.name_is("a b")
        assert not self.category.name_is("a/b")
        assert not self.category.name_is(" a/b")
        assert self.category.name_is("__")
        assert self.category.name_is("34")
        assert self.category.name_is("a8")
        assert self.category.name_is("_a8")
        raises(TypeError, self.category.set_name, " ")
        self.category.set_name("boo")
        assert self.category.get_name() == "boo"
        self.category.set_label("Boo")
        assert self.category.get_label() == "Boo"
        self.category.set_description("casual")
        assert self.category.get_description() == "casual"
        assert get_spec_problems(self.category) == []

    def check_categorized (self):
        class C(Specified, Categorized):
            pass
        self.categorized = C()
        assert get_spec_problems(self.categorized) == []
        self.category1 = Category()
        self.category2 = Category()
        raises(TypeError, self.categorized.add_category, None)
        raises(TypeError, self.categorized.add_category, 42)
        self.categorized.add_category(self.category1)
        assert self.categorized.in_category(self.category1)
        assert not self.categorized.in_category(None)
        assert self.categorized.in_categories([self.category1])
        assert set(self.categorized.get_categories()) == set([self.category1])
        assert set(self.categorized.get_descendants_of(self.category1)) == set(
            [self.category1])
        assert set(
            self.categorized.get_descendants_of(self.category2)) == set([])
        assert not self.categorized.in_categories(
            [self.category1, self.category2])

    def check_ancestry (self):
        self.a = Category("a")
        self.b = Category("b")
        self.c = Category("c")
        self.d = Category("d")
        assert set(self.a.get_children()) == set([])
        assert set(self.b.get_parents()) == set([])
        self.a.add_child(self.b)
        assert set(self.a.get_children()) == set([self.b])
        assert set(self.b.get_parents()) == set([self.a])
        assert set(self.a.get_children()) == set([self.b])
        assert set(self.b.get_parents()) == set([self.a])
        raises(ValueError, self.b.add_child, self.a)
        self.b.add_child(self.c)
        assert set(self.c.get_children()) == set([])
        assert set(self.c.get_parents()) == set([self.b])
        # Now we have a -> b -> c.
        assert set(self.a.get_descendants()) == set([self.b, self.c])
        assert set(self.b.get_descendants()) == set([self.c])
        assert set(self.c.get_descendants()) == set([])
        assert self.c.is_descendant_of(self.a)
        assert self.c.is_descendant_of(self.b)
        assert self.c.is_descendant_of(self.c)
        assert self.c.is_ancestor_of(self.c)
        assert not self.d.is_ancestor_of(self.c)
        self.a.set_children([self.b, self.c])
        # a -> b, a-> c, b-> c, d-> b, d-> c
        assert self.c.is_descendant_of(self.a)
        assert self.c.is_descendant_of(self.b)
        assert self.c.is_descendant_of(self.c)
        raises(ValueError, self.a.set_parents, [self.b, self.c])
        assert self.c.is_descendant_of(self.a)
        assert self.c.is_descendant_of(self.b)
        assert self.c.is_descendant_of(self.c)
        self.a.set_parents([self.d])
        assert self.c.is_descendant_of(self.d)
        self.c.set_parents(list(self.c.get_parents()))
        assert set(list(self.a.expand())) == set([self.a, self.b, self.c])
        assert set(list(self.b.expand())) == set([self.b, self.c])
        assert set(list(self.c.expand())) == set([self.c])
        assert set(self.d.expand()) == set([self.a, self.b, self.c, self.d])
        self.a.remove_child(self.c)
        assert self.a.is_ancestor_of(self.c)
        self.b.remove_child(self.c)
        assert not self.a.is_ancestor_of(self.c)
        assert set(list(self.b.expand())) == set([self.b])
        assert set(list(self.a.expand())) == set([self.a, self.b])
        self.a.get_leaves()
        assert remove_ancestors([self.b, self.a]) == [self.b]
        assert remove_ancestors([]) == []
        assert remove_ancestors([self.a, self.b, self.c]) == [self.b, self.c]
        assert remove_ancestors([self.a, self.c]) == [self.a, self.c]

if __name__ == "__main__":
    CategoryTest()

