def test_pre_process(self): info = GenerationInfoByTarget() info.name = "module_1" info.data.append(self._create_constant_info()) info.data.append(self._create_function_info()) info.data.append(self._create_base_class_1_info()) info.data.append(self._create_base_class_2_info()) info.data.append(self._create_class_info()) info.child_modules.append("submodule_1") dep = Dependency() dep.mod_name = "module_2" dep.add_type("Class1") dep.add_type("Class2") info.dependencies.append(dep) dep = Dependency() dep.mod_name = ".submodule_2" dep.add_type("ClassZ") info.dependencies.append(dep) info.external_modules.append("os") generator = BaseGenerator() processed_info = generator.pre_process("module_1", info) self.assertEqual(info.name, processed_info.name) self.assertEquals(info.child_modules, processed_info.child_modules) self.assertEquals(info.dependencies, processed_info.dependencies) self.assertEquals(info.external_modules, processed_info.external_modules) self.assertEquals(info.data, processed_info.data)
def test_dump_json(self): info = GenerationInfoByTarget() info.name = "module_1" info.data.append(self._create_constant_info()) info.data.append(self._create_function_info()) info.data.append(self._create_base_class_1_info()) info.data.append(self._create_base_class_2_info()) info.data.append(self._create_class_info()) info.child_modules.append("submodule_1") dep = Dependency() dep.mod_name = "module_2" dep.add_type("Class1") dep.add_type("Class2") info.dependencies.append(dep) dep = Dependency() dep.mod_name = ".submodule_2" dep.add_type("ClassZ") info.dependencies.append(dep) info.external_modules.append("os") generator = BaseGenerator() generator.dump_json(self.output_file_path, info) expect_file_path = "{}/{}".format(self.data_dir, "base_generator_test_dump_json.json") with open(expect_file_path, "r") as f: expect_data = { "data": json.load(f) } actual_file_path = self.output_file_path with open(actual_file_path, "r") as f: actual_data = { "data": json.load(f) } self.assertDictEqual(expect_data, actual_data)
def test_generate(self): info = GenerationInfoByTarget() info.name = "module_1" info.data.append(self._create_constant_info()) info.data.append(self._create_function_info()) info.data.append(self._create_base_class_1_info()) info.data.append(self._create_base_class_2_info()) info.data.append(self._create_class_info()) info.child_modules.append("submodule_1") dep = Dependency() dep.mod_name = "module_2" dep.add_type("Class1") dep.add_type("Class2") info.dependencies.append(dep) dep = Dependency() dep.mod_name = ".submodule_2" dep.add_type("ClassZ") info.dependencies.append(dep) info.external_modules.append("os") generator = BaseGenerator() generator.generate(self.output_file_path, info, "pep8") expect_file_path = "{}/{}".format(self.data_dir, "base_generator_test_generate.py") actual_file_path = self.output_file_path with open(actual_file_path, "r") as f: self.log(f.read()) self.assertTrue(filecmp.cmp(expect_file_path, actual_file_path))
def test_single_rule(self): rule_rst_files = [ "{}/{}".format(self.data_dir, "package_analyzer_test_single_rule.rst"), ] config = PackageGeneratorConfig() config.output_dir = self.output_dir config.os = "Linux" config.style_format = "none" config.dump = True config.blender_version = "2.80" config.mod_version = "2.80" config.support_bge = False analyzer = BaseAnalyzer() generator = BaseGenerator() rule = PackageGenerationRule("rule", rule_rst_files, analyzer, generator) pkg_analyzer = PackageAnalyzer(config, [rule]) pkg_analyzer.analyze() pkg_struct = pkg_analyzer.package_structure() self.assertDictEqual(pkg_struct.to_dict(), { "name": None, "children": [ { "name": "module_abc", "children": [], } ] }) entries = pkg_analyzer.entry_points() actual_entries = set([e.fullname() for e in entries]) expect_entries = { "module_abc.Class123", } self.assertSetEqual(expect_entries, actual_entries) gen_info = pkg_analyzer.generation_info() self.assertEqual(len(gen_info.keys()), 1) actual_rule = None actual_gen_info = None for k in gen_info.keys(): if k.name() == "rule": actual_rule = k actual_gen_info = gen_info[k] self.assertIsNotNone(actual_rule) self.assertIsNotNone(actual_gen_info) self.assertEquals(set(actual_gen_info.targets()), {"module_abc.py"}) target_module_abc = actual_gen_info.get_target("module_abc.py") self.assertEqual(len(target_module_abc.data), 1) self.assertEqual(target_module_abc.data[0].type(), "class") self.assertEqual(target_module_abc.data[0].name(), "Class123") self.assertEqual(len(target_module_abc.child_modules), 0) self.assertEqual(len(target_module_abc.dependencies), 0)
def test_exceptional_rules(self): rule_rst_files = [ "{}/{}".format(self.data_dir, "package_analyzer_test_exceptional_rule.rst"), ] config = PackageGeneratorConfig() config.output_dir = self.output_dir config.os = "Linux" config.style_format = "pep8" config.dump = True config.target_version = "2.80" config.mod_version = None config.support_bge = False pkg_generator = PackageGenerator(config) analyzer = BaseAnalyzer() generator = BaseGenerator() rule = PackageGenerationRule("rule", rule_rst_files, analyzer, generator) pkg_generator.add_rule(rule) pkg_generator.generate() expect_files_dir = "{}/package_generator_test_exceptional_rule".format( self.data_dir) actual_files_dir = self.output_dir py_files = [ "module_exceptional.py", ] for file_ in py_files: expect_file_path = "{}/{}".format(expect_files_dir, file_) actual_file_path = "{}/{}".format(actual_files_dir, file_) with open(actual_file_path, "r") as f: self.log( "============= {} =============".format(actual_file_path)) self.log(f.read()) self.assertTrue(filecmp.cmp(expect_file_path, actual_file_path)) json_files = [ "module_exceptional.py-dump.json", ] for file_ in json_files: expect_file_path = "{}/{}".format(expect_files_dir, file_) actual_file_path = "{}/{}".format(actual_files_dir, file_) with open(expect_file_path, "r") as f: expect_json = {"data": json.load(f)} with open(actual_file_path, "r") as f: self.log( "============= {} =============".format(actual_file_path)) data = json.load(f) self.log(str(data)) actual_json = {"data": data} self.assertDictEqual(expect_json, actual_json) self.assertFalse( self.__is_py_typed_exist("{}/py.typed".format(self.output_dir)))
def test_all(self): analyzer = BaseAnalyzer() generator = BaseGenerator() rule = PackageGenerationRule("rule", ["a.rst", "b.rst"], analyzer, generator) self.assertEqual(rule.name(), "rule") self.assertEquals(rule.target_files(), ["a.rst", "b.rst"]) self.assertEqual(rule.analyzer(), analyzer) self.assertEqual(rule.generator(), generator)
def test_multiple_rules(self): rule_1_rst_files = [ "{}/{}".format(self.data_dir, "package_analyzer_test_multiple_rules_1_a.rst"), "{}/{}".format(self.data_dir, "package_analyzer_test_multiple_rules_1_b.rst"), ] rule_2_rst_files = [ "{}/{}".format(self.data_dir, "package_analyzer_test_multiple_rules_2.rst"), ] config = PackageGeneratorConfig() config.output_dir = "{}/output".format(self.output_dir) config.os = "Linux" config.style_format = "none" config.dump = True config.blender_version = "2.80" config.mod_version = None config.support_bge = False analyzer = BaseAnalyzer() generator = BaseGenerator() rule_1 = PackageGenerationRule("rule_1", rule_1_rst_files, analyzer, generator) analyzer = BaseAnalyzer() generator = BaseGenerator() rule_2 = PackageGenerationRule("rule_2", rule_2_rst_files, analyzer, generator) pkg_analyzer = PackageAnalyzer(config, [rule_1, rule_2]) pkg_analyzer.analyze() pkg_struct = pkg_analyzer.package_structure() self.assertDictEqual(pkg_struct.to_dict(), { "name": None, "children": [ { "name": "module_1", "children": [ { "name": "submodule_1", "children": [], } ] }, { "name": "module_2", "children": [], } ] }) entries = pkg_analyzer.entry_points() actual_entries = set([e.fullname() for e in entries]) expect_entries = { "module_1.ClassA", "module_1.submodule_1.BaseClass1", "module_1.submodule_1.function_1", "module_1.submodule_1.DATA_1", "module_2.function_1", } self.assertSetEqual(expect_entries, actual_entries) gen_info = pkg_analyzer.generation_info() self.assertEqual(len(gen_info.keys()), 2) actual_rule_1 = None actual_rule_2 = None actual_gen_info_1 = None actual_gen_info_2 = None for k in gen_info.keys(): if k.name() == "rule_1": actual_rule_1 = k actual_gen_info_1 = gen_info[k] elif k.name() == "rule_2": actual_rule_2 = k actual_gen_info_2 = gen_info[k] self.assertIsNotNone(actual_rule_1) self.assertIsNotNone(actual_rule_2) self.assertIsNotNone(actual_gen_info_1) self.assertIsNotNone(actual_gen_info_2) self.assertEquals(set(actual_gen_info_1.targets()), {"module_1/__init__.py", "module_1/submodule_1.py"}) self.assertEquals(set(actual_gen_info_2.targets()), {"module_2.py"}) target_module_1 = actual_gen_info_1.get_target("module_1/__init__.py") self.assertEqual(len(target_module_1.data), 1) self.assertEqual(target_module_1.data[0].type(), "class") self.assertEqual(target_module_1.data[0].name(), "ClassA") self.assertEquals(target_module_1.child_modules, ["submodule_1"]) self.assertEqual(len(target_module_1.dependencies), 1) self.assertEqual(target_module_1.dependencies[0].mod_name, "module_1.submodule_1") self.assertEquals(target_module_1.dependencies[0].type_lists, ["BaseClass1"]) target_module_1_submodule_1 = actual_gen_info_1.get_target("module_1/submodule_1.py") self.assertEqual(len(target_module_1_submodule_1.data), 3) self.assertEqual(target_module_1_submodule_1.data[0].type(), "class") self.assertEqual(target_module_1_submodule_1.data[0].name(), "BaseClass1") self.assertEqual(target_module_1_submodule_1.data[1].type(), "function") self.assertEqual(target_module_1_submodule_1.data[1].name(), "function_1") self.assertEqual(target_module_1_submodule_1.data[2].type(), "constant") self.assertEqual(target_module_1_submodule_1.data[2].name(), "DATA_1") self.assertEqual(len(target_module_1_submodule_1.child_modules), 0) self.assertEqual(len(target_module_1_submodule_1.dependencies), 0) target_module_2 = actual_gen_info_2.get_target("module_2.py") self.assertEqual(len(target_module_2.data), 1) self.assertEqual(target_module_2.data[0].type(), "function") self.assertEqual(target_module_2.data[0].name(), "function_1") self.assertEqual(len(target_module_2.child_modules), 0) self.assertEqual(len(target_module_2.dependencies), 2) self.assertEqual(target_module_2.dependencies[0].mod_name, "module_1") self.assertEquals(target_module_2.dependencies[0].type_lists, ["ClassA"]) self.assertEqual(target_module_2.dependencies[1].mod_name, "module_1.submodule_1") self.assertEquals(target_module_2.dependencies[1].type_lists, ["BaseClass1"])
def test_multiple_rules(self): rule_2_rst_files = [ "{}/{}".format(self.data_dir, "package_analyzer_test_rule_2_a.rst"), "{}/{}".format(self.data_dir, "package_analyzer_test_rule_2_b.rst"), ] rule_3_rst_files = [ "{}/{}".format(self.data_dir, "package_analyzer_test_rule_3_a.rst"), ] config = PackageGeneratorConfig() config.output_dir = self.output_dir config.os = "Linux" config.style_format = "pep8" config.dump = True config.mod_version = None config.support_bge = False pkg_generator = PackageGenerator(config) analyzer = BaseAnalyzer() generator = BaseGenerator() rule_2 = PackageGenerationRule("rule_2", rule_2_rst_files, analyzer, generator) analyzer = BaseAnalyzer() generator = BaseGenerator() rule_3 = PackageGenerationRule("rule_3", rule_3_rst_files, analyzer, generator) pkg_generator.add_rule(rule_2) pkg_generator.add_rule(rule_3) pkg_generator.generate() expect_files_dir = "{}/package_generator_test_multiple_rules".format( self.data_dir) actual_files_dir = self.output_dir py_files = [ "module_1/__init__.py", "module_1/submodule_1.py", "module_2.py", ] for file_ in py_files: expect_file_path = "{}/{}".format(expect_files_dir, file_) actual_file_path = "{}/{}".format(actual_files_dir, file_) with open(actual_file_path, "r") as f: self.log( "============= {} =============".format(actual_file_path)) self.log(f.read()) self.assertTrue(filecmp.cmp(expect_file_path, actual_file_path)) json_files = [ "module_1/__init__.py-dump.json", "module_1/submodule_1.py-dump.json", "module_2.py-dump.json", ] for file_ in json_files: expect_file_path = "{}/{}".format(expect_files_dir, file_) actual_file_path = "{}/{}".format(actual_files_dir, file_) with open(expect_file_path, "r") as f: expect_json = {"data": json.load(f)} with open(actual_file_path, "r") as f: self.log( "============= {} =============".format(actual_file_path)) data = json.load(f) self.log(str(data)) actual_json = {"data": data} self.assertDictEqual(expect_json, actual_json)