Best Python code snippet using lisa_python
test_idl_generator.py
Source:test_idl_generator.py
1# !/usr/bin/env python32# coding=utf-83"""4* Copyright (c) 2022 Shenzhen Kaihong Digital Industry Development Co., Ltd.5*6* HDF is dual licensed: you can use it either under the terms of7* the GPL, or the BSD license, at your option.8* See the LICENSE file in the root of this repository for complete details.9"""10import json11import sys12import unittest13import CppHeaderParser14sys.path.insert(0, '..')15try:16 from _header_parser import HeaderParser17 from idl_generator import IDLGenerator18finally:19 pass20class IDLGeneratorTestCase(unittest.TestCase):21 def test_install_package(self):22 generator = IDLGenerator()23 generator._install_package("\\h\\audio\\test")24 self.assertEqual(generator._idl, "package h.audio.test;\n\n")25 generator._idl = ""26 generator._install_package(".\\h\\audio\\test")27 self.assertEqual(generator._idl, "package h.audio.test;\n\n")28 generator._idl = ""29 generator._install_package("C:\\h\\audio\\test")30 self.assertEqual(generator._idl, "package h.audio.test;\n\n")31 generator._idl = ""32 generator._install_package("./h/audio/test")33 self.assertEqual(generator._idl, "package h.audio.test;\n\n")34 def test_install_import_interface(self):35 generator = IDLGenerator()36 generator._parse_results = {37 'audio_render.h': {38 'name': 'audio_render.h',39 'path': '.\\include\\audio',40 'import': [], 'enum': [], 'union': [], 'struct': [],41 'interface': [{'name': 'AudioRender', 'members': []}],42 'callback': []43 },44 'audio_test.h': {45 'name': 'audio_test.h',46 'path': '.\\include\\audio',47 'import': ['audio_render.h'], 'enum': [], 'union': [], 'struct': [],48 'interface': [{'name': 'AudioRender', 'members': []}],49 'callback': []50 }51 }52 header = generator._parse_results['audio_test.h']53 generator._install_import(header)54 self.assertEqual(generator._idl, "import include.audio.AudioRender;\n\n")55 def test_install_import_interfaces(self):56 generator = IDLGenerator()57 generator._parse_results = {58 'audio_render.h': {59 'name': 'audio_render.h',60 'path': '.\\include\\audio',61 'import': [], 'enum': [], 'union': [], 'struct': [],62 'interface': [{'name': 'AudioRender', 'members': []}],63 'callback': []64 },65 'audio_adapter.h': {66 'name': 'audio_adapter.h',67 'path': '.\\include\\audio\\adapter',68 'import': [], 'enum': [], 'union': [], 'struct': [],69 'interface': [{'name': 'AudioAdapter', 'members': []}],70 'callback': []71 },72 'audio_test.h': {73 'name': 'audio_test.h',74 'path': '.\\include\\audio',75 'import': ['audio_render.h', 'audio_adapter.h'], 'enum': [], 'union': [], 'struct': [],76 'interface': [{'name': 'AudioRender', 'members': []}],77 'callback': []78 }79 }80 header = generator._parse_results['audio_test.h']81 generator._install_import(header)82 self.assertEqual(generator._idl, "import include.audio.AudioRender;\n"83 "import include.audio.adapter.AudioAdapter;\n\n")84 def test_install_import_multiple_interfaces(self):85 generator = IDLGenerator()86 generator._parse_results = {87 'audio_multiple_interfaces.h': {88 'name': 'audio_multiple_interfaces.h',89 'path': '.\\include',90 'import': [], 'enum': [], 'union': [], 'struct': [],91 'interface': [92 {'name': 'AudioInterfaceOne', 'members': []},93 {'name': 'AudioInterfaceTwo', 'members': []}94 ],95 'callback': []96 },97 'audio_test.h': {98 'name': 'audio_test.h',99 'path': '.\\include\\audio',100 'import': ['audio_multiple_interfaces.h'], 'enum': [], 'union': [], 'struct': [],101 'interface': [{'name': 'AudioRender', 'members': []}],102 'callback': []103 }104 }105 header = generator._parse_results['audio_test.h']106 generator._install_import(header)107 self.assertEqual(generator._idl, "import include.AudioInterfaceOne;\nimport include.AudioInterfaceTwo;\n\n")108 def test_install_import_types(self):109 generator = IDLGenerator()110 generator._parse_results = {111 'audio_types.h': {112 'name': 'audio_types.h',113 'path': '.\\include',114 'import': [],115 'enum': [{'name': 'AudioPortDirection', 'members': [{'name': 'PORT_OUT', 'value': 1}]}],116 'union': [], 'struct': [], 'interface': [], 'callback': []117 },118 'audio_test.h': {119 'name': 'audio_test.h',120 'path': '.\\include\\audio',121 'import': ['audio_types.h'], 'enum': [], 'union': [], 'struct': [],122 'interface': [{'name': 'AudioRender', 'members': []}],123 'callback': []124 }125 }126 header = generator._parse_results["audio_test.h"]127 generator._install_import(header)128 self.assertEqual(generator._idl, "import include.Types;\n\n")129 def test_install_import_merge_types(self):130 generator = IDLGenerator()131 generator._parse_results = {132 'audio_types.h': {133 'name': 'audio_types.h',134 'path': '.\\include',135 'import': ['adapter_types.h'],136 'enum': [{'name': 'AudioPortDirection', 'members': [{'name': 'PORT_OUT', 'value': 1}]}],137 'union': [], 'struct': [], 'interface': [], 'callback': []138 },139 'adapter_types.h': {140 'name': 'adapter_types.h',141 'path': '.\\include',142 'import': [],143 'enum': [{'name': 'AudioAdapter', 'members': [{'name': 'PORT_OUT', 'value': 1}]}],144 'union': [], 'struct': [], 'interface': [], 'callback': []145 },146 'audio_test.h': {147 'name': 'audio_test.h',148 'path': '.\\include\\audio',149 'import': ['audio_types.h', 'adapter_types.h'], 'enum': [], 'union': [], 'struct': [],150 'interface': [{'name': 'AudioRender', 'members': []}],151 'callback': []152 }153 }154 header = generator._parse_results['audio_test.h']155 generator._install_import(header)156 self.assertEqual(generator._idl, "import include.Types;\n\n")157 def test_install_import_types_and_interfaces(self):158 generator = IDLGenerator()159 generator._parse_results = {160 'audio_types.h': {161 'name': 'audio_types.h',162 'path': '.\\include',163 'import': ['adapter_types.h'],164 'enum': [{'name': 'AudioPortDirection', 'members': [{'name': 'PORT_OUT', 'value': 1}]}],165 'union': [], 'struct': [], 'interface': [], 'callback': []166 },167 'audio_adapter.h': {168 'name': 'audio_adapter.h',169 'path': '.\\include\\audio\\adapter',170 'import': [], 'enum': [], 'union': [], 'struct': [],171 'interface': [{'name': 'AudioAdapter', 'members': []}],172 'callback': []173 },174 'audio_test.h': {175 'name': 'audio_test.h',176 'path': '.\\include\\audio',177 'import': ['audio_types.h', 'audio_adapter.h'], 'enum': [], 'union': [], 'struct': [],178 'interface': [{'name': 'AudioRender', 'members': []}],179 'callback': []180 }181 }182 header = generator._parse_results['audio_test.h']183 generator._install_import(header)184 self.assertEqual(generator._idl, "import include.Types;\nimport include.audio.adapter.AudioAdapter;\n\n")185 def test_install_import_callback(self):186 generator = IDLGenerator()187 generator._parse_results = {188 'audio_test.h': {189 'name': 'audio_test.h',190 'path': '.\\include\\audio',191 'import': [], 'enum': [], 'union': [], 'struct': [],192 'interface': [{'name': 'AudioRender', 'members': []}],193 'callback': [194 {'name': 'HotPlugCallback', 'members': []},195 {'name': 'VBlankCallback', 'members': []}196 ]197 }198 }199 header = generator._parse_results['audio_test.h']200 generator._install_import(header)201 self.assertEqual(generator._idl, "import include.audio.HotPlugCallback;\n"202 "import include.audio.VBlankCallback;\n\n")203 def test_install_enum(self):204 generator = IDLGenerator()205 enum = [{206 'name': 'AudioPortDirection',207 'members': [208 {'name': 'PORT_OUT', 'value': 1},209 {'name': 'PORT_IN', 'value': '- 2 // unexpected \'-\''},210 ]}211 ]212 generator._install_enum(enum)213 self.assertEqual(generator._idl, "enum AudioPortDirection {\n"214 " PORT_OUT = 1,\n"215 " PORT_IN = - 2 // unexpected '-',\n"216 "};\n")217 def test_install_stack_union(self):218 generator = IDLGenerator()219 union = [{'name': 'SceneDesc',220 'type': 'union',221 'members': [222 {'name': 'id', 'type': 'uint32_t'},223 {'name': 'desc', 'type': 'const char *'}224 ]}]225 generator._install_stack(union)226 self.assertEqual(generator._idl, "union SceneDesc {\n"227 " unsigned int id;\n"228 " byte[] desc;\n"229 "};\n")230 def test_install_stack_struct(self):231 generator = IDLGenerator()232 struct = [{'name': 'SceneDesc',233 'type': 'struct',234 'members': [235 {'name': 'id', 'type': 'uint32_t'},236 {'name': 'desc', 'type': 'const char *'}237 ]}]238 generator._install_stack(struct)239 self.assertEqual(generator._idl, "struct SceneDesc {\n"240 " unsigned int id;\n"241 " byte[] desc;\n"242 "};\n")243 def test_install_interface(self):244 header_file = """245 typedef struct {246 int32_t (*RunExtraCommand)(struct InputController *self, uint32_t devIndex, uint32_t **cmd);247 int32_t (*RunExtra)(struct InputController *self, struct InputControllerDesc desc);248 } InputController;249 """250 parser = HeaderParser()251 hjson = json.loads(CppHeaderParser.CppHeader(header_file, "string").toJSON())252 parser._extract_interface(hjson["classes"]["InputController"])253 generator = IDLGenerator()254 generator._key_list["InputController"] = "struct"255 generator._key_list["InputControllerDesc"] = "struct"256 generator._install_interface(parser._header_dict.get("interface")[0])257 self.assertEqual(generator._idl, "interface InputController {\n"258 " RunExtraCommand([in] unsigned int devIndex,[out] unsigned int cmd);\n"259 " RunExtra([in] struct InputControllerDesc desc);\n"260 "}\n")261 def test_install_interface_with_unknown_type(self):262 header_file = """263 typedef struct {264 int32_t (*RunExtra)(struct InputControllerDesc desc);265 } InputController;266 """267 parser = HeaderParser()268 hjson = json.loads(CppHeaderParser.CppHeader(header_file, "string").toJSON())269 parser._extract_interface(hjson["classes"]["InputController"])270 generator = IDLGenerator()271 generator._install_interface(parser._header_dict.get("interface")[0])272 self.assertEqual(generator._idl, "interface InputController {\n"273 " RunExtra([in] /* unknown type: [InputControllerDesc] */ desc);\n"274 "}\n")275 def test_install_interface_callback(self):276 header_file = """277 struct IFooCallback {278 int32_t (*PushData)(struct IFooCallback *self, const char* message);279 };280 """281 parser = HeaderParser()282 hjson = json.loads(CppHeaderParser.CppHeader(header_file, "string").toJSON())283 parser._extract_interface(hjson["classes"]["IFooCallback"])284 generator = IDLGenerator()285 generator._install_interface(parser._header_dict.get("interface")[0])286 self.assertEqual(generator._idl, "[callback] interface IFooCallback {\n"287 " PushData([in] byte[] message);\n"288 "}\n")289 def test_convert_from_container_type(self):290 generator = IDLGenerator()291 self.assertEqual(generator._convert_container_type("const map<int, int> test"),292 "Map<int, int>")293 self.assertEqual(generator._convert_container_type("const std::map< int , int > test"),294 "Map<int, int>")295 self.assertEqual(generator._convert_container_type("const std::map<int, std::string> test"),296 "Map<int, String>")297 self.assertEqual(generator._convert_container_type("const std::map<struct KeyType, struct ValueType> test"),298 "Map<KeyType, ValueType>")299 self.assertEqual(generator._convert_container_type("const std::map<unsigned int, unsigned long> test"),300 "Map<unsigned int, unsigned long>")301 self.assertEqual(generator._convert_container_type("const std::vector<int> test"),302 "List<int>")303 self.assertEqual(generator._convert_container_type("const std::vector<struct ValueType> test"),304 "List<ValueType>")305 self.assertEqual(generator._convert_container_type("const std::vector<unsigned long> test"),306 "List<unsigned long>")307if __name__ == "__main__":...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!