コード例 #1
0
ファイル: Parser.py プロジェクト: wilzbach/storyscript
def parser(magic, patch):
    patch.init(Parser)
    parser = Parser()
    parser.algo = "lalr"
    parser.ebnf = None
    parser.lark = magic()
    return parser
コード例 #2
0
ファイル: Parser.py プロジェクト: rohit121/storyscript
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()
コード例 #3
0
ファイル: Parser.py プロジェクト: wilzbach/storyscript
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()
コード例 #4
0
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()}
コード例 #5
0
ファイル: story.py プロジェクト: Laeeth/storyscript
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()
コード例 #6
0
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)
コード例 #7
0
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()}
コード例 #8
0
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())
コード例 #9
0
ファイル: Parser.py プロジェクト: wilzbach/storyscript
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)
コード例 #10
0
ファイル: ast.py プロジェクト: Happy-Ferret/sls
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()
コード例 #11
0
ファイル: Containers.py プロジェクト: zeebonk/platform-engine
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']
コード例 #12
0
ファイル: Containers.py プロジェクト: zeebonk/platform-engine
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"}']
コード例 #13
0
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)
コード例 #14
0
ファイル: Story.py プロジェクト: markthomas93/storyscript
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()
コード例 #15
0
ファイル: Parser.py プロジェクト: rohit121/storyscript
def test_parser_transfomer(patch):
    patch.init(Transformer)
    result = Parser.transformer()
    assert isinstance(result, Transformer)
コード例 #16
0
ファイル: Parser.py プロジェクト: wilzbach/storyscript
def test_parser_init_algo(patch):
    patch.object(Parser, "_lark")
    parser = Parser(algo="algo")
    assert parser.algo == "algo"
コード例 #17
0
ファイル: Parser.py プロジェクト: wilzbach/storyscript
def test_parser_init(patch):
    patch.object(Parser, "_lark")
    parser = Parser()
    assert parser.algo == "lalr"
    assert parser.ebnf is None
コード例 #18
0
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)
コード例 #19
0
ファイル: conftest.py プロジェクト: nomorepanic/storyscript
def parser():
    return Parser()
コード例 #20
0
ファイル: Parser.py プロジェクト: wilzbach/storyscript
def test_parser_indenter(patch):
    patch.init(CustomIndenter)
    assert isinstance(Parser.indenter(), CustomIndenter)
コード例 #21
0
ファイル: Parser.py プロジェクト: wilzbach/storyscript
def test_parser_init_ebnf(patch):
    patch.object(Parser, "_lark")
    parser = Parser(ebnf="grammar.ebnf")
    assert parser.ebnf == "grammar.ebnf"
コード例 #22
0
def test_parser_init_ebnf_file():
    parser = Parser(ebnf_file='grammar.ebnf')
    assert parser.ebnf_file == 'grammar.ebnf'
コード例 #23
0
ファイル: Parser.py プロジェクト: rohit121/storyscript
def test_parser_init_ebnf(patch):
    patch.object(Parser, '_lark')
    parser = Parser(ebnf='grammar.ebnf')
    assert parser.ebnf == 'grammar.ebnf'
コード例 #24
0
def test_story_parse(patch, story, parser):
    story.parse(parser=parser)
    parser.parse.assert_called_with(story.story)
    assert story.tree == Parser.parse()
コード例 #25
0
def test_story_lex(patch, story, parser):
    result = story.lex(parser=parser)
    parser.lex.assert_called_with(story.story)
    assert result == Parser.lex()
コード例 #26
0
ファイル: Parser.py プロジェクト: wilzbach/storyscript
def test_parser_transfomer(patch):
    patch.init(Transformer)
    result = Parser.transformer(allow_single_quotes=False)
    assert isinstance(result, Transformer)
コード例 #27
0
def parser(patch):
    patch.init(Parser)
    patch.many(Parser, ['parse', 'lex'])
    return Parser()
コード例 #28
0
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()
コード例 #29
0
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())
コード例 #30
0
def test_parser_init_algo():
    parser = Parser(algo='algo')
    assert parser.algo == 'algo'