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() action_indices = object() state = GrammarState(['t', '<s,d>'], {}, valid_actions, action_indices, is_nonterminal) next_state = state.take_action('<s,d> -> [lambda x, d]') expected_next_state = GrammarState(['t', 'd'], {('s', 'x'): ['d']}, valid_actions, action_indices, 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 = GrammarState(['t', 'd', '<s,r>'], {('s', 'x'): ['d', '<s,r>']}, valid_actions, action_indices, 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 = GrammarState(['t', 'd', 'r'], {('s', 'x'): ['d', 'r'], ('s', 'y'): ['r']}, valid_actions, action_indices, is_nonterminal) assert next_state.__dict__ == expected_next_state.__dict__ state = expected_next_state next_state = state.take_action('r -> identity') expected_next_state = GrammarState(['t', 'd'], {('s', 'x'): ['d']}, valid_actions, action_indices, is_nonterminal) assert next_state.__dict__ == expected_next_state.__dict__ state = expected_next_state next_state = state.take_action('d -> x') expected_next_state = GrammarState(['t'], {}, valid_actions, action_indices, is_nonterminal) assert next_state.__dict__ == expected_next_state.__dict__
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() action_indices = object() state = GrammarState(['s'], {}, valid_actions, action_indices, is_nonterminal) next_state = state.take_action('s -> [t, r]') expected_next_state = GrammarState(['r', 't'], {}, valid_actions, action_indices, is_nonterminal) assert next_state.__dict__ == expected_next_state.__dict__ state = GrammarState(['r', 't'], {}, valid_actions, action_indices, is_nonterminal) next_state = state.take_action('t -> identity') expected_next_state = GrammarState(['r'], {}, valid_actions, action_indices, is_nonterminal) assert next_state.__dict__ == expected_next_state.__dict__
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() action_indices = object() state = GrammarState([u't', u'<s,d>'], {}, valid_actions, action_indices, is_nonterminal) next_state = state.take_action(u'<s,d> -> [lambda x, d]') expected_next_state = GrammarState([u't', u'd'], {(u's', u'x'): [u'd']}, valid_actions, action_indices, is_nonterminal) assert next_state.__dict__ == expected_next_state.__dict__ state = expected_next_state next_state = state.take_action(u'd -> [<s,r>, d]') expected_next_state = GrammarState([u't', u'd', u'<s,r>'], {(u's', u'x'): [u'd', u'<s,r>']}, valid_actions, action_indices, is_nonterminal) assert next_state.__dict__ == expected_next_state.__dict__ state = expected_next_state next_state = state.take_action(u'<s,r> -> [lambda y, r]') expected_next_state = GrammarState([u't', u'd', u'r'], { (u's', u'x'): [u'd', u'r'], (u's', u'y'): [u'r'] }, valid_actions, action_indices, is_nonterminal) assert next_state.__dict__ == expected_next_state.__dict__ state = expected_next_state next_state = state.take_action(u'r -> identity') expected_next_state = GrammarState([u't', u'd'], {(u's', u'x'): [u'd']}, valid_actions, action_indices, is_nonterminal) assert next_state.__dict__ == expected_next_state.__dict__ state = expected_next_state next_state = state.take_action(u'd -> x') expected_next_state = GrammarState([u't'], {}, valid_actions, action_indices, is_nonterminal) assert next_state.__dict__ == expected_next_state.__dict__
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 = GrammarState(['s'], {}, valid_actions, context_actions, is_nonterminal) next_state = state.take_action('s -> [t, r]') expected_next_state = GrammarState(['r', 't'], {}, valid_actions, context_actions, is_nonterminal) assert next_state.__dict__ == expected_next_state.__dict__ state = GrammarState(['r', 't'], {}, valid_actions, context_actions, is_nonterminal) next_state = state.take_action('t -> identity') expected_next_state = GrammarState(['r'], {}, valid_actions, context_actions, is_nonterminal) assert next_state.__dict__ == expected_next_state.__dict__
def test_take_action_crashes_with_mismatched_types(self): with pytest.raises(AssertionError): state = GrammarState(['s'], {}, {}, {}, is_nonterminal) state.take_action('t -> identity')