test_pytree.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. # Copyright 2006 Google, Inc. All Rights Reserved.
  2. # Licensed to PSF under a Contributor Agreement.
  3. """Unit tests for pytree.py.
  4. NOTE: Please *don't* add doc strings to individual test methods!
  5. In verbose mode, printing of the module, class and method name is much
  6. more helpful than printing of (the first line of) the docstring,
  7. especially when debugging a test.
  8. """
  9. # Testing imports
  10. from . import support
  11. from lib2to3 import pytree
  12. try:
  13. sorted
  14. except NameError:
  15. def sorted(lst):
  16. l = list(lst)
  17. l.sort()
  18. return l
  19. class TestNodes(support.TestCase):
  20. """Unit tests for nodes (Base, Leaf, Node)."""
  21. def test_instantiate_base(self):
  22. if __debug__:
  23. # Test that instantiating Base() raises an AssertionError
  24. self.assertRaises(AssertionError, pytree.Base)
  25. def test_leaf(self):
  26. l1 = pytree.Leaf(100, "foo")
  27. self.assertEqual(l1.type, 100)
  28. self.assertEqual(l1.value, "foo")
  29. def test_leaf_repr(self):
  30. l1 = pytree.Leaf(100, "foo")
  31. self.assertEqual(repr(l1), "Leaf(100, 'foo')")
  32. def test_leaf_str(self):
  33. l1 = pytree.Leaf(100, "foo")
  34. self.assertEqual(str(l1), "foo")
  35. l2 = pytree.Leaf(100, "foo", context=(" ", (10, 1)))
  36. self.assertEqual(str(l2), " foo")
  37. def test_leaf_str_numeric_value(self):
  38. # Make sure that the Leaf's value is stringified. Failing to
  39. # do this can cause a TypeError in certain situations.
  40. l1 = pytree.Leaf(2, 5)
  41. l1.prefix = "foo_"
  42. self.assertEqual(str(l1), "foo_5")
  43. def test_leaf_equality(self):
  44. l1 = pytree.Leaf(100, "foo")
  45. l2 = pytree.Leaf(100, "foo", context=(" ", (1, 0)))
  46. self.assertEqual(l1, l2)
  47. l3 = pytree.Leaf(101, "foo")
  48. l4 = pytree.Leaf(100, "bar")
  49. self.assertNotEqual(l1, l3)
  50. self.assertNotEqual(l1, l4)
  51. def test_leaf_prefix(self):
  52. l1 = pytree.Leaf(100, "foo")
  53. self.assertEqual(l1.prefix, "")
  54. self.assertFalse(l1.was_changed)
  55. l1.prefix = " ##\n\n"
  56. self.assertEqual(l1.prefix, " ##\n\n")
  57. self.assertTrue(l1.was_changed)
  58. def test_node(self):
  59. l1 = pytree.Leaf(100, "foo")
  60. l2 = pytree.Leaf(200, "bar")
  61. n1 = pytree.Node(1000, [l1, l2])
  62. self.assertEqual(n1.type, 1000)
  63. self.assertEqual(n1.children, [l1, l2])
  64. def test_node_repr(self):
  65. l1 = pytree.Leaf(100, "foo")
  66. l2 = pytree.Leaf(100, "bar", context=(" ", (1, 0)))
  67. n1 = pytree.Node(1000, [l1, l2])
  68. self.assertEqual(repr(n1),
  69. "Node(1000, [%s, %s])" % (repr(l1), repr(l2)))
  70. def test_node_str(self):
  71. l1 = pytree.Leaf(100, "foo")
  72. l2 = pytree.Leaf(100, "bar", context=(" ", (1, 0)))
  73. n1 = pytree.Node(1000, [l1, l2])
  74. self.assertEqual(str(n1), "foo bar")
  75. def test_node_prefix(self):
  76. l1 = pytree.Leaf(100, "foo")
  77. self.assertEqual(l1.prefix, "")
  78. n1 = pytree.Node(1000, [l1])
  79. self.assertEqual(n1.prefix, "")
  80. n1.prefix = " "
  81. self.assertEqual(n1.prefix, " ")
  82. self.assertEqual(l1.prefix, " ")
  83. def test_get_suffix(self):
  84. l1 = pytree.Leaf(100, "foo", prefix="a")
  85. l2 = pytree.Leaf(100, "bar", prefix="b")
  86. n1 = pytree.Node(1000, [l1, l2])
  87. self.assertEqual(l1.get_suffix(), l2.prefix)
  88. self.assertEqual(l2.get_suffix(), "")
  89. self.assertEqual(n1.get_suffix(), "")
  90. l3 = pytree.Leaf(100, "bar", prefix="c")
  91. n2 = pytree.Node(1000, [n1, l3])
  92. self.assertEqual(n1.get_suffix(), l3.prefix)
  93. self.assertEqual(l3.get_suffix(), "")
  94. self.assertEqual(n2.get_suffix(), "")
  95. def test_node_equality(self):
  96. n1 = pytree.Node(1000, ())
  97. n2 = pytree.Node(1000, [], context=(" ", (1, 0)))
  98. self.assertEqual(n1, n2)
  99. n3 = pytree.Node(1001, ())
  100. self.assertNotEqual(n1, n3)
  101. def test_node_recursive_equality(self):
  102. l1 = pytree.Leaf(100, "foo")
  103. l2 = pytree.Leaf(100, "foo")
  104. n1 = pytree.Node(1000, [l1])
  105. n2 = pytree.Node(1000, [l2])
  106. self.assertEqual(n1, n2)
  107. l3 = pytree.Leaf(100, "bar")
  108. n3 = pytree.Node(1000, [l3])
  109. self.assertNotEqual(n1, n3)
  110. def test_replace(self):
  111. l1 = pytree.Leaf(100, "foo")
  112. l2 = pytree.Leaf(100, "+")
  113. l3 = pytree.Leaf(100, "bar")
  114. n1 = pytree.Node(1000, [l1, l2, l3])
  115. self.assertEqual(n1.children, [l1, l2, l3])
  116. self.assertIsInstance(n1.children, list)
  117. self.assertFalse(n1.was_changed)
  118. l2new = pytree.Leaf(100, "-")
  119. l2.replace(l2new)
  120. self.assertEqual(n1.children, [l1, l2new, l3])
  121. self.assertIsInstance(n1.children, list)
  122. self.assertTrue(n1.was_changed)
  123. def test_replace_with_list(self):
  124. l1 = pytree.Leaf(100, "foo")
  125. l2 = pytree.Leaf(100, "+")
  126. l3 = pytree.Leaf(100, "bar")
  127. n1 = pytree.Node(1000, [l1, l2, l3])
  128. l2.replace([pytree.Leaf(100, "*"), pytree.Leaf(100, "*")])
  129. self.assertEqual(str(n1), "foo**bar")
  130. self.assertIsInstance(n1.children, list)
  131. def test_leaves(self):
  132. l1 = pytree.Leaf(100, "foo")
  133. l2 = pytree.Leaf(100, "bar")
  134. l3 = pytree.Leaf(100, "fooey")
  135. n2 = pytree.Node(1000, [l1, l2])
  136. n3 = pytree.Node(1000, [l3])
  137. n1 = pytree.Node(1000, [n2, n3])
  138. self.assertEqual(list(n1.leaves()), [l1, l2, l3])
  139. def test_depth(self):
  140. l1 = pytree.Leaf(100, "foo")
  141. l2 = pytree.Leaf(100, "bar")
  142. n2 = pytree.Node(1000, [l1, l2])
  143. n3 = pytree.Node(1000, [])
  144. n1 = pytree.Node(1000, [n2, n3])
  145. self.assertEqual(l1.depth(), 2)
  146. self.assertEqual(n3.depth(), 1)
  147. self.assertEqual(n1.depth(), 0)
  148. def test_post_order(self):
  149. l1 = pytree.Leaf(100, "foo")
  150. l2 = pytree.Leaf(100, "bar")
  151. l3 = pytree.Leaf(100, "fooey")
  152. c1 = pytree.Node(1000, [l1, l2])
  153. n1 = pytree.Node(1000, [c1, l3])
  154. self.assertEqual(list(n1.post_order()), [l1, l2, c1, l3, n1])
  155. def test_pre_order(self):
  156. l1 = pytree.Leaf(100, "foo")
  157. l2 = pytree.Leaf(100, "bar")
  158. l3 = pytree.Leaf(100, "fooey")
  159. c1 = pytree.Node(1000, [l1, l2])
  160. n1 = pytree.Node(1000, [c1, l3])
  161. self.assertEqual(list(n1.pre_order()), [n1, c1, l1, l2, l3])
  162. def test_changed(self):
  163. l1 = pytree.Leaf(100, "f")
  164. self.assertFalse(l1.was_changed)
  165. l1.changed()
  166. self.assertTrue(l1.was_changed)
  167. l1 = pytree.Leaf(100, "f")
  168. n1 = pytree.Node(1000, [l1])
  169. self.assertFalse(n1.was_changed)
  170. n1.changed()
  171. self.assertTrue(n1.was_changed)
  172. l1 = pytree.Leaf(100, "foo")
  173. l2 = pytree.Leaf(100, "+")
  174. l3 = pytree.Leaf(100, "bar")
  175. n1 = pytree.Node(1000, [l1, l2, l3])
  176. n2 = pytree.Node(1000, [n1])
  177. self.assertFalse(l1.was_changed)
  178. self.assertFalse(n1.was_changed)
  179. self.assertFalse(n2.was_changed)
  180. n1.changed()
  181. self.assertTrue(n1.was_changed)
  182. self.assertTrue(n2.was_changed)
  183. self.assertFalse(l1.was_changed)
  184. def test_leaf_constructor_prefix(self):
  185. for prefix in ("xyz_", ""):
  186. l1 = pytree.Leaf(100, "self", prefix=prefix)
  187. self.assertTrue(str(l1), prefix + "self")
  188. self.assertEqual(l1.prefix, prefix)
  189. def test_node_constructor_prefix(self):
  190. for prefix in ("xyz_", ""):
  191. l1 = pytree.Leaf(100, "self")
  192. l2 = pytree.Leaf(100, "foo", prefix="_")
  193. n1 = pytree.Node(1000, [l1, l2], prefix=prefix)
  194. self.assertTrue(str(n1), prefix + "self_foo")
  195. self.assertEqual(n1.prefix, prefix)
  196. self.assertEqual(l1.prefix, prefix)
  197. self.assertEqual(l2.prefix, "_")
  198. def test_remove(self):
  199. l1 = pytree.Leaf(100, "foo")
  200. l2 = pytree.Leaf(100, "foo")
  201. n1 = pytree.Node(1000, [l1, l2])
  202. n2 = pytree.Node(1000, [n1])
  203. self.assertEqual(n1.remove(), 0)
  204. self.assertEqual(n2.children, [])
  205. self.assertEqual(l1.parent, n1)
  206. self.assertEqual(n1.parent, None)
  207. self.assertEqual(n2.parent, None)
  208. self.assertFalse(n1.was_changed)
  209. self.assertTrue(n2.was_changed)
  210. self.assertEqual(l2.remove(), 1)
  211. self.assertEqual(l1.remove(), 0)
  212. self.assertEqual(n1.children, [])
  213. self.assertEqual(l1.parent, None)
  214. self.assertEqual(n1.parent, None)
  215. self.assertEqual(n2.parent, None)
  216. self.assertTrue(n1.was_changed)
  217. self.assertTrue(n2.was_changed)
  218. def test_remove_parentless(self):
  219. n1 = pytree.Node(1000, [])
  220. n1.remove()
  221. self.assertEqual(n1.parent, None)
  222. l1 = pytree.Leaf(100, "foo")
  223. l1.remove()
  224. self.assertEqual(l1.parent, None)
  225. def test_node_set_child(self):
  226. l1 = pytree.Leaf(100, "foo")
  227. n1 = pytree.Node(1000, [l1])
  228. l2 = pytree.Leaf(100, "bar")
  229. n1.set_child(0, l2)
  230. self.assertEqual(l1.parent, None)
  231. self.assertEqual(l2.parent, n1)
  232. self.assertEqual(n1.children, [l2])
  233. n2 = pytree.Node(1000, [l1])
  234. n2.set_child(0, n1)
  235. self.assertEqual(l1.parent, None)
  236. self.assertEqual(n1.parent, n2)
  237. self.assertEqual(n2.parent, None)
  238. self.assertEqual(n2.children, [n1])
  239. self.assertRaises(IndexError, n1.set_child, 4, l2)
  240. # I don't care what it raises, so long as it's an exception
  241. self.assertRaises(Exception, n1.set_child, 0, list)
  242. def test_node_insert_child(self):
  243. l1 = pytree.Leaf(100, "foo")
  244. n1 = pytree.Node(1000, [l1])
  245. l2 = pytree.Leaf(100, "bar")
  246. n1.insert_child(0, l2)
  247. self.assertEqual(l2.parent, n1)
  248. self.assertEqual(n1.children, [l2, l1])
  249. l3 = pytree.Leaf(100, "abc")
  250. n1.insert_child(2, l3)
  251. self.assertEqual(n1.children, [l2, l1, l3])
  252. # I don't care what it raises, so long as it's an exception
  253. self.assertRaises(Exception, n1.insert_child, 0, list)
  254. def test_node_append_child(self):
  255. n1 = pytree.Node(1000, [])
  256. l1 = pytree.Leaf(100, "foo")
  257. n1.append_child(l1)
  258. self.assertEqual(l1.parent, n1)
  259. self.assertEqual(n1.children, [l1])
  260. l2 = pytree.Leaf(100, "bar")
  261. n1.append_child(l2)
  262. self.assertEqual(l2.parent, n1)
  263. self.assertEqual(n1.children, [l1, l2])
  264. # I don't care what it raises, so long as it's an exception
  265. self.assertRaises(Exception, n1.append_child, list)
  266. def test_node_next_sibling(self):
  267. n1 = pytree.Node(1000, [])
  268. n2 = pytree.Node(1000, [])
  269. p1 = pytree.Node(1000, [n1, n2])
  270. self.assertIs(n1.next_sibling, n2)
  271. self.assertEqual(n2.next_sibling, None)
  272. self.assertEqual(p1.next_sibling, None)
  273. def test_leaf_next_sibling(self):
  274. l1 = pytree.Leaf(100, "a")
  275. l2 = pytree.Leaf(100, "b")
  276. p1 = pytree.Node(1000, [l1, l2])
  277. self.assertIs(l1.next_sibling, l2)
  278. self.assertEqual(l2.next_sibling, None)
  279. self.assertEqual(p1.next_sibling, None)
  280. def test_node_prev_sibling(self):
  281. n1 = pytree.Node(1000, [])
  282. n2 = pytree.Node(1000, [])
  283. p1 = pytree.Node(1000, [n1, n2])
  284. self.assertIs(n2.prev_sibling, n1)
  285. self.assertEqual(n1.prev_sibling, None)
  286. self.assertEqual(p1.prev_sibling, None)
  287. def test_leaf_prev_sibling(self):
  288. l1 = pytree.Leaf(100, "a")
  289. l2 = pytree.Leaf(100, "b")
  290. p1 = pytree.Node(1000, [l1, l2])
  291. self.assertIs(l2.prev_sibling, l1)
  292. self.assertEqual(l1.prev_sibling, None)
  293. self.assertEqual(p1.prev_sibling, None)
  294. class TestPatterns(support.TestCase):
  295. """Unit tests for tree matching patterns."""
  296. def test_basic_patterns(self):
  297. # Build a tree
  298. l1 = pytree.Leaf(100, "foo")
  299. l2 = pytree.Leaf(100, "bar")
  300. l3 = pytree.Leaf(100, "foo")
  301. n1 = pytree.Node(1000, [l1, l2])
  302. n2 = pytree.Node(1000, [l3])
  303. root = pytree.Node(1000, [n1, n2])
  304. # Build a pattern matching a leaf
  305. pl = pytree.LeafPattern(100, "foo", name="pl")
  306. r = {}
  307. self.assertFalse(pl.match(root, results=r))
  308. self.assertEqual(r, {})
  309. self.assertFalse(pl.match(n1, results=r))
  310. self.assertEqual(r, {})
  311. self.assertFalse(pl.match(n2, results=r))
  312. self.assertEqual(r, {})
  313. self.assertTrue(pl.match(l1, results=r))
  314. self.assertEqual(r, {"pl": l1})
  315. r = {}
  316. self.assertFalse(pl.match(l2, results=r))
  317. self.assertEqual(r, {})
  318. # Build a pattern matching a node
  319. pn = pytree.NodePattern(1000, [pl], name="pn")
  320. self.assertFalse(pn.match(root, results=r))
  321. self.assertEqual(r, {})
  322. self.assertFalse(pn.match(n1, results=r))
  323. self.assertEqual(r, {})
  324. self.assertTrue(pn.match(n2, results=r))
  325. self.assertEqual(r, {"pn": n2, "pl": l3})
  326. r = {}
  327. self.assertFalse(pn.match(l1, results=r))
  328. self.assertEqual(r, {})
  329. self.assertFalse(pn.match(l2, results=r))
  330. self.assertEqual(r, {})
  331. def test_wildcard(self):
  332. # Build a tree for testing
  333. l1 = pytree.Leaf(100, "foo")
  334. l2 = pytree.Leaf(100, "bar")
  335. l3 = pytree.Leaf(100, "foo")
  336. n1 = pytree.Node(1000, [l1, l2])
  337. n2 = pytree.Node(1000, [l3])
  338. root = pytree.Node(1000, [n1, n2])
  339. # Build a pattern
  340. pl = pytree.LeafPattern(100, "foo", name="pl")
  341. pn = pytree.NodePattern(1000, [pl], name="pn")
  342. pw = pytree.WildcardPattern([[pn], [pl, pl]], name="pw")
  343. r = {}
  344. self.assertFalse(pw.match_seq([root], r))
  345. self.assertEqual(r, {})
  346. self.assertFalse(pw.match_seq([n1], r))
  347. self.assertEqual(r, {})
  348. self.assertTrue(pw.match_seq([n2], r))
  349. # These are easier to debug
  350. self.assertEqual(sorted(r.keys()), ["pl", "pn", "pw"])
  351. self.assertEqual(r["pl"], l1)
  352. self.assertEqual(r["pn"], n2)
  353. self.assertEqual(r["pw"], [n2])
  354. # But this is equivalent
  355. self.assertEqual(r, {"pl": l1, "pn": n2, "pw": [n2]})
  356. r = {}
  357. self.assertTrue(pw.match_seq([l1, l3], r))
  358. self.assertEqual(r, {"pl": l3, "pw": [l1, l3]})
  359. self.assertIs(r["pl"], l3)
  360. r = {}
  361. def test_generate_matches(self):
  362. la = pytree.Leaf(1, "a")
  363. lb = pytree.Leaf(1, "b")
  364. lc = pytree.Leaf(1, "c")
  365. ld = pytree.Leaf(1, "d")
  366. le = pytree.Leaf(1, "e")
  367. lf = pytree.Leaf(1, "f")
  368. leaves = [la, lb, lc, ld, le, lf]
  369. root = pytree.Node(1000, leaves)
  370. pa = pytree.LeafPattern(1, "a", "pa")
  371. pb = pytree.LeafPattern(1, "b", "pb")
  372. pc = pytree.LeafPattern(1, "c", "pc")
  373. pd = pytree.LeafPattern(1, "d", "pd")
  374. pe = pytree.LeafPattern(1, "e", "pe")
  375. pf = pytree.LeafPattern(1, "f", "pf")
  376. pw = pytree.WildcardPattern([[pa, pb, pc], [pd, pe],
  377. [pa, pb], [pc, pd], [pe, pf]],
  378. min=1, max=4, name="pw")
  379. self.assertEqual([x[0] for x in pw.generate_matches(leaves)],
  380. [3, 5, 2, 4, 6])
  381. pr = pytree.NodePattern(type=1000, content=[pw], name="pr")
  382. matches = list(pytree.generate_matches([pr], [root]))
  383. self.assertEqual(len(matches), 1)
  384. c, r = matches[0]
  385. self.assertEqual(c, 1)
  386. self.assertEqual(str(r["pr"]), "abcdef")
  387. self.assertEqual(r["pw"], [la, lb, lc, ld, le, lf])
  388. for c in "abcdef":
  389. self.assertEqual(r["p" + c], pytree.Leaf(1, c))
  390. def test_has_key_example(self):
  391. pattern = pytree.NodePattern(331,
  392. (pytree.LeafPattern(7),
  393. pytree.WildcardPattern(name="args"),
  394. pytree.LeafPattern(8)))
  395. l1 = pytree.Leaf(7, "(")
  396. l2 = pytree.Leaf(3, "x")
  397. l3 = pytree.Leaf(8, ")")
  398. node = pytree.Node(331, [l1, l2, l3])
  399. r = {}
  400. self.assertTrue(pattern.match(node, r))
  401. self.assertEqual(r["args"], [l2])