Exemplo n.º 1
0
    def test_act__updates_state(self):
        expected = {'some': 'object'}
        target = Store(test_state)
        test_action = Action()
        test_action.act = Mock(return_value=expected)

        target.act(test_action)

        assert expected == target.state
Exemplo n.º 2
0
    def test_act__state_not_changed__subscriber_not_triggered(self):
        target = Store(test_state)
        subscriber = Mock()
        target.subscribe(subscriber)
        test_action = Action()
        test_action.act = lambda state: state

        target.act(test_action)

        subscriber.assert_not_called()
Exemplo n.º 3
0
    def test_act__state_changed__subscriber_triggered(self):
        expected = {'some': 'object'}
        target = Store(test_state)
        subscriber = Mock()
        target.subscribe(subscriber)
        test_action = Action()
        test_action.act = Mock(return_value=expected)

        target.act(test_action)

        subscriber.assert_called_once()
Exemplo n.º 4
0
    def test_act__action_gets_state(self):
        target = Store(test_state)

        test_action = Action()

        def assertion(state):
            assert test_state == state
            return state

        test_action.act = assertion

        target.act(test_action)
Exemplo n.º 5
0
    def test_act__two_middlewares__middlewares_are_called(self):
        sideeffect = Mock()

        def middleware1(next):
            def apply(action):
                sideeffect(1)
                next(action)

            return apply

        def middleware2(next):
            def apply(action):
                sideeffect(2)
                next(action)

            return apply

        target = Store(test_state, [middleware1, middleware2])
        test_action = Action()

        target.act(test_action)

        sideeffect.assert_has_calls([
            call(1),
            call(2)
        ])
Exemplo n.º 6
0
    def test_middleware__next_is_called(self):
        next = Mock()
        action = Action()
        middleware = restart_middleware_creator(None)

        middleware(next)(action)

        next.assert_called_once_with(action)
Exemplo n.º 7
0
    def test_middleware__basic_action__returns_what_next_returns(self):
        expected = {'some': 'thing'}
        next = Mock(return_value=expected)
        action = Action()
        middleware = restart_middleware_creator(None)

        actual = middleware(next)(action)

        assert expected == actual
Exemplo n.º 8
0
    def test_middleware__basic_action__restart_is_not_called(self):
        next = Mock()
        action = Action()
        restart = Mock()
        middleware = restart_middleware_creator(restart)

        middleware(next)(action)

        restart.assert_not_called()
Exemplo n.º 9
0
 def valid_actions(self) -> list:
     valid_actions = []
     for act in Action.__members__.values():
         lines = self._get_lines(action=act.get_value())
         if self._game_board.movable(lines=lines,
                                     to_left=Action.left_direction(
                                         act.get_value())):
             valid_actions.append(act.get_value())
     return valid_actions
Exemplo n.º 10
0
    def test_act__one_middleware__middleware_is_called(self):
        sideeffect = Mock()

        def middleware(next):
            def apply(action):
                sideeffect()
                next(action)

            return apply

        target = Store(test_state, [middleware])
        test_action = Action()

        target.act(test_action)

        sideeffect.assert_called_once()
Exemplo n.º 11
0
    def do_action(self, action: str):
        """
        First, extract lines according to user's input, within each line holds the coordinate of each tile
        Then, move and merge tiles
        :param action:
        :return:
        """
        lines = self._get_lines(action=action)

        self._game_board.merge_tile(
            lines=lines, merge_to_left=Action.left_direction(action))

        self._move_count += 1

        self._weighted_score = float(self._game_board.get_score()) / math.log(
            float(self._move_count + 1), 2.)
Exemplo n.º 12
0
    def test_act__returns_a_copy(self):
        target = Action()

        actual = target.act(test_state)

        assert test_state == actual
Exemplo n.º 13
0
    def test_act__returns_another_object(self):
        target = Action()

        actual = target.act(test_state)

        assert not test_state is actual