This repo contains code to mirror other repos. It also contains the code that is getting mirrored.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
1.5 KiB

  1. from __future__ import absolute_import
  2. import unittest
  3. from unittest import TestCase
  4. import copy
  5. import pickle
  6. from lark.tree import Tree, Interpreter, visit_children_decor
  7. class TestTrees(TestCase):
  8. def setUp(self):
  9. self.tree1 = Tree('a', [Tree(x, y) for x, y in zip('bcd', 'xyz')])
  10. def test_deepcopy(self):
  11. assert self.tree1 == copy.deepcopy(self.tree1)
  12. def test_pickle(self):
  13. s = copy.deepcopy(self.tree1)
  14. data = pickle.dumps(s)
  15. assert pickle.loads(data) == s
  16. def test_interp(self):
  17. t = Tree('a', [Tree('b', []), Tree('c', []), 'd'])
  18. class Interp1(Interpreter):
  19. def a(self, tree):
  20. return self.visit_children(tree) + ['e']
  21. def b(self, tree):
  22. return 'B'
  23. def c(self, tree):
  24. return 'C'
  25. self.assertEqual(Interp1().visit(t), list('BCde'))
  26. class Interp2(Interpreter):
  27. @visit_children_decor
  28. def a(self, values):
  29. return values + ['e']
  30. def b(self, tree):
  31. return 'B'
  32. def c(self, tree):
  33. return 'C'
  34. self.assertEqual(Interp2().visit(t), list('BCde'))
  35. class Interp3(Interpreter):
  36. def b(self, tree):
  37. return 'B'
  38. def c(self, tree):
  39. return 'C'
  40. self.assertEqual(Interp3().visit(t), list('BCd'))
  41. if __name__ == '__main__':
  42. unittest.main()