def test_init_when_name_collision_raises_exception(self): """ - Given: - - When: Instantiating a node tree with a cyclic dependency. - Then: Should throw an exception. """ a = Node("A", parent=None) b = Node("B", parent=a) self.assertRaises(NodeError, lambda: Node("B", parent=a))
def test_set_parent_node_with_name_collision_raises_error(self): """ - Given: A node tree structure. - When: Setting node parent to an existing node creating a name collision. - Then: Should raise an exception. """ root = create_node_tree() branch1 = [n for n in root.children if n.name == "BRANCH1"][0] new_leaf11 = Node("LEAF11") self.assertRaises(NodeError, lambda: new_leaf11.set_parent(branch1))
def get_test_app() -> App: """ Auxiliary method to create a test app. :return: A test app with a mock scene """ scene = Mock(wraps=Node("SCENE", children=[])) return App("test_app", scene)
def test_remove_child_node_with_non_existing_instance_raises_exception( self): """ - Given: A node tree structure. - When: Removing a child node with an non existing instance. - Then: Should raise an exception. """ root = create_node_tree() branch1 = [n for n in root.children if n.name == "BRANCH1"][0] self.assertRaises(NodeError, lambda: branch1.remove_child(Node("")))
def test_update_inactive_node_does_not_call_self_update(self): """ - Given: A node tree structure. - When: Calling update on an inactive node. - Then: Should not update self or children. """ delta = 12.1248 context = Context({}) node = Node("NODE") child = Node("CHILD", parent=node) node._update_self = Mock() child._update_self = Mock() node.active = False node.update(delta, context) node._update_self.assert_not_called() child._update_self.assert_not_called()
def test_handle_event_inactive_node_does_not_call_self_handle_event(self): """ - Given: A node tree structure. - When: Calling handle event on an inactive node. - Then: Should not handle event self or children. """ event = Event(-1, {}) context = Context({}) node = Node("NODE") child = Node("CHILD", parent=node) node._handle_event_self = Mock() child._handle_event_self = Mock() node.active = False node.handle_event(event, context) node._handle_event_self.assert_not_called() child._handle_event_self.assert_not_called()
def test_render_invisible_node_does_not_call_self_render(self): """ - Given: A node tree structure. - When: Calling render on an invisible node. - Then: Should not render self or children. """ delta = 12.1248 context = Context({}) node = Node("NODE") child = Node("CHILD", parent=node) node._render_self = Mock() child._render_self = Mock() node.visible = False node.render(delta, context) node._render_self.assert_not_called() child._render_self.assert_not_called()
def test_set_children_nodes_with_name_collision_raises_error(self): """ - Given: A node tree structure. - When: Setting node children to an existing node creating a name collision. - Then: Should raise an exception. """ root = create_node_tree() branch1 = [n for n in root.children if n.name == "BRANCH1"][0] branch2 = [n for n in root.children if n.name == "BRANCH2"][0] new_branch2 = Node("BRANCH2") self.assertRaises( NodeError, lambda: root.set_children([branch1, branch2, new_branch2]))
def test_build_node_calls_self_build(self): """ - Given: A node tree structure. - When: Calling render on a node. - Then: Should handle event self and children. """ context = Context({}) node = Node("NODE") child = Node("CHILD", parent=node) node._build_self = Mock() child._build_self = Mock() node.build(context) node._build_self.assert_called_with(context) child._build_self.assert_called_with(context)
def test_handle_event_node_calls_self_handle_event(self): """ - Given: A node tree structure. - When: Calling handle event on a node. - Then: Should handle event self and children. """ event = Event(-1, {}) context = Context({}) node = Node("NODE") child = Node("CHILD", parent=node) node._handle_event_self = Mock() child._handle_event_self = Mock() node.handle_event(event, context) node._handle_event_self.assert_called_with(event, context) child._handle_event_self.assert_called_with(event, context)
def test_init_returns_a_well_defined_node(self): """ - Given: - - When: Instantiating a node. - Then: Should create a node with correct parameters. """ node = Node("NODE_NAME") self.assertEqual(id(node), node.id) self.assertEqual(True, node.active) self.assertEqual(True, node.visible) self.assertEqual("NODE_NAME", node.name) self.assertEqual("NODE_NAME", node.path) self.assertEqual(None, node.parent) self.assertEqual(0, len(node.children))
def test_update_node_calls_self_update(self): """ - Given: A node tree structure. - When: Calling update on a node. - Then: Should update self and children. """ delta = 12.1248 context = Context({}) node = Node("NODE") child = Node("CHILD", parent=node) node._update_self = Mock() child._update_self = Mock() node.update(delta, context) node._update_self.assert_called_with(delta, context) child._update_self.assert_called_with(delta, context)
def test_render_node_calls_self_render(self): """ - Given: A node tree structure. - When: Calling render on a node. - Then: Should handle event self and children. """ delta = 12.1248 context = Context({}) node = Node("NODE") child = Node("CHILD", parent=node) node._render_self = Mock() child._render_self = Mock() node.render(delta, context) node._render_self.assert_called_with(delta, context) child._render_self.assert_called_with(delta, context)
def test_init_with_children_returns_a_well_defined_node_structure(self): """ - Given: - - When: Instantiating a node tree with children. - Then: Should create a node tree with correct parameters. """ leaf11 = Node("LEAF11") leaf12 = Node("LEAF12") leaf21 = Node("LEAF21") leaf22 = Node("LEAF22") leaf23 = Node("LEAF23") branch1 = Node("BRANCH1", children=[leaf11, leaf12]) branch2 = Node("BRANCH2", children=[leaf21, leaf22, leaf23]) root = Node("ROOT", children=[branch1, branch2]) test = Node("TEST", children=[root]) self.assertEqual(id(branch1), branch1.id) self.assertEqual(True, branch1.active) self.assertEqual(True, branch1.visible) self.assertEqual("BRANCH1", branch1.name) self.assertEqual("TEST/ROOT/BRANCH1", branch1.path) self.assertIs(root, branch1.parent) self.assertListEqual([leaf11, leaf12], branch1.children)
def test_init_with_parent_returns_a_well_defined_node_structure(self): """ - Given: - - When: Instantiating a node tree with parent. - Then: Should create a node tree with correct parameters. """ test = Node("TEST") root = Node("ROOT", parent=test) branch1 = Node("BRANCH1", parent=root) branch2 = Node("BRANCH2", parent=root) leaf11 = Node("LEAF11", parent=branch1) leaf12 = Node("LEAF12", parent=branch1) leaf21 = Node("LEAF21", parent=branch2) leaf22 = Node("LEAF22", parent=branch2) leaf23 = Node("LEAF23", parent=branch2) self.assertEqual(id(branch2), branch2.id) self.assertEqual(True, branch2.active) self.assertEqual(True, branch2.visible) self.assertEqual("BRANCH2", branch2.name) self.assertEqual("TEST/ROOT/BRANCH2", branch2.path) self.assertEqual(root, branch2.parent) self.assertListEqual([leaf21, leaf22, leaf23], branch2.children)
def create_node_tree(): """ Auxiliary method to return an already prebuild node tree. :return: A node tree with a root, 2 branches and 5 leafs. """ return Node("ROOT", children=[ Node("BRANCH1", children=[ Node("LEAF11"), Node("LEAF12"), ]), Node("BRANCH2", children=[ Node("LEAF21"), Node("LEAF22"), Node("LEAF23"), ]) ])
super()._build_self(context) self.context_ping_acc_addr = "scene.{}.ping_acc".format(self.name) context.set(self.context_ping_acc_addr, 0) def _update_self(self, delta: float, context: Context) -> None: ping_acc = context.get(self.context_ping_acc_addr) ping_acc += delta if ping_acc > self.ping_time: self._logger.info("PING") ping_acc -= self.ping_time context.set(self.context_ping_acc_addr, ping_acc) s = Node("ROOT", children=[ Node("BRANCH1", children=[ PingNode("LEAF11", 500), Node("LEAF12"), ]), Node("BRANCH2", children=[ Node("LEAF21"), Node("LEAF22"), PingNode("LEAF23", 1000), ]) ]) app = App("test_app", s, window_size=(1600, 900), fullscreen=False) app.start() app.wait(5) app.stop()