Beispiel #1
0
    def test_take_action_gives_correct_next_states_with_lambda_productions(
            self):
        # state.take_action() doesn't read or change these objects, it just passes them through, so
        # we'll use some sentinels to be sure of that.
        valid_actions = object()
        context_actions = object()

        state = LambdaGrammarStatelet(["t", "<s,d>"], {}, valid_actions,
                                      context_actions, is_nonterminal)
        next_state = state.take_action("<s,d> -> [lambda x, d]")
        expected_next_state = LambdaGrammarStatelet(["t", "d"],
                                                    {("s", "x"): ["d"]},
                                                    valid_actions,
                                                    context_actions,
                                                    is_nonterminal)
        assert next_state.__dict__ == expected_next_state.__dict__

        state = expected_next_state
        next_state = state.take_action("d -> [<s,r>, d]")
        expected_next_state = LambdaGrammarStatelet(
            ["t", "d", "<s,r>"],
            {("s", "x"): ["d", "<s,r>"]},
            valid_actions,
            context_actions,
            is_nonterminal,
        )
        assert next_state.__dict__ == expected_next_state.__dict__

        state = expected_next_state
        next_state = state.take_action("<s,r> -> [lambda y, r]")
        expected_next_state = LambdaGrammarStatelet(
            ["t", "d", "r"],
            {
                ("s", "x"): ["d", "r"],
                ("s", "y"): ["r"]
            },
            valid_actions,
            context_actions,
            is_nonterminal,
        )
        assert next_state.__dict__ == expected_next_state.__dict__

        state = expected_next_state
        next_state = state.take_action("r -> identity")
        expected_next_state = LambdaGrammarStatelet(["t", "d"],
                                                    {("s", "x"): ["d"]},
                                                    valid_actions,
                                                    context_actions,
                                                    is_nonterminal)
        assert next_state.__dict__ == expected_next_state.__dict__

        state = expected_next_state
        next_state = state.take_action("d -> x")
        expected_next_state = LambdaGrammarStatelet(["t"], {}, valid_actions,
                                                    context_actions,
                                                    is_nonterminal)
        assert next_state.__dict__ == expected_next_state.__dict__
Beispiel #2
0
    def test_take_action_gives_correct_next_states_with_lambda_productions(self):
        # state.take_action() doesn't read or change these objects, it just passes them through, so
        # we'll use some sentinels to be sure of that.
        valid_actions = object()
        context_actions = object()

        state = LambdaGrammarStatelet(['t', '<s,d>'], {}, valid_actions, context_actions, is_nonterminal)
        next_state = state.take_action('<s,d> -> [lambda x, d]')
        expected_next_state = LambdaGrammarStatelet(['t', 'd'],
                                                    {('s', 'x'): ['d']},
                                                    valid_actions,
                                                    context_actions,
                                                    is_nonterminal)
        assert next_state.__dict__ == expected_next_state.__dict__

        state = expected_next_state
        next_state = state.take_action('d -> [<s,r>, d]')
        expected_next_state = LambdaGrammarStatelet(['t', 'd', '<s,r>'],
                                                    {('s', 'x'): ['d', '<s,r>']},
                                                    valid_actions,
                                                    context_actions,
                                                    is_nonterminal)
        assert next_state.__dict__ == expected_next_state.__dict__

        state = expected_next_state
        next_state = state.take_action('<s,r> -> [lambda y, r]')
        expected_next_state = LambdaGrammarStatelet(['t', 'd', 'r'],
                                                    {('s', 'x'): ['d', 'r'], ('s', 'y'): ['r']},
                                                    valid_actions,
                                                    context_actions,
                                                    is_nonterminal)
        assert next_state.__dict__ == expected_next_state.__dict__

        state = expected_next_state
        next_state = state.take_action('r -> identity')
        expected_next_state = LambdaGrammarStatelet(['t', 'd'],
                                                    {('s', 'x'): ['d']},
                                                    valid_actions,
                                                    context_actions,
                                                    is_nonterminal)
        assert next_state.__dict__ == expected_next_state.__dict__

        state = expected_next_state
        next_state = state.take_action('d -> x')
        expected_next_state = LambdaGrammarStatelet(['t'],
                                                    {},
                                                    valid_actions,
                                                    context_actions,
                                                    is_nonterminal)
        assert next_state.__dict__ == expected_next_state.__dict__
Beispiel #3
0
    def test_take_action_gives_correct_next_states_with_non_lambda_productions(self):
        # state.take_action() doesn't read or change these objects, it just passes them through, so
        # we'll use some sentinels to be sure of that.
        valid_actions = object()
        context_actions = object()

        state = LambdaGrammarStatelet(['s'], {}, valid_actions, context_actions, is_nonterminal)
        next_state = state.take_action('s -> [t, r]')
        expected_next_state = LambdaGrammarStatelet(['r', 't'], {}, valid_actions, context_actions, is_nonterminal)
        assert next_state.__dict__ == expected_next_state.__dict__

        state = LambdaGrammarStatelet(['r', 't'], {}, valid_actions, context_actions, is_nonterminal)
        next_state = state.take_action('t -> identity')
        expected_next_state = LambdaGrammarStatelet(['r'], {}, valid_actions, context_actions, is_nonterminal)
        assert next_state.__dict__ == expected_next_state.__dict__
Beispiel #4
0
 def test_take_action_crashes_with_mismatched_types(self):
     with pytest.raises(AssertionError):
         state = LambdaGrammarStatelet(["s"], {}, {}, {}, is_nonterminal)
         state.take_action("t -> identity")
 def test_take_action_crashes_with_mismatched_types(self):
     with pytest.raises(AssertionError):
         state = LambdaGrammarStatelet(['s'], {}, {}, {}, is_nonterminal)
         state.take_action('t -> identity')