def test_error_message(patch): patch.many(ErrorCodes, ["is_error", "get_error"]) ErrorCodes.is_error.return_value = True ErrorCodes.get_error.return_value = [None, "foo"] e2 = CompilerError("my_custom_error") assert str(e2) == "foo" ErrorCodes.is_error.return_value = False e3 = CompilerError(None) assert str(e3) == "Unknown compiler error"
def test_error_message(patch): patch.many(ErrorCodes, ['is_error', 'get_error']) ErrorCodes.is_error.return_value = True ErrorCodes.get_error.return_value = [None, 'foo'] e2 = CompilerError('my_custom_error') assert str(e2) == 'foo' ErrorCodes.is_error.return_value = False e3 = CompilerError(None) assert str(e3) == 'Unknown compiler error'
def function_block(self, tree, scope): if tree.function_statement.function_output: if not self.has_return(tree): t = tree.function_statement.function_output raise CompilerError('return_required', tree=t) for ret in tree.find_data('return_statement'): ret_sym, obj = self.return_statement(ret, scope) ret_type = ret_sym.type() obj.expect(ret_sym.can_write(), 'return_type_readonly', source=ret_type) node = obj # obj might not have any tokens, e.g. {} if obj.line() is None: node = ret node.expect( self.return_type.can_be_assigned(ret_type), 'return_type_differs', target=self.return_type, source=ret_type ) else: # function has no return output, so only `return` may be used for ret in tree.find_data('return_statement'): ret_sym, obj = self.return_statement(ret, scope) ret_type = ret_sym.type() # obj might not have any tokens, e.g. {} obj.expect( ret_type == NoneType.instance(), 'function_without_output_return', return_type=ret_type )
def function_block(self, tree, scope): if tree.function_statement.function_output: if not self.has_return(tree): t = tree.function_statement.function_output raise CompilerError('return_required', tree=t) for ret in tree.find_data('return_statement'): ret_type, obj = self.return_statement(ret, scope) obj.expect(self.return_type.can_be_assigned(ret_type), 'return_type_differs', target=self.return_type, source=ret_type) else: # function has no return output, so only `return` may be used for ret in tree.find_data('return_statement'): ret_type, obj = self.return_statement(ret, scope) obj.expect( ret_type == NoneType.instance(), 'function_without_output_return', )
def test_storyerror_hint_redeclared(patch, storyerror, magic): storyerror.error = CompilerError('reserved_keyword', format_args={'keyword': 'foo'}) storyerror.error_tuple = ErrorCodes.reserved_keyword assert storyerror.hint() == '`foo` is a reserved keyword'
def test_storyerror_hint_unidentified_compiler_error(storyerror, patch): patch.object(StoryError, '_internal_error') storyerror.error_tuple = ErrorCodes.unidentified_error storyerror.error = CompilerError(None) assert storyerror.hint() == storyerror._internal_error()
def test_story_modules_no_extension(magic, story): import_tree = magic() import_tree.string.child.return_value = magic(value='"hello"') story.tree = magic() story.tree.find_data.return_value = [import_tree] result = story.modules() assert result == ['hello.story'] def test_story_compile(patch, story, compiler): story.compile() Compiler.compile.assert_called_with(story.tree, story=story, features=None) assert story.compiled == Compiler.compile() @mark.parametrize('error', [StorySyntaxError('error'), CompilerError('error')]) def test_story_compiler_error(patch, story, compiler, error): """ Ensures Story.compiler uses Story.error in case of StorySyntaxError. """ Compiler.compile.side_effect = error patch.object(Story, 'error', return_value=Exception('error')) with raises(Exception): story.compile() Story.error.assert_called_with(error) 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_storyerror_hint_redeclared(patch, storyerror, magic): storyerror.error = CompilerError("reserved_keyword", format_args={"keyword": "foo"}) storyerror.error_tuple = ErrorCodes.reserved_keyword assert storyerror.hint() == "`foo` is a reserved keyword"
def test_compiler_error_extra_parameters(): e2 = CompilerError("my_custom_error", format_args={"a": 2}) assert e2.format_args.a == 2
def test_compiler_error_extra_parameters(): e2 = CompilerError('my_custom_error', format={'a': 2}) assert e2.format.a == 2