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)
def test_context_get_raises_error_if_path_contains_item(self): """ - Given: Context with test data - When: Calling ``get`` with an illegal key path. - Then: Should raise an error. """ data = get_test_data() context = Context(data) self.assertRaises(PyEngyError, lambda: context.get("root.branch2.object.bad_key"))
def test_context_get_returns_value_at_path(self): """ - Given: Context with test data - When: Calling ``get`` with an existing key path. - Then: Should return the item at given key path. """ data = get_test_data() context = Context(data) test_dict = context.get("root.branch1") self.assertIs(test_dict, data["root"]["branch1"])
def test_context_get_returns_none_if_missing_key_and_raise_if_missing_is_not_set( self): """ - Given: Context with test data - When: Calling ``get`` with a missing key path and raise if missing not set. - Then: Should return None. """ data = get_test_data() context = Context(data) self.assertEqual( None, context.get("root.branch2.missing", raise_if_missing=False))
def test_context_set_creates_internal_dicts_if_missing_key(self): """ - Given: Context with test data - When: Calling ``set`` with a missing key path. - Then: Should set the item at given key path creating any necessary intermediate dicts. """ data = get_test_data() context = Context(data) context.set("root.branch3.new_key", "new_value") self.assertEqual("new_value", data["root"]["branch3"]["new_key"])
def test_context_set_changes_value_at_path(self): """ - Given: Context with test data - When: Calling ``set`` with an existing key path. - Then: Should set the item at given key path. """ data = get_test_data() context = Context(data) context.set("root.branch1.number", 532) self.assertEqual(532, data["root"]["branch1"]["number"])
def test_context_remove_deletes_value_at_path(self): """ - Given: Context with test data - When: Calling ``remove`` with an existing key path. - Then: Should remove and return the item at given key path. """ data = get_test_data() context = Context(data) removed = context.remove("root.branch1.string") self.assertEqual("value", removed) self.assertDictEqual({"number": 23}, data["root"]["branch1"])
def test_context_get_raises_error_if_missing_key_and_raise_if_missing_is_set( self): """ - Given: Context with test data - When: Calling ``get`` with a missing key path and raise if missing set. - Then: Should raise an error. """ data = get_test_data() context = Context(data) self.assertRaises( PyEngyError, lambda: context.get("root.branch2.missing", raise_if_missing=True))
def __init__(self, name, scene: Node, window_size: Tuple[int, int] = (640, 480), fullscreen: bool = False): """ Instantiates a new PyEngy app. :param name: The name of the app. Should be unique. :param scene: The scene for the app to handle. :param window_size: The size of the window. :param fullscreen: If true, will set to fullscreen. Otherwise will set to window. """ # Create the app variables and thread self.name = name self._scene = scene self._stop_event = threading.Event() self._app = threading.Thread(target=self.__run_app, name=name, daemon=True) # Set window parameters self._screen = None self._window_size = window_size self._fullscreen = fullscreen self._background = None # Set defaults for execution variables reset_time(self.name) self._previous_time = 0.0 self._current_time = 0.0 self._logger = get_logger("", self.name) self._context = Context({})
def _build_self(self, context: Context) -> None: """ Handles the node build. :param context: Contains the context data of the application. """ self.app_name = context.get("metadata.app_name") self._logger = get_logger(self.path, self.app_name)
def test_context_init_saves_given_data(self): """ - Given: Test data - When: Initializing context with given data. - Then: Should return a context with given data. """ data = get_test_data() context = Context(data) self.assertIs(data, context.data)
def __build_context(self) -> Context: """ Auxiliary method to build the context of the app. :return: The context of the app. """ return Context({ "metadata": { "app_name": self.name }, "screen": self._screen })
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_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_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_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_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 _build_self(self, context: Context) -> None: super()._build_self(context) self.context_ping_acc_addr = "scene.{}.ping_acc".format(self.name) context.set(self.context_ping_acc_addr, 0)