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('main', target=build.SEMANTIC_ANALYSIS, program_text=src, flags=[build.TEST_BUILTINS], 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 run_test(self, testcase): a = [] try: line = testcase.input[0] mask = '' if line.startswith('##'): mask = '(' + line[2:].strip() + ')$' src = '\n'.join(testcase.input) result = build.build(program_path='main', target=build.TYPE_CHECK, program_text=src, flags=[build.TEST_BUILTINS], alt_lib_path=testconfig.test_temp_dir) map = result.types 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_cgen(testcase): # Build the program. text = '\n'.join(testcase.input) program = '_program.py' try: build.build(program, target=build.C, program_text=text, 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 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 run_test(self, testcase): a = [] try: src = '\n'.join(testcase.input) build.build('main', target=build.TYPE_CHECK, program_text=src, flags=[build.TEST_BUILTINS], 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 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'): result = build.build('main', target=build.SEMANTIC_ANALYSIS, program_text=src, flags=[build.TEST_BUILTINS], alt_lib_path=test_temp_dir) files = result.files else: files = {'main': parse(src, 'main')} a = [] first = True # Produce an output containing the pretty-printed forms (with original # formatting) of all the relevant source files. for fnam in sorted(files.keys()): f = files[fnam] # Omit the builtins and files marked for omission. if (not f.path.endswith(os.sep + 'builtins.py') and '-skip.' not in f.path): # Add file name + colon for files other than the first. if not first: a.append('{}:'.format(fix_path(remove_prefix( f.path, test_temp_dir)))) v = OutputVisitor() f.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_generation(testcase): """Perform a mypy-to-Python source code transformation test case.""" any a expected = testcase.output for i, s in enumerate(expected): expected[i] = s.replace('<prefix>', PREFIX) # 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. result = build.build('main', target=build.SEMANTIC_ANALYSIS, program_text=src, flags=[build.TEST_BUILTINS], alt_lib_path=test_temp_dir) a = [] first = True # Produce an output containing the pretty-printed forms (with original # formatting) of all the relevant source files. for fnam in sorted(result.files.keys()): f = result.files[fnam] # Omit the builtins module and files marked for omission. if not f.path.endswith(os.sep + 'builtins.py') and '-skip.' not in f.path: # Add file name + colon for files other than the first. if not first: a.append('{}:'.format( fix_path(remove_prefix(f.path, test_temp_dir)))) ver = 3 # Generate Python 2 instead of 3? if '-2' in testcase.name: ver = 2 v = PythonGenerator(ver) f.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_semanal_error(testcase): """Perform a test case.""" try: src = '\n'.join(testcase.input) build.build('main', target=build.SEMANTIC_ANALYSIS, program_text=src, flags=[build.TEST_BUILTINS], alt_lib_path=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): """Perform a test case.""" try: # Build test case input. src = '\n'.join(testcase.input) result = build.build('main', target=build.SEMANTIC_ANALYSIS, program_text=src, flags=[build.TEST_BUILTINS], 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))