def _generate_token_list(): return [ Connector.build(TerminalSymbol.OPEN), Variable.build('x'), Connector.build(TerminalSymbol.PLUS), Variable.build('y'), Connector.build(TerminalSymbol.CLOSE) ]
def _str_to_token_list(expr): token_list = [] for char in expr: if char in TERMINAL_SYMBOL_TRANSLATIONS: token_list.append( Connector.build(TERMINAL_SYMBOL_TRANSLATIONS[char])) else: token_list.append(Variable.build(char)) return token_list
def _generate_production_from_terminal_symbols(terminal_symbols): # List of variable names to generate test variables from variable_names = ['d', 'c', 'b', 'a'] # Build Token from each supplied TerminalSymbol & add to production production = [] for ts in terminal_symbols: if ts is not TerminalSymbol.VARIABLE: production.append(Connector.build(ts)) else: production.append(Variable.build(variable_names.pop())) return production
def test_get_next_production_populated_list(): token_list = [Connector.build(TerminalSymbol.OPEN)] result = NonTerminalSymbol.EXPRESSION._get_next_production(token_list) assert result is PRODUCTIONS[NonTerminalSymbol.EXPRESSION][ TerminalSymbol.OPEN]
def test_build_returns_internal_node(): node = LeafNode.build(Connector.build(TerminalSymbol.PLUS)) assert type(node) == LeafNode
def test_is_fruitful_returns_true(): ln = LeafNode.build(Connector.build(TerminalSymbol.PLUS)) assert ln.is_fruitful()
def test_to_list_with_connector(): conn = Connector.build(TerminalSymbol.PLUS) ln = LeafNode.build(conn) assert ln.to_list() == [conn]
def test_str_with_connector(): ln = LeafNode.build(Connector.build(TerminalSymbol.PLUS)) assert str(ln) == '+'
def test_match_1_token_seq__1_token_prod_fails(): _, _, seq = _generate_1_token_symbol_sequence() prod = [Connector.build(TerminalSymbol.MINUS)] state = seq.match(prod) assert state is FAILURE
def test_connector_not_equal(): conn = Connector.build(TerminalSymbol.PLUS) conn2 = Connector.build(TerminalSymbol.MINUS) assert conn != conn2
def test_error_on_no_token_type(): with pytest.raises(ValueError): Connector.build(None)
def test_connector_str_invalid_type(): conn = Connector.build(AbstractToken()) with pytest.raises(ValueError): str(conn)
def test_connector_str(): assert str(Connector.build(TerminalSymbol.DIVIDE)) == '/'
def test_connector_type_matches(): conn = Connector.build(TerminalSymbol.MINUS) assert Connector.matches(conn, TerminalSymbol.MINUS)
def test_connector_caching(): conn = Connector.build(TerminalSymbol.PLUS) assert conn is Connector.build(TerminalSymbol.PLUS)
def test_error_on_variable_token_type(): with pytest.raises(ValueError): Connector.build(TerminalSymbol.VARIABLE)