-
Notifications
You must be signed in to change notification settings - Fork 6
/
test_console_script.py
159 lines (133 loc) · 4.94 KB
/
test_console_script.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import re
import pytest
from compact_json import _get_version
def test_version(script_runner):
ret = script_runner.run(["compact-json", "--version"], print_result=False)
assert ret.success
assert ret.stdout == _get_version() + "\n"
assert ret.stderr == ""
def test_help(script_runner):
ret = script_runner.run(["compact-json", "--help"], print_result=False)
assert ret.success
assert "Format JSON into compact, human readable form" in ret.stdout
assert "Indent N" in ret.stdout
assert ret.stderr == ""
REF_ARG_TEST = """//{
// "ObjectColumnsArrayRows": {
// "Katherine": [ "blue" , "lightblue", "black" ],
// "Logan" : [ "yellow" , "blue" , "black", "red" ],
// "Erik" : [ "red" , "purple" ],
// "Jean" : [ "lightgreen", "yellow" , "black" ]
// },
// "ArrayColumnsArrayRows" : [
// [ 0.1, 3.5, 10.50, 6.5, 2.50, 0.60 ], [ 0.1, 0.1, 1.20, 2.1, 6.70, 4.40 ], [ 0.4, 1.9, 4.40, 5.4, 2.35, 2.01 ],
// [ 7.4, 1.2, 0.01, 0.1, 2.91, 0.20 ]
// ],
// "DissimilarArrayRows" : {
// "primes" : [ 2, 3, 5, 7, 11 ],
// "powersOf2" : [ 1, 2, 4, 8, 16, 32, 64, 128, 256 ],
// "factorsOf12": [ 2, 2, 3 ],
// "someZeros" : [ 0, 0, 0, 0 ]
// }
//}
""" # noqa: W291, E501
REF_UNICODE_TEST = """{
"Thai": {
"Abkhazia": "อับฮาเซีย",
"Afghanistan": "อัฟกานิสถาน",
"Albania": "แอลเบเนีย"
},
"Lao": {"Afghanistan": "ອັຟການິດສະຖານ"},
"Uyghur": {"Albania": "ئالبانىيە"},
"Hindi, Marathi, Sanskrit": {"Albania": "अल्बानिया"},
"Western Armenian": {"Albania": "Ալբանիա"}
}
""" # noqa: W291
def test_args(script_runner, pytestconfig):
ret = script_runner.run(
[
"compact-json",
"--indent=2",
"--tab-indent",
"--justify-numbers",
"--prefix-string=//",
"--align-properties",
"--bracket-padding=simple",
"--max-compact-list-complexity=2",
"--max-inline-length=120",
"tests/data/test-12.json",
],
print_result=False,
)
if pytestconfig.getoption("test_verbose") and ret.stdout != REF_ARG_TEST:
json_string_dbg = ">" + re.sub(r"\n", "<\n>", ret.stdout) + "<"
ref_json_dbg = ">" + re.sub(r"\n", "<\n>", REF_ARG_TEST) + "<"
print("===== TEST")
print(json_string_dbg)
print("===== REF")
print(ref_json_dbg)
print("=====")
assert ret.stderr == ""
assert ret.success
assert ret.stdout == REF_ARG_TEST
def test_unicode(script_runner):
ret = script_runner.run(
[
"compact-json",
"--east-asian-chars",
"--no-ensure-ascii",
"--crlf",
"tests/data/test-issue-4a.json",
],
print_result=False,
)
assert ret.stderr == ""
assert ret.success
ref = REF_UNICODE_TEST.replace("\n", "\r\n")
assert ret.stdout == ref
def test_debug(script_runner):
ret = script_runner.run(["compact-json", "--debug", "tests/data/test-1.json"])
assert "DEBUG:compact_json.formatter:format_table_dict_list" in ret.stderr
assert ret.success
assert '"title": "Sample Konfabulator Widget"' in ret.stdout
@pytest.mark.script_launch_mode("subprocess")
def test_main(script_runner):
ret = script_runner.run(["python3", "-m", "compact_json", "--help"])
assert ret.stderr == ""
assert ret.success
assert "[-h] [-V] [--output" in ret.stdout
@pytest.mark.script_launch_mode("subprocess")
def test_stdin(script_runner):
with open("tests/data/test-bool.json") as fh:
ret = script_runner.run(["compact-json", "-"], stdin=fh)
assert ret.stderr == ""
assert ret.success
assert ret.stdout == '{ "bools": {"true": true, "false": false} }\n'
def test_multifile(script_runner):
ret = script_runner.run(
["compact-json", "tests/data/test-bool.json", "tests/data/test-bool.json"],
)
assert ret.stderr == ""
assert ret.success
assert ret.stdout == '{ "bools": {"true": true, "false": false} }\n' * 2
def test_output(script_runner, tmp_path):
tmp_file = tmp_path / "test.json"
ret = script_runner.run(
["compact-json", "tests/data/test-bool.json", "--output", str(tmp_file)],
)
assert ret.stderr == ""
assert ret.success
assert tmp_file.read_text() == '{ "bools": {"true": true, "false": false} }\n'
def test_output_mismatched_number_of_files(script_runner):
ret = script_runner.run(
[
"compact-json",
"tests/data/test-bool.json",
"--output",
"foo",
"--output",
"bar",
],
)
assert ret.stderr == "compact-json: the numbers of input and output file names do not match\n"
assert ret.returncode == 1