Esempio n. 1
0
def test_serialization():
    """
    1. Generate C++ code for messages from example.cmf and write it to example.h.
    2. Generate instances of the messages as well as tests that round trip serialize and deserialize them.
    3. Run a test to verify integer serialization produces the expected results
    4. Compile that C++ code via g++
    5. Run the compiled C++ code as a test
    """
    with open("../grammar.ebnf") as f:
        print("Reading ../grammar.ebnf")
        grammar = f.read()
        with open("../../example.cmf") as f2:
            print("Reading ../../example.cmf")
            cmf = f2.read()
        ast, _ = cmfc.parse(grammar, cmf)
        # Uncomment to show the generated AST for debugging purposes
        # pprint(ast)
        run_command(f'mkdir -p {OUTPUT_DIR}')
        header, code, tests = generate_code_and_tests(ast, "example.hpp")
        with open(f"{OUTPUT_DIR}/example.hpp", "w") as f:
            f.write(header)
        with open(f"{OUTPUT_DIR}/example.cpp", "w") as f:
            f.write(code)
        with open(f"{OUTPUT_DIR}/test_serialization.cpp", "w") as f:
            f.write(tests)
    compile_cmf_lib()
    compile_tests()
    run_tests()
Esempio n. 2
0
def gen_ast():
    """ Generate an AST for example.cmf """
    with open("../grammar.ebnf") as f:
        print("Reading ../grammar.ebnf")
        grammar = f.read()
        with open("../../example.cmf") as f2:
            print("Reading ../../example.cmf")
            cmf = f2.read()
        ast, _ = cmfc.parse(grammar, cmf)
        return ast
Esempio n. 3
0
def test_serialization():
    """
    1. Generate C++ code for messages from example.cmf and write it to example.h.
    2. Generate instances of the messages as well as tests that round trip serialize and deserialize them.
    3. Compile that C++ code via g++
    4. Run the compiled C++ code as a test
    """
    with open("../grammar.ebnf") as f:
        print("Reading ../grammar.ebnf")
        grammar = f.read()
        with open("../../example.cmf") as f2:
            print("Reading ../../example.cmf")
            cmf = f2.read()
        ast, _ = cmfc.parse(grammar, cmf)
        # Uncomment to show the generated AST for debugging purposes
        # pprint(ast)
        assert os.system(f'mkdir -p {OUTPUT_DIR}') == 0
        code, tests = generate_code_and_tests(ast)
        with open(f"{OUTPUT_DIR}/example.h", "w") as f3:
            f3.write(code)
        with open(f"{OUTPUT_DIR}/test_serialization.cpp", "w") as f4:
            f4.write(tests)
    compile_tests()
    run_tests()