def test_var_array_override_cpp(gen_paths): # type: ignore """ Make sure we can override the type generated for variable-length arrays. """ language_option_overrides = { 'variable_array_type': 'scotec::TerribleArray<{TYPE},{MAX_SIZE}>' } root_namespace = str(gen_paths.dsdl_dir / Path("radar")) compound_types = pydsdl.read_namespace( root_namespace, [], allow_unregulated_fixed_port_id=True) language_context = LanguageContext( 'cpp', language_options=language_option_overrides) namespace = build_namespace_tree(compound_types, root_namespace, gen_paths.out_dir, language_context) generator = DSDLCodeGenerator(namespace) generator.generate_all(False) assert_pattern_match_in_file( gen_paths.find_outfile_in_namespace("radar.Phased", namespace), re.compile( r'\s*scotec::TerribleArray<float,2677>\s+antennae_per_bank;\s*'), re.compile(r'\s*std::array<float,3>\s+bank_normal_rads;\s*'))
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 DSDLCodeGenerator, SupportGenerator return (DSDLCodeGenerator(namespace, **kwargs), SupportGenerator(namespace, **kwargs))
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 = DSDLCodeGenerator(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 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"]) return generated_values
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 = DSDLCodeGenerator(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