-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
365 lines (319 loc) · 12.4 KB
/
tests.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
"""Tests for kwbar."""
import contextlib
import io
import itertools
import random
import re
import sys
import unittest
import warnings
from unittest.mock import patch
def strip_ansi(string: str) -> str:
"""Remove ANSI escape sequences from a string."""
return re.sub(r"\x1b[^m]*m", "", string)
def is_ascii(string: str) -> bool:
"""Return True if the line is ASCII."""
return all(ord(c) < 128 for c in string)
class TestTests(unittest.TestCase):
"""Test the test utility functions."""
def test_strip_ansi(self):
"""Test strip_ansi function."""
self.assertEqual(strip_ansi("\x1b[31mHello\x1b[0m"), "Hello")
def test_is_ascii(self):
"""Test is_ascii function."""
self.assertTrue(is_ascii("Hello"))
self.assertFalse(is_ascii("こんにちは"))
class TestKwbar(unittest.TestCase):
"""Test cases for kwbar."""
kwbar = None
def setUp(self):
"""Set up the test case."""
self.patcher = patch("sys.stdout.isatty", return_value=True)
self.patcher.start()
import kwbar as _kwbar
self.kwbar = _kwbar
def tearDown(self):
"""Tear down the test case."""
self.patcher.stop()
sys.modules.pop("kwbar", None)
self.kwbar = None
def test_kwbar_empty_input(self):
"""Test that kwbar raises an exception with empty input."""
# Arrange
kwbar = self.kwbar
# Act & Assert
with self.assertRaises(ValueError):
kwbar.kwbar()
def test_fuzz_fixed_width_no_warnings(self):
"""Test that kwbar does not overrun or raise warnings with a fixed width."""
# Arrange
kwbar = self.kwbar
kwbar.WIDTH = 24
kwbar.TRUNCATE = 4
for _ in range(10_000):
data = {
str(float(v)): v for v in (random.randint(0, 10_000) for _ in range(5))
}
out = io.StringIO()
with warnings.catch_warnings(record=True) as w, contextlib.redirect_stdout(
out
):
# Act
kwbar.kwbar(**data)
# Assert
self.assertEqual(len(w), 0, "warnings.warn was called")
for line in out.getvalue().splitlines():
line = strip_ansi(line)
self.assertLessEqual(len(line), kwbar.WIDTH)
def test_warns_over_width(self):
"""Test that kwbar warns when the output overflows."""
# Arrange
kwbar = self.kwbar
kwbar.WIDTH = 19
kwbar.TRUNCATE = 4
out = io.StringIO()
with warnings.catch_warnings(record=True) as w, contextlib.redirect_stdout(out):
# Act
kwbar.kwbar(one=1, two=2, pi=3.14)
# Assert
self.assertGreaterEqual(len(w), 1, "warnings.warn was not called")
self.assertIn("overflow", str(w[0].message))
def test_ascii_warns_over_width(self):
"""Test that kwbar warns when the output overflows when in ASCII mode."""
# Arrange
kwbar = self.kwbar
kwbar.ascii()
kwbar.BEFORE = False
kwbar.WIDTH = 19
kwbar.TRUNCATE = 4
out = io.StringIO()
with warnings.catch_warnings(record=True) as w, contextlib.redirect_stdout(out):
# Act
kwbar.kwbar(one=1, two=2, pi=3.14)
# Assert
self.assertGreaterEqual(len(w), 1, "warnings.warn was not called")
self.assertIn("overflow", str(w[0].message))
def test_ascii_mode(self):
"""Test that kwbar only outputs ASCII after ascii() is called."""
# Arrange
kwbar = self.kwbar
kwbar.WIDTH = 40
kwbar.TRUNCATE = 4
kwbar.ascii()
out = io.StringIO()
with contextlib.redirect_stdout(out):
# Act
kwbar.kwbar(one=1, two=2, pi=3.14)
for line in out.getvalue().splitlines():
# Assert
self.assertTrue(is_ascii(line))
def test_hotdog_mode(self):
"""Test that kwbar uses hotdog mode when requested."""
# Arrange
kwbar = self.kwbar
kwbar.WIDTH = 40
kwbar.TRUNCATE = 4
kwbar.hotdog()
out = io.StringIO()
with contextlib.redirect_stdout(out):
# Act
kwbar.kwbar(one=1, two=2, pi=3.14)
# Assert
self.assertTrue("🌭" in out.getvalue())
def test_partial_hotdogs(self):
"""Test that hotdogs counts and rounding are correct.
Hotdog mode shows to the nearest 1/8th of a hotdog:
- 1/8 = 0.125 [0.0000, 0.1875) A nibble
- 1/4 = 0.250 [0.1875, 0.3125) A generous bite
- 3/8 = 0.375 [0.3125, 0.4375) More than a bite
- 1/2 = 0.500 [0.4375, 0.5625) A fair share
- 5/8 = 0.625 [0.5625, 0.6875) A little more than half a dog
- 3/4 = 0.750 [0.6875, 0.8125) A good portion
- 7/8 = 0.875 [0.8125, 0.9375) Nearly a whole hotdog
- 🌭 = 1.000 [0.9375, 1.0000] Close enough
"""
# Arrange
kwbar = self.kwbar
kwbar.WIDTH = 23
kwbar.TRUNCATE = 10
# This will make the max bar width of 12 dogs
kwbar.hotdog()
kwbar.SHOW_VALS = False
kwbar.BEFORE = True
out = io.StringIO()
with warnings.catch_warnings(record=True) as w, contextlib.redirect_stdout(out):
# Act
kwbar.kwbar(
dozen=12,
half=6,
pie=3.1,
quarter=0.1875,
abomination=0.31251, # 5/8 is not a useful fraction
nearly=0.9374999999999999,
kinda=0.9375,
)
# Assert
# No warnings should be raised
self.assertEqual(len(w), 0, "warnings.warn was called")
# Check that there are a dozen dogs in line 0
self.assertEqual(out.getvalue().splitlines()[0].count("🌭"), 12)
# Verify that half a dozen is indeed 6 hotdogs
self.assertEqual(out.getvalue().splitlines()[1].count("🌭"), 6)
# Assert that a pie is about 3 and 1/8 hotdogs
self.assertTrue(out.getvalue().splitlines()[2].endswith("🌭🌭🌭⅛"))
# Assert that 0.1876 is shown as 1/4 hotdog
self.assertTrue(out.getvalue().splitlines()[3].endswith("¼"))
# Assert that 0.31251 is shown as 3/8 hotdog
self.assertTrue(out.getvalue().splitlines()[4].endswith("⅜"))
# Assert that 0.9374999999999999 is shown as 7/8 hotdog
self.assertTrue(out.getvalue().splitlines()[5].endswith("⅞"))
# Assert that 0.9375 is shown as 1 hotdog
self.assertTrue(out.getvalue().splitlines()[6].endswith("🌭"))
def test_ascii_outside_no_vals(self):
"""Test correct bar lengths when no values are shown (OUTSIDE = True)."""
kwbar = self.kwbar
kwbar.ascii()
kwbar.TRUNCATE = 10
kwbar.WIDTH = kwbar.TRUNCATE + 12 + 1
kwbar.SHOW_VALS = True
kwbar.BEFORE = False
out = io.StringIO()
with warnings.catch_warnings(record=True) as w, contextlib.redirect_stdout(out):
# Act
kwbar.kwbar(
dozen=12,
half=6,
pie=3.1,
two=2,
near_nil=0.1,
near_one=0.9375,
)
# Assert
# A warnings should be raised
self.assertEqual(len(w), 1, "warnings.warn was not called")
# Check that line 0 has 12 chars after the last space
self.assertEqual(len(out.getvalue().splitlines()[0].split()[-1]), 12)
def test_ascii_outside_values(self):
"""Test correct bar lengths when values are shown outside (before) bars."""
kwbar = self.kwbar
kwbar.ascii()
kwbar.WIDTH = 33
kwbar.TRUNCATE = 10
kwbar.SHOW_VALS = True
kwbar.BEFORE = True
out = io.StringIO()
with warnings.catch_warnings(record=True) as w, contextlib.redirect_stdout(out):
# Act
kwbar.kwbar(
dozen=12,
half=6,
pie=3.1,
two=2,
near_nil=0.1,
near_one=0.9375,
)
# Assert
# No warnings should be raised
self.assertEqual(len(w), 0, "warnings.warn was called")
# Check that there are a dozen Xs in line 0
self.assertEqual(out.getvalue().splitlines()[0].count("X"), 12)
# Verify that half a dozen is indeed 6 Xs
self.assertEqual(out.getvalue().splitlines()[1].count("X"), 6)
# Assert that a pie is about 3 XXXs
self.assertEqual(out.getvalue().splitlines()[2].count("X"), 3)
# Assert that two is shown as 2 Xs
self.assertEqual(out.getvalue().splitlines()[3].count("X"), 2)
# Assert that near zero showns no X
self.assertEqual(out.getvalue().splitlines()[4].count("X"), 0)
# Assert that near one shows 1 X
self.assertEqual(out.getvalue().splitlines()[5].count("X"), 1)
def test_fuzz_outside_has_no_effect_when_show_vals_false(self):
"""Test that OUTSIDE makes no difference when SHOW_VALS is False."""
kwbar = self.kwbar
kwbar.ascii()
kwbar.WIDTH = 25
kwbar.SHOW_VALS = False
# Generate random data
random.seed(0)
for _ in range(100):
data = {str(v): v for v in (random.randint(0, 10_000) for _ in range(5))}
outside_out = io.StringIO()
inside_out = io.StringIO()
# Set OUTSIDE
kwbar.BEFORE = True
with warnings.catch_warnings(
record=True
) as outside_w, contextlib.redirect_stdout(outside_out):
# Act
kwbar.kwbar(**data)
# Unset OUTSIDE
kwbar.BEFORE = False
with warnings.catch_warnings(
record=True
) as inside_w, contextlib.redirect_stdout(inside_out):
# Act
kwbar.kwbar(**data)
# Assert
# No warnings should be raised
self.assertEqual(len(outside_w), 0, "warnings.warn was called")
self.assertEqual(len(inside_w), 0, "warnings.warn was called")
# Assert that the output is the same
self.assertEqual(outside_out.getvalue(), inside_out.getvalue())
def test_warning_triggers_correctly(self):
"""Test that a fixed width output warns when expected."""
# Arrange
kwbar = self.kwbar
WIDTHS = (23, 24, 32, 40, 48, 64, 72, 80)
TRUNCATES = range(1, 11)
data = {
"one": 1,
"half": 0.5,
}
for width, truncate in itertools.product(WIDTHS, TRUNCATES):
kwbar.WIDTH = width
kwbar.TRUNCATE = truncate
# Sanity check
should_fit = (kwbar.WIDTH - kwbar.TRUNCATE - kwbar.SF - 17) >= 0
# Act
out = io.StringIO()
with warnings.catch_warnings(record=True) as w, contextlib.redirect_stdout(
out
):
kwbar.kwbar(**data)
# Assert
self.assertEqual(
len(w), not should_fit, "warning did not match expectation"
)
if should_fit:
max_line_len = max(len(line) for line in strip_ansi(out.getvalue()))
if max_line_len > kwbar.WIDTH:
print()
print(out.getvalue())
self.assertLessEqual(max_line_len, kwbar.WIDTH)
def test_fuzz_fix_width_outside(self):
"""Ensure that width is fixed when OUTSIDE = True and TRUNCATE > 1."""
# Arrange
kwbar = self.kwbar
kwbar.BEFORE = True
WIDTHS = (23, 24, 32, 40, 48, 64, 72, 80)
TRUNCATES = range(1, 11)
random.seed(0)
for _ in range(100):
for width, truncate in itertools.product(WIDTHS, TRUNCATES):
data = {
str(x): x for x in (random.randint(0, 10_000) for _ in range(5))
}
kwbar.WIDTH = width
kwbar.TRUNCATE = truncate
# Act
out = io.StringIO()
with warnings.catch_warnings(
record=True
) as w, contextlib.redirect_stdout(out):
kwbar.kwbar(**data)
# Assert
self.assertEqual(len(w), 0, "warnings.warn was called")
max_line_len = max(len(line) for line in strip_ansi(out.getvalue()))
self.assertLessEqual(max_line_len, width)
if __name__ == "__main__":
unittest.main()