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') assert e.value.message() == \ """Internal error occured: ICE
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
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""")
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
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.'
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'
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."
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}}] } ]
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.'
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
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)
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'
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']
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.")
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()
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().output() ) assert result["tree"]["1"]["method"] == "function" assert result["tree"]["1"]["function"] == "f" assert result["tree"]["1"]["output"] == ["int"]
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.'
def test_functions_function_return(): """ Ensures that return statements are compiled correctly """ result = ( Api.loads("function f returns int\n return 0").result().output() ) assert result["tree"]["2"]["method"] == "return" assert result["tree"]["2"]["args"] == [{"$OBJECT": "int", "int": 0}] assert result["tree"]["2"]["parent"] == "1"
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()
def test_functions_function(): """ Ensures that functions are compiled correctly. """ result = Api.loads('function f\n x = 0').result() assert result['tree']['1']['method'] == 'function' assert result['tree']['1']['function'] == 'f' assert result['tree']['1']['next'] == '2' assert result['tree']['2']['method'] == 'expression' assert result['tree']['2']['args'] == [{'$OBJECT': 'int', 'int': 0}] assert result['tree']['2']['parent'] == '1'
def test_api_loads(patch): """ Ensures Api.loads can compile a story from a string """ patch.init(Story) patch.init(Features) patch.object(Story, 'process') result = Api.loads('string').result() Story.__init__.assert_called_with('string', ANY) assert isinstance(Story.__init__.call_args[0][1], Features) Story.process.assert_called_with() assert result == Story.process()
def test_api_load_map_compiling_try_block_loads(): """ Ensures Api.load functions return errors """ s = Api.loads('foo =') assert len(s.deprecations()) == 0 assert len(s.warnings()) == 0 e = s.errors()[0] assert e.short_message() == 'E0007: Missing value after `=`' e.with_color = False assert e.message() == \ """Error: syntax error in story at line 1, column 6
def test_functions_function_argument(): """ Ensures that functions with an argument are compiled correctly """ result = Api.loads('function echo a:string\n x = a').result() args = [{ '$OBJECT': 'arg', 'arg': {'$OBJECT': 'type', 'type': 'string'}, 'name': 'a' }] assert result['tree']['1']['function'] == 'echo' assert result['tree']['1']['args'] == args assert result['tree']['2']['args'] == [{'$OBJECT': 'path', 'paths': ['a']}]
def test_functions_function(): """ Ensures that functions are compiled correctly. """ result = Api.loads("function f\n x = 0").result().output() assert result["tree"]["1"]["method"] == "function" assert result["tree"]["1"]["function"] == "f" assert "next" not in result["tree"]["1"] assert "next" not in result["tree"]["2"] assert result["tree"]["2"]["method"] == "expression" assert result["tree"]["2"]["args"] == [{"$OBJECT": "int", "int": 0}] assert result["tree"]["2"]["parent"] == "1"
def run(self, ws, doc): """ Run diagnostics on a document. """ log.info('Diagnostics for: %s', doc.uri) errors = [] compilation = Api.loads(doc.text()) errors = [self.to_error(e) for e in compilation.errors()] self.endpoint.notify('textDocument/publishDiagnostics', { 'uri': doc.uri, 'diagnostics': errors })
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.") s = Api.loads("string") e = s.errors()[0] assert str(e) == "ICE"
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()], })
def test_api_loads(patch, magic): """ Ensures Api.loads can compile a story from a string """ patch.init(Story) patch.init(Features) patch.object(Story, "process") Story.context = magic() result = Api.loads("string").result() Story.__init__.assert_called_with( "string", ANY, scope=None, backend="json", hub=None, ) assert isinstance(Story.__init__.call_args[0][1], Features) Story.process.assert_called_with() assert result == Story.process()
def test_functions_function_argument(): """ Ensures that functions with an argument are compiled correctly """ result = Api.loads("function echo a:string\n x = a").result().output() args = [ { "$OBJECT": "arg", "arg": {"$OBJECT": "type", "type": "string"}, "name": "a", } ] assert result["tree"]["1"]["function"] == "echo" assert result["tree"]["1"]["args"] == args assert result["tree"]["2"]["args"] == [{"$OBJECT": "path", "paths": ["a"]}]
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": [], })