Exemple #1
0
def test_empty_namespace(gen_paths):  # type: ignore
    """Test a namespace object with no children."""
    namespace = Namespace('', gen_paths.dsdl_dir, gen_paths.out_dir, LanguageContext(extension='.txt'))
    assert namespace.full_name == ''
    assert namespace.output_folder == gen_paths.out_dir
    assert namespace.source_file_path == gen_paths.dsdl_dir
    assert len(namespace.data_types) == 0
    assert (gen_paths.out_dir / Path('_')).with_suffix('.txt') == namespace.find_output_path_for_type(namespace)
    assert namespace == namespace
    assert hash(namespace) == hash(namespace)
    assert str(namespace) == str(namespace)
    with pytest.raises(KeyError):
        namespace.find_output_path_for_type(DummyType())
Exemple #2
0
def create_support_generator(
        namespace: nunavut.Namespace) -> 'AbstractGenerator':
    """
    Create a new :class:`Generator <nunavut.generators.AbstractGenerator>` that uses embedded support
    headers, libraries, and other types needed to use generated serialization code for the
    :func:`target language <nunavut.lang.LanguageContext.get_target_language>`. If no target language
    is set or if serialization support has been turned off a no-op generator will be returned instead.
    """
    class _NoOpSupportGenerator(AbstractGenerator):
        def get_templates(self) -> typing.Iterable[pathlib.Path]:
            return []

        def generate_all(self,
                         is_dryrun: bool = False,
                         allow_overwrite: bool = True) \
                -> typing.Iterable[pathlib.Path]:
            return []

    target_language = namespace.get_language_context().get_target_language()

    if target_language is None or target_language.omit_serialization_support:
        return _NoOpSupportGenerator(namespace, nunavut.YesNoDefault.DEFAULT)
    else:

        #  Create the sub-folder to copy-to based on the support namespace.
        sub_folders = pathlib.Path('')

        for namespace_part in target_language.support_namespace:
            sub_folders = sub_folders / pathlib.Path(namespace_part)

        return CopyFromPackageGenerator(namespace,
                                        target_language.support_files,
                                        sub_folders)
Exemple #3
0
def filter_namespace_doc(ns: nunavut.Namespace) -> str:
    result = ""
    for t, _ in ns.get_nested_types():
        if t.short_name == "_":
            result = t.doc
            break
    return result
Exemple #4
0
def test_python35_resolve_behavior(gen_paths):  # type: ignore
    """Make sure Python3.5 and Python 3.6 throw the same exception here."""
    language_context = LanguageContext('c')
    with pytest.raises(FileNotFoundError):
        Namespace('foo.bar',
                  gen_paths.dsdl_dir / Path("scotec"),
                  gen_paths.out_dir,
                  language_context)
Exemple #5
0
def test_namespace_eq(gen_paths):  # type: ignore
    """Verify the get_all_namespaces method in Namespace"""
    language_context = LanguageContext(extension='.json')
    namespace0, _, _ = gen_test_namespace(gen_paths, language_context)
    namespace1 = Namespace('', gen_paths.dsdl_dir, gen_paths.out_dir, language_context)
    assert namespace0 == namespace0
    assert namespace1 == namespace1
    assert namespace0 != namespace1
    assert "foo" != namespace0
Exemple #6
0
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})
Exemple #7
0
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
Exemple #8
0
    def find_outfile_in_namespace(typename: str,
                                  namespace: Namespace,
                                  type_version: pydsdl.Version = None) \
            -> typing.Optional[str]:
        found_outfile = None  # type: typing.Optional[str]
        for dsdl_type, outfile in namespace.get_all_types():
            if dsdl_type.full_name == typename:
                if type_version is not None:
                    if isinstance(dsdl_type, pydsdl.CompositeType) and type_version == dsdl_type.version:
                        found_outfile = str(outfile)
                        break
                    # else ignore this since it's either a namespace or it's not the version
                    # of the type we're looking for.
                elif found_outfile is not None:
                    raise RuntimeError('Type {} had more than one version for this test but no type version argument was'
                                   ' provided.'.format(typename))
                else:
                    found_outfile = str(outfile)

        return found_outfile
Exemple #9
0
 def find_outfile_in_namespace(typename: str, namespace: Namespace) -> typing.Optional[str]:
     for dsdl_type, outfile in namespace.get_all_types():
         if dsdl_type.full_name == typename:
             return str(outfile)
     return None