-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_delta.py
606 lines (520 loc) · 18.8 KB
/
test_delta.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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
try:
import unittest2 as unittest
except ImportError:
import unittest
import delta
import re
import time
import os
import threading
import signal
import sys
try:
from io import StringIO
except ImportError:
from StringIO import StringIO
class StringChunkTestCase(unittest.TestCase):
def test_plain(self):
c = delta.StringChunk(u'example')
p = c.plain()
self.assertEqual(p.static_str, u'example')
def test_whitespace(self):
c = delta.StringChunk(u'ex\nample')
p = c.whitespace()
self.assertEqual(p.static_str, u' \n ')
def test_format(self):
c = delta.StringChunk(u'example')
values = list(u'abcd')
self.assertEqual(c.format(values), u'example')
self.assertEqual(values, list(u'abcd'))
def test_as_regex(self):
s = u'ex|amp[l]e'
c = delta.StringChunk(s)
rx = c.as_regex()
self.assertEqual(rx, u'ex\\|amp\\[l\\]e')
self.assertTrue(re.match(rx, s))
class NumberChunkTestCase(unittest.TestCase):
def test_detect_int(self):
c = delta.NumberChunk.detect(u' ', u'999', False)
self.assertEqual(c.format_str(), u' {0:+4}')
def test_detect_float(self):
c = delta.NumberChunk.detect(u' ', u'999.99', False)
self.assertEqual(c.format_str(), u' {0:+7.2f}')
def test_detect_no_space(self):
c = delta.NumberChunk.detect(u'', u'999', False)
self.assertEqual(c.format_str(), u'{0:<+3}')
def test_detect_no_space_first(self):
c = delta.NumberChunk.detect(u'', u'999', True)
self.assertEqual(c.format_str(), u'{0:+3}')
def test_detect_leading_zeros_int(self):
c = delta.NumberChunk.detect(u'', u'003', False)
self.assertEqual(c.format_str(), u'{0:+03}')
def test_detect_leading_zeros_float(self):
c = delta.NumberChunk.detect(u'', u'003.99', False)
self.assertEqual(c.format_str(), u'{0:+06.2f}')
def test_detect_zero(self):
c = delta.NumberChunk.detect(u'', u'0', False)
self.assertEqual(c.format_str(), u'{0:<+1}')
def test_detect_flex_int(self):
c = delta.NumberChunk.detect(u' ', u'5', False, flex=True)
self.assertEqual(c.format_str(), u' {0:+2}')
def test_detect_no_flex_int(self):
c = delta.NumberChunk.detect(u' ', u'5', False, flex=False)
self.assertEqual(c.format_str(), u' {0:+1}')
def test_detect_flex_float(self):
c = delta.NumberChunk.detect(u' ', u'5.99', False, flex=True)
self.assertEqual(c.format_str(), u' {0:+5.2f}')
def test_detect_no_flex_float(self):
c = delta.NumberChunk.detect(u' ', u'5.99', False, flex=False)
self.assertEqual(c.format_str(), u' {0:+4.2f}')
def test_plain(self):
c = delta.NumberChunk.detect(u' ', u'999', False)
self.assertEqual(c.plain().format_str(), u' {0:4}')
def test_colorize_positive(self):
s = delta.NumberChunk.colorize(1, u'foo')
self.assertEqual(s, u'\x1b[32mfoo\x1b[0m')
def test_colorize_negative(self):
s = delta.NumberChunk.colorize(-1, u'foo')
self.assertEqual(s, u'\x1b[31mfoo\x1b[0m')
def test_colorize_zero(self):
s = delta.NumberChunk.colorize(0, u'foo')
self.assertEqual(s, u'foo')
def test_format_color(self):
c = delta.NumberChunk.detect(u' ', u'999', False)
values = [1, 2, 3, 4]
self.assertEqual(c.format(values, True), u'\x1b[32m +1\x1b[0m')
self.assertEqual(values, [2, 3, 4])
def test_whitespace(self):
c = delta.NumberChunk.detect(u' ', u'999', False)
self.assertEqual(c.whitespace().format([0], False), u' +0')
def test_as_regex(self):
c = delta.NumberChunk.detect(u' ', u'999', False)
rx = re.compile(c.as_regex())
self.assertTrue(rx.match(' 999'))
class FormatTestCase(unittest.TestCase):
def test_format(self):
f = delta.Format([
delta.StringChunk(u'hello'),
delta.NumberChunk.detect(u' ', u'999', False),
])
values = [1, 2, 3, 4]
self.assertEqual(f.format(values), u'hello\x1b[32m +1\x1b[0m')
self.assertEqual(values, [1, 2, 3, 4])
def test_format_no_colors(self):
f = delta.Format([
delta.StringChunk(u'hello'),
delta.NumberChunk.detect(u' ', u'999', False),
])
values = [1, 2, 3, 4]
self.assertEqual(f.format(values, False), u'hello +1')
self.assertEqual(values, [1, 2, 3, 4])
def test_format_no_colors(self):
f = delta.Format([
delta.StringChunk(u'hello'),
delta.NumberChunk.detect(u' ', u'999', False),
])
values = [1, 2, 3, 4]
self.assertEqual(f.plain().format(values), u'hello 1')
self.assertEqual(values, [1, 2, 3, 4])
def test_format_whitespace(self):
f = delta.Format([
delta.StringChunk(u'hello'),
delta.NumberChunk.detect(u' ', u'999', False),
])
values = [1, 2, 3, 4]
self.assertEqual(f.whitespace().format(values), u' \x1b[32m +1\x1b[0m')
self.assertEqual(values, [1, 2, 3, 4])
class ParserTestCase(unittest.TestCase):
def test_num_int(self):
n = delta.Parser.num('999')
self.assertEqual(n, 999)
self.assertIsInstance(n, int)
def test_num_float(self):
n = delta.Parser.num('999.00')
self.assertEqual(n, 999)
self.assertIsInstance(n, float)
def test_grouper(self):
elts = range(1, 11)
chunks = delta.Parser.grouper(elts, 3)
chunks = list(chunks)
self.assertListEqual(chunks, [
(1, 2, 3),
(4, 5, 6),
(7, 8, 9),
(10, None, None)])
def test_parse_simple(self):
parser = delta.Parser(use_colors=False)
line = u'908638.24 1797254.54'
fmt, deltas, values = parser.parse(line)
self.assertEqual(fmt.format(values), line)
self.assertIsNone(deltas)
self.assertListEqual(values, [908638.24, 1797254.54])
def test_parse_flex(self):
parser = delta.Parser(use_colors=False)
line = u'0.05 0.08 0.06 1/175 19537'
fmt, deltas, values = parser.parse(line)
self.assertEqual(fmt.format(values), u' 0.05 0.08 0.06 1/175 19537')
def test_process_simple(self):
parser = delta.Parser(use_colors=False)
line = u'908638.24 1797254.54'
fmt, deltas, values = parser.process(line)
self.assertEqual(fmt.format(values), line)
self.assertIsNone(deltas)
self.assertListEqual(values, [908638.24, 1797254.54])
def test_process_relative(self):
parser = delta.Parser(use_colors=False)
parser.process(u'1000')
_, deltas, values = parser.process(u'1001')
self.assertEqual(values, [1001])
self.assertEqual(deltas, [1])
_, deltas, values = parser.process(u'1002')
self.assertEqual(values, [1002])
self.assertEqual(deltas, [1])
def test_process_absolute(self):
parser = delta.Parser(absolute=True, use_colors=False)
parser.process(u'1000')
_, deltas, values = parser.process(u'1001')
self.assertEqual(values, [1001])
self.assertEqual(deltas, [1])
_, deltas, values = parser.process(u'1002')
self.assertEqual(values, [1002])
self.assertEqual(deltas, [2])
class TestPrinter(delta.Printer):
@classmethod
def now(self):
return u'NOW'
class PrinterTestCase(unittest.TestCase):
def test_printer_simple(self):
sio = StringIO()
printer = TestPrinter(sio, timestamps=False, separators=False, orig=False, skip_zeros=False)
f = delta.Format([
delta.StringChunk(u'hello'),
delta.NumberChunk.detect(u' ', u'999', False),
delta.StringChunk(u'\n'),
], colors=False)
printer.output(f.plain(), None, [999])
printer.output(f, [1], [1000])
printer.output(f, [0], [1000])
printer.output(f, [-2], [998])
self.assertEqual(sio.getvalue(), u'''hello 999
hello +1
hello +0
hello -2
''')
def test_printer_timestamps(self):
sio = StringIO()
printer = TestPrinter(sio, timestamps=True, separators=False, orig=False, skip_zeros=False)
f = delta.Format([
delta.StringChunk(u'hello'),
delta.NumberChunk.detect(u' ', u'999', False),
delta.StringChunk(u'\n'),
], colors=False)
printer.output(f.plain(), None, [999])
printer.output(f, [1], [1000])
printer.output(f, [1], [1001])
printer.output(f, [-2], [999])
self.assertEqual(sio.getvalue(), u'''NOW: hello 999
NOW: hello +1
NOW: hello +1
NOW: hello -2
''')
def test_printer_timestamps_separators(self):
sio = StringIO()
printer = TestPrinter(sio, timestamps=True, separators=True, orig=False, skip_zeros=False)
f = delta.Format([
delta.StringChunk(u'hello'),
delta.NumberChunk.detect(u' ', u'999', False),
delta.StringChunk(u'\n'),
], colors=False)
printer.output(f.plain(), None, [999])
printer.output(f.plain(), None, [999])
printer.separator()
printer.output(f, [1], [1000])
printer.output(f, [1], [1000])
printer.separator()
printer.output(f, [1], [1001])
printer.output(f, [1], [1001])
printer.separator()
printer.output(f, [-2], [999])
printer.output(f, [-2], [999])
self.assertEqual(sio.getvalue(), u'''NOW: hello 999
NOW: hello 999
NOW
NOW: hello +1
NOW: hello +1
NOW
NOW: hello +1
NOW: hello +1
NOW
NOW: hello -2
NOW: hello -2
''')
def test_printer_orig(self):
sio = StringIO()
printer = TestPrinter(sio, timestamps=False, separators=False, orig=True, skip_zeros=False)
f = delta.Format([
delta.StringChunk(u'hello'),
delta.NumberChunk.detect(u' ', u'999', False),
delta.StringChunk(u'\n'),
], colors=False)
printer.output(f.plain(), None, [999])
printer.output(f, [1], [1000])
printer.output(f, [1], [1001])
printer.output(f, [-2], [999])
self.assertEqual(sio.getvalue(), u'''hello 999
hello 1000
+1
hello 1001
+1
hello 999
-2
''')
def test_printer_orig_no_values(self):
sio = StringIO()
printer = TestPrinter(sio, timestamps=False, separators=False, orig=True, skip_zeros=False)
f = delta.Format([
delta.StringChunk(u'hello\n'),
], colors=False)
printer.output(f.plain(), None, [])
printer.output(f, [], [])
self.assertEqual(sio.getvalue(), u'''hello
hello
''')
def test_printer_skipzeros(self):
sio = StringIO()
printer = TestPrinter(sio, timestamps=False, separators=False, orig=False, skip_zeros=True)
f = delta.Format([
delta.StringChunk(u'hello'),
delta.NumberChunk.detect(u' ', u'999', False),
delta.StringChunk(u'\n'),
], colors=False)
printer.output(f.plain(), None, [999])
printer.output(f, [1], [1000])
printer.output(f, [0], [1000])
printer.output(f, [-2], [998])
self.assertEqual(sio.getvalue(), u'''hello 999
hello +1
hello -2
''')
def test_printer_separators_single_line(self):
sio = StringIO()
printer = TestPrinter(sio, timestamps=False, separators=True, orig=False, skip_zeros=False)
f = delta.Format([
delta.StringChunk(u'hello'),
delta.NumberChunk.detect(u' ', u'999', False),
delta.StringChunk(u'\n'),
], colors=False)
printer.separator()
printer.output(f.plain(), None, [999])
printer.separator()
printer.output(f, [1], [1000])
printer.separator()
printer.output(f, [0], [1000])
printer.separator()
printer.output(f, [-2], [998])
self.assertEqual(sio.getvalue(), u'''hello 999
hello +1
hello +0
hello -2
''')
def test_printer_separators_single_line_skip_zeros(self):
sio = StringIO()
printer = TestPrinter(sio, timestamps=False, separators=True, orig=False, skip_zeros=True)
f = delta.Format([
delta.StringChunk(u'hello'),
delta.NumberChunk.detect(u' ', u'999', False),
delta.StringChunk(u'\n'),
], colors=False)
printer.separator()
printer.output(f.plain(), None, [999])
printer.separator()
printer.output(f, [1], [1000])
printer.separator()
printer.output(f, [0], [1000])
printer.separator()
printer.output(f, [-2], [998])
self.assertEqual(sio.getvalue(), u'''hello 999
hello +1
--- NOW
hello -2
''')
def test_printer_separators_multiline(self):
sio = StringIO()
printer = TestPrinter(sio, timestamps=False, separators=True, orig=False, skip_zeros=False)
f = delta.Format([
delta.StringChunk(u'hello'),
delta.NumberChunk.detect(u' ', u'999', False),
delta.StringChunk(u'\n'),
], colors=False)
printer.separator()
printer.output(f.plain(), None, [999])
printer.output(f.plain(), None, [99])
printer.separator()
printer.output(f, [1], [1000])
printer.output(f, [1], [100])
printer.separator()
printer.output(f, [0], [1000])
printer.output(f, [0], [100])
printer.separator()
printer.output(f, [-2], [998])
printer.output(f, [-2], [98])
self.assertEqual(sio.getvalue(), u'''hello 999
hello 99
--- NOW
hello +1
hello +1
--- NOW
hello +0
hello +0
--- NOW
hello -2
hello -2
''')
class FeedTestCase(unittest.TestCase):
def test_fd_feed(self):
def threadfunc(wfd):
wfd.write(u'hello\n')
wfd.flush()
wfd.write(u'hello\n')
wfd.flush()
time.sleep(0.11)
wfd.write(u'hello\n')
wfd.flush()
wfd.close()
r, w = os.pipe()
rfd = os.fdopen(r, u'r')
wfd = os.fdopen(w, u'w')
thd = threading.Thread(target=threadfunc, args=(wfd,))
thd.start()
feed = delta.fd_feed(rfd, 0.1)
self.assertEqual(next(feed), u'hello\n')
self.assertEqual(next(feed), u'hello\n')
self.assertIs(next(feed), delta.separator)
self.assertEqual(next(feed), u'hello\n')
self.assertRaises(StopIteration, next, feed)
thd.join()
rfd.close()
def test_command_feed(self):
feed = delta.command_feed([u'/bin/echo', u'hello'], 0.1)
self.assertIs(next(feed), delta.separator)
self.assertEqual(next(feed), u'hello\n')
self.assertIs(next(feed), delta.separator)
self.assertEqual(next(feed), u'hello\n')
self.assertIs(next(feed), delta.separator)
self.assertEqual(next(feed), u'hello\n')
def test_command_feed_count(self):
feed = delta.command_feed([u'/bin/echo', u'hello'], 0.1, 2)
self.assertIs(next(feed), delta.separator)
self.assertEqual(next(feed), u'hello\n')
self.assertIs(next(feed), delta.separator)
self.assertEqual(next(feed), u'hello\n')
self.assertRaises(StopIteration, next, feed)
def test_command_feed_shell(self):
feed = delta.command_feed((u'echo hello',), 0.1)
self.assertIs(next(feed), delta.separator)
self.assertEqual(next(feed), u'hello\n')
self.assertIs(next(feed), delta.separator)
self.assertEqual(next(feed), u'hello\n')
self.assertIs(next(feed), delta.separator)
self.assertEqual(next(feed), u'hello\n')
class UtilsTestCase(unittest.TestCase):
def test_run(self):
def threadfunc(wfd):
wfd.write(u'hello 100\n')
wfd.flush()
wfd.write(u'hello 102\n')
wfd.flush()
time.sleep(0.11)
wfd.write(u'hello 103\n')
wfd.flush()
wfd.close()
r, w = os.pipe()
rfd = os.fdopen(r, u'r')
wfd = os.fdopen(w, u'w')
thd = threading.Thread(target=threadfunc, args=(wfd,))
thd.start()
sio = StringIO()
feed = delta.fd_feed(rfd, 0.1)
parser = delta.Parser(flex=True, absolute=False, use_colors=False)
printer = delta.Printer(sio, timestamps=False, separators=False, orig=False, skip_zeros=False)
delta.run(feed, parser, printer)
self.assertEqual(sio.getvalue(), u'''hello 100
hello +2
hello +1
''')
thd.join()
rfd.close()
def test_use_separators(self):
cases = {
(u'true', u'always', False, False): True,
(u'true', u'never', False, False): False,
(u'true', u'auto', False, False): True,
(None, u'always', False, False): True,
(None, u'never', False, False): False,
(None, u'auto', False, False): False,
(None, u'auto', False, True): False,
(None, u'auto', True, False): True,
(None, u'auto', True, True): False,
}
for (cmd, separators, skip_zeros, timestamps), output in cases.items():
self.assertEqual(delta.use_separators(cmd, separators, skip_zeros, timestamps), output)
def test_use_colors(self):
r, w = os.pipe()
rfd = os.fdopen(r, u'r')
wfd = os.fdopen(w, u'w')
rfd.close()
self.assertTrue(delta.use_colors(u'always', wfd))
self.assertFalse(delta.use_colors(u'never', wfd))
self.assertFalse(delta.use_colors(u'auto', wfd))
wfd.close()
try:
with open(u'/dev/tty') as fp:
self.assertTrue(delta.use_colors(u'always', fp))
self.assertFalse(delta.use_colors(u'never', fp))
self.assertTrue(delta.use_colors(u'auto', fp))
except IOError:
pass
class CliTestCase(unittest.TestCase):
def test_cli_stdin(self):
stdin = StringIO(u'hello 1\nhello 2\nhello 3\n')
stdout = StringIO()
delta.real_cli(
stdin=stdin,
stdout=stdout,
cmd=None,
timestamps=False,
interval=5,
flex=True,
separators=False,
color=False,
orig=False,
skip_zeros=False,
absolute=False,
count=None)
self.assertEqual(stdout.getvalue(), u'''hello 1
hello +1
hello +1
''')
def test_cli_cmd(self):
stdout = StringIO()
delta.real_cli(
stdin=StringIO(),
stdout=stdout,
cmd=('echo "hello 1"',),
timestamps=False,
interval=0.1,
flex=True,
separators=False,
color=False,
orig=False,
skip_zeros=False,
absolute=False,
count=5)
self.assertEqual(stdout.getvalue(), u'''hello 1
hello +0
hello +0
hello +0
hello +0
''')
if __name__ == '__main__':
unittest.main()