def test_when_generating_closure_expect_valid_items(self): # declarations: extended = self.grammar.extend() item = ParserItem.item_for(extended.rules_of(NonTerminal("E"))[0]) # when: items = Closure(item, extended).closure() # then: self.assertEqual(items, [ ParserItem.from_string('E -> . S'), ParserItem.from_string('S -> . a A') ])
def test_when_generating_state_finite_automaton_expect_valid_transitions( self): # when: transitions = StateFiniteAutomaton(self.grammar).transitions # then: self.assertEqual(len(transitions), 8) self.assertEqual( (transitions[0].source.items, transitions[0].destination.items, transitions[0].symbol), ([ ParserItem.from_string('E -> . S'), ParserItem.from_string('S -> .aA') ], [ParserItem.from_string('E -> S.')], NonTerminal("S")))
def test_when_getting_next_state_from_parser_step_expect_next_state(self): # given: action_table = ActionTable(StateFiniteAutomaton(self.grammar)) analyzer = SemanticAnalyzer(action_table, Symbol.from_string("abbbc")) # when: next_state = analyzer.next_state # then: self.assertEqual(next_state.items, [ ParserItem.from_string('S -> a.A'), ParserItem.from_string('A -> .bA'), ParserItem.from_string('A -> .c') ])
def test_when_shifting_analyzer_expect_valid_next_parser_step(self): # given: action_table = ActionTable(StateFiniteAutomaton(self.grammar)) analyzer = SemanticAnalyzer(action_table, Symbol.from_string("abbbc")) # when: next_parser_step = analyzer.shift().parser_step # then: self.assertEqual(next_parser_step.current_state.items, [ ParserItem.from_string('S -> a.A'), ParserItem.from_string('A -> .bA'), ParserItem.from_string('A -> .c') ])
def test_when_is_going_to_next_state_expect_valid_next_state(self): # declarations: extended = self.grammar.extend() item = ParserItem.item_for(extended.rules_of(NonTerminal("E"))[0]) items = Closure(item, extended).closure() state = State(items, self.grammar) # when actual = state.go_to(Symbol("a")) # then: self.assertEqual(actual.items, [ ParserItem.from_string('S -> a . A'), ParserItem.from_string('A -> . b A'), ParserItem.from_string('A -> . c') ])
def test_when_item_parser_not_represents_rule_expect_item_parser_not_equal_to_rule( self): # give: item = ParserItem.from_string("E -> S.") # when/then: self.assertFalse(item.equals_rule(Rule.from_string("S -> a A")))