def test_has_import(self): source = Source().load(models.TEST_REPO) self.assertTrue(utils.has_import("f1", source.uasts[0])) self.assertTrue(utils.has_import("test_lib", source.uasts[0])) source = Source().load(models.TEST_LIB) self.assertFalse(utils.has_import("f1", source.uasts[0])) self.assertFalse(utils.has_import("test_lib", source.uasts[0]))
def test_get_imports(self): source = Source().load(models.TEST_REPO) uast = source.uasts[0] imports = utils.get_imports(uast) true_imports = {"f1", "test_lib"} self.assertEqual(imports, true_imports) source = Source().load(models.TEST_LIB) uast = source.uasts[0] imports = utils.get_imports(uast) true_imports = set() self.assertEqual(imports, true_imports)
def test_convert_model(self): from ast2vec.bblfsh_roles import Node lib_model = Source().load(models.TEST_LIB) functions_bow = get_func_names_bow(lib_model) functions_bow = process_lib_functions(functions_bow) self.assertEqual(functions_bow, {"f1": 1, "f2": 1, "f3": 1, "f35": 1}) repo_model = Source().load(models.TEST_REPO) converter = Source2Func(models.LIB_NAME, functions_bow) functons = converter.convert_model(repo_model) self.assertEqual(len(functons), 1) functon_obj = functons[0] self.assertEqual(functon_obj[0], "example.py") self.assertEqual(type(functon_obj[1]), Node) self.assertEqual(functon_obj[2], "def f():\n f2()\n f2()\n f3()\n f1()\n f3()\n f3()") self.assertEqual(functon_obj[3][0], 4) self.assertEqual(functon_obj[3][1], 11)
def load_default_source_model(self, tmpdir): path = Repo2SourceTransformer.prepare_filename(DATA_DIR_SOURCE, tmpdir) validate_asdf_file(self, path) return Source().load(source=path)
def test_uast_to_bag(self): source = Source().load(models.TEST_REPO) uast = source.uasts[0] bag = utils.uast_to_bag(uast) true_bag = {"test_lib": 2, "f": 2, "f1": 2, "f2": 2, "f3": 4} self.assertEqual(bag, true_bag)
def test_get_func_names_bow(self): source = Source().load(models.TEST_LIB) bow = utils.get_func_names_bow(source) true_bow = {"f1": 1, "f2": 1, "f3": 1, "f35": 1} self.assertEqual(bow, true_bow)