| 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 | from typing import Optional
|
| 13 |
|
| 14 | import libc
|
| 15 |
|
| 16 | TEST_DATA_FILENAME = os.path.join(os.path.dirname(__file__), "pretty_test.txt")
|
| 17 |
|
| 18 |
|
| 19 | def IntValue(i):
|
| 20 | # type: (int) -> value_t
|
| 21 | return value.Int(mops.IntWiden(i))
|
| 22 |
|
| 23 |
|
| 24 | class PrettyTest(unittest.TestCase):
|
| 25 |
|
| 26 | @classmethod
|
| 27 | def setUpClass(cls):
|
| 28 | # Use settings that make testing easier.
|
| 29 | cls.printer = pretty.PrettyPrinter()
|
| 30 | cls.printer.SetUseStyles(False)
|
| 31 | cls.printer.SetShowTypePrefix(False)
|
| 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 | 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 | if lineno is not None:
|
| 51 | print("ON LINE " + str(lineno + 1))
|
| 52 | self.assertEqual(buf.getvalue(), expected)
|
| 53 |
|
| 54 | def testsFromFile(self):
|
| 55 | chunks = [(None, -1, [])]
|
| 56 | for lineno, line in enumerate(
|
| 57 | open(TEST_DATA_FILENAME).read().splitlines()):
|
| 58 | if line.startswith("> "):
|
| 59 | chunks[-1][2].append(line[2:])
|
| 60 | elif line.startswith("#"):
|
| 61 | pass
|
| 62 | elif line.strip() == "":
|
| 63 | pass
|
| 64 | else:
|
| 65 | for keyword in ["Width", "Input", "Expect"]:
|
| 66 | if line.startswith(keyword):
|
| 67 | if chunks[-1][0] != keyword:
|
| 68 | chunks.append((keyword, lineno, []))
|
| 69 | parts = line.split(" > ", 1)
|
| 70 | if len(parts) == 2:
|
| 71 | chunks[-1][2].append(parts[1])
|
| 72 | break
|
| 73 | else:
|
| 74 | raise Exception(
|
| 75 | "Invalid pretty printing test case line. Lines must start with one of: Width, Input, Expect, >, #",
|
| 76 | line)
|
| 77 |
|
| 78 | test_cases = []
|
| 79 | width = 80
|
| 80 | value = ""
|
| 81 | for (keyword, lineno, lines) in chunks:
|
| 82 | block = "\n".join(lines)
|
| 83 | if keyword == "Width":
|
| 84 | width = int(block)
|
| 85 | elif keyword == "Input":
|
| 86 | value = block
|
| 87 | elif keyword == "Expect":
|
| 88 | test_cases.append((width, value, block, lineno))
|
| 89 | else:
|
| 90 | pass
|
| 91 |
|
| 92 | for (width, value, expected, lineno) in test_cases:
|
| 93 | self.assertPretty(width, value, expected, lineno)
|
| 94 |
|
| 95 | def testStyles(self):
|
| 96 | self.printer.SetUseStyles(True)
|
| 97 | self.assertPretty(
|
| 98 | 20, '[null, "ok", 15]', '[' + ansi.BOLD + ansi.RED + 'null' +
|
| 99 | ansi.RESET + ", " + ansi.GREEN + '"ok"' + ansi.RESET + ", " +
|
| 100 | ansi.YELLOW + '15' + ansi.RESET + ']')
|
| 101 | self.printer.SetUseStyles(False)
|
| 102 |
|
| 103 | def testTypePrefix(self):
|
| 104 | self.printer.SetShowTypePrefix(True)
|
| 105 | self.assertPretty(25, '[null, "ok", 15]', '(List) [null, "ok", 15]')
|
| 106 | self.assertPretty(24, '[null, "ok", 15]', '(List)\n[null, "ok", 15]')
|
| 107 | self.printer.SetShowTypePrefix(False)
|
| 108 |
|
| 109 |
|
| 110 | if __name__ == '__main__':
|
| 111 | # To simulate the OVM_MAIN patch in pythonrun.c
|
| 112 | libc.cpython_reset_locale()
|
| 113 | unittest.main()
|