Exemplo n.º 1
0
 def setUp(self):
     self.maxDiff = None
     Paths = namedtuple('Prop', 'enums_package structs_package struct_class')
     paths = Paths(enums_package='com.smartdevicelink.proxy.rpc.enums',
                   structs_package='com.smartdevicelink.proxy.rpc',
                   struct_class='com.smartdevicelink.proxy.RPCStruct')
     keywords = Generator().get_key_words()
     self.producer = StructsProducer(paths, ['SamplingRate'], ('Image',), keywords)
Exemplo n.º 2
0
    def setUp(self):
        self.maxDiff = None
        key_words = ('value', 'id')

        self.producer = StructsProducer('SDLRPCStruct',
                                        enum_names=(),
                                        struct_names=['Image'],
                                        key_words=key_words)
Exemplo n.º 3
0
 def setUp(self):
     self.maxDiff = None
     Prop = namedtuple(
         'Prop', 'structs_dir_name enums_dir_name path_to_struct_class')
     paths = Prop(enums_dir_name='../enums',
                  structs_dir_name='../structs',
                  path_to_struct_class='../RpcStruct.js')
     key_words = ('value', )
     self.producer = StructsProducer(paths, ['Image'],
                                     mapping={},
                                     key_words=key_words)
Exemplo n.º 4
0
    def main(self):
        """
        Entry point for parser and generator
        :return: None
        """
        args = self.get_parser()

        self.versions_compatibility_validating()

        enum_names, struct_names, interface = self.parser(
            xml=args.source_xml,
            xsd=args.source_xsd,
            pattern=args.regex_pattern)

        paths = self.get_paths()
        key_words = self.get_key_words()

        if args.enums and interface.enums:
            self.process(args.output_directory, args.skip, args.overwrite,
                         tuple(interface.enums.values()),
                         EnumsProducer(paths, key_words))
        if args.structs and interface.structs:
            self.process(
                args.output_directory, args.skip, args.overwrite,
                tuple(interface.structs.values()),
                StructsProducer(paths, enum_names, struct_names, key_words))
        if args.functions and interface.functions:
            self.process(
                args.output_directory, args.skip, args.overwrite,
                tuple(interface.functions.values()),
                FunctionsProducer(paths, enum_names, struct_names, key_words))
class TestStructsProducer(TestCase):
    def setUp(self):
        self.maxDiff = None
        Prop = namedtuple(
            'Prop', 'structs_dir_name enums_dir_name path_to_struct_class')
        paths = Prop(enums_dir_name='../enums',
                     structs_dir_name='../structs',
                     path_to_struct_class='../RpcStruct.js')
        key_words = ('value', )
        self.producer = StructsProducer(paths, ['Image'],
                                        mapping={},
                                        key_words=key_words)

    def test_SoftButton(self):
        members = OrderedDict()
        members['image'] = Param(name='image',
                                 param_type=Struct(name='Image'),
                                 description=['Optional image'])
        members['value'] = Param(name='value', param_type=String())
        item = Struct(name='SoftButton', members=members)
        expected = OrderedDict()
        expected['file_name'] = 'SoftButton'
        expected['name'] = 'SoftButton'
        expected['file_name'] = 'SoftButton'
        expected['imports'] = {
            self.producer.imports(what='Image', wherefrom='./Image.js'),
            self.producer.imports(what='RpcStruct',
                                  wherefrom='../RpcStruct.js')
        }
        expected['methods'] = (self.producer.methods(
            description=['Optional image'],
            external='Image',
            key='KEY_IMAGE',
            method_title='Image',
            param_name='image',
            type='Image',
            param_values={},
            deprecated=None,
            history=None,
            since=None),
                               self.producer.methods(description=[],
                                                     external=None,
                                                     key='KEY_VALUE',
                                                     method_title='ValueParam',
                                                     param_name='value',
                                                     type='String',
                                                     param_values={},
                                                     deprecated=None,
                                                     history=None,
                                                     since=None))
        expected['params'] = (self.producer.params(key='KEY_IMAGE',
                                                   value="'image'"),
                              self.producer.params(key='KEY_VALUE',
                                                   value="'value'"))
        expected['since'] = None
        expected['history'] = None
        expected['deprecated'] = None
        expected['extend'] = 'RpcStruct'
        result = self.producer.transform(item)
        self.assertDictEqual(expected, result)
Exemplo n.º 6
0
    def main(self):
        """
        Entry point for parser and generator
        :return: None
        """
        args = self.get_parser()
        self.config_logging(args.verbose)
        self.versions_compatibility_validating()
        self.output_directory = args.output_directory

        interface = self.loop.run_until_complete(
            self.parser(args.source_xml, args.source_xsd))
        paths = self.get_paths()

        self.env = [args.templates_directory] + [
            join(args.templates_directory, k) for k in vars(interface).keys()
        ]

        filtered, enum_names, struct_names = self.filter_pattern(
            interface, args.regex_pattern)

        tasks = []
        key_words = self.get_key_words()

        functions_transformer = FunctionsProducer(paths, enum_names,
                                                  struct_names, key_words)
        if args.enums and filtered.enums:
            tasks.append(
                self.process_main(args.skip, args.overwrite, filtered.enums,
                                  EnumsProducer(paths.enum_class, key_words)))
        if args.structs and filtered.structs:
            tasks.append(
                self.process_main(
                    args.skip, args.overwrite, filtered.structs,
                    StructsProducer(paths.struct_class, enum_names,
                                    struct_names, key_words)))
        if args.functions and filtered.functions:
            tasks.append(
                self.process_main(args.skip, args.overwrite,
                                  filtered.functions, functions_transformer))
            tasks.append(
                self.process_function_name(args.skip, args.overwrite,
                                           interface.functions,
                                           interface.structs,
                                           functions_transformer))
        if tasks:
            self.loop.run_until_complete(asyncio.wait(tasks))
        else:
            self.logger.warning('Nothing matched with "%s"',
                                args.regex_pattern)

        self.loop.close()
Exemplo n.º 7
0
    def main(self):
        """
        Entry point for parser and generator
        :return: None
        """
        args = self.get_parser()
        self.config_logging(args.verbose)
        self.logger.debug('parsed arguments:\n%s', pformat((vars(args))))
        self.env = args.templates_directory

        self.versions_compatibility_validating()

        paths = self.get_paths()

        try:
            interface = Parser().parse(args.source_xml, args.source_xsd)
        except ParseError as error1:
            self.logger.error(error1)
            sys.exit(1)

        filtered, names = self.filter_pattern(interface, args.regex_pattern)

        key_words = self.get_key_words()
        mappings = self.get_mappings()

        functions_transformer = FunctionsProducer(paths, names, mappings,
                                                  key_words)
        if args.enums and filtered.enums:
            directory = args.output_directory.joinpath(
                self.evaluate_instance_directory(paths.enums_dir_name))
            self.process(directory, args.skip, args.overwrite, filtered.enums,
                         EnumsProducer(paths, mappings, key_words))
        if args.structs and filtered.structs:
            directory = args.output_directory.joinpath(
                self.evaluate_instance_directory(paths.structs_dir_name))
            self.process(directory, args.skip, args.overwrite,
                         filtered.structs,
                         StructsProducer(paths, names, mappings, key_words))
        if args.functions and filtered.functions:
            directory = args.output_directory.joinpath(
                self.evaluate_instance_directory(paths.functions_dir_name))
            self.process(directory, args.skip, args.overwrite,
                         filtered.functions, functions_transformer)
        self.process_function_name(
            args.output_directory.joinpath(paths.rpc_creator),
            paths.functions_dir_name, args.skip, args.overwrite,
            interface.functions, functions_transformer,
            mappings.get('functions', OrderedDict()))
Exemplo n.º 8
0
class TestStructsProducer(TestCase):
    """
    The structures of tests in this class was prepared to cover all possible combinations of code branching in tested
    class StructsProducer.
    All names of Structs and nested elements doesn't reflating with real Structs
    and could be replaces with some meaningless names.

    After performing Tests there are following initial test code coverage:
    generator/transformers/common_producer.py	72%
    generator/transformers/structs_producer.py	100%
    """
    def setUp(self):
        self.maxDiff = None
        key_words = ('value', 'id')

        self.producer = StructsProducer('SDLRPCStruct',
                                        enum_names=(),
                                        struct_names=['Image'],
                                        key_words=key_words)

    def test_CloudAppProperties(self):
        """
        generator/transformers/common_producer.py	64%
        generator/transformers/structs_producer.py	100%
        """
        members = OrderedDict()
        members['appID'] = Param(name='appID', param_type=String())
        members['value'] = Param(name='value', param_type=String())
        item = Struct(name='CloudAppProperties', members=members)
        expected = OrderedDict()
        expected['origin'] = 'CloudAppProperties'
        expected['name'] = 'SDLCloudAppProperties'
        expected['extends_class'] = 'SDLRPCStruct'
        expected['imports'] = {
            '.m': set(),
            '.h': {
                'enum': {'SDLRPCStruct'},
                'struct': set()
            }
        }
        expected['params'] = (
            self.producer.param_named(
                constructor_argument='appID',
                constructor_argument_override=None,
                constructor_prefix='AppID',
                deprecated=False,
                description=[
                    '{"default_value": null, "max_length": null, "min_length": null}'
                ],
                for_name='object',
                mandatory=True,
                method_suffix='AppID',
                modifier='strong',
                of_class='NSString.class',
                origin='appID',
                since=None,
                type_native='NSString *',
                type_sdl='NSString *'),
            self.producer.param_named(
                constructor_argument='valueParam',
                constructor_argument_override=None,
                constructor_prefix='ValueParam',
                deprecated=False,
                description=[
                    '{"default_value": null, "max_length": null, "min_length": null}'
                ],
                for_name='object',
                mandatory=True,
                method_suffix='ValueParam',
                modifier='strong',
                of_class='NSString.class',
                origin='valueParam',
                since=None,
                type_native='NSString *',
                type_sdl='NSString *'))

        argument = [
            self.producer.argument_named(variable='appID',
                                         deprecated=False,
                                         constructor_argument='appID',
                                         origin='appID'),
            self.producer.argument_named(variable='valueParam',
                                         deprecated=False,
                                         constructor_argument='valueParam',
                                         origin='valueParam')
        ]

        expected['constructors'] = (self.producer.constructor_named(
            all=argument,
            arguments=argument,
            deprecated=False,
            self='',
            init='AppID:(NSString *)appID valueParam:(NSString *)valueParam'),
                                    )

        actual = self.producer.transform(item)
        self.assertDictEqual(expected, actual)

    def test_TouchEvent(self):
        """
        generator/transformers/common_producer.py	69%
        generator/transformers/structs_producer.py	100%
        """
        item = Struct(name='TouchEvent',
                      members={
                          'id':
                          Param(name='id',
                                param_type=Integer(max_value=9, min_value=0))
                      })
        expected = OrderedDict()
        expected['origin'] = 'TouchEvent'
        expected['name'] = 'SDLTouchEvent'
        expected['extends_class'] = 'SDLRPCStruct'
        expected['imports'] = {
            '.h': {
                'enum': {'SDLRPCStruct'},
                'struct': set()
            },
            '.m': set()
        }
        expected['params'] = (self.producer.param_named(
            constructor_argument='idParam',
            constructor_argument_override=None,
            constructor_prefix='IdParam',
            deprecated=False,
            description=[
                '{"default_value": null, "max_value": 9, "min_value": 0}'
            ],
            for_name='object',
            mandatory=True,
            method_suffix='IdParam',
            modifier='strong',
            of_class='NSNumber.class',
            origin='idParam',
            since=None,
            type_native='UInt8',
            type_sdl='NSNumber<SDLUInt> *'), )

        argument = [
            self.producer.argument_named(variable='idParam',
                                         deprecated=False,
                                         constructor_argument='@(idParam)',
                                         origin='idParam')
        ]

        expected['constructors'] = (self.producer.constructor_named(
            all=argument,
            arguments=argument,
            deprecated=False,
            self='',
            init='IdParam:(UInt8)idParam'), )

        actual = self.producer.transform(item)
        self.assertDictEqual(expected, actual)
Exemplo n.º 9
0
class TestStructsProducer(unittest.TestCase):
    def setUp(self):
        self.maxDiff = None
        Paths = namedtuple('Prop', 'enums_package structs_package struct_class')
        paths = Paths(enums_package='com.smartdevicelink.proxy.rpc.enums',
                      structs_package='com.smartdevicelink.proxy.rpc',
                      struct_class='com.smartdevicelink.proxy.RPCStruct')
        keywords = Generator().get_key_words()
        self.producer = StructsProducer(paths, ['SamplingRate'], ('Image',), keywords)

    def comparison(self, expected, actual):
        actual_params = dict(zip(map(lambda k: k.origin, actual['params']), actual['params']))
        for param in expected['params']:
            for field in self.producer.params._fields:
                self.assertEqual(getattr(param, field), getattr(actual_params[param.origin], field, None))
        expected_filtered = dict(filter(lambda e: e[0] != 'params', expected.items()))
        actual_filtered = dict(filter(lambda e: e[0] != 'params', actual.items()))

        self.assertDictEqual(expected_filtered, actual_filtered)

    def test_AudioPassThruCapabilities(self):
        members = OrderedDict()
        members['range'] = Param(name='range', param_type=Float(max_value=10000.0, min_value=0.0))
        members['samplingRate'] = Param(name='samplingRate', param_type=Enum(name='SamplingRate'))
        item = Struct(name='AudioPassThruCapabilities', members=members)
        expected = {
            'class_name': 'AudioPassThruCapabilities',
            'extends_class': 'RPCStruct',
            'package_name': 'com.smartdevicelink.proxy.rpc',
            'imports': ['androidx.annotation.NonNull', '', 'com.smartdevicelink.proxy.RPCStruct',
                        'com.smartdevicelink.proxy.rpc.enums.SamplingRate',
                        'com.smartdevicelink.util.SdlDataTypeConverter', '', 'java.util.Hashtable'],
            'deprecated': None,
            'since': None,
            'history': None,
            'params': (self.producer.params(deprecated=None, key='KEY_RANGE',
                                            last='range', mandatory=True,
                                            origin='range', return_type='Float',
                                            since=None, title='Range', description=None, param_doc=None, name=None),
                       self.producer.params(deprecated=None, key='KEY_SAMPLING_RATE',
                                            last='samplingRate', mandatory=True,
                                            origin='samplingRate', return_type='SamplingRate', name=None,
                                            since=None, title='SamplingRate', description=None, param_doc=None)),
        }
        actual = self.producer.transform(item)

        self.comparison(expected, actual)

    def test_CloudAppProperties(self):
        item = Struct(name='CloudAppProperties', members={
            'nicknames': Param(name='nicknames',
                               param_type=Array(element_type=String(max_length=100, min_length=0), max_size=100,
                                                min_size=0)),
        })
        expected = {
            'class_name': 'CloudAppProperties',
            'extends_class': 'RPCStruct',
            'package_name': 'com.smartdevicelink.proxy.rpc',
            'imports': ['androidx.annotation.NonNull', '', 'com.smartdevicelink.proxy.RPCStruct', '',
                        'java.util.Hashtable', 'java.util.List'],
            'deprecated': None,
            'since': None,
            'history': None,
            'params': (self.producer.params(deprecated=None, key='KEY_NICKNAMES',
                                            last='nicknames', mandatory=True,
                                            origin='nicknames', return_type='List<String>', name=None,
                                            since=None, title='Nicknames', description=None, param_doc=None),),
        }
        actual = self.producer.transform(item)
        self.comparison(expected, actual)

    def test_SoftButton(self):
        item = Struct(name='SoftButton', members={
            'image': Param(name='image', param_type=Struct(name='Image'), description=['Optional image']),
        }, description=['\n            Describes different audio type configurations for PerformAudioPassThru.\n     '])
        expected = {
            'package_name': 'com.smartdevicelink.proxy.rpc',
            'imports': ['androidx.annotation.NonNull', '', 'com.smartdevicelink.proxy.RPCStruct', '',
                        'java.util.Hashtable'],
            'class_name': 'SoftButton',
            'extends_class': 'RPCStruct',
            'since': None,
            'history': None,
            'deprecated': None,
            'description': ['Describes different audio type configurations for '
                            'PerformAudioPassThru.'],
            'params': (self.producer.params(deprecated=None, description=['Optional image'], key='KEY_IMAGE',
                                            last='image', mandatory=True, origin='image', return_type='Image',
                                            since=None, title='Image', param_doc=None, name=None),)
        }
        actual = self.producer.transform(item)
        self.comparison(expected, actual)

    def test_OASISAddress(self):
        item = Struct(name='OASISAddress', members={
            'countryName': Param(name='countryName', param_type=String(max_length=200))
        })
        expected = {
            'package_name': 'com.smartdevicelink.proxy.rpc',
            'imports': ['androidx.annotation.NonNull', '', 'com.smartdevicelink.proxy.RPCStruct', '',
                        'java.util.Hashtable'],
            'class_name': 'OASISAddress',
            'extends_class': 'RPCStruct',
            'since': None,
            'history': None,
            'deprecated': None,
            'params': (
                self.producer.params(deprecated=None, key='KEY_COUNTRY_NAME', last='countryName', mandatory=True,
                                     origin='countryName', return_type='String', since=None, title='CountryName',
                                     description=None, param_doc=None, name=None),)
        }
        actual = self.producer.transform(item)
        self.comparison(expected, actual)

    def test_LocationDetails(self):
        item = Struct(name='LocationDetails', members={
            'searchAddress': Param(name='searchAddress', param_type=Struct(name='OASISAddress'))
        })
        expected = {
            'package_name': 'com.smartdevicelink.proxy.rpc',
            'imports': ['androidx.annotation.NonNull', '', 'com.smartdevicelink.proxy.RPCStruct', '',
                        'java.util.Hashtable'],
            'class_name': 'LocationDetails',
            'extends_class': 'RPCStruct',
            'since': None,
            'history': None,
            'deprecated': None,
            'params': (
                self.producer.params(deprecated=None, key='KEY_SEARCH_ADDRESS', last='searchAddress', mandatory=True,
                                     origin='searchAddress', return_type='OASISAddress', since=None,
                                     title='SearchAddress', description=None, param_doc=None, name=None),)
        }
        actual = self.producer.transform(item)
        self.comparison(expected, actual)

    def test_SingleTireStatus(self):
        item = Struct(name='SingleTireStatus', members={
            'tpms': Param(name='tpms', param_type=Enum(name='TPMS'))
        })
        expected = {
            'package_name': 'com.smartdevicelink.proxy.rpc',
            'imports': ['androidx.annotation.NonNull', '', 'com.smartdevicelink.proxy.RPCStruct', '',
                        'java.util.Hashtable'],
            'class_name': 'SingleTireStatus',
            'extends_class': 'RPCStruct',
            'since': None,
            'history': None,
            'deprecated': None,
            'params': (
                self.producer.params(deprecated=None, key='KEY_TPMS', last='tpms', mandatory=True, origin='tpms',
                                     return_type='TPMS', since=None, title='Tpms', description=None, param_doc=None,
                                     name=None),)
        }
        actual = self.producer.transform(item)
        self.comparison(expected, actual)