Example #1
0
 def setUp(self):
     self.maxDiff = None
     Prop = namedtuple(
         'Prop', 'enums_dir_name structs_dir_name path_to_enum_class')
     paths = Prop(enums_dir_name='../enums',
                  structs_dir_name='../structs',
                  path_to_enum_class='../../util/Enum.js')
     self.producer = EnumsProducer(paths)
Example #2
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))
Example #3
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()
Example #4
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()))
Example #5
0
class TestEnumsProducer(unittest.TestCase):
    def setUp(self):
        self.maxDiff = None
        Paths = namedtuple('Prop', 'enums_package')
        paths = Paths(enums_package='com.smartdevicelink.proxy.rpc.enums')
        keywords = Generator().get_key_words()
        self.producer = EnumsProducer(paths, keywords)

    def comparison(self, expected, actual):
        actual = OrderedDict(sorted(actual.items()))
        actual['params'] = tuple(sorted(actual['params'], key=lambda x: x.name))
        expected = OrderedDict(sorted(expected.items()))
        actual_params = dict(zip(map(lambda k: k.name, actual['params']), actual['params']))
        expected_params = dict(zip(map(lambda k: k.name, expected['params']), expected['params']))
        for key, param in actual_params.copy().items():
            self.assertTrue(key in expected_params)
        for key, param in expected_params.copy().items():
            self.assertTrue(key in actual_params)
            keys = actual_params[key]._asdict()
            Params = namedtuple('Params', sorted(keys))
            p = {name: getattr(param, name) for name in keys.keys()}
            expected_params[key] = Params(**p)
        expected['params'] = tuple(sorted(expected_params.values(), key=lambda x: x.name))

        self.assertDictEqual(expected, actual)

    def test_deprecated(self):
        item = Enum(name='TestDeprecated', deprecated=True, elements={
            'PRIMARY_WIDGET': EnumElement(name='PRIMARY_WIDGET', internal_name='PRIMARY_WIDGET', value=1,
                                          deprecated=True)
        })
        expected = {
            'kind': 'simple',
            'package_name': 'com.smartdevicelink.proxy.rpc.enums',
            'class_name': 'TestDeprecated',
            'params': (
                self.producer.params(name='PRIMARY_WIDGET', origin='PRIMARY_WIDGET', deprecated=True,
                                     internal=1, description=None, since=None, value=None),),
            'since': None,
            'history': None,
            'deprecated': True
        }
        actual = self.producer.transform(item)

        self.comparison(expected, actual)

    def test_Language(self):
        item = Enum(name='Language', elements={
            'EN-US': EnumElement(name='EN-US', internal_name='EN-US')
        })
        expected = {
            'kind': 'custom',
            'package_name': 'com.smartdevicelink.proxy.rpc.enums',
            'class_name': 'Language',
            'params': (
                self.producer.params(name='EN_US', origin='EN-US', internal='"EN-US"', description=None, since=None,
                                     value=None, deprecated=None),),
            'since': None,
            'history': None,
            'deprecated': None,
            'imports': {'java.util.EnumSet'}
        }
        actual = self.producer.transform(item)

        self.comparison(expected, actual)

    def test_PredefinedWindows(self):
        elements = OrderedDict()
        elements['DEFAULT_WINDOW'] = EnumElement(name='DEFAULT_WINDOW', value=0)
        elements['PRIMARY_WIDGET'] = EnumElement(name='PRIMARY_WIDGET', value=1)
        item = Enum(name='PredefinedWindows', elements=elements)
        expected = {
            'kind': 'simple',
            'package_name': 'com.smartdevicelink.proxy.rpc.enums',
            'class_name': 'PredefinedWindows',
            'params': (self.producer.params(name='DEFAULT_WINDOW', origin='DEFAULT_WINDOW',
                                            internal=0, description=None, since=None, value=None,
                                            deprecated=None),
                       self.producer.params(name='PRIMARY_WIDGET', origin='PRIMARY_WIDGET',
                                            internal=1, description=None, since=None, value=None,
                                            deprecated=None)),
            'since': None,
            'history': None,
            'deprecated': None
        }
        actual = self.producer.transform(item)

        self.comparison(expected, actual)

    def test_SamplingRate(self):
        item = Enum(name='SamplingRate', elements={
            '8KHZ': EnumElement(name='8KHZ', internal_name='SamplingRate_8KHZ')
        })
        expected = {
            'kind': 'custom',
            'package_name': 'com.smartdevicelink.proxy.rpc.enums',
            'class_name': 'SamplingRate',
            'imports': {'java.util.EnumSet'},
            'params': (
                self.producer.params(name='_8KHZ', origin='8KHZ', internal='"8KHZ"', description=None, since=None,
                                     value=None,
                                     deprecated=None),),
            'since': None,
            'history': None,
            'deprecated': None
        }
        actual = self.producer.transform(item)

        self.comparison(expected, actual)

    def test_Result(self):
        elements = OrderedDict()
        elements['SUCCESS'] = EnumElement(name='SUCCESS', description=['The request succeeded'])
        elements['VEHICLE_DATA_NOT_AVAILABLE'] = EnumElement(name='VEHICLE_DATA_NOT_AVAILABLE', since='2.0.0')
        item = Enum(name='Result', elements=elements)
        expected = {
            'kind': 'simple',
            'package_name': 'com.smartdevicelink.proxy.rpc.enums',
            'class_name': 'Result',
            'params': (
                self.producer.params(name='SUCCESS', origin='SUCCESS', internal=None,
                                     description=['The request succeeded'],
                                     since=None, value=None, deprecated=None),
                self.producer.params(name='VEHICLE_DATA_NOT_AVAILABLE', origin='VEHICLE_DATA_NOT_AVAILABLE',
                                     internal=None, description=None, since='2.0.0', value=None, deprecated=None)),
            'since': None,
            'history': None,
            'deprecated': None
        }
        actual = self.producer.transform(item)

        self.comparison(expected, actual)

    def test_DisplayType(self):
        item = Enum(name='DisplayType', deprecated=True, since='5.0.0', elements={
            'CID': EnumElement(name='CID', since='3.0.0')
        })
        expected = {
            'kind': 'simple',
            'package_name': 'com.smartdevicelink.proxy.rpc.enums',
            'class_name': 'DisplayType',
            'params': (
                self.producer.params(name='CID', origin='CID', internal=None, description=None,
                                     since='3.0.0', value=None, deprecated=None),),
            'since': '5.0.0',
            'history': None,
            'deprecated': True
        }
        actual = self.producer.transform(item)

        self.comparison(expected, actual)

    def test_SpeechCapabilities(self):
        item = Enum(name='SpeechCapabilities', since='1.0.0', elements={
            'TEXT': EnumElement(name='TEXT', internal_name='SC_TEXT')
        })
        expected = {
            'kind': 'simple',
            'package_name': 'com.smartdevicelink.proxy.rpc.enums',
            'class_name': 'SpeechCapabilities',
            'params': (
                self.producer.params(name='TEXT', origin='TEXT', description=None,
                                     since=None, value=None, deprecated=None, internal=None),),
            'since': '1.0.0',
            'history': None,
            'deprecated': None
        }
        actual = self.producer.transform(item)

        self.comparison(expected, actual)

    def test_VrCapabilities1(self):
        item = Enum(name='VrCapabilities', elements={
            'TEXT': EnumElement(name='TEXT', internal_name='VR_TEXT')
        })
        expected = {
            'kind': 'simple',
            'package_name': 'com.smartdevicelink.proxy.rpc.enums',
            'class_name': 'VrCapabilities',
            'params': (
                self.producer.params(name='TEXT', origin='TEXT', description=None,
                                     since=None, value=None, deprecated=None, internal=None),),
            'since': None,
            'history': None,
            'deprecated': None
        }
        actual = self.producer.transform(item)

        self.comparison(expected, actual)

    def test_ButtonName(self):
        item = Enum(name='ButtonName', elements={
            'OK': EnumElement(name='OK')
        })
        expected = {
            'kind': 'simple',
            'package_name': 'com.smartdevicelink.proxy.rpc.enums',
            'class_name': 'ButtonName',
            'params': (
                self.producer.params(deprecated=None, value=None, description=None,
                                     name='OK', origin='OK', since=None, internal=None),),
            'since': None,
            'history': None,
            'deprecated': None
        }
        actual = self.producer.transform(item)

        self.comparison(expected, actual)

    def test_Dimension(self):
        item = Enum(name='Dimension', elements={
            'NO_FIX': EnumElement(name='NO_FIX', internal_name='Dimension_NO_FIX'),
            '2D': EnumElement(name='2D', internal_name='Dimension_2D')
        })
        expected = {
            'kind': 'custom',
            'package_name': 'com.smartdevicelink.proxy.rpc.enums',
            'class_name': 'Dimension',
            'params': (
                self.producer.params(deprecated=None, value=None, description=None,
                                     name='NO_FIX', origin='NO_FIX', since=None, internal='"NO_FIX"'),
                self.producer.params(deprecated=None, value=None, description=None,
                                     name='_2D', origin='2D', since=None, internal='"2D"'),),
            'since': None,
            'history': None,
            'deprecated': None,
            'imports': {'java.util.EnumSet'}
        }
        actual = self.producer.transform(item)

        self.comparison(expected, actual)

    def test_VehicleDataEventStatus(self):
        item = Enum(name='VehicleDataEventStatus', elements={
            'NO_EVENT': EnumElement(name='NO_EVENT', internal_name='VDES_NO_EVENT')
        })
        expected = {
            'kind': 'simple',
            'package_name': 'com.smartdevicelink.proxy.rpc.enums',
            'class_name': 'VehicleDataEventStatus',
            'params': (
                self.producer.params(deprecated=None, value=None, description=None,
                                     name='NO_EVENT', origin='NO_EVENT', since=None, internal=None),),
            'since': None,
            'history': None,
            'deprecated': None,
        }
        actual = self.producer.transform(item)

        self.comparison(expected, actual)

    def test_SystemCapabilityType(self):
        elements = OrderedDict()
        elements['DISPLAYS'] = EnumElement(name='DISPLAYS')
        elements['NAVIGATION'] = EnumElement(name='NAVIGATION')

        item = Enum(name='SystemCapabilityType', elements=elements)
        expected = {
            'kind': 'simple',
            'package_name': 'com.smartdevicelink.proxy.rpc.enums',
            'class_name': 'SystemCapabilityType',
            'params': (
                self.producer.params(deprecated=None, value=None, description=None,
                                     name='DISPLAYS', origin='DISPLAYS', since=None, internal=None),
                self.producer.params(deprecated=None, value=None, description=None,
                                     name='NAVIGATION', origin='NAVIGATION', since=None, internal=None)),
            'since': None,
            'history': None,
            'deprecated': None,
        }
        actual = self.producer.transform(item)

        self.comparison(expected, actual)
Example #6
0
 def setUp(self):
     self.maxDiff = None
     Paths = namedtuple('Prop', 'enums_package')
     paths = Paths(enums_package='com.smartdevicelink.proxy.rpc.enums')
     keywords = Generator().get_key_words()
     self.producer = EnumsProducer(paths, keywords)
Example #7
0
    def setUp(self):
        self.maxDiff = None
        key_words = Generator().get_key_words()

        self.producer = EnumsProducer('SDLEnum', key_words)
Example #8
0
class TestEnumsProducer(TestCase):
    """
    The structures of tests in this class was prepared to cover all possible combinations of code branching in tested
    class EnumsProducer.
    All names of Enums and nested elements doesn't reflating with real Enums
    and could be replaces with some meaningless names.

    After performing Tests there are following initial test code coverage:
    generator/transformers/common_producer.py	34%
    generator/transformers/enums_producer.py	100%
    """

    def setUp(self):
        self.maxDiff = None
        key_words = Generator().get_key_words()

        self.producer = EnumsProducer('SDLEnum', key_words)

    def test_FunctionID(self):
        """
        generator/transformers/common_producer.py	34%
        generator/transformers/enums_producer.py	80%
        """
        elements = OrderedDict()
        elements['RESERVED'] = EnumElement(name='RESERVED', value=0)
        elements['RegisterAppInterfaceID'] = EnumElement(name='RegisterAppInterfaceID', hex_value=1)
        elements['PerformAudioPassThruID'] = EnumElement(name='PerformAudioPassThruID', hex_value=10)

        item = Enum(name='FunctionID', elements=elements)
        expected = OrderedDict()
        expected['origin'] = 'FunctionID'
        expected['name'] = 'SDLFunctionID'
        expected['imports'] = {
            '.h': {'SDLEnum'},
            '.m': ['SDLEnum']
        }
        expected['history'] = None
        expected['params'] = (
            self.producer.param_named(history=None, description=[], name='Reserved', origin='RESERVED', since=None, deprecated=False),
            self.producer.param_named(history=None, description=[], name='RegisterAppInterface', origin='RegisterAppInterfaceID',
                                      since=None, deprecated=False),
            self.producer.param_named(history=None, description=[], name='PerformAudioPassThru', origin='PerformAudioPassThruID',
                                      since=None, deprecated=False),)

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

    def test_TextFieldName(self):
        """
        generator/transformers/common_producer.py	34%
        generator/transformers/enums_producer.py	98%
        """
        elements = OrderedDict()
        elements['SUCCESS'] = EnumElement(name='SUCCESS')
        elements['mainField1'] = EnumElement(name='mainField1')
        elements['H264'] = EnumElement(name='H264')
        elements['UNSUPPORTED_REQUEST'] = EnumElement(name='UNSUPPORTED_REQUEST')
        item = Enum(name='TextFieldName', elements=elements)

        expected = OrderedDict()
        expected['origin'] = 'TextFieldName'
        expected['name'] = 'SDLTextFieldName'
        expected['imports'] = {
            '.h': {'SDLEnum'},
            '.m': ['SDLEnum']
        }
        expected['history'] = None
        expected['params'] = (
            self.producer.param_named(history=None, description=[], name='Success', origin='SUCCESS', since=None,
                                      deprecated=False),
            self.producer.param_named(history=None, description=[], name='MainField1', origin='mainField1', since=None,
                                      deprecated=False),
            self.producer.param_named(history=None, description=[], name='H264', origin='H264', since=None, deprecated=False),
            self.producer.param_named(history=None, description=[], name='UnsupportedRequest', origin='UNSUPPORTED_REQUEST',
                                      since=None, deprecated=False))

        actual = self.producer.transform(item)
        self.assertDictEqual(expected, actual)
Example #9
0
class TestEnumsProducer(TestCase):
    def setUp(self):
        self.maxDiff = None
        Prop = namedtuple(
            'Prop', 'enums_dir_name structs_dir_name path_to_enum_class')
        paths = Prop(enums_dir_name='../enums',
                     structs_dir_name='../structs',
                     path_to_enum_class='../../util/Enum.js')
        self.producer = EnumsProducer(paths)

    def test_FunctionID(self):
        elements = OrderedDict()
        elements['RESERVED'] = EnumElement(name='RESERVED', value=0)
        elements['RegisterAppInterfaceID'] = EnumElement(
            name='RegisterAppInterfaceID', hex_value=1)
        elements['PerformAudioPassThruID'] = EnumElement(
            name='PerformAudioPassThruID', hex_value=10)

        item = Enum(name='FunctionID', elements=elements)
        expected = OrderedDict()
        expected['file_name'] = 'FunctionID'
        expected['name'] = 'FunctionID'
        expected['imports'] = {
            self.producer.imports(what='Enum', wherefrom='../../util/Enum.js')
        }
        expected['methods'] = (self.producer.methods(method_title='RESERVED',
                                                     description=[],
                                                     type='Number',
                                                     since=None,
                                                     history=None,
                                                     deprecated=None),
                               self.producer.methods(
                                   method_title='RegisterAppInterface',
                                   description=[],
                                   type='Number',
                                   since=None,
                                   history=None,
                                   deprecated=None),
                               self.producer.methods(
                                   method_title='PerformAudioPassThru',
                                   description=[],
                                   type='Number',
                                   since=None,
                                   history=None,
                                   deprecated=None))
        expected['params'] = (self.producer.params(key='RESERVED',
                                                   value="'RESERVED'"),
                              self.producer.params(key='RegisterAppInterface',
                                                   value='0x01'),
                              self.producer.params(key='PerformAudioPassThru',
                                                   value='0x10'))
        expected['since'] = None
        expected['history'] = None
        expected['deprecated'] = None
        expected['extend'] = 'Enum'
        result = self.producer.transform(item)
        self.assertDictEqual(expected, result)

    def test_Result(self):
        elements = OrderedDict()
        elements['SUCCESS'] = EnumElement(name='SUCCESS')
        item = Enum(name='Result', elements=elements)
        expected = OrderedDict()
        expected['file_name'] = 'Result'
        expected['name'] = 'Result'
        expected['file_name'] = 'Result'
        expected['imports'] = {
            self.producer.imports(what='Enum', wherefrom='../../util/Enum.js')
        }
        expected['methods'] = (self.producer.methods(method_title='SUCCESS',
                                                     description=[],
                                                     type='String',
                                                     since=None,
                                                     history=None,
                                                     deprecated=None), )
        expected['params'] = (self.producer.params(key='SUCCESS',
                                                   value="'SUCCESS'"), )
        expected['since'] = None
        expected['history'] = None
        expected['deprecated'] = None
        expected['extend'] = 'Enum'
        result = self.producer.transform(item)
        self.assertDictEqual(expected, result)