Beispiel #1
0
                    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 remove_prefix(path, prefix):
    regexp = '^' + prefix.replace('\\', '\\\\')
    np = re.sub(regexp, '', path)
    if np.startswith(os.sep):
        np = np[1:]
    return np


def fix_path(path):
    return path.replace('\\', '/')


if __name__ == '__main__':
    run_test(OutputSuite(), sys.argv[1:])
Beispiel #2
0
def builtins_wrapper(func, path):
    """Decorate a function that implements a data-driven test case to copy an
    alternative builtins module implementation in place before performing the
    test case. Clean up after executing the test case.
    """
    return lambda testcase: perform_test(func, path, testcase)


def perform_test(func, path, testcase):
    for path, _ in testcase.files:
        if os.path.basename(path) == "builtins.py":
            default_builtins = False
            break
    else:
        # Use default builtins.
        builtins = os.path.join(test_temp_dir, "builtins.py")
        shutil.copyfile(path, builtins)
        default_builtins = True

    # Actually peform the test case.
    func(testcase)

    if default_builtins:
        # Clean up.
        os.remove(builtins)


if __name__ == "__main__":
    run_test(DyncheckTransformSuite(), sys.argv[1:])
Beispiel #3
0
        # 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))


class CGenSuite(Suite):
    def __init__(self):
        self.test_compile = CGenCompileSuite()
        self.test_run = CGenRunSuite()
        super().__init__()


if __name__ == '__main__':
    run_test(CGenSuite(), sys.argv[1:])
Beispiel #4
0
        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 get_func_names(expected):
    res = []
    for s in expected:
        m = re.match(r'def ([_a-zA-Z0-9.*$]+):', s)
        if m:
            res.append(m.group(1))
    if not res:
        raise RuntimeError('No function name in test case output')
    return res


if __name__ == '__main__':
    run_test(IcodeGenerationSuite(), sys.argv[1:])
Beispiel #5
0
        first = True
        # Transform each file separately.
        for t in trees:
            # Skip the builtins module and files with '_skip.' in the path.
            if not t.path.endswith('/builtins.py') and '_skip.' not in t.path:
                if not first:
                    # Display path for files other than the first.
                    a.append('{}:'.format(
                        remove_prefix(t.path, test_temp_dir)))
                
                # Transform parse tree and produce the code for operations.
                # Note that currently we generate this for each file
                # separately; this needs to be fixed eventually.
                v = DyncheckTransformVisitor(types, symtable, True)
                t.accept(v)
                s = generate_runtime_support(t)
                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))

    
if __name__ == '__main__':
    import sys
    run_test(DyncheckOpGenSuite(), sys.argv[1:])
Beispiel #6
0

def expand_caller_kinds(kinds_or_names):
    kinds = []
    names = []
    for k in kinds_or_names:
        if isinstance(k, str):
            kinds.append(ARG_NAMED)
            names.append(k)
        else:
            kinds.append(k)
            names.append(None)
    return kinds, names


def expand_callee_kinds(kinds_and_names):
    kinds = []
    names = []
    for v in kinds_and_names:
        if isinstance(v, tuple):
            kinds.append(v[0])
            names.append(v[1])
        else:
            kinds.append(v)
            names.append(None)
    return kinds, names


if __name__ == '__main__':
    run_test(MapActualsToFormalsSuite(), sys.argv[1:])
Beispiel #7
0
         'check-interfaces.test',
         'check-super.test',
         'check-modules.test',
         'check-generic-subtyping.test',
         'check-unsupported.test']


class TypeCheckSuite(Suite):
    def cases(self):
        c = []
        for f in files:
            c += parse_test_cases(os.path.join(test_data_prefix, f),
                                  self.run_test, test_temp_dir, True)
        return c
    
    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))


if __name__ == '__main__':
    run_test(TypeCheckSuite(), sys.argv[1:])
Beispiel #8
0
import testsemanal
import testcheck
import testoutput
import testpythongen


class AllSuite(Suite):
    def __init__(self):
        self.test_types = testtypes.TypesSuite()
        self.test_typeops = testtypes.TypeOpsSuite()
        self.test_join = testtypes.JoinSuite()
        self.test_meet = testtypes.MeetSuite()
        self.test_subtypes = testsubtypes.SubtypingSuite()
        self.test_solve = testsolve.SolveSuite()
        self.test_infer = testinfer.MapActualsToFormalsSuite()
        self.test_lex = testlex.LexerSuite()
        self.test_parse = testparse.ParserSuite()
        self.test_parse_errors = testparse.ParseErrorSuite()
        self.test_semanal = testsemanal.SemAnalSuite()
        self.test_semanal_errors = testsemanal.SemAnalErrorSuite()
        self.test_semanal_symtable = testsemanal.SemAnalSymtableSuite()
        self.test_semanal_typeinfos = testsemanal.SemAnalTypeInfoSuite()
        self.test_check = testcheck.TypeCheckSuite()
        self.test_output = testoutput.OutputSuite()
        self.test_pythongen = testpythongen.PythonGenerationSuite()
        super().__init__()


if __name__ == '__main__':
    run_test(AllSuite(), sys.argv[1:])
Beispiel #9
0
        for f in python_eval_files:
            c += parse_test_cases(os.path.join(test_data_prefix, f),
                                  test_python_evaluation, test_temp_dir, True)
        return c


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))


if __name__ == '__main__':
    run_test(PythonEvaluationSuite(), sys.argv[1:])
Beispiel #10
0
                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))


import sys

if __name__ == '__main__':
    run_test(TypeExportSuite(), sys.argv[1:])
Beispiel #11
0
        # 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))


if __name__ == '__main__':
    run_test(PythonGenerationSuite(), sys.argv[1:])
Beispiel #12
0
    def cases(self):
        # Test case descriptions are in an external file.
        return parse_test_cases(os.path.join(testconfig.test_data_prefix,
                                             'parse-errors.test'),
                                test_parse_error)


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))


class CombinedParserSuite(Suite):
    def __init__(self):
        self.test_parse = ParserSuite()
        self.test_parse_errors = ParseErrorSuite()
        super().__init__()


if __name__ == '__main__':
    run_test(CombinedParserSuite(), sys.argv[1:])
Beispiel #13
0
        return c
    
    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))


class CombinedSemAnalSuite(Suite):
    def __init__(self):
        self.test_semanal = SemAnalSuite()
        self.test_semanal_errors = SemAnalErrorSuite()
        self.test_semanal_symtable = SemAnalSymtableSuite()
        self.test_semanal_typeinfos = SemAnalTypeInfoSuite()
        super().__init__()


if __name__ == '__main__':
    run_test(CombinedSemAnalSuite(), sys.argv[1:])
Beispiel #14
0
        a = []
        first = True
        # Transform each file separately.
        for t in trees:
            # Skip the builtins module and files with '_skip.' in the path.
            if not t.path.endswith('/builtins.py') and '_skip.' not in t.path:
                if not first:
                    # Display path for files other than the first.
                    a.append('{}:'.format(remove_prefix(t.path,
                                                        test_temp_dir)))

                # Transform parse tree and produce the code for operations.
                # Note that currently we generate this for each file
                # separately; this needs to be fixed eventually.
                v = DyncheckTransformVisitor(types, symtable, True)
                t.accept(v)
                s = generate_runtime_support(t)
                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))


if __name__ == '__main__':
    import sys
    run_test(DyncheckOpGenSuite(), sys.argv[1:])