test_util.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. """ Test suite for the code in fixer_util """
  2. # Testing imports
  3. from . import support
  4. # Local imports
  5. from lib2to3.pytree import Node, Leaf
  6. from lib2to3 import fixer_util
  7. from lib2to3.fixer_util import Attr, Name, Call, Comma
  8. from lib2to3.pgen2 import token
  9. def parse(code, strip_levels=0):
  10. # The topmost node is file_input, which we don't care about.
  11. # The next-topmost node is a *_stmt node, which we also don't care about
  12. tree = support.parse_string(code)
  13. for i in range(strip_levels):
  14. tree = tree.children[0]
  15. tree.parent = None
  16. return tree
  17. class MacroTestCase(support.TestCase):
  18. def assertStr(self, node, string):
  19. if isinstance(node, (tuple, list)):
  20. node = Node(fixer_util.syms.simple_stmt, node)
  21. self.assertEqual(str(node), string)
  22. class Test_is_tuple(support.TestCase):
  23. def is_tuple(self, string):
  24. return fixer_util.is_tuple(parse(string, strip_levels=2))
  25. def test_valid(self):
  26. self.assertTrue(self.is_tuple("(a, b)"))
  27. self.assertTrue(self.is_tuple("(a, (b, c))"))
  28. self.assertTrue(self.is_tuple("((a, (b, c)),)"))
  29. self.assertTrue(self.is_tuple("(a,)"))
  30. self.assertTrue(self.is_tuple("()"))
  31. def test_invalid(self):
  32. self.assertFalse(self.is_tuple("(a)"))
  33. self.assertFalse(self.is_tuple("('foo') % (b, c)"))
  34. class Test_is_list(support.TestCase):
  35. def is_list(self, string):
  36. return fixer_util.is_list(parse(string, strip_levels=2))
  37. def test_valid(self):
  38. self.assertTrue(self.is_list("[]"))
  39. self.assertTrue(self.is_list("[a]"))
  40. self.assertTrue(self.is_list("[a, b]"))
  41. self.assertTrue(self.is_list("[a, [b, c]]"))
  42. self.assertTrue(self.is_list("[[a, [b, c]],]"))
  43. def test_invalid(self):
  44. self.assertFalse(self.is_list("[]+[]"))
  45. class Test_Attr(MacroTestCase):
  46. def test(self):
  47. call = parse("foo()", strip_levels=2)
  48. self.assertStr(Attr(Name("a"), Name("b")), "a.b")
  49. self.assertStr(Attr(call, Name("b")), "foo().b")
  50. def test_returns(self):
  51. attr = Attr(Name("a"), Name("b"))
  52. self.assertEqual(type(attr), list)
  53. class Test_Name(MacroTestCase):
  54. def test(self):
  55. self.assertStr(Name("a"), "a")
  56. self.assertStr(Name("foo.foo().bar"), "foo.foo().bar")
  57. self.assertStr(Name("a", prefix="b"), "ba")
  58. class Test_Call(MacroTestCase):
  59. def _Call(self, name, args=None, prefix=None):
  60. """Help the next test"""
  61. children = []
  62. if isinstance(args, list):
  63. for arg in args:
  64. children.append(arg)
  65. children.append(Comma())
  66. children.pop()
  67. return Call(Name(name), children, prefix)
  68. def test(self):
  69. kids = [None,
  70. [Leaf(token.NUMBER, 1), Leaf(token.NUMBER, 2),
  71. Leaf(token.NUMBER, 3)],
  72. [Leaf(token.NUMBER, 1), Leaf(token.NUMBER, 3),
  73. Leaf(token.NUMBER, 2), Leaf(token.NUMBER, 4)],
  74. [Leaf(token.STRING, "b"), Leaf(token.STRING, "j", prefix=" ")]
  75. ]
  76. self.assertStr(self._Call("A"), "A()")
  77. self.assertStr(self._Call("b", kids[1]), "b(1,2,3)")
  78. self.assertStr(self._Call("a.b().c", kids[2]), "a.b().c(1,3,2,4)")
  79. self.assertStr(self._Call("d", kids[3], prefix=" "), " d(b, j)")
  80. class Test_does_tree_import(support.TestCase):
  81. def _find_bind_rec(self, name, node):
  82. # Search a tree for a binding -- used to find the starting
  83. # point for these tests.
  84. c = fixer_util.find_binding(name, node)
  85. if c: return c
  86. for child in node.children:
  87. c = self._find_bind_rec(name, child)
  88. if c: return c
  89. def does_tree_import(self, package, name, string):
  90. node = parse(string)
  91. # Find the binding of start -- that's what we'll go from
  92. node = self._find_bind_rec('start', node)
  93. return fixer_util.does_tree_import(package, name, node)
  94. def try_with(self, string):
  95. failing_tests = (("a", "a", "from a import b"),
  96. ("a.d", "a", "from a.d import b"),
  97. ("d.a", "a", "from d.a import b"),
  98. (None, "a", "import b"),
  99. (None, "a", "import b, c, d"))
  100. for package, name, import_ in failing_tests:
  101. n = self.does_tree_import(package, name, import_ + "\n" + string)
  102. self.assertFalse(n)
  103. n = self.does_tree_import(package, name, string + "\n" + import_)
  104. self.assertFalse(n)
  105. passing_tests = (("a", "a", "from a import a"),
  106. ("x", "a", "from x import a"),
  107. ("x", "a", "from x import b, c, a, d"),
  108. ("x.b", "a", "from x.b import a"),
  109. ("x.b", "a", "from x.b import b, c, a, d"),
  110. (None, "a", "import a"),
  111. (None, "a", "import b, c, a, d"))
  112. for package, name, import_ in passing_tests:
  113. n = self.does_tree_import(package, name, import_ + "\n" + string)
  114. self.assertTrue(n)
  115. n = self.does_tree_import(package, name, string + "\n" + import_)
  116. self.assertTrue(n)
  117. def test_in_function(self):
  118. self.try_with("def foo():\n\tbar.baz()\n\tstart=3")
  119. class Test_find_binding(support.TestCase):
  120. def find_binding(self, name, string, package=None):
  121. return fixer_util.find_binding(name, parse(string), package)
  122. def test_simple_assignment(self):
  123. self.assertTrue(self.find_binding("a", "a = b"))
  124. self.assertTrue(self.find_binding("a", "a = [b, c, d]"))
  125. self.assertTrue(self.find_binding("a", "a = foo()"))
  126. self.assertTrue(self.find_binding("a", "a = foo().foo.foo[6][foo]"))
  127. self.assertFalse(self.find_binding("a", "foo = a"))
  128. self.assertFalse(self.find_binding("a", "foo = (a, b, c)"))
  129. def test_tuple_assignment(self):
  130. self.assertTrue(self.find_binding("a", "(a,) = b"))
  131. self.assertTrue(self.find_binding("a", "(a, b, c) = [b, c, d]"))
  132. self.assertTrue(self.find_binding("a", "(c, (d, a), b) = foo()"))
  133. self.assertTrue(self.find_binding("a", "(a, b) = foo().foo[6][foo]"))
  134. self.assertFalse(self.find_binding("a", "(foo, b) = (b, a)"))
  135. self.assertFalse(self.find_binding("a", "(foo, (b, c)) = (a, b, c)"))
  136. def test_list_assignment(self):
  137. self.assertTrue(self.find_binding("a", "[a] = b"))
  138. self.assertTrue(self.find_binding("a", "[a, b, c] = [b, c, d]"))
  139. self.assertTrue(self.find_binding("a", "[c, [d, a], b] = foo()"))
  140. self.assertTrue(self.find_binding("a", "[a, b] = foo().foo[a][foo]"))
  141. self.assertFalse(self.find_binding("a", "[foo, b] = (b, a)"))
  142. self.assertFalse(self.find_binding("a", "[foo, [b, c]] = (a, b, c)"))
  143. def test_invalid_assignments(self):
  144. self.assertFalse(self.find_binding("a", "foo.a = 5"))
  145. self.assertFalse(self.find_binding("a", "foo[a] = 5"))
  146. self.assertFalse(self.find_binding("a", "foo(a) = 5"))
  147. self.assertFalse(self.find_binding("a", "foo(a, b) = 5"))
  148. def test_simple_import(self):
  149. self.assertTrue(self.find_binding("a", "import a"))
  150. self.assertTrue(self.find_binding("a", "import b, c, a, d"))
  151. self.assertFalse(self.find_binding("a", "import b"))
  152. self.assertFalse(self.find_binding("a", "import b, c, d"))
  153. def test_from_import(self):
  154. self.assertTrue(self.find_binding("a", "from x import a"))
  155. self.assertTrue(self.find_binding("a", "from a import a"))
  156. self.assertTrue(self.find_binding("a", "from x import b, c, a, d"))
  157. self.assertTrue(self.find_binding("a", "from x.b import a"))
  158. self.assertTrue(self.find_binding("a", "from x.b import b, c, a, d"))
  159. self.assertFalse(self.find_binding("a", "from a import b"))
  160. self.assertFalse(self.find_binding("a", "from a.d import b"))
  161. self.assertFalse(self.find_binding("a", "from d.a import b"))
  162. def test_import_as(self):
  163. self.assertTrue(self.find_binding("a", "import b as a"))
  164. self.assertTrue(self.find_binding("a", "import b as a, c, a as f, d"))
  165. self.assertFalse(self.find_binding("a", "import a as f"))
  166. self.assertFalse(self.find_binding("a", "import b, c as f, d as e"))
  167. def test_from_import_as(self):
  168. self.assertTrue(self.find_binding("a", "from x import b as a"))
  169. self.assertTrue(self.find_binding("a", "from x import g as a, d as b"))
  170. self.assertTrue(self.find_binding("a", "from x.b import t as a"))
  171. self.assertTrue(self.find_binding("a", "from x.b import g as a, d"))
  172. self.assertFalse(self.find_binding("a", "from a import b as t"))
  173. self.assertFalse(self.find_binding("a", "from a.d import b as t"))
  174. self.assertFalse(self.find_binding("a", "from d.a import b as t"))
  175. def test_simple_import_with_package(self):
  176. self.assertTrue(self.find_binding("b", "import b"))
  177. self.assertTrue(self.find_binding("b", "import b, c, d"))
  178. self.assertFalse(self.find_binding("b", "import b", "b"))
  179. self.assertFalse(self.find_binding("b", "import b, c, d", "c"))
  180. def test_from_import_with_package(self):
  181. self.assertTrue(self.find_binding("a", "from x import a", "x"))
  182. self.assertTrue(self.find_binding("a", "from a import a", "a"))
  183. self.assertTrue(self.find_binding("a", "from x import *", "x"))
  184. self.assertTrue(self.find_binding("a", "from x import b, c, a, d", "x"))
  185. self.assertTrue(self.find_binding("a", "from x.b import a", "x.b"))
  186. self.assertTrue(self.find_binding("a", "from x.b import *", "x.b"))
  187. self.assertTrue(self.find_binding("a", "from x.b import b, c, a, d", "x.b"))
  188. self.assertFalse(self.find_binding("a", "from a import b", "a"))
  189. self.assertFalse(self.find_binding("a", "from a.d import b", "a.d"))
  190. self.assertFalse(self.find_binding("a", "from d.a import b", "a.d"))
  191. self.assertFalse(self.find_binding("a", "from x.y import *", "a.b"))
  192. def test_import_as_with_package(self):
  193. self.assertFalse(self.find_binding("a", "import b.c as a", "b.c"))
  194. self.assertFalse(self.find_binding("a", "import a as f", "f"))
  195. self.assertFalse(self.find_binding("a", "import a as f", "a"))
  196. def test_from_import_as_with_package(self):
  197. # Because it would take a lot of special-case code in the fixers
  198. # to deal with from foo import bar as baz, we'll simply always
  199. # fail if there is an "from ... import ... as ..."
  200. self.assertFalse(self.find_binding("a", "from x import b as a", "x"))
  201. self.assertFalse(self.find_binding("a", "from x import g as a, d as b", "x"))
  202. self.assertFalse(self.find_binding("a", "from x.b import t as a", "x.b"))
  203. self.assertFalse(self.find_binding("a", "from x.b import g as a, d", "x.b"))
  204. self.assertFalse(self.find_binding("a", "from a import b as t", "a"))
  205. self.assertFalse(self.find_binding("a", "from a import b as t", "b"))
  206. self.assertFalse(self.find_binding("a", "from a import b as t", "t"))
  207. def test_function_def(self):
  208. self.assertTrue(self.find_binding("a", "def a(): pass"))
  209. self.assertTrue(self.find_binding("a", "def a(b, c, d): pass"))
  210. self.assertTrue(self.find_binding("a", "def a(): b = 7"))
  211. self.assertFalse(self.find_binding("a", "def d(b, (c, a), e): pass"))
  212. self.assertFalse(self.find_binding("a", "def d(a=7): pass"))
  213. self.assertFalse(self.find_binding("a", "def d(a): pass"))
  214. self.assertFalse(self.find_binding("a", "def d(): a = 7"))
  215. s = """
  216. def d():
  217. def a():
  218. pass"""
  219. self.assertFalse(self.find_binding("a", s))
  220. def test_class_def(self):
  221. self.assertTrue(self.find_binding("a", "class a: pass"))
  222. self.assertTrue(self.find_binding("a", "class a(): pass"))
  223. self.assertTrue(self.find_binding("a", "class a(b): pass"))
  224. self.assertTrue(self.find_binding("a", "class a(b, c=8): pass"))
  225. self.assertFalse(self.find_binding("a", "class d: pass"))
  226. self.assertFalse(self.find_binding("a", "class d(a): pass"))
  227. self.assertFalse(self.find_binding("a", "class d(b, a=7): pass"))
  228. self.assertFalse(self.find_binding("a", "class d(b, *a): pass"))
  229. self.assertFalse(self.find_binding("a", "class d(b, **a): pass"))
  230. self.assertFalse(self.find_binding("a", "class d: a = 7"))
  231. s = """
  232. class d():
  233. class a():
  234. pass"""
  235. self.assertFalse(self.find_binding("a", s))
  236. def test_for(self):
  237. self.assertTrue(self.find_binding("a", "for a in r: pass"))
  238. self.assertTrue(self.find_binding("a", "for a, b in r: pass"))
  239. self.assertTrue(self.find_binding("a", "for (a, b) in r: pass"))
  240. self.assertTrue(self.find_binding("a", "for c, (a,) in r: pass"))
  241. self.assertTrue(self.find_binding("a", "for c, (a, b) in r: pass"))
  242. self.assertTrue(self.find_binding("a", "for c in r: a = c"))
  243. self.assertFalse(self.find_binding("a", "for c in a: pass"))
  244. def test_for_nested(self):
  245. s = """
  246. for b in r:
  247. for a in b:
  248. pass"""
  249. self.assertTrue(self.find_binding("a", s))
  250. s = """
  251. for b in r:
  252. for a, c in b:
  253. pass"""
  254. self.assertTrue(self.find_binding("a", s))
  255. s = """
  256. for b in r:
  257. for (a, c) in b:
  258. pass"""
  259. self.assertTrue(self.find_binding("a", s))
  260. s = """
  261. for b in r:
  262. for (a,) in b:
  263. pass"""
  264. self.assertTrue(self.find_binding("a", s))
  265. s = """
  266. for b in r:
  267. for c, (a, d) in b:
  268. pass"""
  269. self.assertTrue(self.find_binding("a", s))
  270. s = """
  271. for b in r:
  272. for c in b:
  273. a = 7"""
  274. self.assertTrue(self.find_binding("a", s))
  275. s = """
  276. for b in r:
  277. for c in b:
  278. d = a"""
  279. self.assertFalse(self.find_binding("a", s))
  280. s = """
  281. for b in r:
  282. for c in a:
  283. d = 7"""
  284. self.assertFalse(self.find_binding("a", s))
  285. def test_if(self):
  286. self.assertTrue(self.find_binding("a", "if b in r: a = c"))
  287. self.assertFalse(self.find_binding("a", "if a in r: d = e"))
  288. def test_if_nested(self):
  289. s = """
  290. if b in r:
  291. if c in d:
  292. a = c"""
  293. self.assertTrue(self.find_binding("a", s))
  294. s = """
  295. if b in r:
  296. if c in d:
  297. c = a"""
  298. self.assertFalse(self.find_binding("a", s))
  299. def test_while(self):
  300. self.assertTrue(self.find_binding("a", "while b in r: a = c"))
  301. self.assertFalse(self.find_binding("a", "while a in r: d = e"))
  302. def test_while_nested(self):
  303. s = """
  304. while b in r:
  305. while c in d:
  306. a = c"""
  307. self.assertTrue(self.find_binding("a", s))
  308. s = """
  309. while b in r:
  310. while c in d:
  311. c = a"""
  312. self.assertFalse(self.find_binding("a", s))
  313. def test_try_except(self):
  314. s = """
  315. try:
  316. a = 6
  317. except:
  318. b = 8"""
  319. self.assertTrue(self.find_binding("a", s))
  320. s = """
  321. try:
  322. b = 8
  323. except:
  324. a = 6"""
  325. self.assertTrue(self.find_binding("a", s))
  326. s = """
  327. try:
  328. b = 8
  329. except KeyError:
  330. pass
  331. except:
  332. a = 6"""
  333. self.assertTrue(self.find_binding("a", s))
  334. s = """
  335. try:
  336. b = 8
  337. except:
  338. b = 6"""
  339. self.assertFalse(self.find_binding("a", s))
  340. def test_try_except_nested(self):
  341. s = """
  342. try:
  343. try:
  344. a = 6
  345. except:
  346. pass
  347. except:
  348. b = 8"""
  349. self.assertTrue(self.find_binding("a", s))
  350. s = """
  351. try:
  352. b = 8
  353. except:
  354. try:
  355. a = 6
  356. except:
  357. pass"""
  358. self.assertTrue(self.find_binding("a", s))
  359. s = """
  360. try:
  361. b = 8
  362. except:
  363. try:
  364. pass
  365. except:
  366. a = 6"""
  367. self.assertTrue(self.find_binding("a", s))
  368. s = """
  369. try:
  370. try:
  371. b = 8
  372. except KeyError:
  373. pass
  374. except:
  375. a = 6
  376. except:
  377. pass"""
  378. self.assertTrue(self.find_binding("a", s))
  379. s = """
  380. try:
  381. pass
  382. except:
  383. try:
  384. b = 8
  385. except KeyError:
  386. pass
  387. except:
  388. a = 6"""
  389. self.assertTrue(self.find_binding("a", s))
  390. s = """
  391. try:
  392. b = 8
  393. except:
  394. b = 6"""
  395. self.assertFalse(self.find_binding("a", s))
  396. s = """
  397. try:
  398. try:
  399. b = 8
  400. except:
  401. c = d
  402. except:
  403. try:
  404. b = 6
  405. except:
  406. t = 8
  407. except:
  408. o = y"""
  409. self.assertFalse(self.find_binding("a", s))
  410. def test_try_except_finally(self):
  411. s = """
  412. try:
  413. c = 6
  414. except:
  415. b = 8
  416. finally:
  417. a = 9"""
  418. self.assertTrue(self.find_binding("a", s))
  419. s = """
  420. try:
  421. b = 8
  422. finally:
  423. a = 6"""
  424. self.assertTrue(self.find_binding("a", s))
  425. s = """
  426. try:
  427. b = 8
  428. finally:
  429. b = 6"""
  430. self.assertFalse(self.find_binding("a", s))
  431. s = """
  432. try:
  433. b = 8
  434. except:
  435. b = 9
  436. finally:
  437. b = 6"""
  438. self.assertFalse(self.find_binding("a", s))
  439. def test_try_except_finally_nested(self):
  440. s = """
  441. try:
  442. c = 6
  443. except:
  444. b = 8
  445. finally:
  446. try:
  447. a = 9
  448. except:
  449. b = 9
  450. finally:
  451. c = 9"""
  452. self.assertTrue(self.find_binding("a", s))
  453. s = """
  454. try:
  455. b = 8
  456. finally:
  457. try:
  458. pass
  459. finally:
  460. a = 6"""
  461. self.assertTrue(self.find_binding("a", s))
  462. s = """
  463. try:
  464. b = 8
  465. finally:
  466. try:
  467. b = 6
  468. finally:
  469. b = 7"""
  470. self.assertFalse(self.find_binding("a", s))
  471. class Test_touch_import(support.TestCase):
  472. def test_after_docstring(self):
  473. node = parse('"""foo"""\nbar()')
  474. fixer_util.touch_import(None, "foo", node)
  475. self.assertEqual(str(node), '"""foo"""\nimport foo\nbar()\n\n')
  476. def test_after_imports(self):
  477. node = parse('"""foo"""\nimport bar\nbar()')
  478. fixer_util.touch_import(None, "foo", node)
  479. self.assertEqual(str(node), '"""foo"""\nimport bar\nimport foo\nbar()\n\n')
  480. def test_beginning(self):
  481. node = parse('bar()')
  482. fixer_util.touch_import(None, "foo", node)
  483. self.assertEqual(str(node), 'import foo\nbar()\n\n')
  484. def test_from_import(self):
  485. node = parse('bar()')
  486. fixer_util.touch_import("html", "escape", node)
  487. self.assertEqual(str(node), 'from html import escape\nbar()\n\n')
  488. def test_name_import(self):
  489. node = parse('bar()')
  490. fixer_util.touch_import(None, "cgi", node)
  491. self.assertEqual(str(node), 'import cgi\nbar()\n\n')
  492. class Test_find_indentation(support.TestCase):
  493. def test_nothing(self):
  494. fi = fixer_util.find_indentation
  495. node = parse("node()")
  496. self.assertEqual(fi(node), "")
  497. node = parse("")
  498. self.assertEqual(fi(node), "")
  499. def test_simple(self):
  500. fi = fixer_util.find_indentation
  501. node = parse("def f():\n x()")
  502. self.assertEqual(fi(node), "")
  503. self.assertEqual(fi(node.children[0].children[4].children[2]), " ")
  504. node = parse("def f():\n x()\n y()")
  505. self.assertEqual(fi(node.children[0].children[4].children[4]), " ")