This repository was archived by the owner on Feb 26, 2025. It is now read-only.
forked from manrajgrover/halo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_halo_notebook.py
435 lines (337 loc) · 13.9 KB
/
test_halo_notebook.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
# -*- coding: utf-8 -*-
"""This module tests HaloNotebook spinners.
"""
import os
import re
import sys
import time
import unittest
from spinners.spinners import Spinners
from halo import HaloNotebook
from halo._utils import get_terminal_columns, is_supported
from tests._utils import decode_utf_8_text, encode_utf_8_text, find_colors, strip_ansi
from termcolor import COLORS
if sys.version_info.major == 2:
get_coded_text = encode_utf_8_text
else:
get_coded_text = decode_utf_8_text
if is_supported():
frames = [get_coded_text(frame) for frame in Spinners['dots'].value['frames']]
default_spinner = Spinners['dots'].value
else:
frames = [get_coded_text(frame) for frame in Spinners['line'].value['frames']]
default_spinner = Spinners['line'].value
class TestHaloNotebook(unittest.TestCase):
"""Test HaloNotebook enum for attribute values.
"""
TEST_FOLDER = os.path.dirname(os.path.abspath(__file__))
def setUp(self):
"""Set up things before beginning of each test.
"""
pass
def _get_test_output(self, spinner, no_ansi=True):
"""Clean the output from Output widget and return it in list form.
Returns
-------
list
Clean output from Output widget
"""
output = {}
output_text = []
output_colors = []
for line in spinner.output.outputs:
if no_ansi:
clean_line = strip_ansi(line['text'].strip('\r'))
else:
clean_line = line['text'].strip('\r')
if clean_line != '':
output_text.append(get_coded_text(clean_line))
colors_found = find_colors(line['text'].strip('\r'))
if colors_found:
tmp = []
for color in colors_found:
tmp.append(re.sub(r'[^0-9]', '', color, flags=re.I))
output_colors.append(tmp)
output['text'] = output_text
output['colors'] = output_colors
return output
def test_basic_spinner(self):
"""Test the basic of basic spinners.
"""
spinner = HaloNotebook(text='foo', spinner='dots')
spinner.start()
time.sleep(1)
output = self._get_test_output(spinner)['text']
spinner.stop()
self.assertEqual(output[0], '{} foo'.format(frames[0]))
self.assertEqual(output[1], '{} foo'.format(frames[1]))
self.assertEqual(output[2], '{} foo'.format(frames[2]))
self.assertEqual(spinner.output.outputs, spinner._output(''))
def test_text_spinner_color(self):
"""Test basic spinner with available colors color (both spinner and text)
"""
for color, color_int in COLORS.items():
spinner = HaloNotebook(text='foo', text_color=color, color=color, spinner='dots')
spinner.start()
time.sleep(1)
output = self._get_test_output(spinner)['colors']
spinner.stop()
# check if spinner colors match
self.assertEqual(color_int, int(output[0][0]))
self.assertEqual(color_int, int(output[1][0]))
self.assertEqual(color_int, int(output[2][0]))
# check if text colors match
self.assertEqual(color_int, int(output[0][1]))
self.assertEqual(color_int, int(output[1][1]))
self.assertEqual(color_int, int(output[2][1]))
def test_text_stripping(self):
"""Test the text being stripped before output.
"""
spinner = HaloNotebook(text='foo\n', spinner='dots')
spinner.start()
time.sleep(1)
output = self._get_test_output(spinner)['text']
self.assertEqual(output[0], '{} foo'.format(frames[0]))
self.assertEqual(output[1], '{} foo'.format(frames[1]))
self.assertEqual(output[2], '{} foo'.format(frames[2]))
spinner.succeed('foo\n')
output = self._get_test_output(spinner)['text']
pattern = re.compile(r'(✔|v) foo', re.UNICODE)
self.assertRegexpMatches(output[-1], pattern)
def test_text_ellipsing(self):
"""Test the text gets ellipsed if it's too long
"""
text = 'This is a text that it is too long. In fact, it exceeds the eighty column standard ' \
'terminal width, which forces the text frame renderer to add an ellipse at the end of the ' \
'text. ' * 6
spinner = HaloNotebook(text=text, spinner='dots')
spinner.start()
time.sleep(1)
output = self._get_test_output(spinner)['text']
terminal_width = get_terminal_columns()
# -6 of the ' (...)' ellipsis, -2 of the spinner and space
self.assertEqual(output[0], '{} {} (...)'.format(frames[0], text[:terminal_width - 6 - 2]))
self.assertEqual(output[1], '{} {} (...)'.format(frames[1], text[:terminal_width - 6 - 2]))
self.assertEqual(output[2], '{} {} (...)'.format(frames[2], text[:terminal_width - 6 - 2]))
spinner.succeed('End!')
output = self._get_test_output(spinner)['text']
pattern = re.compile(r'(✔|v) End!', re.UNICODE)
self.assertRegexpMatches(output[-1], pattern)
def test_text_animation(self):
"""Test the text gets animated when it is too long
"""
text = 'This is a text that it is too long. In fact, it exceeds the eighty column standard ' \
'terminal width, which forces the text frame renderer to add an ellipse at the end of the ' \
'text. ' * 6
spinner = HaloNotebook(text=text, spinner='dots', animation='marquee')
spinner.start()
time.sleep(1)
output = self._get_test_output(spinner)['text']
terminal_width = get_terminal_columns()
self.assertEqual(output[0], '{} {}'.format(frames[0], text[:terminal_width - 2]))
self.assertEqual(output[1], '{} {}'.format(frames[1], text[1:terminal_width - 1]))
self.assertEqual(output[2], '{} {}'.format(frames[2], text[2:terminal_width]))
spinner.succeed('End!')
output = self._get_test_output(spinner)['text']
pattern = re.compile(r'(✔|v) End!', re.UNICODE)
self.assertRegexpMatches(output[-1], pattern)
def test_context_manager(self):
"""Test the basic of basic spinners used through the with statement.
"""
with HaloNotebook(text='foo', spinner='dots') as spinner:
time.sleep(1)
output = self._get_test_output(spinner)['text']
self.assertEqual(output[0], '{} foo'.format(frames[0]))
self.assertEqual(output[1], '{} foo'.format(frames[1]))
self.assertEqual(output[2], '{} foo'.format(frames[2]))
self.assertEqual(spinner.output.outputs, spinner._output(''))
def test_decorator_spinner(self):
"""Test basic usage of spinners with the decorator syntax."""
@HaloNotebook(text="foo", spinner="dots")
def decorated_function():
time.sleep(1)
spinner = decorated_function.__closure__[1].cell_contents
output = self._get_test_output(spinner)['text']
return output
output = decorated_function()
self.assertEqual(output[0], '{} foo'.format(frames[0]))
self.assertEqual(output[1], '{} foo'.format(frames[1]))
self.assertEqual(output[2], '{} foo'.format(frames[2]))
def test_initial_title_spinner(self):
"""Test Halo with initial title.
"""
spinner = HaloNotebook('bar')
spinner.start()
time.sleep(1)
output = self._get_test_output(spinner)['text']
spinner.stop()
self.assertEqual(output[0], '{} bar'.format(frames[0]))
self.assertEqual(output[1], '{} bar'.format(frames[1]))
self.assertEqual(output[2], '{} bar'.format(frames[2]))
self.assertEqual(spinner.output.outputs, spinner._output(''))
def test_id_not_created_before_start(self):
"""Test Spinner ID not created before start.
"""
spinner = HaloNotebook()
self.assertEqual(spinner.spinner_id, None)
def test_ignore_multiple_start_calls(self):
"""Test ignoring of multiple start calls.
"""
spinner = HaloNotebook()
spinner.start()
spinner_id = spinner.spinner_id
spinner.start()
self.assertEqual(spinner.spinner_id, spinner_id)
spinner.stop()
def test_chaining_start(self):
"""Test chaining start with constructor
"""
spinner = HaloNotebook().start()
spinner_id = spinner.spinner_id
self.assertIsNotNone(spinner_id)
spinner.stop()
def test_succeed(self):
"""Test succeed method
"""
spinner = HaloNotebook()
spinner.start('foo')
spinner.succeed('foo')
output = self._get_test_output(spinner)['text']
pattern = re.compile(r'(✔|v) foo', re.UNICODE)
self.assertRegexpMatches(output[-1], pattern)
spinner.stop()
def test_succeed_with_new_text(self):
"""Test succeed method with new text
"""
spinner = HaloNotebook()
spinner.start('foo')
spinner.succeed('bar')
output = self._get_test_output(spinner)['text']
pattern = re.compile(r'(✔|v) bar', re.UNICODE)
self.assertRegexpMatches(output[-1], pattern)
spinner.stop()
def test_info(self):
"""Test info method
"""
spinner = HaloNotebook()
spinner.start('foo')
spinner.info()
output = self._get_test_output(spinner)['text']
pattern = re.compile(r'(ℹ|¡) foo', re.UNICODE)
self.assertRegexpMatches(output[-1], pattern)
spinner.stop()
def test_fail(self):
"""Test fail method
"""
spinner = HaloNotebook()
spinner.start('foo')
spinner.fail()
output = self._get_test_output(spinner)['text']
pattern = re.compile(r'(✖|×) foo', re.UNICODE)
self.assertRegexpMatches(output[-1], pattern)
spinner.stop()
def test_warning(self):
"""Test warn method
"""
spinner = HaloNotebook()
spinner.start('foo')
spinner.warn('Warning!')
output = self._get_test_output(spinner)['text']
pattern = re.compile(r'(⚠|!!) Warning!', re.UNICODE)
self.assertRegexpMatches(output[-1], pattern)
spinner.stop()
def test_spinner_getters_setters(self):
"""Test spinner getters and setters.
"""
spinner = HaloNotebook()
self.assertEqual(spinner.text, '')
self.assertEqual(spinner.color, 'cyan')
self.assertIsNone(spinner.spinner_id)
spinner.spinner = 'dots12'
spinner.text = 'bar'
spinner.color = 'red'
self.assertEqual(spinner.text, 'bar')
self.assertEqual(spinner.color, 'red')
if is_supported():
self.assertEqual(spinner.spinner, Spinners['dots12'].value)
else:
self.assertEqual(spinner.spinner, default_spinner)
spinner.spinner = 'dots11'
if is_supported():
self.assertEqual(spinner.spinner, Spinners['dots11'].value)
else:
self.assertEqual(spinner.spinner, default_spinner)
spinner.spinner = 'foo_bar'
self.assertEqual(spinner.spinner, default_spinner)
# Color is None
spinner.color = None
spinner.start()
spinner.stop()
self.assertIsNone(spinner.color)
def test_unavailable_spinner_defaults(self):
"""Test unavailable spinner defaults.
"""
spinner = HaloNotebook('dot')
self.assertEqual(spinner.text, 'dot')
self.assertEqual(spinner.spinner, default_spinner)
def test_if_enabled(self):
"""Test if spinner is enabled
"""
spinner = HaloNotebook(text="foo", enabled=False)
spinner.start()
time.sleep(1)
output = self._get_test_output(spinner)['text']
spinner.clear()
spinner.stop()
self.assertEqual(len(output), 0)
self.assertEqual(output, [])
def test_invalid_placement(self):
"""Test invalid placement of spinner.
"""
with self.assertRaises(ValueError):
HaloNotebook(placement='')
HaloNotebook(placement='foo')
HaloNotebook(placement=None)
spinner = HaloNotebook(placement='left')
with self.assertRaises(ValueError):
spinner.placement = ''
spinner.placement = 'foo'
spinner.placement = None
def test_default_placement(self):
"""Test default placement of spinner.
"""
spinner = HaloNotebook()
self.assertEqual(spinner.placement, 'left')
def test_right_placement(self):
"""Test right placement of spinner.
"""
spinner = HaloNotebook(text="foo", placement="right")
spinner.start()
time.sleep(1)
output = self._get_test_output(spinner)['text']
(text, _) = output[-1].split(" ")
self.assertEqual(text, "foo")
spinner.succeed()
output = self._get_test_output(spinner)['text']
(text, symbol) = output[-1].split(" ")
pattern = re.compile(r"(✔|v)", re.UNICODE)
self.assertEqual(text, "foo")
self.assertRegexpMatches(symbol, pattern)
spinner.stop()
def test_spinner_color(self):
"""Test ANSI escape characters are present
"""
for color, color_int in COLORS.items():
spinner = HaloNotebook(color=color)
spinner.start()
output = self._get_test_output(spinner, no_ansi=False)
spinner.stop()
output_merged = [arr for c in output['colors'] for arr in c]
self.assertEquals(str(color_int) in output_merged, True)
def tearDown(self):
"""Clean up things after every test.
"""
pass
if __name__ == '__main__':
SUITE = unittest.TestLoader().loadTestsFromTestCase(TestHaloNotebook)
unittest.TextTestRunner(verbosity=2).run(SUITE)