OILS / asdl / format_test.py View on Github | oilshell.org

46 lines, 27 significant
1#!/usr/bin/env python2
2"""format_test.py: Tests for format.py."""
3
4import cStringIO
5import unittest
6
7from asdl import format as fmt
8
9from _devbuild.gen import typed_demo_asdl as demo_asdl # module under test
10
11
12class FormatTest(unittest.TestCase):
13
14 def testSimpleSum(self):
15 node = demo_asdl.op_id_e.Plus
16 # This calls __repr__, but does NOT call asdl/format.py
17 print(node)
18
19 array = demo_asdl.op_array([node, node])
20 print(array)
21
22 def testRepeatedString(self):
23 node = demo_asdl.assign('declare', ['-r', '-x'])
24
25 f = cStringIO.StringIO()
26 f1 = fmt.TextOutput(f)
27 f2 = fmt.HtmlOutput(f)
28
29 for ast_f in [f1, f2]:
30 tree = node.PrettyTree()
31
32 fmt.PrintTree(tree, ast_f)
33 pretty_str = f.getvalue()
34 print(pretty_str)
35
36 if ast_f is f1:
37 self.assertEqual('(assign name:declare flags:[-r -x])',
38 pretty_str)
39
40 t2 = node.AbbreviatedTree()
41
42 fmt.PrintTree(t2, ast_f)
43
44
45if __name__ == '__main__':
46 unittest.main()