def parser(magic, patch): patch.init(Parser) parser = Parser() parser.algo = "lalr" parser.ebnf = None parser.lark = magic() return parser
def test_parser_parse(patch, parser): """ Ensures the build method can build the grammar """ patch.many(Parser, ['transformer']) result = parser.parse('source') parser.lark.parse.assert_called_with('source\n') Parser.transformer().transform.assert_called_with(parser.lark.parse()) assert result == Parser.transformer().transform()
def test_parser_parse(patch, parser): """ Ensures the build method can build the grammar """ patch.many(Parser, ["transformer"]) result = parser.parse("source", allow_single_quotes=False) parser.lark.parse.assert_called_with("source\n") Parser.transformer().transform.assert_called_with(parser.lark.parse()) assert result == Parser.transformer().transform()
def test_app_parse(patch, parser, read_story): """ Ensures App.parse runs Parser.parse """ patch.object(Compiler, 'compile') result = App.parse(['test.story']) App.read_story.assert_called_with('test.story') Parser.__init__.assert_called_with(ebnf_file=None) Parser().parse.assert_called_with(App.read_story()) Compiler.compile.assert_called_with(Parser().parse()) assert result == {'test.story': Compiler.compile()}
def test_story_parse(patch, story): patch.init(Parser) patch.object(Parser, 'parse') story.parse() Parser.__init__.assert_called_with(ebnf_file=None) Parser.parse.assert_called_with(story.story, debug=False) assert story.tree == Parser.parse()
def test_parser_lark(patch, parser): patch.init(Lark) patch.many(Parser, ['indenter', 'grammar']) result = parser.lark() Lark.__init__.assert_called_with(parser.grammar(), parser=parser.algo, postlex=Parser.indenter()) assert isinstance(result, Lark)
def test_app_lexer(patch, read_story): patch.init(Parser) patch.object(Parser, 'lex') patch.object(App, 'get_stories', return_value=['one.story']) result = App.lex('/path') App.read_story.assert_called_with('one.story') Parser.lex.assert_called_with(App.read_story()) assert result == {'one.story': Parser.lex()}
def test_parser_parser_unexpected_token(capsys, patch, magic, parser): patch.init(StoryError) patch.object(StoryError, 'message') patch.many(Parser, ['lark', 'transformer']) Parser.lark().parse.side_effect = UnexpectedToken(magic(), 'exp', 0, 1) with raises(SystemExit): parser.parse('source', debug=False) out, err = capsys.readouterr() assert out == '{}\n'.format(StoryError.message())
def test_parser_lark(patch, parser): """ Ensures Parser.lark can produce the correct Lark instance. """ patch.init(Lark) patch.many(Parser, ["indenter", "grammar"]) result = parser._lark() kwargs = {"parser": parser.algo, "postlex": Parser.indenter()} Lark.__init__.assert_called_with(parser.grammar(), **kwargs) assert isinstance(result, Lark)
def test_ast(magic, patch): registry = magic() context = magic() patch.object(Parser, 'parse') patch.object(ASTAnalyzer, 'try_ast') context.line = 'foo' ast = ASTAnalyzer(service_registry=registry) result = ast.complete(context) Parser.parse.assert_called_with(context.line) ASTAnalyzer.try_ast.assert_called_with(Parser.parse(), context.word, False) assert result == ASTAnalyzer.try_ast()
def test_containers_format_command_no_arguments(story): story_text = 'alpine echo\n' story.context = {} story.app.services = { 'alpine': { ServiceConstants.config: { 'actions': { 'echo': {} } } } } story.tree = Compiler.compile(Parser().parse(story_text))['tree'] assert Containers.format_command( story, story.line('1'), 'alpine', 'echo' ) == ['echo']
def test_containers_format_command(story): """ Ensures a simple resolve can be performed """ story_text = 'alpine echo msg:"foo"\n' story.context = {} story.app.services = { 'alpine': { ServiceConstants.config: { 'actions': { 'echo': { 'arguments': {'msg': {'type': 'string'}} } } } } } story.tree = Compiler.compile(Parser().parse(story_text))['tree'] assert Containers.format_command( story, story.line('1'), 'alpine', 'echo' ) == ['echo', '{"msg":"foo"}']
def test_parser_parser_unexpected_input_debug(patch, magic, parser): patch.many(Parser, ['lark', 'transformer']) Parser.lark().parse.side_effect = UnexpectedInput(magic(), 0, 0, 0) with raises(UnexpectedInput): parser.parse('source', debug=True)
def test_story_parse(patch, story, parser): story.parse(parser=parser) parser.parse.assert_called_with(story.story, allow_single_quotes=False) assert story.tree == Parser.parse()
def test_parser_transfomer(patch): patch.init(Transformer) result = Parser.transformer() assert isinstance(result, Transformer)
def test_parser_init_algo(patch): patch.object(Parser, "_lark") parser = Parser(algo="algo") assert parser.algo == "algo"
def test_parser_init(patch): patch.object(Parser, "_lark") parser = Parser() assert parser.algo == "lalr" assert parser.ebnf is None
def test_parser_parser_unexpected_token_debug(patch, magic, parser): patch.many(Parser, ['lark', 'transformer']) Parser.lark().parse.side_effect = UnexpectedToken(magic(), 'exp', 0, 1) with raises(UnexpectedToken): parser.parse('source', debug=True)
def parser(): return Parser()
def test_parser_indenter(patch): patch.init(CustomIndenter) assert isinstance(Parser.indenter(), CustomIndenter)
def test_parser_init_ebnf(patch): patch.object(Parser, "_lark") parser = Parser(ebnf="grammar.ebnf") assert parser.ebnf == "grammar.ebnf"
def test_parser_init_ebnf_file(): parser = Parser(ebnf_file='grammar.ebnf') assert parser.ebnf_file == 'grammar.ebnf'
def test_parser_init_ebnf(patch): patch.object(Parser, '_lark') parser = Parser(ebnf='grammar.ebnf') assert parser.ebnf == 'grammar.ebnf'
def test_story_parse(patch, story, parser): story.parse(parser=parser) parser.parse.assert_called_with(story.story) assert story.tree == Parser.parse()
def test_story_lex(patch, story, parser): result = story.lex(parser=parser) parser.lex.assert_called_with(story.story) assert result == Parser.lex()
def test_parser_transfomer(patch): patch.init(Transformer) result = Parser.transformer(allow_single_quotes=False) assert isinstance(result, Transformer)
def parser(patch): patch.init(Parser) patch.many(Parser, ['parse', 'lex']) return Parser()
def test_parser_lex(patch, parser): patch.many(Parser, ['lark', 'indenter']) result = parser.lex('source') Parser.lark().lex.assert_called_with('source') assert result == Parser.lark().lex()
def test_story_parse_lower(patch, story, parser): patch.object(Lowering, 'process') story.parse(parser=parser, lower=True) parser.parse.assert_called_with(story.story) Lowering.process.assert_called_with(Parser.parse()) assert story.tree == Lowering.process(Lowering.process())
def test_parser_init_algo(): parser = Parser(algo='algo') assert parser.algo == 'algo'