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