Example #1
0
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_text=src, program_path="main", target=build.TRANSFORM, 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)
    )
Example #2
0
def test_op_gen(testcase):
    """Perform a type operation support data and code genereation test case."""
    any a
    expected = remove_comment_lines(testcase.output)
    try:
        src = '\n'.join(testcase.input)
        # Parse and type check the input program.
        trees, symtable, infos, types = build(src, 'main', False,
                                              test_temp_dir, True)
        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))
Example #3
0
def test_op_gen(testcase):
    """Perform a type operation support data and code genereation test case."""
    expected = remove_comment_lines(testcase.output)
    try:
        src = '\n'.join(testcase.input)
        # Parse and type check the input program.
        trees, symtable, infos, types = build(src, 'main', False,
                                              test_temp_dir, True)
        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))
Example #4
0
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))
Example #5
0
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.
        trees, symtable, infos, types = build(
            program_text=src,
            program_file_name="main",
            use_test_builtins=False,
            alt_lib_path=test_temp_dir,
            do_type_check=True,
        )
        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 pretty-printed output.
                v = DyncheckTransformVisitor(types, symtable, True)
                t.accept(v)
                # Pretty print the transformed tree.
                v2 = PrettyPrintVisitor()
                t.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)
    )