def test_RegisterAppInterface(self): params = OrderedDict() params['syncMsgVersion'] = Param( name='syncMsgVersion', param_type=Struct(name='SyncMsgVersion')) params['ttsName'] = Param( name='ttsName', param_type=Array(element_type=Struct(name='TTSChunk'))) item = Function(name='RegisterAppInterface', function_id=None, message_type=EnumElement(name='request'), params=params) expected = self.expected_template.copy() expected['kind'] = 'request' expected['function_id'] = 'REGISTER_APP_INTERFACE' expected['class_name'] = 'RegisterAppInterface' expected['extends_class'] = 'RPCRequest' expected['history'] = None expected['imports'] = [ 'androidx.annotation.NonNull', '', 'com.smartdevicelink.protocol.enums.FunctionID', 'com.smartdevicelink.proxy.RPCRequest', '', 'java.util.Hashtable', 'java.util.List' ] expected['params'] = (self.producer.params(deprecated=None, key='KEY_SDL_MSG_VERSION', last='sdlMsgVersion', mandatory=True, SuppressWarnings=None, origin='syncMsgVersion', return_type='SdlMsgVersion', since=None, title='SdlMsgVersion', description=None, param_doc=None, name=None), self.producer.params( SuppressWarnings='unchecked', deprecated=None, key='KEY_TTS_NAME', last='ttsName', mandatory=True, origin='ttsName', description=None, return_type='List<TTSChunk>', since=None, title='TtsName', param_doc=None, name=None)) actual = self.producer.transform(item) self.comparison(expected, actual)
def _parse_struct(self, element, prefix): """Parse element as structure. :param element: an instance of Element (from ElementTree) :param prefix: empty string :return: an instance of model.Struct """ params, subelements, attrib = self._parse_base_item(element, prefix) # Create an empty object for new type to types collection. # This is needed for parser to apply type for struct members # that consist of its own type. # E.g.: # struct VideoStreamingCapability { # ... # VideoStreamingCapability additionalVideoStreamingCapabilities[] # } struct = Struct(**params) self._add_type(struct) for attribute in attrib: if attribute in ["scope", "deprecated", "removed"]: params[attribute] = attrib[attribute] elif attribute in ["since", "until"]: params[attribute] = self._parse_version(attrib[attribute]) else: raise ParseError( "Unexpected attribute '{}' in struct '{}'".format( attribute, params["name"])) members = OrderedDict() for subelement in subelements: if subelement.tag == "param": self._evaluate_element( members, self._parse_struct_param(subelement, prefix)) else: raise ParseError( "Unexpected subelement '{}' in struct '{}'".format( subelement.name, params["name"])) params["members"] = members # Remove empty object for new type to prevent errors of adding such type twice (see self._add_type). # After return statement of current method is done the new type with all its params # will be added into types collection. self._types.pop(struct.name, None) return Struct(**params)
def test_process_function_name(self): """ generator/transformers/common_producer.py 29% generator/transformers/functions_producer.py 61% """ functions = { 'RegisterAppInterface': Function(name='RegisterAppInterface', function_id=EnumElement(name='RegisterAppInterfaceID'), since='3.0.0', message_type=EnumElement(name='request'), description=['RegisterAppInterface description'], params={ 'syncMsgVersion': Param(name='syncMsgVersion', param_type=Float(), since='3.5.0', description=['syncMsgVersion description'])}), 'OnHMIStatus': Function(name='OnHMIStatus', function_id=EnumElement(name='OnHMIStatusID'), since='4.0.0', message_type=EnumElement(name='notification'), description=['OnHMIStatus description'], params={ 'acEnable': Param(name='acEnable', param_type=Integer(), since='4.5.0', description=['acEnable description'])})} structs = { 'SoftButton': Struct(name='SoftButton', members={ 'image': Param(name='image', param_type=String(), since='1.0.0', description=['image description']), 'dayColorScheme': Param(name='dayColorScheme', param_type=Struct(name='TemplateColorScheme', description=[ '\nA color scheme for all display layout templates.\n']))}), 'PresetBankCapabilities': Struct(name='PresetBankCapabilities', members={ 'availableHdChannelsAvailable': Param(name='availableHdChannelsAvailable', param_type=Boolean(), since='2.0.0', description=['availableHDChannelsAvailable description'])})} expected = [ self.producer.common_names( description=['OnHMIStatus description'], name='OnHMIStatus', origin='OnHMIStatus', since='4.0.0'), self.producer.common_names( description=['RegisterAppInterface description'], name='RegisterAppInterface', origin='RegisterAppInterface', since='3.0.0')] actual = self.producer.get_function_names(functions) self.assertListEqual(expected, actual['params']) expected = [ self.producer.common_names(description=['acEnable description'], name='AcEnable', origin='acEnable', since='4.5.0'), self.producer.common_names(description=['availableHDChannelsAvailable description'], since='2.0.0', name='AvailableHdChannelsAvailable', origin='availableHdChannelsAvailable'), self.producer.common_names(description=[], name='DayColorScheme', origin='dayColorScheme', since=None), self.producer.common_names(description=['image description'], name='Image', origin='image', since='1.0.0'), self.producer.common_names(description=['syncMsgVersion description'], name='SdlMsgVersion', origin='syncMsgVersion', since='3.5.0')] actual = self.producer.get_simple_params(functions, structs) self.assertCountEqual(expected, actual['params'])
def _parse_struct(self, element, prefix): """Parse element as structure. :param element: an instance of Element (from ElementTree) :param prefix: empty string :return: an instance of model.Struct """ params, subelements, attrib = self._parse_base_item(element, prefix) for attribute in attrib: if attribute in ["scope", "deprecated", "removed"]: params[attribute] = attrib[attribute] elif attribute in ["since", "until"]: params[attribute] = self._parse_version(attrib[attribute]) else: raise ParseError( "Unexpected attribute '{}' in struct '{}'".format( attribute, params["name"])) members = OrderedDict() for subelement in subelements: if subelement.tag == "param": self._evaluate_element( members, self._parse_struct_param(subelement, prefix)) else: raise ParseError( "Unexpected subelement '{}' in struct '{}'".format( subelement.name, params["name"])) params["members"] = members return Struct(**params)
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_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)
def test_CreateInteractionChoiceSet(self): """ generator/transformers/common_producer.py 67% generator/transformers/functions_producer.py 63% """ params = OrderedDict() params['choiceSet'] = Param( name='choiceSet', param_type=Array(element_type=Struct(name='Choice'))) item = Function( name='CreateInteractionChoiceSet', function_id=EnumElement(name='CreateInteractionChoiceSetID'), message_type=EnumElement(name='request'), params=params) expected = OrderedDict() expected['origin'] = 'CreateInteractionChoiceSet' expected['name'] = 'SDLCreateInteractionChoiceSet' expected['extends_class'] = 'SDLRPCRequest' expected['imports'] = { '.m': {'SDLChoice'}, '.h': { 'struct': {'SDLChoice'}, 'enum': {'SDLRPCRequest'} } } expected['params'] = (self.producer.param_named( constructor_argument='choiceSet', constructor_argument_override=None, constructor_prefix='ChoiceSet', deprecated=False, description=[], for_name='objects', mandatory=True, method_suffix='ChoiceSet', modifier='strong', of_class='SDLChoice.class', origin='choiceSet', since=None, type_native='NSArray<SDLChoice *> *', type_sdl='NSArray<SDLChoice *> *'), ) argument = [ self.producer.argument_named(variable='choiceSet', deprecated=False, constructor_argument='choiceSet', origin='choiceSet') ] expected['constructors'] = (self.producer.constructor_named( all=argument, arguments=argument, deprecated=False, self=True, init='ChoiceSet:(NSArray<SDLChoice *> *)choiceSet'), ) 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': [] }, '.m': [ 'NSMutableDictionary+Store', 'SDLRPCParameterNames', 'SDLTouchEvent' ] } expected['history'] = None expected['params'] = (self.producer.param_named( history=None, constructor_argument='idParam', constructor_argument_override=None, constructor_prefix='IdParam', deprecated=False, description=['{"num_min_value": 0, "num_max_value": 9}'], 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, self='', init='IdParam:(UInt8)idParam'), ) actual = self.producer.transform(item) self.assertDictEqual(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': ['android.support.annotation.NonNull', '', 'com.smartdevicelink.proxy.RPCStruct', '', 'java.util.Hashtable'], 'class_name': 'LocationDetails', 'extends_class': 'RPCStruct', 'since': 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_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': ['android.support.annotation.NonNull', '', 'com.smartdevicelink.proxy.RPCStruct', '', 'java.util.Hashtable'], 'class_name': 'SoftButton', 'extends_class': 'RPCStruct', 'since': 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_CreateInteractionChoiceSetRequest(self): params = OrderedDict() params['choiceSet'] = Param( name='choiceSet', param_type=Array(element_type=Struct( name='Choice', description=['A choice is an option given to ' ]))) item = Function(name='CreateInteractionChoiceSet', function_id=Enum(name='CreateInteractionChoiceSetID'), description=['creates interaction'], message_type=EnumElement(name='request'), params=params) expected = OrderedDict() expected['file_name'] = 'CreateInteractionChoiceSet' expected['name'] = 'CreateInteractionChoiceSet' expected['imports'] = { self.producer.imports(what='Choice', wherefrom='../structs/Choice.js'), self.producer.imports(what='RpcRequest', wherefrom='../RpcRequest.js'), self.producer.imports(what='FunctionID', wherefrom='../enums/FunctionID.js') } expected['methods'] = (self.producer.methods( key='KEY_CHOICE_SET', method_title='ChoiceSet', external='Choice', description=['A choice is an option given to'], param_name='set', type='Choice[]', param_values={}, since=None, history=None, deprecated=None), ) expected['params'] = (self.producer.params(key='KEY_CHOICE_SET', value="'choiceSet'"), ) expected['description'] = ['creates interaction'] expected['since'] = None expected['history'] = None expected['deprecated'] = None expected['func'] = 'CreateInteractionChoiceSet' expected['extend'] = 'RpcRequest' result = self.producer.transform(item) self.assertDictEqual(expected, result)
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': ['android.support.annotation.NonNull', '', 'com.smartdevicelink.proxy.RPCStruct', '', 'java.util.Hashtable'], 'class_name': 'SingleTireStatus', 'extends_class': 'RPCStruct', 'since': 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)
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': ['android.support.annotation.NonNull', '', 'com.smartdevicelink.proxy.RPCStruct', '', 'java.util.Hashtable'], 'class_name': 'OASISAddress', 'extends_class': 'RPCStruct', 'since': 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_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': ['android.support.annotation.NonNull', '', 'com.smartdevicelink.proxy.RPCStruct', '', 'java.util.Hashtable', 'java.util.List'], 'deprecated': None, 'since': 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 transform(self, item: Struct, render: dict = None) -> dict: """ Main entry point for transforming each Enum/Function/Struct into output dictionary, which going to be applied to Jinja2 template :param item: instance of Enum/Function/Struct :param render: dictionary with pre filled entries, which going to be filled/changed by reference :return: dictionary which going to be applied to Jinja2 template """ item.name = self._replace_sync(item.name) name = 'SDL' + item.name imports = {'.h': {'enum': set(), 'struct': set()}, '.m': set()} imports['.h']['enum'].add(self.struct_class) if not render: render = OrderedDict() render['origin'] = item.name render['name'] = name render['extends_class'] = self.struct_class render['imports'] = imports super(StructsProducer, self).transform(item, render) return render
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_RegisterAppInterfaceRequest(self): params = OrderedDict() params['syncMsgVersion'] = Param(name='syncMsgVersion', param_type=Struct( name='SyncMsgVersion', description=['Specifies the'], members={ 'majorVersion': Param(name='majorVersion', param_type=Integer()) }), description=['See SyncMsgVersion']) params['fullAppID'] = Param(name='fullAppID', description=['ID used'], param_type=String()) params['dayColorScheme'] = Param( name='dayColorScheme', param_type=Struct( name='TemplateColorScheme', description=[ '\n A color scheme for all display layout templates.\n ' ])) params['ttsName'] = Param( name='ttsName', description=['\n TTS string for'], param_type=Array(element_type=Struct(name='TTSChunk', description=['A TTS chunk']))) item = Function(name='RegisterAppInterface', function_id=Enum(name='RegisterAppInterfaceID'), message_type=EnumElement(name='request'), params=params) expected = OrderedDict() expected['file_name'] = 'RegisterAppInterface' expected['name'] = 'RegisterAppInterface' expected['imports'] = { self.producer.imports(what='SdlMsgVersion', wherefrom='../structs/SdlMsgVersion.js'), self.producer.imports( what='TemplateColorScheme', wherefrom='../structs/TemplateColorScheme.js'), self.producer.imports(what='TTSChunk', wherefrom='../structs/TTSChunk.js'), self.producer.imports(what='RpcRequest', wherefrom='../RpcRequest.js'), self.producer.imports(what='FunctionID', wherefrom='../enums/FunctionID.js') } expected['methods'] = ( self.producer.methods(key='KEY_SDL_MSG_VERSION', method_title='SdlMsgVersion', external='SdlMsgVersion', description=['See SyncMsgVersion'], param_name='version', type='SdlMsgVersion', param_values={}, since=None, history=None, deprecated=None), self.producer.methods(key='KEY_FULL_APP_ID', method_title='FullAppID', external=None, description=['ID used'], param_name='id', type='String', param_values={}, since=None, history=None, deprecated=None), self.producer.methods( key='KEY_DAY_COLOR_SCHEME', param_name='scheme', method_title='DayColorScheme', external='TemplateColorScheme', description=[ 'A color scheme for all display layout templates.' ], type='TemplateColorScheme', param_values={}, since=None, history=None, deprecated=None), self.producer.methods(key='KEY_TTS_NAME', param_name='name', method_title='TtsName', external='TTSChunk', description=['TTS string for'], type='TTSChunk[]', param_values={}, since=None, history=None, deprecated=None)) expected['params'] = (self.producer.params(key='KEY_SDL_MSG_VERSION', value="'syncMsgVersion'"), self.producer.params(key='KEY_FULL_APP_ID', value="'fullAppID'"), self.producer.params(key='KEY_DAY_COLOR_SCHEME', value="'dayColorScheme'"), self.producer.params(key='KEY_TTS_NAME', value="'ttsName'"), self.producer.params(key='APP_ID_MAX_LENGTH', value=10)) expected['since'] = None expected['history'] = None expected['deprecated'] = None expected['func'] = 'RegisterAppInterface' expected['extend'] = 'RpcRequest' result = self.producer.transform(item) self.assertDictEqual(expected, result)
def test_full_generation(self): """Test full generation using SDLPRC SmartSchema generator. Creates output files which is captured by the mock and compare them with sample files with correct code. This test requires valid test_expected_sdlrpc.h and test_expected_sdlrpc.cc in the same as this module. """ self.maxDiff = None expected_h_file_content = Path(__file__).parents[0].joinpath('test_expected_sdlrpc.h').read_text() expected_cc_file_content = Path(__file__).parents[0].joinpath('test_expected_sdlrpc.cc').read_text() generator = SmartFactorySDLRPC.CodeGenerator() message_type_elements = collections.OrderedDict() message_type_elements[u"request"] = EnumElement(name=u"request") message_type_elements[u"response"] = EnumElement( name=u"response") message_type_elements[u"notification"] = EnumElement( name=u"notification") message_type = Enum(name=u"messageType", elements=message_type_elements) elements1 = collections.OrderedDict() elements1[u"name1"] = EnumElement( name=u"name1", design_description=DESIGN_DESCRIPTION, todos=TODOS, value=u"1") elements1[u"name2"] = EnumElement( name="name2", description=DESCRIPTION, issues=ISSUES, internal_name=u"internal_name2") enum1 = Enum(name=u"Enum1", todos=TODOS, elements=elements1) elements2 = collections.OrderedDict() elements2[u"xxx"] = EnumElement(name=u"xxx", internal_name=u"val_1") elements2[u"yyy"] = EnumElement(name=u"yyy", internal_name=u"val_2", value=u"100") elements2[u"zzz"] = EnumElement(name=u"val_3") enum2 = Enum(name=u"E2", elements=elements2) elements3 = collections.OrderedDict() elements3["1"] = EnumElement(name="xxx", internal_name="_1") elements3["2"] = EnumElement(name="xxx", internal_name="_2") elements3["3"] = EnumElement(name="xxx", internal_name="_3") enum3 = Enum(name="Enum_new2", elements=elements3) elements4 = collections.OrderedDict() elements4["name1"] = EnumElement(name="xxx", internal_name="_11") elements4["name2"] = EnumElement(name="xxx", internal_name="_22") enum4 = Enum(name="Enum_new4", elements=elements4) enums = collections.OrderedDict() enums["Enum1"] = enum1 enums["Enum2"] = enum2 enums["Enum3"] = enum3 enums["Enum4"] = enum4 enums["messageType"] = message_type params1 = collections.OrderedDict() params1["1"] = Param( name="param1", design_description=DESIGN_DESCRIPTION, description=DESCRIPTION, issues=ISSUES, todos=TODOS, param_type=enum4, default_value=elements4["name1"]) params1["2"] = Param( name="param2", param_type=EnumSubset( name="sub1", enum=enum1, allowed_elements={"e1": elements1["name1"]}), default_value=elements1["name1"]) functions = collections.OrderedDict() functions["Function1"] = Function( name="Function1", function_id=elements1["name1"], message_type=message_type_elements["request"], params=params1) functions["Function2"] = Function( name="Function2", function_id=elements2["xxx"], message_type=message_type_elements["response"]) functions["Function3"] = Function( name="Function2", function_id=elements2["yyy"], message_type=message_type_elements["notification"]) members1 = collections.OrderedDict() members1["m1"] = Param(name="intParam", param_type=Integer(max_value=2)) members1["m11"] = Param(name="doubleParam", param_type=Float(min_value=0.333), is_mandatory=False) members1["m222"] = Param(name="boolParam", param_type=Boolean()) members1["m2"] = Param(name="structParam", param_type=Struct(name="Struct2")) members1["aaa"] = Param(name="enumParam", param_type=enum1) members1["bbb"] = Param(name="enumParam1", param_type=enum1) members1["xxx"] = Param( name="enumSubset1", param_type=EnumSubset( name="sub", enum=enum1, allowed_elements={"e1": elements1["name1"]}), is_mandatory=False) members1["1"] = Param( name="arrayOfInt", param_type=Array(min_size=0, max_size=20, element_type=Boolean()), is_mandatory=False) members1["2"] = Param( name="arrayOfEnum1", param_type=Array(min_size=0, max_size=20, element_type=enum1), is_mandatory=False) members1["3"] = Param( name="arrayOfEnum3", param_type=Array(min_size=10, max_size=40, element_type=enum3), is_mandatory=True) members1["4"] = Param( name="arrayOfEnum4", param_type=Array( min_size=10, max_size=41, element_type=EnumSubset( name="sub1", enum=enum1, allowed_elements={"e1": elements1["name1"]}))) members1["5"] = Param( name="arrayOfEnum5", param_type=Array( min_size=10, max_size=42, element_type=EnumSubset( name="sub2", enum=enum1, allowed_elements={"e1": elements1["name2"]}))) members1["6"] = Param( name="arrayOfEnum6", param_type=Array( min_size=10, max_size=43, element_type=EnumSubset( name="sub3", enum=enum4, allowed_elements={"e1": elements4["name2"]}))) structs = collections.OrderedDict() structs["Struct1"] = Struct( name="Struct1", design_description=DESIGN_DESCRIPTION, issues=ISSUES, members=members1) structs["Struct2"] = Struct(name="Struct2", issues=ISSUES) interface = Interface(enums=enums, structs=structs, functions=functions, params={"param1": "value1", "param2": "value2"}) os.path.exists = MagicMock(return_value=True) uuid.uuid1 = MagicMock( return_value=uuid.UUID("12345678123456781234567812345678")) codecs.open = MagicMock() generator.generate(interface=interface, filename="Test.xml", namespace="XXX::YYY::ZZZ", destination_dir="/some/test/dir") os.path.exists.assert_has_calls([call('/some/test/dir')]) open_result = codecs.open mock_calls = open_result.mock_calls self.assertEqual(mock_calls[0], call('/some/test/dir/Test.h', mode='w', encoding='utf-8'), "Invalid header file creation") self.assertEqual(mock_calls[4], call('/some/test/dir/Test_schema.h', mode='w', encoding='utf-8'), "Invalid source file creation") try: self.assertSequenceEqual(str(mock_calls[2])[27:-2].replace("\\n", "\n"), expected_h_file_content, "Invalid header file content") self.assertSequenceEqual(str(mock_calls[6])[27:-2].replace("\\n", "\n"), expected_cc_file_content, "Invalid source file content") except AssertionError as message: print(message)
def test_RegisterAppInterfaceRequest(self): """ generator/transformers/common_producer.py 85% generator/transformers/functions_producer.py 63% """ params = OrderedDict() params['syncMsgVersion'] = Param(name='syncMsgVersion', param_type=Struct( name='SyncMsgVersion', description=['Specifies the'], members={ 'majorVersion': Param(name='majorVersion', param_type=Integer()) }), description=['See SyncMsgVersion'], is_mandatory=True) params['fullAppID'] = Param(name='fullAppID', description=['ID used'], param_type=String(), is_mandatory=False) params['dayColorScheme'] = Param( name='dayColorScheme', param_type=Struct( name='TemplateColorScheme', description=[ '\n A color scheme for all display layout templates.\n ' ]), is_mandatory=False) params['ttsName'] = Param( name='ttsName', description=['\n TTS string for'], is_mandatory=False, param_type=Array(element_type=Struct(name='TTSChunk', description=['A TTS chunk']))) params['isMediaApplication'] = Param( name='isMediaApplication', param_type=Boolean(), description=[ '\n Indicates if the application is a media or a ' ], is_mandatory=True) item = Function( name='RegisterAppInterface', function_id=EnumElement(name='RegisterAppInterfaceID'), since='1.0.0', description=[ '\n Establishes an interface with a mobile application.\n ' 'Before registerAppInterface no other commands will be accepted/executed.\n ' ], message_type=EnumElement(name='request'), params=params) expected = OrderedDict() expected['origin'] = 'RegisterAppInterface' expected['name'] = 'SDLRegisterAppInterface' expected['extends_class'] = 'SDLRPCRequest' expected['imports'] = { '.h': { 'enum': {'SDLRPCRequest'}, 'struct': {'SDLTemplateColorScheme', 'SDLTTSChunk', 'SDLSdlMsgVersion'} }, '.m': {'SDLTemplateColorScheme', 'SDLTTSChunk', 'SDLSdlMsgVersion'} } expected['description'] = [ 'Establishes an interface with a mobile application. Before registerAppInterface no ' 'other commands will be', 'accepted/executed.' ] expected['since'] = '1.0.0' expected['params'] = ( self.producer.param_named(constructor_argument='sdlMsgVersion', constructor_argument_override=None, constructor_prefix='SdlMsgVersion', deprecated=False, description=['See SyncMsgVersion'], for_name='object', mandatory=True, method_suffix='SdlMsgVersion', modifier='strong', of_class='SDLSdlMsgVersion.class', origin='sdlMsgVersion', since=None, type_native='SDLSdlMsgVersion *', type_sdl='SDLSdlMsgVersion *'), self.producer.param_named( constructor_argument='fullAppID', constructor_argument_override=None, constructor_prefix='FullAppID', deprecated=False, description=[ 'ID used', '{"default_value": null, "max_length": null, "min_length": null}' ], for_name='object', mandatory=False, method_suffix='FullAppID', modifier='strong', of_class='NSString.class', origin='fullAppID', since=None, type_native='NSString *', type_sdl='NSString *'), self.producer.param_named(constructor_argument='dayColorScheme', constructor_argument_override=None, mandatory=False, constructor_prefix='DayColorScheme', deprecated=False, description=[], for_name='object', method_suffix='DayColorScheme', modifier='strong', of_class='SDLTemplateColorScheme.class', origin='dayColorScheme', since=None, type_native='SDLTemplateColorScheme *', type_sdl='SDLTemplateColorScheme *'), self.producer.param_named(constructor_argument='ttsName', constructor_argument_override=None, constructor_prefix='TtsName', deprecated=False, description=['TTS string for'], for_name='objects', mandatory=False, method_suffix='TtsName', modifier='strong', of_class='SDLTTSChunk.class', origin='ttsName', since=None, type_native='NSArray<SDLTTSChunk *> *', type_sdl='NSArray<SDLTTSChunk *> *'), self.producer.param_named( constructor_argument='isMediaApplication', constructor_argument_override=None, constructor_prefix='IsMediaApplication', deprecated=False, description=['Indicates if the application is a media or a'], for_name='object', mandatory=True, method_suffix='IsMediaApplication', modifier='strong', of_class='NSNumber.class', origin='isMediaApplication', since=None, type_native='BOOL', type_sdl='NSNumber<SDLBool> *')) mandatory_arguments = [ self.producer.argument_named(variable='sdlMsgVersion', deprecated=False, origin='sdlMsgVersion', constructor_argument='sdlMsgVersion'), self.producer.argument_named( variable='isMediaApplication', deprecated=False, origin='isMediaApplication', constructor_argument='@(isMediaApplication)') ] not_mandatory_arguments = [ self.producer.argument_named(variable='fullAppID', deprecated=False, origin='fullAppID', constructor_argument='fullAppID'), self.producer.argument_named( variable='dayColorScheme', deprecated=False, origin='dayColorScheme', constructor_argument='dayColorScheme'), self.producer.argument_named(variable='ttsName', deprecated=False, origin='ttsName', constructor_argument='ttsName') ] mandatory_init = 'SdlMsgVersion:(SDLSdlMsgVersion *)sdlMsgVersion ' \ 'isMediaApplication:(BOOL)isMediaApplication' expected['constructors'] = ( self.producer.constructor_named(all=mandatory_arguments, arguments=mandatory_arguments, deprecated=False, init=mandatory_init, self=True), self.producer.constructor_named( all=mandatory_arguments + not_mandatory_arguments, arguments=not_mandatory_arguments, deprecated=False, init=mandatory_init + ' fullAppID:(nullable NSString *)fullAppID dayColorScheme:(nullable ' 'SDLTemplateColorScheme *)dayColorScheme ttsName:(nullable NSArray<SDLTTSChunk ' '*> *)ttsName', self=re.sub(r'([\w\d]+:)\([\w\d\s<>*]*\)([\w\d]+\s*)', r'\1\2', mandatory_init))) actual = self.producer.transform(item) self.assertDictEqual(expected, actual)