def test_semanal(testcase): """Perform a semantic analysis test case. The testcase argument contains a description of the test case (inputs and output). """ try: src = '\n'.join(testcase.input) trees, symtable, infos, types = build(src, 'main', True, test_temp_dir) a = [] # Include string representations of the source files in the actual # output. for t in trees: # Omit the builtins module and files with a special marker in the # path. # TODO the test is not reliable if (not t.path.endswith(os.sep + 'builtins.py') and not os.path.basename(t.path).startswith('_') and not os.path.splitext( os.path.basename(t.path))[0].endswith('_')): a += str(t).split('\n') except CompileError as e: a = e.messages assert_string_arrays_equal( testcase.output, a, 'Invalid semantic analyzer output ({}, line {})'.format(testcase.file, testcase.line))
def run_test(self, testcase): a = [] try: line = testcase.input[0] mask = '' if line.startswith('##'): mask = '(' + line[2:].strip() + ')$' src = '\n'.join(testcase.input) map = build(src, 'main', True, testconfig.test_temp_dir, True)[3] kk = map.keys() keys = [] for k in kk: if k.line is not None and k.line != -1 and map[k]: if (re.match(mask, short_type(k)) or (isinstance(k, NameExpr) and re.match(mask, k.name))): keys.append(k) for key in sorted(keys, key=lambda n: (n.line, short_type(n), str(n) + str(map[n]))): ts = str(map[key]).replace('*', '') # Remove erased tags ts = ts.replace('__main__.', '') a.append('{}({}) : {}'.format(short_type(key), key.line, ts)) except CompileError as e: a = e.messages assert_string_arrays_equal( testcase.output, a, 'Invalid type checker output ({}, line {})'.format(testcase.file, testcase.line))
def test_semanal(testcase): """Perform a semantic analysis test case. The testcase argument contains a description of the test case (inputs and output). """ try: src = '\n'.join(testcase.input) result = build.build(src, 'main', target=build.SEMANTIC_ANALYSIS, test_builtins=True, alt_lib_path=test_temp_dir) a = [] # Include string representations of the source files in the actual # output. for fnam in sorted(result.files.keys()): f = result.files[fnam] # Omit the builtins module and files with a special marker in the # path. # TODO the test is not reliable if (not f.path.endswith(os.sep + 'builtins.py') and not os.path.basename(f.path).startswith('_') and not os.path.splitext( os.path.basename(f.path))[0].endswith('_')): a += str(f).split('\n') except CompileError as e: a = e.messages assert_string_arrays_equal( testcase.output, a, 'Invalid semantic analyzer output ({}, line {})'.format(testcase.file, testcase.line))
def test_cgen(testcase): # Build the program. text = '\n'.join(testcase.input) program = '_program.py' try: build.build(text, program, target=build.C, alt_lib_path='lib') # Run the program. outfile = './_program' outb = subprocess.check_output([outfile], stderr=subprocess.STDOUT) # Split output into lines. out = [s.rstrip('\n\r') for s in str(outb, 'utf8').splitlines()] # Remove temp file. os.remove(outfile) except errors.CompileError as e: out = e.messages # Include line-end comments in the expected output. # Note: # characters in string literals can confuse this. for s in testcase.input: m = re.search(' #(.*)', s) if m: testcase.output.append(m.group(1).strip()) # Verify output. assert_string_arrays_equal(testcase.output, out, 'Invalid output ({}, line {})'.format( testcase.file, testcase.line))
def run_test(self, testcase): a = [] try: src = '\n'.join(testcase.input) build(src, 'main', True, test_temp_dir, True) except CompileError as e: a = normalize_error_messages(e.messages) assert_string_arrays_equal( testcase.output, a, 'Invalid type checker output ({}, line {})'.format( testcase.file, testcase.line))
def test_parse_error(testcase): try: # Compile temporary file. parse('\n'.join(testcase.input), INPUT_FILE_NAME) raise AssertionFailure('No errors reported') except CompileError as e: # Verify that there was a compile error and that the error messages # are equivalent. assert_string_arrays_equal( testcase.output, e.messages, 'Invalid compiler output ({}, line {})'.format(testcase.file, testcase.line))
def test_parser(testcase): """Perform a single parser test case. The argument contains the description of the test case. """ try: n = parse('\n'.join(testcase.input)) a = str(n).split('\n') except CompileError as e: a = e.messages assert_string_arrays_equal(testcase.output, a, 'Invalid parser output ({}, line {})'.format( testcase.file, testcase.line))
def test_semanal_error(testcase): """Perform a test case.""" try: src = '\n'.join(testcase.input) build(src, 'main', True, test_temp_dir) raise AssertionError('No errors reported in {}, line {}'.format( testcase.file, testcase.line)) except CompileError as e: # Verify that there was a compile error and that the error messages # are equivalent. assert_string_arrays_equal( testcase.output, normalize_error_messages(e.messages), 'Invalid compiler output ({}, line {})'.format(testcase.file, testcase.line))
def run_test(self, testcase): a = [] try: src = '\n'.join(testcase.input) build.build(src, 'main', target=build.TYPE_CHECK, test_builtins=True, alt_lib_path=test_temp_dir) except CompileError as e: a = normalize_error_messages(e.messages) assert_string_arrays_equal( testcase.output, a, 'Invalid type checker output ({}, line {})'.format( testcase.file, testcase.line))
def run_test(self, testcase): """Perform a test case.""" try: # Build test case input. src = '\n'.join(testcase.input) trees, symtable, infos, types = build(src, 'main', True, test_temp_dir) # The output is the symbol table converted into a string. a = str(infos).split('\n') except CompileError as e: a = e.messages assert_string_arrays_equal( testcase.output, a, 'Invalid semantic analyzer output ({}, line {})'.format( testcase.file, testcase.line))
def run_test(self, testcase): """Perform a test case.""" try: # Build test case input. src = '\n'.join(testcase.input) result = build.build(src, 'main', target=build.SEMANTIC_ANALYSIS, test_builtins=True, alt_lib_path=test_temp_dir) # The output is the symbol table converted into a string. a = str(result.typeinfos).split('\n') except CompileError as e: a = e.messages assert_string_arrays_equal( testcase.output, a, 'Invalid semantic analyzer output ({}, line {})'.format( testcase.file, testcase.line))
def test_python_evaluation(testcase): # Write the program to a file. program = "_program.py" outfile = "_program.out" f = open(program, "w") for s in testcase.input: f.write("{}\n".format(s)) f.close() # Run the program. outb = subprocess.check_output([python_path, os.path.expanduser(mypy_path), "mypy.py", program]) # Split output into lines. out = [s.rstrip("\n\r") for s in str(outb, "utf8").splitlines()] # Remove temp file. os.remove(program) assert_string_arrays_equal( testcase.output, out, "Invalid output ({}, line {})".format(testcase.file, testcase.line) )
def test_output(testcase): """Perform an identity source code transformation test case.""" expected = testcase.output if expected == []: expected = testcase.input try: src = '\n'.join(testcase.input) # Parse and analyze the source program. # Parse and semantically analyze the source program. any trees, any symtable, any infos, any types # Test case names with a special suffix get semantically analyzed. This # lets us test that semantic analysis does not break source code pretty # printing. if testcase.name.endswith('_SemanticAnalyzer'): trees, symtable, infos, types = build(src, 'main', True, test_temp_dir) else: trees = [parse(src, 'main')] a = [] first = True # Produce an output containing the pretty-printed forms (with original # formatting) of all the relevant source files. for t in trees: # Omit the builtins and files marked for omission. if (not t.path.endswith(os.sep + 'builtins.py') and '-skip.' not in t.path): # Add file name + colon for files other than the first. if not first: a.append('{}:'.format(fix_path(remove_prefix( t.path, test_temp_dir)))) v = OutputVisitor() t.accept(v) s = v.output() if s != '': a += s.split('\n') first = False except CompileError as e: a = e.messages assert_string_arrays_equal( expected, a, 'Invalid source code output ({}, line {})'.format( testcase.file, testcase.line))
def test_python_evaluation(testcase): # Write the program to a file. program = '_program.py' outfile = '_program.out' f = open(program, 'w') for s in testcase.input: f.write('{}\n'.format(s)) f.close() # Run the program. outb = subprocess.check_output([python_path, os.path.expanduser(mypy_path), 'mypy.py', program]) # Split output into lines. out = [s.rstrip('\n\r') for s in str(outb, 'utf8').splitlines()] # Remove temp file. os.remove(program) assert_string_arrays_equal(testcase.output, out, 'Invalid output ({}, line {})'.format( testcase.file, testcase.line))
def run_test(self, testcase): """Perform a test case.""" try: # Build test case input. src = '\n'.join(testcase.input) trees, symtable, infos, types = build(src, 'main', True, test_temp_dir) # The output is the symbol table converted into a string. a = [] for f in sorted(symtable.keys()): if f != 'builtins': a.append('{}:'.format(f)) for s in str(symtable[f].names).split('\n'): a.append(' ' + s) except CompileError as e: a = e.messages assert_string_arrays_equal( testcase.output, a, 'Invalid semantic analyzer output ({}, line {})'.format( testcase.file, testcase.line))
def test_python_generation(testcase): """Perform a mypy-to-Python source code transformation test case.""" any a expected = testcase.output # By default, assume an identity translation. This is useful for # dynamically typed code. if expected == []: expected = testcase.input try: src = '\n'.join(testcase.input) # Parse and semantically analyze the source program. trees, symtable, infos, types = build(src, 'main', True, test_temp_dir) a = [] first = True # Produce an output containing the pretty-printed forms (with original # formatting) of all the relevant source files. for t in trees: # Omit the builtins module and files marked for omission. if not t.path.endswith(os.sep + 'builtins.py') and '-skip.' not in t.path: # Add file name + colon for files other than the first. if not first: a.append('{}:'.format( fix_path(remove_prefix(t.path, test_temp_dir)))) ver = 3 # Generate Python 2 instead of 3? if '-2' in testcase.name: ver = 2 v = PythonGenerator(ver) t.accept(v) s = v.output() if s != '': a += s.split('\n') first = False except CompileError as e: a = e.messages assert_string_arrays_equal( expected, a, 'Invalid source code output ({}, line {})'.format( testcase.file, testcase.line))
def run_test(self, testcase): """Perform a test case.""" try: # Build test case input. src = '\n'.join(testcase.input) result = build.build(src, 'main', target=build.SEMANTIC_ANALYSIS, test_builtins=True, alt_lib_path=test_temp_dir) # The output is the symbol table converted into a string. a = [] for f in sorted(result.files.keys()): if f != 'builtins': a.append('{}:'.format(f)) for s in str(result.files[f].names).split('\n'): a.append(' ' + s) except CompileError as e: a = e.messages assert_string_arrays_equal( testcase.output, a, 'Invalid semantic analyzer output ({}, line {})'.format( testcase.file, testcase.line))