def test_remove_duplicates_normal_and_from(self): src = """ import a.b from a import b """ tree = ast.parse(src) self.assertFalse(import_utils.remove_duplicates(tree)) self.assertEqual(len(tree.body), 2)
def test_remove_duplicates_aliases(self): src = """ import a import a as ax import a as ax2 import a as ax """ tree = ast.parse(src) self.assertTrue(import_utils.remove_duplicates(tree)) self.assertEqual(len(tree.body), 3) self.assertEqual(tree.body[0].names[0].asname, None) self.assertEqual(tree.body[1].names[0].asname, 'ax') self.assertEqual(tree.body[2].names[0].asname, 'ax2')
def test_remove_duplicates_empty_node(self): src = """ import a, b, c import b, c """ tree = ast.parse(src) self.assertTrue(import_utils.remove_duplicates(tree)) self.assertEqual(len(tree.body), 1) self.assertEqual(len(tree.body[0].names), 3) self.assertEqual(tree.body[0].names[0].name, 'a') self.assertEqual(tree.body[0].names[1].name, 'b') self.assertEqual(tree.body[0].names[2].name, 'c')
def test_remove_duplicates(self): src = """ import a import b import c import b import d """ tree = ast.parse(src) self.assertTrue(import_utils.remove_duplicates(tree)) self.assertEqual(len(tree.body), 4) self.assertEqual(tree.body[0].names[0].name, 'a') self.assertEqual(tree.body[1].names[0].name, 'b') self.assertEqual(tree.body[2].names[0].name, 'c') self.assertEqual(tree.body[3].names[0].name, 'd')
def test_remove_duplicates_multiple(self): src = """ import a, b import b, c import d, a, e, f """ tree = ast.parse(src) self.assertTrue(import_utils.remove_duplicates(tree)) self.assertEqual(len(tree.body), 3) self.assertEqual(len(tree.body[0].names), 2) self.assertEqual(tree.body[0].names[0].name, 'a') self.assertEqual(tree.body[0].names[1].name, 'b') self.assertEqual(len(tree.body[1].names), 1) self.assertEqual(tree.body[1].names[0].name, 'c') self.assertEqual(len(tree.body[2].names), 3) self.assertEqual(tree.body[2].names[0].name, 'd') self.assertEqual(tree.body[2].names[1].name, 'e') self.assertEqual(tree.body[2].names[2].name, 'f')