Beispiel #1
0
def test_storyerror_highlight(patch, storyerror, error):
    """
    Ensures StoryError.highlight produces the correct text.
    """
    patch.many(StoryError, ["get_line", "int_line", "symbols"])
    error.column = "1"
    result = storyerror.highlight()
    highlight = StoryError.symbols()
    args = (storyerror.int_line(), StoryError.get_line().replace(), highlight)
    assert result == "{}|    {}\n{}".format(*args)
Beispiel #2
0
def test_storyerror_highlight(patch, storyerror, error):
    """
    Ensures StoryError.highlight produces the correct text.
    """
    patch.many(StoryError, ['get_line', 'int_line', 'symbols'])
    error.column = '1'
    result = storyerror.highlight()
    highlight = StoryError.symbols()
    args = (storyerror.int_line(), StoryError.get_line().replace(), highlight)
    assert result == '{}|    {}\n{}'.format(*args)
Beispiel #3
0
def test_storyerror_internal(patch):
    """
    Ensures that an internal error gets properly constructed
    """
    patch.object(StoryError, 'unnamed_error')
    e = StoryError.internal_error(Exception('ICE happened'))
    msg = (
        'Internal error occured: ICE happened\n'
        'Please report at https://github.com/storyscript/storyscript/issues')
    StoryError.unnamed_error.assert_called_with(msg)
    assert e == StoryError.internal_error(msg)
Beispiel #4
0
def test_exceptions_storyerror_compile_template_tree(patch, error):
    patch.object(StoryError, 'tree_template')
    error.item.data = 'data'
    result = error.compile_template()
    args = (error.item, error.item.line())
    StoryError.tree_template.assert_called_with(*args)
    assert result == StoryError.tree_template()
Beispiel #5
0
def test_storyerror_echo(patch, storyerror):
    """
    Ensures StoryError.echo prints StoryError.message
    """
    patch.object(click, 'echo')
    patch.object(StoryError, 'message')
    storyerror.echo()
    click.echo.assert_called_with(StoryError.message())
Beispiel #6
0
def test_storyerror_unnamed_error(patch):
    patch.init(StoryError)
    patch.init(CompilerError)
    e = StoryError.unnamed_error('Unknown error happened')
    assert isinstance(e, StoryError)
    assert CompilerError.__init__.call_count == 1
    assert isinstance(StoryError.__init__.call_args[0][0], CompilerError)
    assert StoryError.__init__.call_args[0][1] is None
Beispiel #7
0
def test_exceptions_storyerror_compile_template_dict(patch, error):
    """
    Ensures compile_template can handle dictionary items.
    """
    patch.object(StoryError, 'tree_template')
    error.item = {'value': 'value', 'line': '1'}
    result = error.compile_template()
    StoryError.tree_template.assert_called_with('value', '1')
    assert result == StoryError.tree_template()
Beispiel #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())
Beispiel #9
0
def test_storyerror_internal(patch):
    """
    Ensures that an internal error gets properly constructed
    """
    patch.init(StoryError)
    e = Exception('.ICE.')
    error = StoryError.internal_error(e)
    assert isinstance(error, StoryError)
    StoryError.__init__.assert_called_with(e, story=None)
Beispiel #10
0
def test_storyerror_internal_message(patch):
    """
    Ensures that the internal error message gets properly built
    """
    error = Exception('.ICE.')
    expected = (
        'Internal error occured: .ICE.\n'
        'Please report at https://github.com/storyscript/storyscript/issues')
    assert StoryError._internal_error(error) == expected
Beispiel #11
0
def test_storyerror_header(patch, storyerror, error):
    """
    Ensures StoryError.header returns the correct text.
    """
    patch.object(click, 'style')
    patch.object(StoryError, 'name')
    template = 'Error: syntax error in {} at line {}, column {}'
    result = storyerror.header()
    click.style.assert_called_with(StoryError.name(), bold=True)
    assert result == template.format(click.style(), error.line, error.column)
Beispiel #12
0
def test_exceptions_storyerror_compile_template_input(patch, magic, error):
    """
    Ensures compile_template can handle UnexpectedInput items.
    """
    patch.object(StoryError, 'token_template')
    error.item.context = 'context'
    result = error.compile_template()
    args = (error.item.context, error.item.line, error.item.column)
    StoryError.token_template.assert_called_with(*args)
    assert result == StoryError.token_template()
Beispiel #13
0
def test_exceptions_storyerror_compile_template_error(patch, magic, error):
    """
    Ensures compile_template can handle UnexpectedToken items.
    """
    patch.object(StoryError, 'token_template')
    error.item.token = magic()
    result = error.compile_template()
    args = (error.item.token.value, error.item.line, error.item.column)
    StoryError.token_template.assert_called_with(*args)
    assert result == StoryError.token_template()
Beispiel #14
0
def test_storyerror_create_error(patch):
    """
    Ensures that Errors without Tokens can be created
    """
    patch.init(StoryError)
    patch.init(CompilerError)
    error = StoryError.create_error('error_code')
    assert isinstance(error, StoryError)
    CompilerError.__init__.assert_called_with('error_code', format_args={})
    assert isinstance(StoryError.__init__.call_args[0][0], CompilerError)
    assert StoryError.__init__.call_args[0][1] is None
Beispiel #15
0
def test_storyerror_create_error_kwargs(patch):
    """
    Ensures that Errors without Tokens can be created and kwargs are passed on.
    """
    patch.init(StoryError)
    patch.init(CompilerError)
    error = StoryError.create_error('error_code', a=0)
    assert isinstance(error, StoryError)
    CompilerError.__init__.assert_called_with('error_code', format={'a': 0})
    assert isinstance(StoryError.__init__.call_args[0][0], CompilerError)
    assert StoryError.__init__.call_args[0][1] is None
Beispiel #16
0
def test_api_load_story_error(patch, magic):
    """
    Ensures Api.loads handles unknown errors
    """
    patch.init(Story)
    patch.object(Story, "from_stream")
    Story.from_stream.side_effect = StoryError.internal_error(".error.")
    stream = magic()
    s = Api.load(stream)
    e = s.errors()[0]

    assert e.message().startswith("E0001: Internal error occured: .error.")
Beispiel #17
0
def test_run_story_error(magic, patch):
    endpoint = magic()
    se = StoryError(None, None)
    patch.init(Story)
    patch.object(Diagnostics, 'to_error')
    patch.object(Api, 'loads')
    Api.loads().errors.return_value = [se]
    d = Diagnostics(endpoint=endpoint)
    doc = Document(uri='.my.uri.', text='a = 0')
    d.run(ws=magic(), doc=doc)
    d.to_error.assert_called_with(se)
    endpoint.notify.assert_called_with('textDocument/publishDiagnostics', {
        'uri': doc.uri,
        'diagnostics': [Diagnostics.to_error()],
    })
Beispiel #18
0
def test_run_story_error_internal(magic, patch):
    endpoint = magic()
    se = StoryError(None, None)
    patch.init(Story)
    patch.object(Diagnostics, "to_error")
    patch.object(Api, "loads")
    Api.loads().errors.return_value = [se]
    d = Diagnostics(endpoint=endpoint)
    doc = Document(uri=".my.uri.", text="a = 0")
    d.run(ws=magic(), doc=doc)
    d.to_error.assert_not_called()
    endpoint.notify.assert_called_with("textDocument/publishDiagnostics", {
        "uri": doc.uri,
        "diagnostics": [],
    })
Beispiel #19
0
def test_storyerror_init_path():
    storyerror = StoryError('error', 'story', path='hello.story')
    assert storyerror.path == 'hello.story'
Beispiel #20
0
def storyerror(error, magic):
    story = magic()
    return StoryError(error, story)
Beispiel #21
0
def test_exceptions_storyerror_message_reason(patch, error):
    patch.many(StoryError, ['compile_template', 'escape_string', 'reason'])
    error.error_type = 'else'
    result = error.message()
    assert result == '{}. Reason: {}'.format(StoryError.escape_string(),
                                             StoryError.reason())
Beispiel #22
0
def test_exceptions_storyerror_message(patch, error):
    patch.many(StoryError, ['compile_template', 'escape_string'])
    result = error.message()
    StoryError.escape_string.assert_called_with(StoryError.compile_template())
    assert result == StoryError.escape_string()
Beispiel #23
0
def test_exceptions_storyerror_str_(patch, error):
    patch.object(StoryError, 'message', return_value='pretty')
    assert str(error) == StoryError.message()
Beispiel #24
0
def test_exceptions_storyerror_escape_string(magic):
    string = magic()
    assert StoryError.escape_string(string) == string.encode().decode()
Beispiel #25
0
def error(magic):
    return StoryError('unknown', magic(spec=['line', 'column']))
Beispiel #26
0
def storyerror(error):
    return StoryError(error, 'story')
Beispiel #27
0
def test_exceptions_storyerror_init():
    error = StoryError('unknown', 'item')
    assert error.error_type == 'unknown'
    assert error.item == 'item'
    assert issubclass(StoryError, SyntaxError)
Beispiel #28
0
        result = runner.run(test.test, args=args, exit_code=expected_exit_code)

    test.compile_app.assert_called_with('my_app', debug)

    if mock_tree_as_none:
        assert result.stdout == ''
    elif no_stories_found:
        assert 'No stories found' in result.stdout
    else:
        assert 'Looking good!' in result.stdout
        assert 'a1.story' in result.stdout
        assert 'a2.story' in result.stdout


@mark.parametrize('force_compilation_error,compilation_exc',
                  [(True, StoryError('E100', 'a.story', path='foo')),
                   (True, BaseException('oh no!')), (False, None)])
@mark.parametrize('nested_dir', [True, False])
def test_compile_app(runner, patch, init_sample_app_in_cwd,
                     pre_init_cli_runner, nested_dir, force_compilation_error,
                     compilation_exc):
    app_name_for_analytics = 'my_special_app'

    patch.object(cli, 'get_asyncy_yaml', return_value='asyncy_yml_content')
    patch.object(cli, 'track')

    patch.object(click, 'echo')

    if force_compilation_error:
        patch.object(App, 'compile', side_effect=compilation_exc)
Beispiel #29
0
def test_exceptions_storyerror_compile_template(patch, error):
    patch.object(StoryError, 'token_template')
    result = error.compile_template()
    args = (error.item, error.item.line, error.item.column)
    StoryError.token_template.assert_called_with(*args)
    assert result == StoryError.token_template()