def test_two_lines(self): ''' Call find_column with two-line string. ''' input_data = ('1 2 3\n' '4 5 6\n') pos = 8 real_column = parse.find_column(input_data, pos) expected_column = 2 misc.assert_equal(self, expected_column, real_column)
def test_class_type_decl(self): ''' Parse class decl. ''' input_string = ( 'class MyClass {\n' ' store {\n' ' field1 Int\n' ' field2 Float\n' ' }\n' '}\n' ) real_ast = _parse(input_string) expected_ast = ast.Module( decl_list=[ ast.ClassDecl( name='MyClass', field_list=[ ast.Field( name='field1', datatype=datatype.SimpleDataType('Int'), ), ast.Field( name='field2', datatype=datatype.SimpleDataType('Float'), ) ] ) ] ) misc.assert_equal(self, expected_ast, real_ast)
def check_translation(test_case, input_ast, expected_output): ''' Small helper func. ''' input_ast.ident_list = ident_table.ident_table(input_ast) input_ast = datatype.mark_out_datatypes(ast_=input_ast) generator_ = generator.Generator(input_ast) real_output = generator_.generate() misc.assert_equal(test_case, textwrap.dedent(expected_output), real_output)
def test_simple_return_1(self): ''' Parse return stmt with integer. ''' input_string = 'func start { return 1 }' real_ast = _parse(input_string) expected_ast = _std_module() expected_ast.decl_list[0].body.append(ast.Return(expr=ast.Number(1)), ) misc.assert_equal(self, expected_ast, real_ast)
def test_func_body_2_empty_blocks(self): ''' Parse fnction with empty blocks. ''' input_string = 'func start { {} {} }' real_ast = _parse(input_string) expected_ast = _std_module() expected_ast.decl_list[0].body = [[], []] misc.assert_equal(self, expected_ast, real_ast)
def test_one_line(self): ''' Call find_column with one-line string. ''' input_data = '1 2 3\n' pos = 3 real_column = parse.find_column(input_data, pos) expected_column = 3 misc.assert_equal(self, expected_column, real_column)
def test_one_line(self): ''' Call find_column with one-line string. ''' input_data = '1 2 3\n' pos = 3 real_column = parse.find_column(input_data, pos) expected_column = 3 misc.assert_equal(self, expected_column, real_column)
def test_func_body_2_empty_blocks(self): ''' Parse fnction with empty blocks. ''' input_string = 'func start { {} {} }' real_ast = _parse(input_string) expected_ast = _std_module() expected_ast.decl_list[0].body = [[], []] misc.assert_equal(self, expected_ast, real_ast)
def test_class_decl_with_methods(self): ''' Parse class decl with binded func. ''' input_string = ''' class X { bind { func n1 {} func n2 {} } } ''' real_ast = _parse(input_string) expected_ast = ast.Module(decl_list=[ ast.ClassDecl( name='X', decl_list=[ ast.FuncDecl( name='n1', signature=ast.FuncSignature(), ), ast.FuncDecl( name='n2', signature=ast.FuncSignature(), ), ], ) ]) misc.assert_equal(self, expected_ast, real_ast)
def test_empty_string(self): ''' Call find_column with empty string. ''' input_data = '' pos = 0 real_column = parse.find_column(input_data, pos) expected_column = 0 misc.assert_equal(self, expected_column, real_column)
def test_class_decl_with_methods(self): ''' Parse class decl with binded func. ''' input_string = ''' class X { bind { func n1 {} func n2 {} } } ''' real_ast = _parse(input_string) expected_ast = ast.Module( decl_list=[ ast.ClassDecl( name='X', decl_list=[ ast.FuncDecl( name='n1', signature=ast.FuncSignature(), ), ast.FuncDecl( name='n2', signature=ast.FuncSignature(), ), ], ) ] ) misc.assert_equal(self, expected_ast, real_ast)
def test_simple_func_with_2_params(self): ''' Parse func that takes two params. ''' input_string = 'func testfunc (par1 ParamType, par2 ParamType) {}' real_ast = _parse(input_string) signature = ast.FuncSignature( param_list=[ ast.Param( name='par1', datatype=datatype.SimpleDataType('ParamType'), ), ast.Param( name='par2', datatype=datatype.SimpleDataType('ParamType'), ) ] ) expected_ast = ast.Module( decl_list=[ ast.FuncDecl( name='testfunc', signature=signature, ) ] ) misc.assert_equal(self, expected_ast, real_ast)
def check_translation(test_case, input_ast, expected_output): ''' Small helper func. ''' input_ast.ident_list = ident_table.ident_table(input_ast) input_ast = datatype.mark_out_datatypes(ast_=input_ast) generator_ = generator.Generator(input_ast) real_output = generator_.generate() misc.assert_equal(test_case, textwrap.dedent(expected_output), real_output)
def test_simple_return_2(self): ''' Parse return stmt without any value. ''' input_string = 'func start { return }' real_ast = _parse(input_string) expected_ast = _std_module() expected_ast.decl_list[0].body.append(ast.Return(expr=None), ) misc.assert_equal(self, expected_ast, real_ast)
def test_empty_string(self): ''' Call find_column with empty string. ''' input_data = '' pos = 0 real_column = parse.find_column(input_data, pos) expected_column = 0 misc.assert_equal(self, expected_column, real_column)
def test_simple_func_call(self): ''' Parse simple func call. ''' input_string = 'func start { fname2() }' real_ast = _parse(input_string) expected_ast = _std_module() funccall = ast.FuncCall(expr=ast.Ident('fname2'), ) expected_ast.decl_list[0].body.append(funccall) misc.assert_equal(self, expected_ast, real_ast)
def test_simple_return_3(self): ''' Parse return stmt with func call. ''' input_string = 'func start { return x() }' real_ast = _parse(input_string) expected_ast = _std_module() expected_ast.decl_list[0].body.append( ast.Return(expr=ast.FuncCall(expr=ast.Ident('x'), ), ), ) misc.assert_equal(self, expected_ast, real_ast)
def test_nested_func_call_1(self): ''' Parse nested func call. ''' input_string = 'func start { a()() }' real_ast = _parse(input_string) expected_ast = _std_module() expected_ast.decl_list[0].body.append( ast.FuncCall(expr=ast.FuncCall(expr=ast.Ident('a'), ), )) misc.assert_equal(self, expected_ast, real_ast)
def test_string(self): ''' Parse anythong with string. ''' input_string = 'func start { return "hi" }' real_ast = _parse(input_string) expected_ast = _std_module() expected_ast.decl_list[0].body.append( ast.Return(expr=ast.String('hi')), ) misc.assert_equal(self, expected_ast, real_ast)
def test_simple_import_2(self): ''' Parse import stmt with two modules. ''' input_string = 'import {module1 module2}' real_ast = _parse(input_string) expected_ast = ast.Module( import_list=['module1', 'module2'], ) misc.assert_equal(self, expected_ast, real_ast)
def test_var_decl_without_initialization(self): ''' Parse var decl stmt. ''' input_string = 'func start { testVar := Int() }' real_ast = _parse(input_string) expected_ast = _std_module() var = ast.VarDecl(name='testVar', expr=ast.FuncCall(expr=ast.Ident('Int'), )) expected_ast.decl_list[0].body.append(var) misc.assert_equal(self, expected_ast, real_ast)
def test_simple_return_2(self): ''' Parse return stmt without any value. ''' input_string = 'func start { return }' real_ast = _parse(input_string) expected_ast = _std_module() expected_ast.decl_list[0].body.append( ast.Return(expr=None), ) misc.assert_equal(self, expected_ast, real_ast)
def test_simple_return_1(self): ''' Parse return stmt with integer. ''' input_string = 'func start { return 1 }' real_ast = _parse(input_string) expected_ast = _std_module() expected_ast.decl_list[0].body.append( ast.Return(expr=ast.Number(1)), ) misc.assert_equal(self, expected_ast, real_ast)
def test_simple_func_call(self): ''' Parse simple func call. ''' input_string = 'func start { fname2() }' real_ast = _parse(input_string) expected_ast = _std_module() funccall = ast.FuncCall( expr=ast.Ident('fname2'), ) expected_ast.decl_list[0].body.append(funccall) misc.assert_equal(self, expected_ast, real_ast)
def test_two_lines(self): ''' Call find_column with two-line string. ''' input_data = ( '1 2 3\n' '4 5 6\n' ) pos = 8 real_column = parse.find_column(input_data, pos) expected_column = 2 misc.assert_equal(self, expected_column, real_column)
def test_string(self): ''' Parse anythong with string. ''' input_string = 'func start { return "hi" }' real_ast = _parse(input_string) expected_ast = _std_module() expected_ast.decl_list[0].body.append( ast.Return( expr=ast.String('hi')), ) misc.assert_equal(self, expected_ast, real_ast)
def test_simple_func(self): ''' Parse minimal fnction decl. ''' input_string = 'func testfunc2 {}' real_ast = _parse(input_string) expected_ast = ast.Module(decl_list=[ ast.FuncDecl( name='testfunc2', signature=ast.FuncSignature(), ) ]) misc.assert_equal(self, expected_ast, real_ast)
def test_simple_if(self): ''' Parse if stmt. ''' input_string = 'func start { if 1 {} }' real_ast = _parse(input_string) expected_ast = _std_module() expected_ast.decl_list[0].body.append( ast.If( condition=ast.Number(1), branch_if=[], )) misc.assert_equal(self, expected_ast, real_ast)
def test_var_decl_with_ctor(self): ''' Parse var decl stmt with constructor call. ''' input_string = 'func start { p := Parser() }' real_ast = _parse(input_string) expected_ast = _std_module() var = ast.VarDecl( name='p', expr=ast.FuncCall(expr=ast.Ident('Parser'), ), ) expected_ast.decl_list[0].body.append(var) misc.assert_equal(self, expected_ast, real_ast)
def test_generic_func_1(self): ''' First test of generic funcs. ''' input_string = 'func testFunc <Int> {}' real_ast = _parse(input_string) expected_ast = ast.Module(decl_list=[ ast.FuncDecl( name='testFunc', signature=ast.FuncSignature(generic_param_list=['Int'], ), ) ]) misc.assert_equal(self, expected_ast, real_ast)
def test_const_decl(self): ''' Parse constant decl. ''' input_string = 'const importantIdent Int := 10' real_ast = _parse(input_string) expected_ast = ast.Module(decl_list=[ ast.ConstDecl( name='importantIdent', datatype=datatype.SimpleDataType('Int'), expr=ast.Number(10), ) ]) misc.assert_equal(self, expected_ast, real_ast)
def test_simple_func_with_return_value(self): ''' Parse func that returns Int. ''' input_string = 'func testfunc2 -> Int {}' real_ast = _parse(input_string) expected_ast = ast.Module(decl_list=[ ast.FuncDecl( name='testfunc2', signature=ast.FuncSignature( return_type=datatype.SimpleDataType('Int'), ), ) ]) misc.assert_equal(self, expected_ast, real_ast)
def test_simple_if(self): ''' Parse if stmt. ''' input_string = 'func start { if 1 {} }' real_ast = _parse(input_string) expected_ast = _std_module() expected_ast.decl_list[0].body.append( ast.If( condition=ast.Number(1), branch_if=[], ) ) misc.assert_equal(self, expected_ast, real_ast)
def test_simple_return_3(self): ''' Parse return stmt with func call. ''' input_string = 'func start { return x() }' real_ast = _parse(input_string) expected_ast = _std_module() expected_ast.decl_list[0].body.append( ast.Return( expr=ast.FuncCall( expr=ast.Ident('x'), ), ), ) misc.assert_equal(self, expected_ast, real_ast)
def test_var_decl_without_initialization(self): ''' Parse var decl stmt. ''' input_string = 'func start { testVar := Int() }' real_ast = _parse(input_string) expected_ast = _std_module() var = ast.VarDecl( name='testVar', expr=ast.FuncCall( expr=ast.Ident('Int'), ) ) expected_ast.decl_list[0].body.append(var) misc.assert_equal(self, expected_ast, real_ast)
def test_simple_func(self): ''' Parse minimal fnction decl. ''' input_string = 'func testfunc2 {}' real_ast = _parse(input_string) expected_ast = ast.Module( decl_list=[ ast.FuncDecl( name='testfunc2', signature=ast.FuncSignature(), ) ] ) misc.assert_equal(self, expected_ast, real_ast)
def test_var_decl_with_initialization(self): ''' Parse var decl stmt with initiaization and without explicit type. ''' input_string = 'func start { testVar := 666 }' real_ast = _parse(input_string) expected_ast = _std_module() var = ast.VarDecl( name='testVar', expr=ast.Number(666), ) expected_ast.decl_list[0].body.append(var) misc.assert_equal(self, expected_ast, real_ast)
def test_var_decl_with_ctor(self): ''' Parse var decl stmt with constructor call. ''' input_string = 'func start { p := Parser() }' real_ast = _parse(input_string) expected_ast = _std_module() var = ast.VarDecl( name='p', expr=ast.FuncCall( expr=ast.Ident('Parser'), ), ) expected_ast.decl_list[0].body.append(var) misc.assert_equal(self, expected_ast, real_ast)
def test_simple(self): ''' Basic misc.flatten_tree() test. ''' input_list = [ [ '1', '2', ], [[['3']]], '4' ] real_output = misc.flatten_tree(input_list) expected_output = ['1', '2', '3', '4'] misc.assert_equal(self, expected_output, real_output)
def test_var_decl_with_initialization(self): ''' Parse var decl stmt with initiaization and without explicit type. ''' input_string = 'func start { testVar := 666 }' real_ast = _parse(input_string) expected_ast = _std_module() var = ast.VarDecl( name='testVar', expr=ast.Number(666), ) expected_ast.decl_list[0].body.append(var) misc.assert_equal(self, expected_ast, real_ast)
def test_nested_func_call_1(self): ''' Parse nested func call. ''' input_string = 'func start { a()() }' real_ast = _parse(input_string) expected_ast = _std_module() expected_ast.decl_list[0].body.append( ast.FuncCall( expr=ast.FuncCall( expr=ast.Ident('a'), ), ) ) misc.assert_equal(self, expected_ast, real_ast)
def try_to_compile_and_run_file( test_case, c_file_name, input_mis_code, expected_stdout, ): # translate mis to ANSI C and write to file translate_mis_to_c_and_write_to_file( input_mis_code=input_mis_code, filename=c_file_name, ) if sys.platform == 'win32': exe_file_name = c_file_name.replace('.c', '.exe') else: exe_file_name = c_file_name.replace('.c', '') # compile c code with c compiler compiler_proc = subprocess.Popen( ['tcc', c_file_name, '-o', exe_file_name], stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True, ) compiler_out, compiler_err = compiler_proc.communicate() assert os.path.isfile(c_file_name) os.remove(c_file_name) assert compiler_out == '' test_case.assertEqual( compiler_err, '', 'ANSI C compiler error:\n' + compiler_err, ) # run compiler program and check its output if sys.platform == 'win32': cmd = [exe_file_name] else: cmd = ['./' + exe_file_name] proc = subprocess.Popen( cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True, ) out, err = proc.communicate() assert os.path.isfile(exe_file_name) if out != '': misc.assert_equal(test_case, '\n', out[-1]) os.remove(exe_file_name) misc.assert_equal(test_case, expected_stdout, out) test_case.assertEqual(err, '', 'Compiled prog error:\n' + err)
def test_simple_func_with_param(self): ''' Parse func that takes one param. ''' input_string = 'func testfunc (param ParamType) {}' real_ast = _parse(input_string) signature = ast.FuncSignature(param_list=[ ast.Param(name='param', datatype=datatype.SimpleDataType('ParamType')) ], ) expected_ast = ast.Module( decl_list=[ast.FuncDecl( name='testfunc', signature=signature, )]) misc.assert_equal(self, expected_ast, real_ast)
def test_type_prefix_1(self): input_string = 'func testFunc -> RM:Int {}' real_ast = _parse(input_string) expected_ast = ast.Module(decl_list=[ ast.FuncDecl( name='testFunc', signature=ast.FuncSignature( return_type=datatype.SimpleDataType( name='Int', prefix_list=['R', 'M'], ), ), ) ]) misc.assert_equal(self, expected_ast, real_ast)
def test_const_decl(self): ''' Parse constant decl. ''' input_string = 'const importantIdent Int := 10' real_ast = _parse(input_string) expected_ast = ast.Module( decl_list=[ ast.ConstDecl( name='importantIdent', datatype=datatype.SimpleDataType('Int'), expr=ast.Number(10), ) ] ) misc.assert_equal(self, expected_ast, real_ast)
def test_generic_func_1(self): ''' First test of generic funcs. ''' input_string = 'func testFunc <Int> {}' real_ast = _parse(input_string) expected_ast = ast.Module( decl_list=[ ast.FuncDecl( name='testFunc', signature=ast.FuncSignature( generic_param_list=['Int'], ), ) ] ) misc.assert_equal(self, expected_ast, real_ast)
def test_simple_func_with_return_value(self): ''' Parse func that returns Int. ''' input_string = 'func testfunc2 -> Int {}' real_ast = _parse(input_string) expected_ast = ast.Module( decl_list=[ ast.FuncDecl( name='testfunc2', signature=ast.FuncSignature( return_type=datatype.SimpleDataType('Int'), ), ) ] ) misc.assert_equal(self, expected_ast, real_ast)
def check_translation( test_case, input_mis_code, expected_c_code, expected_stdout='', ): ''' Small helper func. ''' real_output = translate_mis_to_c(textwrap.dedent(input_mis_code)) misc.assert_equal(test_case, textwrap.dedent(expected_c_code), real_output) c_file_name = misc.get_caller_func_name().replace('test_', '') + '_out.c' try_to_compile_and_run_file( test_case, c_file_name, input_mis_code, expected_stdout, )
def test_var_decl_with_init_2(self): ''' Parse var decl stmt with complex initiaization. ''' input_string = 'func start { v2 := plus(1 2) }' real_ast = _parse(input_string) expected_ast = _std_module() var = ast.VarDecl( name='v2', expr=ast.FuncCall( expr=ast.Ident('plus'), arg_list=[ast.Number(1), ast.Number(2)], ), ) expected_ast.decl_list[0].body.append(var) misc.assert_equal(self, expected_ast, real_ast)
def test_var_decl_with_init_2(self): ''' Parse var decl stmt with complex initiaization. ''' input_string = 'func start { v2 := plus(1 2) }' real_ast = _parse(input_string) expected_ast = _std_module() var = ast.VarDecl( name='v2', expr=ast.FuncCall( expr=ast.Ident('plus'), arg_list=[ast.Number(1), ast.Number(2)], ), ) expected_ast.decl_list[0].body.append(var) misc.assert_equal(self, expected_ast, real_ast)
def test_simple_integer_var_decl(self): input_ast = ast.Module( decl_list=[ ast.FuncDecl( name='start', signature=ast.FuncSignature(), body=[ ast.VarDecl( name='testVar', expr=ast.Number(666), ), ], ) ] ) def get_expected_output(): expected_output = ast.Module( decl_list=[ ast.FuncDecl( name='start', signature=ast.FuncSignature(), body=[ ast.VarDecl( name='testVar', expr=ast.Number(666), datatype=datatype.SimpleDataType('Int'), ), ], ) ] ) expected_start_func = expected_output.decl_list[0] expected_start_func.constants = { 'const_0': ast.Number(value=666), } expected_start_func.vars = { 'testVar': datatype.SimpleDataType('Int'), } var_decl = expected_start_func.body[0] var_decl.rvalue_expr.binded_var_name = 'const_0' return expected_output expected_output = get_expected_output() real_output = datatype.mark_out_datatypes(input_ast) misc.assert_equal(self, expected_output, real_output)
def test_type_prefix_1(self): input_string = 'func testFunc -> RM:Int {}' real_ast = _parse(input_string) expected_ast = ast.Module( decl_list=[ ast.FuncDecl( name='testFunc', signature=ast.FuncSignature( return_type=datatype.SimpleDataType( name='Int', prefix_list=['R', 'M'], ), ), ) ] ) misc.assert_equal(self, expected_ast, real_ast)
def test_simple_func_decl(self): input_ast = ast.Module( decl_list=[ ast.FuncDecl( name='start', signature=ast.FuncSignature(), ) ] ) expected_output = ast.Module( decl_list=[ ast.FuncDecl( name='start', signature=ast.FuncSignature(), ) ] ) real_output = datatype.mark_out_datatypes(input_ast) misc.assert_equal(self, expected_output, real_output)
def test_var_decl_with_ctor_and_args(self): ''' Parse var decl stmt with constructor call with args. ''' input_string = 'func start { p := Parser(lexer 1) }' real_ast = _parse(input_string) expected_ast = _std_module() expected_ast.decl_list[0].body.append( ast.VarDecl( name='p', expr=ast.FuncCall( expr=ast.Ident('Parser'), arg_list=[ ast.Ident('lexer'), ast.Number(1), ], ), )) misc.assert_equal(self, expected_ast, real_ast)
def test_class_type_decl(self): ''' Parse class decl. ''' input_string = ('class MyClass {\n' ' store {\n' ' field1 Int\n' ' field2 Float\n' ' }\n' '}\n') real_ast = _parse(input_string) expected_ast = ast.Module(decl_list=[ ast.ClassDecl(name='MyClass', field_list=[ ast.Field( name='field1', datatype=datatype.SimpleDataType('Int'), ), ast.Field( name='field2', datatype=datatype.SimpleDataType('Float'), ) ]) ]) misc.assert_equal(self, expected_ast, real_ast)
def test_empty_module(self): ''' Parse empty string. ''' input_string = '' real_ast = _parse(input_string) expected_ast = ast.Module() misc.assert_equal(self, expected_ast, real_ast)
def test_simple_import_2(self): ''' Parse import stmt with two modules. ''' input_string = 'import {module1 module2}' real_ast = _parse(input_string) expected_ast = ast.Module(import_list=['module1', 'module2'], ) misc.assert_equal(self, expected_ast, real_ast)
def test_empty_import(self): ''' Parse empty import stmt. ''' input_string = 'import {}' real_ast = _parse(input_string) expected_ast = ast.Module() misc.assert_equal(self, expected_ast, real_ast)
def test_passed(self): ''' Test passed. ''' mock = TestCaseMock() misc.assert_equal(mock, 1, 1) self.assertEqual(mock.is_ok, True)