def test_transform(testcase): """Perform a runtime checking transformation test case.""" expected = remove_comment_lines(testcase.output) func_names = get_func_names(expected) try: # Construct input as a single single. src = '\n'.join(testcase.input) # Parse and type check the input program. result = build.build(program_path='main', target=build.ICODE, program_text=src, alt_lib_path=test_temp_dir) a = [] for fn in func_names: a.append('def {}:'.format(fn)) try: funccode = result.icode[fn] except KeyError: raise RuntimeError('no icode for %s (%s)' % ( fn, list(result.icode.keys()))) code = icode.render(funccode) a.extend(code) except CompileError as e: a = e.messages assert_string_arrays_equal_wildcards( expected, a, 'Invalid source code output ({}, line {})'.format(testcase.file, testcase.line))
def test_cgen_compile(testcase): # Build the program. text = '\n'.join(testcase.input) try: build.build('_program.py', target=build.C, program_text=text, alt_lib_path='lib', flags=[build.COMPILE_ONLY]) outfile = '_program.c' f = open(outfile) out = [s.rstrip('\n\r') for s in f.readlines()] f.close() os.remove(outfile) except errors.CompileError as e: out = e.messages # Verify output. assert_string_arrays_equal_wildcards(testcase.output, out, 'Invalid output ({}, line {})'.format( testcase.file, testcase.line))
def test_transform(testcase): """Perform a runtime checking transformation test case.""" expected = remove_comment_lines(testcase.output) try: # Construct input as a single single. src = '\n'.join(testcase.input) # Parse and type check the input program. Perform transform manually # so that we can skip some files. result = build.build(program_path='main', target=build.TRANSFORM, program_text=src, alt_lib_path=test_temp_dir) a = [] first = True # Transform each file separately. for fnam in sorted(result.files.keys()): f = result.files[fnam] # Skip the builtins module and files with '_skip.' in the path. if not f.path.endswith('/builtins.py') and '_skip.' not in f.path: if not first: # Display path for files other than the first. a.append('{}:'.format(remove_prefix(f.path, test_temp_dir))) # Pretty print the transformed tree. v2 = PrettyPrintVisitor() f.accept(v2) s = v2.output() if s != '': a += s.split('\n') first = False except CompileError as e: a = e.messages assert_string_arrays_equal_wildcards( expected, a, 'Invalid source code output ({}, line {})'.format(testcase.file, testcase.line))