def test_custom_filter_and_test_redefinition(gen_paths): # type: ignore language_context = LanguageContext(extension='.json') namespace = Namespace('', Path(), PurePath(), language_context) with pytest.raises(RuntimeError): Generator(namespace, additional_filters={'type_to_include_path': lambda T: ''}, additional_tests={'custom_test': lambda T: False}) with pytest.raises(RuntimeError): Generator(namespace, additional_filters={'custom_filter': lambda T: ''}, additional_tests={'primitive': lambda T: False})
def test_get_templates(gen_paths): # type: ignore """ Verifies the nunavut.jinja.Generator.get_templates() method. """ root_namespace_dir = gen_paths.dsdl_dir / Path("uavcan") root_namespace = str(root_namespace_dir) serializable_types = read_namespace(root_namespace, []) language_context = LanguageContext(extension='.json') namespace = build_namespace_tree(serializable_types, root_namespace_dir, gen_paths.out_dir, language_context) generator = Generator(namespace, templates_dir=gen_paths.templates_dir) templates = generator.get_templates() count = 0 for template in templates: count += 1 assert count > 0 # Do it twice just to cover in-memory cache templates = generator.get_templates() count = 0 for template in templates: count += 1 assert count > 0
def test_namespace_generation(gen_paths): # type: ignore """Test actually generating a namepace file.""" language_context = LanguageContext(extension='.json', namespace_output_stem='__module__') namespace, root_namespace_path, compound_types = gen_test_namespace( gen_paths, language_context) assert len(compound_types) == 2 generator = Generator(namespace, True, language_context, gen_paths.templates_dir / Path('default')) generator.generate_all() for nested_namespace in namespace.get_nested_namespaces(): nested_namespace_path = Path(root_namespace_path) / Path( *nested_namespace.full_name.split('.')[1:]) assert nested_namespace.source_file_path == str(nested_namespace_path) outfile = gen_paths.find_outfile_in_namespace("scotec.mcu", namespace) assert (outfile is not None) with open(str(outfile), 'r') as json_file: json_blob = json.load(json_file) assert json_blob is not None assert json_blob['scotec.mcu']['namespace'] == 'scotec.mcu' output_path_for_timer = namespace.find_output_path_for_type( compound_types[0]) assert (gen_paths.out_dir / 'scotec' / 'mcu' / 'Timer_0_1').with_suffix('.json') == output_path_for_timer
def test_three_roots(gen_paths): # type: ignore """ Generates a type that uses another type from a different root namespace. """ root_namespace = str(gen_paths.dsdl_dir / Path("scotec")) includes = [str(gen_paths.dsdl_dir / Path("huckco")), str(gen_paths.dsdl_dir / Path("esmeinc"))] compound_types = read_namespace(root_namespace, includes, allow_unregulated_fixed_port_id=True) language_context = LanguageContext(extension='.json') namespace = build_namespace_tree(compound_types, root_namespace, gen_paths.out_dir, language_context) generator = Generator(namespace, templates_dir=gen_paths.templates_dir) generator.generate_all(False) # Now read back in and verify outfile = gen_paths.find_outfile_in_namespace("scotec.FatherType", namespace) assert (outfile is not None) with open(str(outfile), 'r') as json_file: json_blob = json.load(json_file) assert len(json_blob) > 0 assert len(json_blob['scotec.FatherType']['attributes']) == 2 assert json_blob['scotec.FatherType']['attributes'][0]['type'] == 'huckco.SonType.0.1' assert json_blob['scotec.FatherType']['attributes'][1]['type'] == 'esmeinc.DaughterType.0.1'
def test_namespace_stropping(gen_paths, language_key, expected_file_ext, expected_stropp_part_0, expected_stropp_part_1): """Test generating a namespace that uses a reserved keyword for a given language.""" language_context = LanguageContext(language_key) namespace, root_namespace_path, compound_types = gen_test_namespace( gen_paths, language_context) assert len(compound_types) == 2 generator = Generator(namespace, True, language_context, gen_paths.templates_dir / Path('default')) generator.generate_all() expected_stropped_ns = 'scotec.{}.{}'.format(expected_stropp_part_0, expected_stropp_part_1) outfile = gen_paths.find_outfile_in_namespace(expected_stropped_ns, namespace) assert (outfile is not None) with open(str(outfile), 'r') as json_file: json_blob = json.load(json_file) assert json_blob is not None output_path_for_stropped = namespace.find_output_path_for_type( compound_types[1]) expected_stable_path = gen_paths.out_dir / 'scotec' expected_path_and_file = expected_stable_path / expected_stropp_part_0 / expected_stropp_part_1 / 'ATOMIC_TYPE_0_1' assert expected_path_and_file.with_suffix( expected_file_ext) == output_path_for_stropped
def test_custom_filter_and_test(gen_paths): # type: ignore root_path = str(gen_paths.dsdl_dir / Path("uavcan")) output_path = gen_paths.out_dir / 'filter_and_test' compound_types = read_namespace(root_path, []) language_context = LanguageContext(extension='.json') namespace = build_namespace_tree(compound_types, root_path, output_path, language_context) template_path = gen_paths.templates_dir / Path('custom_filter_and_test') generator = Generator(namespace, False, language_context, template_path, additional_filters={'custom_filter': lambda T: 'hi mum'}, additional_tests={'custom_test': lambda T: True}) generator.generate_all() outfile = gen_paths.find_outfile_in_namespace("uavcan.time.SynchronizedTimestamp", namespace) assert (outfile is not None) with open(str(outfile), 'r') as json_file: json_blob = json.load(json_file) assert json_blob is not None assert json_blob['filter_result'] == 'hi mum' assert json_blob['test_result'] == 'yes'
def test_instance_tests(gen_paths): # type: ignore """ Verifies that instance tests are added for pydsdl.SerializableType and all of its subclasses. """ root_namespace_dir = gen_paths.dsdl_dir / Path("buncho") type_map = read_namespace(str(root_namespace_dir), '') language_context = LanguageContext('js') namespace = build_namespace_tree(type_map, root_namespace_dir, gen_paths.out_dir, language_context) generator = Generator(namespace, False, language_context, gen_paths.templates_dir) generator.generate_all(False) outfile = gen_paths.find_outfile_in_namespace("buncho.serializables", namespace) assert (outfile is not None) with open(str(outfile), 'r') as json_file: json_blob = json.load(json_file) assert json_blob is not None assert json_blob["this_field_is_an_int32"]["isSerializableType"] is True assert json_blob["this_field_is_an_int32"]["isIntegerType"] is True assert json_blob["this_field_is_an_int32"]["isFloatType"] is False assert json_blob["this_field_is_a_float"]["isSerializableType"] is True assert json_blob["this_field_is_a_float"]["isIntegerType"] is False assert json_blob["this_field_is_a_float"]["isFloatType"] is True
def ptest_lang_cpp(gen_paths, implicit): # type: ignore """Generates and verifies JSON with values filtered using the cpp language module. """ root_namespace_dir = gen_paths.dsdl_dir / Path("langtest") root_namespace = str(root_namespace_dir) compound_types = read_namespace(root_namespace, [], allow_unregulated_fixed_port_id=True) if implicit: templates_dirs = [gen_paths.templates_dir / Path("implicit") / Path("cpp")] else: templates_dirs = [gen_paths.templates_dir / Path("explicit")] templates_dirs.append(gen_paths.templates_dir / Path("common")) language_context = LanguageContext('cpp' if implicit else None, '.hpp' if not implicit else None) namespace = build_namespace_tree(compound_types, root_namespace_dir, gen_paths.out_dir, language_context) generator = Generator(namespace, templates_dir=templates_dirs) generator.generate_all(False) # Now read back in and verify outfile = gen_paths.find_outfile_in_namespace("langtest.cpp.ns.TestType", namespace) assert (outfile is not None) generated_values = {} # type: Dict with open(str(outfile), 'r') as python_file: exec(python_file.read(), generated_values) assert len(generated_values) lang_cpp_output = generated_values["tests"]["lang_cpp"] assert lang_cpp_output["namespace"] == "langtest.cpp.ns" assert lang_cpp_output["namespace_open"] == r'''namespace langtest { namespace cpp { namespace ns { ''' assert lang_cpp_output["namespace_open_wo_nl"] == r'''namespace langtest { namespace cpp { namespace ns { ''' assert lang_cpp_output["namespace_close"] == r'''} } } ''' assert lang_cpp_output["namespace_close_w_comments"] == r'''} // namespace ns } // namespace cpp } // namespace langtest ''' return generated_values
def create_generators(namespace: nunavut.Namespace, **kwargs: typing.Any) -> \ typing.Tuple['AbstractGenerator', 'AbstractGenerator']: """ Create the two generators used by Nunavut; a code-generator and a support-library generator. :param nunavut.Namespace namespace: The namespace to generate code within. :param kwargs: A list of arguments that are forwarded to the generator constructors. :return: Tuple with the first item being the code-generator and the second the support-library generator. """ from nunavut.jinja import Generator return (Generator(namespace, **kwargs), create_support_generator(namespace))
def ptest_lang_py(gen_paths, implicit, unique_name_evaluator): # type: ignore """ Generates and verifies JSON with values filtered using the python language support module. """ root_namespace_dir = gen_paths.dsdl_dir / Path("langtest") root_namespace = str(root_namespace_dir) if implicit: templates_dirs = [ gen_paths.templates_dir / Path("implicit") / Path("py") ] else: templates_dirs = [gen_paths.templates_dir / Path("explicit")] templates_dirs.append(gen_paths.templates_dir / Path("common")) compound_types = read_namespace(root_namespace, '', allow_unregulated_fixed_port_id=True) language_context = LanguageContext('py' if implicit else None, '.py' if not implicit else None) namespace = build_namespace_tree(compound_types, root_namespace_dir, gen_paths.out_dir, language_context) generator = Generator(namespace, False, language_context, templates_dirs) generator.generate_all(False) # Now read back in and verify outfile = gen_paths.find_outfile_in_namespace("langtest.py.TestType", namespace) assert (outfile is not None) generated_values = {} # type: Dict with open(str(outfile), 'r') as python_file: exec(python_file.read(), generated_values) assert len(generated_values) > 0 lang_py_output = generated_values["tests"]["lang_py"] unique_name_evaluator(r'_NAME\d+_', lang_py_output["unique_name_0"]) unique_name_evaluator(r'_NAME\d+_', lang_py_output["unique_name_1"]) unique_name_evaluator(r'_name\d+_', lang_py_output["unique_name_2"]) assert "identifier_zero" == lang_py_output["id_0"] many_unique_names = lang_py_output.get("many_unique_names") if many_unique_names is not None: for name in many_unique_names: unique_name_evaluator(r'_f\d+_', name) return generated_values
def parameterized_test_namespace_(gen_paths, templates_subdir): # type: ignore language_context = LanguageContext(extension='.json') namespace, root_namespace_path, _ = gen_test_namespace( gen_paths, language_context) generator = Generator(namespace, False, language_context, gen_paths.templates_dir / Path(templates_subdir)) generator.generate_all() assert namespace.source_file_path == root_namespace_path assert namespace.full_name == 'scotec' for nested_namespace in namespace.get_nested_namespaces(): nested_namespace_path = Path(root_namespace_path) / Path( *nested_namespace.full_name.split('.')[1:]) assert nested_namespace.source_file_path == str(nested_namespace_path)
def test_realgen(gen_paths, lang_key): # type: ignore """ Sanity test that runs through the entire public, regulated set of UAVCAN types and generates some basic C code. """ root_namespace_dir = gen_paths.root_dir / Path("submodules") / Path( "public_regulated_data_types") / Path("uavcan") type_map = read_namespace(str(root_namespace_dir), '') language_context = LanguageContext(lang_key) namespace = build_namespace_tree(type_map, root_namespace_dir, gen_paths.out_dir, language_context) generator = Generator(namespace, False, language_context) generator.generate_all(False)
def test_TestType_0_1(gen_paths): # type: ignore """ Generates a JSON blob and then reads it back in. This test uses an extremely simple DSDL type to generate JSON then reads this JSON back in and parses it using Python's built-in parser. """ root_namespace_dir = gen_paths.dsdl_dir / Path("uavcan") root_namespace = str(root_namespace_dir) language_context = LanguageContext(extension='.json') namespace = build_namespace_tree(read_namespace(root_namespace, ''), root_namespace_dir, gen_paths.out_dir, language_context) generator = Generator(namespace, False, language_context, gen_paths.templates_dir) generator.generate_all(False) # Now read back in and verify outfile = gen_paths.find_outfile_in_namespace("uavcan.test.TestType", namespace) assert (outfile is not None) with open(str(outfile), 'r') as json_file: json_blob = json.load(json_file) assert json_blob is not None assert len(json_blob) == 1 uavcan_namespace = json_blob[0] assert uavcan_namespace["type"] == "namespace" assert uavcan_namespace["name"] == "uavcan.test" assert len(uavcan_namespace["contents"]) == 1 test_type = uavcan_namespace["contents"][0] assert test_type["name"] == "TestType" assert test_type["version"]["major"] == 0 assert test_type["version"]["minor"] == 1 assert len(test_type["attributes"]) == 2 test_attr_0 = test_type["attributes"][0] assert test_attr_0["name"] == "data" assert test_attr_0["type"] == "uint56" assert test_attr_0["bit_length"] == 56 assert test_attr_0["cast_mode"] == "TRUNCATED" test_attr_1 = test_type["attributes"][1] assert test_attr_1["name"] == "const_bool_example" assert test_attr_1["type"] == "uint1" assert test_attr_1["bit_length"] == 1 assert test_attr_1["cast_mode"] == "SATURATED"
def test_bfs_of_type_for_template(gen_paths): # type: ignore """ Verifies that our template to type lookup logic does a breadth-first search for a valid template when searching type names. """ language_context = LanguageContext(extension='.json') empty_namespace = Namespace('', gen_paths.dsdl_dir, gen_paths.out_dir, language_context) generator = Generator(empty_namespace, templates_dir=gen_paths.templates_dir) subject = d() template_file = generator.filter_type_to_template(subject) assert str(Path('c').with_suffix( Generator.TEMPLATE_SUFFIX)) == template_file assert generator.filter_type_to_template(subject) == template_file
def test_template_assert(gen_paths): # type: ignore """ Tests our template assertion extension. """ root_path = str(gen_paths.dsdl_dir / Path("uavcan")) output_path = gen_paths.out_dir / 'assert' compound_types = read_namespace(root_path, []) language_context = LanguageContext(extension='.json') namespace = build_namespace_tree(compound_types, root_path, output_path, language_context) template_path = gen_paths.templates_dir / Path('assert') generator = Generator(namespace, templates_dir=template_path) try: generator.generate_all() assert False except TemplateAssertionError as e: e.filename == str(template_path / "Any.j2") e.filename == 2 e.message == 'Template assertion failed.'
def test_type_to_include(gen_paths): # type: ignore """Test the type_to_include filter.""" root_path = str(gen_paths.dsdl_dir / Path("uavcan")) output_path = gen_paths.out_dir / 'type_to_include' compound_types = read_namespace(root_path, []) language_context = LanguageContext(extension='.json') namespace = build_namespace_tree(compound_types, root_path, output_path, language_context) template_path = gen_paths.templates_dir / Path('type_to_include') generator = Generator(namespace, templates_dir=template_path) generator.generate_all() outfile = gen_paths.find_outfile_in_namespace( "uavcan.time.SynchronizedTimestamp", namespace) assert (outfile is not None) with open(str(outfile), 'r') as json_file: json_blob = json.load(json_file) assert json_blob is not None assert json_blob['include'] == "uavcan/time/SynchronizedTimestamp_1_0.json"
def test_one_template(gen_paths): # type: ignore """ Verifies that we can use only a SeralizableType.j2 as the only template when no service types are present. """ root_namespace_dir = gen_paths.dsdl_dir / Path("uavcan") root_namespace = str(root_namespace_dir) serializable_types = read_namespace(root_namespace, []) language_context = LanguageContext(extension='.json') namespace = build_namespace_tree(serializable_types, root_namespace_dir, gen_paths.out_dir, language_context) generator = Generator(namespace, templates_dir=gen_paths.templates_dir) generator.generate_all(False) outfile = gen_paths.find_outfile_in_namespace("uavcan.time.TimeSystem", namespace) assert (outfile is not None) with open(str(outfile), 'r') as json_file: json_blob = json.load(json_file) assert json_blob['uavcan.time.TimeSystem']['namespace'] == 'uavcan.time' assert json_blob['uavcan.time.TimeSystem']['is_serializable']
def test_anygen(gen_paths, lang_key): # type: ignore """ Verifies that any dsdl type will resolve to an ``Any`` template. """ root_namespace_dir = gen_paths.dsdl_dir / Path("uavcan") type_map = read_namespace(str(root_namespace_dir), []) language_context = LanguageContext(extension='.json') namespace = build_namespace_tree(type_map, root_namespace_dir, str(gen_paths.out_dir), language_context) generator = Generator(namespace, templates_dir=gen_paths.templates_dir) generator.generate_all(False) outfile = gen_paths.find_outfile_in_namespace( "uavcan.time.SynchronizedTimestamp", namespace) assert (outfile is not None) with open(str(outfile), 'r') as json_file: json_blob = json.load(json_file) assert json_blob is not None assert json_blob["full_name"] == "uavcan.time.SynchronizedTimestamp"
def ptest_lang_c(gen_paths, implicit, unique_name_evaluator): # type: ignore """ Generates and verifies JSON with values filtered using the c language support module. """ root_namespace_dir = gen_paths.dsdl_dir / Path("langtest") if implicit: templates_dirs = [ gen_paths.templates_dir / Path("implicit") / Path("c") ] else: templates_dirs = [gen_paths.templates_dir / Path("explicit")] templates_dirs.append(gen_paths.templates_dir / Path("common")) root_namespace = str(root_namespace_dir) compound_types = read_namespace(root_namespace, '', allow_unregulated_fixed_port_id=True) language_context = LanguageContext('c' if implicit else None, '.h' if not implicit else None) namespace = build_namespace_tree(compound_types, root_namespace_dir, gen_paths.out_dir, language_context) generator = Generator(namespace, False, language_context, templates_dirs) generator.generate_all(False) # Now read back in and verify outfile = gen_paths.find_outfile_in_namespace("langtest.c.TestType", namespace) assert (outfile is not None) generated_values = {} # type: Dict with open(str(outfile), 'r') as python_file: exec(python_file.read(), generated_values) assert len(generated_values) > 0 lang_c_output = generated_values["tests"]["lang_c"] assert lang_c_output["namespace"] == "langtest.c" assert lang_c_output["namespace_macrofy"] == "LANGTEST_C" assert lang_c_output["ctype_std truncated uint8"] == "uint8_t" assert lang_c_output["ctype_std saturated int8"] == "int8_t" assert lang_c_output["ctype_std truncated uint9"] == "uint16_t" assert lang_c_output["ctype_std saturated int9"] == "int16_t" assert lang_c_output["ctype truncated uint8"] == "unsigned char" assert lang_c_output["ctype saturated int8"] == "char" assert lang_c_output["ctype truncated uint9"] == "unsigned int" assert lang_c_output["ctype saturated int9"] == "int" assert lang_c_output["ctype_std truncated uint32"] == "uint32_t" assert lang_c_output["ctype_std saturated int32"] == "int32_t" assert lang_c_output["ctype_std truncated uint64"] == "uint64_t" assert lang_c_output["ctype_std saturated int64"] == "int64_t" assert lang_c_output["ctype truncated uint32"] == "unsigned long" assert lang_c_output["ctype saturated int32"] == "long" assert lang_c_output["ctype truncated uint64"] == "unsigned long long" assert lang_c_output["ctype saturated int64"] == "long long" assert lang_c_output["ctype saturated bool"] == "BOOL" assert lang_c_output["ctype_std saturated bool"] == "bool" unique_name_evaluator(r'_nAME\d+_', lang_c_output["unique_name_0"]) unique_name_evaluator(r'_nAME\d+_', lang_c_output["unique_name_1"]) unique_name_evaluator(r'_naME\d+_', lang_c_output["unique_name_2"]) unique_name_evaluator(r'_\d+_', lang_c_output["unique_name_3"]) return generated_values
def ptest_lang_c(gen_paths: Any, implicit: bool, unique_name_evaluator: Any, use_standard_types: bool, configurable_language_context_factory: Callable) -> Dict: """ Generates and verifies JSON with values filtered using the c language support module. """ root_namespace_dir = gen_paths.dsdl_dir / Path("langtest") if implicit: templates_dirs = [gen_paths.templates_dir / Path("implicit") / Path("c")] else: templates_dirs = [gen_paths.templates_dir / Path("explicit")] templates_dirs.append(gen_paths.templates_dir / Path("common")) root_namespace = str(root_namespace_dir) compound_types = read_namespace(root_namespace, [], allow_unregulated_fixed_port_id=True) config_overrides = {'nunavut.lang.c': {'use_standard_types': use_standard_types}} language_context = configurable_language_context_factory(config_overrides, 'c' if implicit else None, '.h' if not implicit else None) namespace = build_namespace_tree(compound_types, root_namespace_dir, gen_paths.out_dir, language_context) generator = Generator(namespace, templates_dir=templates_dirs) generator.generate_all(False) # Now read back in and verify outfile = gen_paths.find_outfile_in_namespace("langtest.c.TestType", namespace) assert (outfile is not None) generated_values = {} # type: Dict with open(str(outfile), 'r') as python_file: exec(python_file.read(), generated_values) assert len(generated_values) > 0 # cspell: disable lang_c_output = generated_values["tests"]["lang_c"] assert lang_c_output["namespace"] == "langtest.c" assert lang_c_output["namespace_macrofy"] == "LANGTEST_C" if use_standard_types: assert lang_c_output["ctype truncated uint8"] == "uint8_t" assert lang_c_output["ctype saturated int8"] == "int8_t" assert lang_c_output["ctype truncated uint9"] == "uint16_t" assert lang_c_output["ctype saturated int9"] == "int16_t" else: assert lang_c_output["ctype truncated uint8"] == "unsigned char" assert lang_c_output["ctype saturated int8"] == "char" assert lang_c_output["ctype truncated uint9"] == "unsigned int" assert lang_c_output["ctype saturated int9"] == "int" if use_standard_types: assert lang_c_output["ctype truncated uint32"] == "uint32_t" assert lang_c_output["ctype saturated int32"] == "int32_t" assert lang_c_output["ctype truncated uint64"] == "uint64_t" assert lang_c_output["ctype saturated int64"] == "int64_t" else: assert lang_c_output["ctype truncated uint32"] == "unsigned long" assert lang_c_output["ctype saturated int32"] == "long" assert lang_c_output["ctype truncated uint64"] == "unsigned long long" assert lang_c_output["ctype saturated int64"] == "long long" assert lang_c_output["ctype saturated bool"] == "bool" unique_name_evaluator(r'_nAME\d+_', lang_c_output["unique_name_0"]) unique_name_evaluator(r'_nAME\d+_', lang_c_output["unique_name_1"]) unique_name_evaluator(r'_naME\d+_', lang_c_output["unique_name_2"]) unique_name_evaluator(r'_\d+_', lang_c_output["unique_name_3"]) # cspell: enable return generated_values