Exemple #1
0
def test_api_load_map_compiling_try_block():
    """
    Ensures Api.load functions return errors
    """
    files = {'asd': 'foo'}
    with raises(StoryError) as e:
        Api.load_map(files)
    assert e.value.short_message() == 'E0040: No operator provided'
Exemple #2
0
def test_api_load_map_compiling_try_block():
    """
    Ensures Api.load_map functions return errors
    """
    files = {"asd": "foo ="}
    with raises(StoryError) as e:
        Api.load_map(files).check_success()
    assert e.value.short_message() == "E0007: Missing value after `=`"
Exemple #3
0
def test_api_load_map_compiling_try_block():
    """
    Ensures Api.load functions return errors
    """
    files = {'asd': 'foo ='}
    with raises(StoryError) as e:
        Api.load_map(files)
    assert e.value.short_message() == 'E0007: Missing value after `=`'
Exemple #4
0
def test_api_load_map_compiling_try_block_loads():
    """
    Ensures Api.load functions return errors
    """
    with raises(StoryError) as e:
        Api.loads('foo')
    assert e.value.short_message() == 'E0040: No operator provided'
    e.value.with_color = False
    assert e.value.message() == \
        """Error: syntax error in story at line 1, column 1
Exemple #5
0
def test_api_loads_ice():
    """
    Simulate an ICE during loads
    """
    with patch.object(Story, 'process') as p:
        p.side_effect = Exception('ICE')
        with raises(StoryError) as e:
            Api.loads('foo').check_success()
        assert e.value.message() == \
            """Internal error occured: ICE
Exemple #6
0
def test_api_load_ice():
    """
    Simulate an ICE during load
    """
    with patch.object(Story, 'from_stream') as p:
        p.side_effect = Exception('ICE')
        with raises(StoryError) as e:
            Api.load('foo')
        assert e.value.message() == \
            """Internal error occured: ICE
Exemple #7
0
def test_api_loads_ice():
    """
    Simulate an ICE during loads
    """
    with patch.object(Story, "process") as p:
        p.side_effect = Exception("ICE")
        with raises(StoryError) as e:
            Api.loads("foo").check_success()
        assert (e.value.message() == """E0001: Internal error occured: ICE
Please report at https://github.com/storyscript/storyscript/issues""")
Exemple #8
0
def test_api_load_map_compiling_try_block_loads():
    """
    Ensures Api.load functions return errors
    """
    with raises(StoryError) as e:
        Api.loads('foo =')
    assert e.value.short_message() == 'E0007: Missing value after `=`'
    e.value.with_color = False
    assert e.value.message() == \
        """Error: syntax error in story at line 1, column 6
Exemple #9
0
def test_api_load_map_ice():
    """
    Simulate an ICE during load_map
    """
    with patch.object(Bundle, 'bundle') as p:
        p.side_effect = Exception('ICE')
        with raises(StoryError) as e:
            Api.load_map({})
        assert e.value.message() == \
            """Internal error occured: ICE
Exemple #10
0
def test_api_load_map_syntax_error():
    """
    Ensures Api.load functions return errors
    """
    files = {'asd': 'foo ='}
    with raises(StoryError) as e:
        Api.load_map(files)
    assert e.value.short_message() == 'E0007: Missing value after `=`'
    e.value.with_color = False
    assert e.value.message() == \
        """Error: syntax error in story at line 1, column 6
Exemple #11
0
def test_api_loads_internal_error_debug(patch):
    """
    Ensures Api.loads handles unknown errors with debug=True
    """
    patch.init(Story)
    patch.object(Story, "process")
    patch.object(StoryError, "internal_error")
    StoryError.internal_error.return_value = Exception("ICE")
    Story.process.side_effect = Exception("An unknown error.")
    with raises(Exception) as e:
        Api.loads("string", features={"debug": True}).check_success()

    assert str(e.value) == "An unknown error."
Exemple #12
0
def test_api_loads_internal_error_debug(patch):
    """
    Ensures Api.loads handles unknown errors with debug=True
    """
    patch.init(Story)
    patch.object(Story, 'process')
    patch.object(StoryError, 'internal_error')
    StoryError.internal_error.return_value = Exception('ICE')
    Story.process.side_effect = Exception('An unknown error.')
    with raises(Exception) as e:
        Api.loads('string', features={'debug': True}).check_success()

    assert str(e.value) == 'An unknown error.'
Exemple #13
0
def test_api_load_map_internal_error_debug(patch):
    """
    Ensures Api.loads handles unknown errors with debug=True
    """
    patch.init(Bundle)
    patch.object(Bundle, 'bundle')
    patch.object(StoryError, 'internal_error')
    StoryError.internal_error.return_value = Exception('ICE')
    Bundle.bundle.side_effect = Exception('An unknown error.')
    with raises(Exception) as e:
        Api.load_map({}, debug=True).check_success()

    assert str(e.value) == 'An unknown error.'
Exemple #14
0
def test_api_loads_internal_error(patch):
    """
    Ensures Api.loads handles unknown errors
    """
    patch.init(Story)
    patch.object(Story, 'process')
    patch.object(StoryError, 'internal_error')
    StoryError.internal_error.return_value = Exception('ICE')
    Story.process.side_effect = Exception('An unknown error.')
    with raises(Exception) as e:
        Api.loads('string')

    assert str(e.value) == 'ICE'
Exemple #15
0
def test_api_load_internal_error_debug(patch, magic):
    """
    Ensures Api.load handles unknown errors with debug=True
    """
    patch.init(Story)
    patch.object(Story, 'from_stream')
    patch.object(StoryError, 'internal_error')
    StoryError.internal_error.return_value = Exception('ICE')
    Story.from_stream.side_effect = Exception('An unknown error.')
    stream = magic()
    with raises(Exception) as e:
        Api.load(stream, debug=True).check_success()

    assert str(e.value) == 'An unknown error.'
Exemple #16
0
def test_api_load_internal_error_debug(patch, magic):
    """
    Ensures Api.load handles unknown errors with debug=True
    """
    patch.init(Story)
    patch.object(Story, "from_stream")
    patch.object(StoryError, "internal_error")
    StoryError.internal_error.return_value = Exception("ICE")
    Story.from_stream.side_effect = Exception("An unknown error.")
    stream = magic()
    with raises(Exception) as e:
        Api.load(stream, features={"debug": True}).check_success()

    assert str(e.value) == "An unknown error."
Exemple #17
0
def test_compiler_int_mutation_arguments(mocker):
    """
    Test integer mutation with arguments (through mocked fake mutations)
    """
    hub = Hub('int increment a:int -> int\n'
              'int decrement b:int -> int')
    mocker.patch.object(Hub, 'instance', return_value=hub)
    source = 'a = 0.increment(a: 1).decrement(b:2)'
    result = Api.loads(source, features={'debug': True})
    result.check_success()
    result = result.result()
    assert result['tree']['1.1']['method'] == 'mutation'
    assert result['tree']['1.1']['args'] == [
        {'$OBJECT': 'int', 'int': 0},
        {
            '$OBJECT': 'mutation',
            'mutation': 'increment',
            'args': [{'$OBJECT': 'arg', 'name': 'a',
                       'arg': {'$OBJECT': 'int', 'int': 1}}]
        }
    ]
    assert result['tree']['1.2']['method'] == 'mutation'
    assert result['tree']['1.2']['args'] == [
        {'$OBJECT': 'path', 'paths': ['__p-1.1']},
        {
            '$OBJECT': 'mutation',
            'mutation': 'decrement',
            'args': [{'$OBJECT': 'arg', 'name': 'b',
                       'arg': {'$OBJECT': 'int', 'int': 2}}]
        }
    ]
Exemple #18
0
def test_compiler_mutation_chained(source):
    """
    Ensures that chained mutations are compiled correctly
    """
    result = Api.loads(source).result()
    args = [{
        '$OBJECT': 'int',
        'int': 1
    }, {
        '$OBJECT': 'mutation',
        'mutation': 'increment',
        'args': []
    }, {
        '$OBJECT':
        'mutation',
        'mutation':
        'format',
        'args': [{
            '$OBJECT': 'arg',
            'name': 'to',
            'arg': {
                '$OBJECT': 'string',
                'string': 'string'
            }
        }]
    }]
    assert result['tree']['1']['args'] == args
Exemple #19
0
def run_test(story_path):
    story_string = None
    with io.open(story_path, 'r') as f:
        story_string = f.read()

    expected_path = path.splitext(story_path)[0]
    parsed_features = parse_features(features, story_string)
    if path.isfile(expected_path + '.json'):
        expected_story = None
        with io.open(expected_path + '.json', 'r') as f:
            expected_story = f.read()

        # deserialize the expected story
        expected_story = json.loads(expected_story)
        return run_test_story(story_string, expected_story, parsed_features)

    if path.isfile(expected_path + '.error'):
        expected_output = None
        with io.open(expected_path + '.error', 'r') as f:
            expected_output = f.read().strip()

        # deserialize the expected story
        return run_fail_story(story_string, expected_output, parsed_features)

    # If no expected file has been found, print the current output to the user
    # (for handy copy&paste)
    e = Api.loads(story_string, features)
    if e.result():
        print(e.result())
    else:
        e.errors()[0].echo()
    assert 0, f'{story_path} has no expected result file.'
Exemple #20
0
def fail(source):
    """
    Expect the source code to error.
    """
    s = Api.loads(source, features=features)
    assert not s.success()
    e = s.errors()[0]
    assert isinstance(e.error, CompilerError)
Exemple #21
0
def test_functions_function_returns():
    """
    Ensures that functions with a return type are compiled correctly
    """
    result = Api.loads('function f returns int\n    return 0').result()
    assert result['tree']['1']['method'] == 'function'
    assert result['tree']['1']['function'] == 'f'
    assert result['tree']['1']['output'] == ['int']
Exemple #22
0
def test_functions_function_return():
    """
    Ensures that return statements are compiled correctly
    """
    result = Api.loads('function f returns int\n    return 0').result()
    assert result['tree']['2']['method'] == 'return'
    assert result['tree']['2']['args'] == [{'$OBJECT': 'int', 'int': 0}]
    assert result['tree']['2']['parent'] == '1'
Exemple #23
0
def test_api_loads_with_hub():
    """
    Ensures Api.load functions return errors
    """
    hub = ServiceWrapper(services=None)
    story = Api.loads("http server", backend="semantic", hub=hub)
    e = story.errors()[0]
    assert (e.short_message() ==
            "E0139: Service `http` does not exist on the hub.")
Exemple #24
0
def test_api_loads_with_scope():
    """
    Ensures Api.load functions return errors
    """
    scope = Scope.root()
    symbol = Symbol("a", IntType.instance())
    scope.insert(symbol)
    story = Api.loads("foo = a", backend="semantic", scope=scope)
    story.check_success()
Exemple #25
0
def test_api_load_ice():
    """
    Simulate an ICE during load
    """
    with patch.object(Story, "from_stream") as p:
        p.side_effect = Exception("ICE")
        s = Api.load("foo")
        e = s.errors()[0]
        assert (e.message() == """E0001: Internal error occured: ICE
Please report at https://github.com/storyscript/storyscript/issues""")
Exemple #26
0
def test_api_load_ice():
    """
    Simulate an ICE during load
    """
    with patch.object(Story, 'from_stream') as p:
        p.side_effect = Exception('ICE')
        s = Api.load('foo')
        e = s.errors()[0]
        assert e.message() == \
            """Internal error occured: ICE
Exemple #27
0
def test_api_load_map_ice():
    """
    Simulate an ICE during load_map
    """
    with patch.object(Bundle, "bundle") as p:
        p.side_effect = Exception("ICE")
        s = Api.load_map({})
        e = s.errors()[0]
        assert (e.message() == """E0001: Internal error occured: ICE
Please report at https://github.com/storyscript/storyscript/issues""")
Exemple #28
0
def test_api_load_map_ice():
    """
    Simulate an ICE during load_map
    """
    with patch.object(Bundle, 'bundle') as p:
        p.side_effect = Exception('ICE')
        s = Api.load_map({})
        e = s.errors()[0]
        assert e.message() == \
            """Internal error occured: ICE
Exemple #29
0
def test_api_loads(patch):
    """
    Ensures Api.loads can compile a story from a string
    """
    patch.init(Story)
    patch.object(Story, 'process')
    result = Api.loads('string').result()
    Story.__init__.assert_called_with('string')
    Story.process.assert_called_with()
    assert result == Story.process()
Exemple #30
0
def run_fail_story(source, expected_output, features):
    s = Api.loads(source, features)
    errors = s.errors()
    if len(errors) > 0:
        assert len(errors) == 1, 'Only one error supported for now'
        result = unstyle(errors[0].message())
        assert expected_output == result
        return

    assert 0, 'The story was expected to fail, but did not fail.'