OILS / data_lang / pretty_test.py View on Github | oilshell.org

117 lines, 87 significant
1#!/usr/bin/env python2
2# coding=utf8
3
4import os
5import unittest
6
7from _devbuild.gen.value_asdl import value, value_t
8from core import ansi
9from data_lang import j8
10from data_lang import pretty # module under test
11from mycpp import mylib, mops
12from typing import Optional
13
14import libc
15
16TEST_DATA_FILENAME = os.path.join(os.path.dirname(__file__), "pretty_test.txt")
17
18
19def IntValue(i):
20 # type: (int) -> value_t
21 return value.Int(mops.IntWiden(i))
22
23
24class PrettyTest(unittest.TestCase):
25
26 def setUp(self):
27 # Use settings that make testing easier.
28 self.printer = pretty.PrettyPrinter()
29 self.encoder = pretty.ValueEncoder()
30 self.encoder.SetUseStyles(False)
31 self.encoder.SetYshStyle()
32
33 def assertPretty(self, width, value_str, expected, lineno=None):
34 # type: (int, str, str, Optional[int]) -> None
35 parser = j8.Parser(value_str, True)
36 val = parser.ParseValue()
37
38 buf = mylib.BufWriter()
39 self.printer.SetMaxWidth(width)
40
41 doc = self.encoder.Value(val)
42 self.printer.PrintDoc(doc, buf)
43
44 actual = buf.getvalue()
45
46 if actual != expected:
47 # Print the different with real newlines, for easier reading.
48 print("ACTUAL:")
49 print(actual)
50 print("EXPECTED:")
51 print(expected)
52 print("END")
53 if lineno is not None:
54 print("ON LINE " + str(lineno + 1))
55 self.assertEqual(buf.getvalue(), expected)
56
57 def testsFromFile(self):
58 # TODO: convert tests to this new style
59 self.encoder.SetShowTypePrefix(False)
60 self.encoder.ysh_style = False
61
62 chunks = [(None, -1, [])]
63 for lineno, line in enumerate(
64 open(TEST_DATA_FILENAME).read().splitlines()):
65 if line.startswith("> "):
66 chunks[-1][2].append(line[2:])
67 elif line.startswith("#"):
68 pass
69 elif line.strip() == "":
70 pass
71 else:
72 for keyword in ["Width", "Input", "Expect"]:
73 if line.startswith(keyword):
74 if chunks[-1][0] != keyword:
75 chunks.append((keyword, lineno, []))
76 parts = line.split(" > ", 1)
77 if len(parts) == 2:
78 chunks[-1][2].append(parts[1])
79 break
80 else:
81 raise Exception(
82 "Invalid pretty printing test case line. Lines must start with one of: Width, Input, Expect, >, #",
83 line)
84
85 test_cases = []
86 width = 80
87 value = ""
88 for (keyword, lineno, lines) in chunks:
89 block = "\n".join(lines)
90 if keyword == "Width":
91 width = int(block)
92 elif keyword == "Input":
93 value = block
94 elif keyword == "Expect":
95 test_cases.append((width, value, block, lineno))
96 else:
97 pass
98
99 for (width, value, expected, lineno) in test_cases:
100 self.assertPretty(width, value, expected, lineno)
101
102 def testStyles(self):
103 self.encoder.SetUseStyles(True)
104 self.assertPretty(
105 20, '[null, "ok", 15]', '(' + ansi.MAGENTA + 'List' + ansi.RESET +
106 ')\n[' + ansi.RED + 'null' + ansi.RESET + ", " + ansi.GREEN +
107 "'ok'" + ansi.RESET + ", " + ansi.YELLOW + '15' + ansi.RESET + ']')
108
109 def testTypePrefix(self):
110 self.assertPretty(25, '[null, "ok", 15]', "(List) [null, 'ok', 15]")
111 self.assertPretty(24, '[null, "ok", 15]', "(List)\n[null, 'ok', 15]")
112
113
114if __name__ == '__main__':
115 # To simulate the OVM_MAIN patch in pythonrun.c
116 libc.cpython_reset_locale()
117 unittest.main()