|
- from __future__ import absolute_import
-
- import unittest
- from unittest import TestCase
- import copy
- import pickle
-
- from lark.tree import Tree, Interpreter, visit_children_decor
-
-
- class TestTrees(TestCase):
- def setUp(self):
- self.tree1 = Tree('a', [Tree(x, y) for x, y in zip('bcd', 'xyz')])
-
- def test_deepcopy(self):
- assert self.tree1 == copy.deepcopy(self.tree1)
-
- def test_pickle(self):
- s = copy.deepcopy(self.tree1)
- data = pickle.dumps(s)
- assert pickle.loads(data) == s
-
-
- def test_interp(self):
- t = Tree('a', [Tree('b', []), Tree('c', []), 'd'])
-
- class Interp1(Interpreter):
- def a(self, tree):
- return self.visit_children(tree) + ['e']
-
- def b(self, tree):
- return 'B'
-
- def c(self, tree):
- return 'C'
-
- self.assertEqual(Interp1().visit(t), list('BCde'))
-
- class Interp2(Interpreter):
- @visit_children_decor
- def a(self, values):
- return values + ['e']
-
- def b(self, tree):
- return 'B'
-
- def c(self, tree):
- return 'C'
-
- self.assertEqual(Interp2().visit(t), list('BCde'))
-
-
-
-
- if __name__ == '__main__':
- unittest.main()
|