예제 #1
0
def test_generate_docstrings_option() -> None:
    assert (pseudocode_to_python(
        dedent("""
                    # Leading comment...

                    # And the rest...


                    foo():
                        # Leading comment...

                        # And the rest...
                        return bar()
                """).strip(),
        generate_docstrings=False,
    ) == dedent("""
                    # Leading comment...

                    # And the rest...


                    def foo():
                        # Leading comment...

                        # And the rest...
                        return bar()
            """).strip())
예제 #2
0
def main(*args: Any) -> int:
    parser = ArgumentParser(description="""
            Convert a VC-2 pseudocode listing into equivalent Python code.
        """)
    parser.add_argument(
        "pseudocode_file",
        type=FileType("r"),
        default=sys.stdin,
        nargs="?",
    )
    parser.add_argument(
        "python_file",
        type=FileType("w"),
        default=sys.stdout,
        nargs="?",
    )

    args = parser.parse_args(*args)

    try:
        python = pseudocode_to_python(
            args.pseudocode_file.read(),  # type: ignore
            add_translation_note=True,
        )
    except (ParseError, ASTConstructionError) as e:
        sys.stderr.write(f"Syntax error: {str(e)}\n")
        return 1

    args.python_file.write(f"{python}\n")  # type: ignore
    return 0
예제 #3
0
def test_indent_option() -> None:
    assert (pseudocode_to_python(
        dedent("""
                    foo():
                      return bar()
                """).strip(),
        indent="        ",
    ) == dedent("""
                def foo():
                        return bar()
            """).strip())
예제 #4
0
def test_add_translation_note_option() -> None:
    assert (pseudocode_to_python(
        dedent("""
                    # Leading comment...

                    foo():
                        return bar()
                """).strip(),
        add_translation_note=True,
    ) == dedent('''
                    # This file was automatically translated from a pseudocode listing.

                    """
                    Leading comment...
                    """


                    def foo():
                        return bar()
            ''').strip())
예제 #5
0
def test_pseudocode_samples(name: str) -> None:
    # Sanity check that the translation is valid Python
    pseudocode = getattr(pseudocode_samples, name)
    python = pseudocode_to_python(pseudocode)
    ast.parse(python)
예제 #6
0
def test_pseudocode_to_python() -> None:
    assert pseudocode_to_python(
        "foo(): return bar()") == "def foo():\n    return bar()"