-
Notifications
You must be signed in to change notification settings - Fork 4
/
compositecore_test.py
110 lines (97 loc) · 4.75 KB
/
compositecore_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
from pygame.tests.test_utils import unittest
from compositecore import Composite, Leaf
class DummyComponent(Leaf):
def __init__(self, component_type, tags=[]):
super(DummyComponent, self).__init__()
self.component_type = component_type
self.tags = set(tags)
class TestComposition(unittest.TestCase):
def test_when_adding_component_of_existing_type_old_component_should_be_removed(self):
c = Composite()
component_first = DummyComponent("x")
component_second = DummyComponent("x")
c.set_child(component_first)
c.set_child(component_second)
self.assertFalse(c.x is component_first)
self.assertTrue(c.x is component_second)
def test_when_composite_should_be_able_to_get_children_of_tag(self):
c = Composite()
component1 = DummyComponent("x", "a")
component2 = DummyComponent("y", "a")
component3 = DummyComponent("z", "a")
component4 = DummyComponent("Q", "Q")
c.set_child(component1)
c.set_child(component2)
c.set_child(component3)
c.set_child(component4)
self.assertFalse(component4 in c.get_children_with_tag("a"))
self.assertTrue(component1 in c.get_children_with_tag("a"))
self.assertTrue(component2 in c.get_children_with_tag("a"))
self.assertTrue(component3 in c.get_children_with_tag("a"))
def test_when_a_component_is_removed_it_should_not_be_gettable_by_tag(self):
c = Composite()
component1 = DummyComponent("x", "a")
c.set_child(component1)
self.assertEqual(len(c.get_children_with_tag("a")), 1)
self.assertTrue(component1 in c.get_children_with_tag("a"))
c.remove_component(component1)
self.assertEqual(len(c.get_children_with_tag("a")), 0)
self.assertFalse(component1 in c.get_children_with_tag("a"))
def test_when_a_component_is_removed_by_type_it_should_not_be_gettable_by_tag(self):
c = Composite()
component1 = DummyComponent("x", "a")
c.set_child(component1)
self.assertEqual(len(c.get_children_with_tag("a")), 1)
self.assertTrue(component1 in c.get_children_with_tag("a"))
c.remove_component_of_type("x")
self.assertEqual(len(c.get_children_with_tag("a")), 0)
self.assertFalse(component1 in c.get_children_with_tag("a"))
def test_when_a_component_is_overwritten_it_should_not_be_gettable_by_tag(self):
c = Composite()
component1 = DummyComponent("x", "a")
component2 = DummyComponent("x", "a")
component3 = DummyComponent("x", "a")
c.set_child(component1)
c.set_child(component2)
c.set_child(component3)
self.assertEqual(len(c.get_children_with_tag("a")), 1)
self.assertFalse(component1 in c.get_children_with_tag("a"))
self.assertFalse(component2 in c.get_children_with_tag("a"))
self.assertTrue(component3 in c.get_children_with_tag("a"))
def test_get_relative_of_type_should_return_uncle_if_uncle_matches(self):
parent = Composite()
grandparent = Composite()
uncle = Composite("C")
component1 = DummyComponent("A")
component2 = DummyComponent("B")
grandparent.set_child(parent)
grandparent.set_child(uncle)
parent.set_child(component1)
parent.set_child(component2)
self.assertTrue(component1.has_relative_of_type("C"),
"No relative with component " + component1.component_type + " was found.")
self.assertTrue(component1.get_relative_of_type("C") is uncle)
def test_get_relative_of_type_should_return_sibling_if_both_uncle_and_sibling_matches(self):
parent = Composite("X")
grandparent = Composite("Y")
uncle = Composite("C")
component1 = DummyComponent("Z")
component2 = DummyComponent("W")
component3 = DummyComponent("C")
grandparent.set_child(parent)
grandparent.set_child(uncle)
parent.set_child(component1)
parent.set_child(component2)
parent.set_child(component3)
self.assertTrue(component1.has_relative_of_type("C"),
"No relative with component " + component1.component_type + " was found.")
self.assertTrue(component1.get_relative_of_type("C") is component3)
def test_get_relative_of_type_should_return_sibling_if_sibling_matches(self):
c = Composite()
component1 = DummyComponent("A")
component2 = DummyComponent("B")
c.set_child(component1)
c.set_child(component2)
self.assertTrue(component1.has_relative_of_type("B"),
"No relative with component " + component1.component_type + " was found.")
self.assertTrue(component1.get_relative_of_type("B") is component2)