This repo contains code to mirror other repos. It also contains the code that is getting mirrored.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 

58 行
1.3 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. if __name__ == '__main__':
  36. unittest.main()