collimator 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,695 @@
1
+ require 'test/unit'
2
+ require 'collimator'
3
+ require 'date'
4
+ require 'stringio'
5
+ require 'test_helper'
6
+
7
+ class TestTable < Test::Unit::TestCase
8
+ include Collimator
9
+
10
+ def setup
11
+ Table.clear_all
12
+ end
13
+
14
+ def teardown
15
+ $stdout = STDOUT
16
+ end
17
+
18
+ def test_just_table
19
+
20
+ header_text = ['head A', 'head B', 'head C']
21
+ header_width = [12, 10, 14]
22
+ col_just = [:left, :right, :left]
23
+ table_header = 'a table of stuff'
24
+
25
+ Table.header(table_header, :padding => 4, :justification => :left)
26
+ Table.header('second', :padding => 1, :justification => :right)
27
+
28
+ (0..header_text.length - 1).each do |i|
29
+ Table.column(header_text[i], :width => header_width[i], :padding => 2, :justification => col_just[i])
30
+ end
31
+
32
+ (1..5).each do |i|
33
+ data = [i, (i+32).chr.to_s, i*i]
34
+ Table.row(data)
35
+ end
36
+
37
+ Table.footer("this data is for practice", :padding => 1, :justification => :center)
38
+
39
+ out = capture_output do
40
+ Table.tabulate
41
+ end
42
+
43
+ table_text = out.string
44
+ table_lines = table_text.split("\n")
45
+ assert_equal 14, table_lines.length
46
+
47
+ assert_equal '+--------------------------------------------+', table_lines[0]
48
+ assert_equal '| a table of stuff |', table_lines[1]
49
+ assert_equal '| second |', table_lines[2]
50
+ assert_equal '+--------------------------------------------+', table_lines[3]
51
+ assert_equal '| head A | head B | head C |', table_lines[4]
52
+ assert_equal '|--------------+------------+----------------|', table_lines[5]
53
+ assert_equal '| 1 | ! | 1 |', table_lines[6]
54
+ assert_equal '| 2 | " | 4 |', table_lines[7]
55
+ assert_equal '| 3 | # | 9 |', table_lines[8]
56
+ assert_equal '| 4 | $ | 16 |', table_lines[9]
57
+ assert_equal '| 5 | % | 25 |', table_lines[10]
58
+ assert_equal '+--------------------------------------------+', table_lines[11]
59
+ assert_equal '| this data is for practice |', table_lines[12]
60
+ assert_equal '+--------------------------------------------+', table_lines[13]
61
+
62
+ end
63
+
64
+ def test_just_table__empty_data_locations
65
+ header_text = ['head A', 'head B', 'head C']
66
+ header_width = [12, 10, 14]
67
+ col_just = [:left, :right, :left]
68
+ table_header = 'a table of stuff'
69
+
70
+ Table.header(table_header, :padding => 4, :justification => :left)
71
+ Table.header('second', :padding => 1, :justification => :right)
72
+
73
+ (0..header_text.length - 1).each do |i|
74
+ Table.column(header_text[i], :width => header_width[i], :padding => 2, :justification => col_just[i])
75
+ end
76
+
77
+ (1..5).each do |i|
78
+ data = [i, (i+32).chr.to_s, i*i]
79
+
80
+ if i == 1
81
+ data = [i]
82
+ end
83
+ if i == 3
84
+ data = [i, (i+32).chr.to_s]
85
+ end
86
+ if i == 4
87
+ data = []
88
+ end
89
+ Table.row(data)
90
+ end
91
+
92
+ Table.footer("this data is for practice", :padding => 1, :justification => :center)
93
+
94
+ out = capture_output do
95
+ Table.tabulate
96
+ end
97
+
98
+ table_text = out.string
99
+ table_lines = table_text.split("\n")
100
+
101
+ assert_equal 14, table_lines.length
102
+
103
+ assert_equal '+--------------------------------------------+', table_lines[0]
104
+ assert_equal '| a table of stuff |', table_lines[1]
105
+ assert_equal '| second |', table_lines[2]
106
+ assert_equal '+--------------------------------------------+', table_lines[3]
107
+ assert_equal '| head A | head B | head C |', table_lines[4]
108
+ assert_equal '|--------------+------------+----------------|', table_lines[5]
109
+ assert_equal '| 1 | | |', table_lines[6]
110
+ assert_equal '| 2 | " | 4 |', table_lines[7]
111
+ assert_equal '| 3 | # | |', table_lines[8]
112
+ assert_equal '| | | |', table_lines[9]
113
+ assert_equal '| 5 | % | 25 |', table_lines[10]
114
+ assert_equal '+--------------------------------------------+', table_lines[11]
115
+ assert_equal '| this data is for practice |', table_lines[12]
116
+ assert_equal '+--------------------------------------------+', table_lines[13]
117
+
118
+ end
119
+
120
+ def test_just_table__with_separator
121
+ header_text = ['head A', 'head B', 'head C']
122
+ header_width = [12, 10, 14]
123
+ col_just = [:left, :right, :left]
124
+ table_header = 'a table of stuff'
125
+
126
+ Table.header(table_header, :padding => 4, :justification => :left)
127
+ Table.header('second', :padding => 1, :justification => :right)
128
+
129
+ (0..header_text.length - 1).each do |i|
130
+ Table.column(header_text[i], :width => header_width[i], :padding => 2, :justification => col_just[i])
131
+ end
132
+
133
+ (1..5).each do |i|
134
+ data = [i, (i+32).chr.to_s, i*i]
135
+ if i == 3
136
+ Table.separator
137
+ end
138
+ Table.row(data)
139
+ end
140
+
141
+ Table.footer("this data is for practice", :padding => 1, :justification => :center)
142
+
143
+ out = capture_output do
144
+ Table.tabulate
145
+ end
146
+
147
+ table_text = out.string
148
+ table_lines = table_text.split("\n")
149
+
150
+ assert_equal 15, table_lines.length
151
+
152
+ assert_equal '+--------------------------------------------+', table_lines[0]
153
+ assert_equal '| a table of stuff |', table_lines[1]
154
+ assert_equal '| second |', table_lines[2]
155
+ assert_equal '+--------------------------------------------+', table_lines[3]
156
+ assert_equal '| head A | head B | head C |', table_lines[4]
157
+ assert_equal '|--------------+------------+----------------|', table_lines[5]
158
+ assert_equal '| 1 | ! | 1 |', table_lines[6]
159
+ assert_equal '| 2 | " | 4 |', table_lines[7]
160
+ assert_equal '|--------------+------------+----------------|', table_lines[8]
161
+ assert_equal '| 3 | # | 9 |', table_lines[9]
162
+ assert_equal '| 4 | $ | 16 |', table_lines[10]
163
+ assert_equal '| 5 | % | 25 |', table_lines[11]
164
+ assert_equal '+--------------------------------------------+', table_lines[12]
165
+ assert_equal '| this data is for practice |', table_lines[13]
166
+ assert_equal '+--------------------------------------------+', table_lines[14]
167
+
168
+ end
169
+
170
+ def test_change_border_horizontal_and_corner_characters
171
+
172
+ header_text = ['head A', 'head B', 'head C']
173
+ header_width = [12, 10, 14]
174
+ col_just = [:left, :right, :left]
175
+ table_header = 'a table of stuff'
176
+
177
+ Table.set_border '.'
178
+ Table.set_horizontal '='
179
+ Table.set_corner '0'
180
+
181
+ Table.header(table_header, :padding => 4, :justification => :left)
182
+ Table.header('second', :padding => 1, :justification => :right)
183
+
184
+ (0..header_text.length - 1).each do |i|
185
+ Table.column(header_text[i], :width => header_width[i], :padding => 2, :justification => col_just[i])
186
+ end
187
+
188
+ (1..5).each do |i|
189
+ data = [i, (i+32).chr.to_s, i*i]
190
+ Table.row(data)
191
+ end
192
+
193
+ Table.footer("this data is for practice", :padding => 1, :justification => :center)
194
+
195
+ out = capture_output do
196
+ Table.tabulate
197
+ end
198
+
199
+ table_text = out.string
200
+ table_lines = table_text.split("\n")
201
+ assert_equal 14, table_lines.length
202
+
203
+ assert_equal '0============================================0', table_lines[0]
204
+ assert_equal '. a table of stuff .', table_lines[1]
205
+ assert_equal '. second .', table_lines[2]
206
+ assert_equal '0============================================0', table_lines[3]
207
+ assert_equal '. head A . head B . head C .', table_lines[4]
208
+ assert_equal '.==============0============0================.', table_lines[5]
209
+ assert_equal '. 1 . ! . 1 .', table_lines[6]
210
+ assert_equal '. 2 . " . 4 .', table_lines[7]
211
+ assert_equal '. 3 . # . 9 .', table_lines[8]
212
+ assert_equal '. 4 . $ . 16 .', table_lines[9]
213
+ assert_equal '. 5 . % . 25 .', table_lines[10]
214
+ assert_equal '0============================================0', table_lines[11]
215
+ assert_equal '. this data is for practice .', table_lines[12]
216
+ assert_equal '0============================================0', table_lines[13]
217
+
218
+ end
219
+
220
+ def test_center_table
221
+
222
+ header_text = ['head A', 'head B', 'head C']
223
+ header_width = [12, 10, 14]
224
+ col_just = [:center, :center, :center]
225
+ table_header = 'a table of stuff'
226
+
227
+ Table.header(table_header, :padding => 4, :justification => :left)
228
+ Table.header('second', :padding => 1, :justification => :right)
229
+
230
+ (0..header_text.length - 1).each do |i|
231
+ Table.column(header_text[i], :width => header_width[i], :padding => 0, :justification => col_just[i])
232
+ end
233
+
234
+ (1..5).each do |i|
235
+ data = [i, (i+32).chr.to_s, i*i]
236
+ Table.row(data)
237
+ end
238
+
239
+ Table.footer("this data is for practice", :padding => 1, :justification => :center)
240
+
241
+ out = capture_output do
242
+ Table.tabulate
243
+ end
244
+
245
+ table_text = out.string
246
+ table_lines = table_text.split("\n")
247
+ assert_equal 14, table_lines.length
248
+
249
+ assert_equal '+--------------------------------------+', table_lines[0]
250
+ assert_equal '| a table of stuff |', table_lines[1]
251
+ assert_equal '| second |', table_lines[2]
252
+ assert_equal '+--------------------------------------+', table_lines[3]
253
+ assert_equal '| head A | head B | head C |', table_lines[4]
254
+ assert_equal '|------------+----------+--------------|', table_lines[5]
255
+ assert_equal '| 1 | ! | 1 |', table_lines[6]
256
+ assert_equal '| 2 | " | 4 |', table_lines[7]
257
+ assert_equal '| 3 | # | 9 |', table_lines[8]
258
+ assert_equal '| 4 | $ | 16 |', table_lines[9]
259
+ assert_equal '| 5 | % | 25 |', table_lines[10]
260
+ assert_equal '+--------------------------------------+', table_lines[11]
261
+ assert_equal '| this data is for practice |', table_lines[12]
262
+ assert_equal '+--------------------------------------+', table_lines[13]
263
+
264
+ end
265
+
266
+ def test_decimal_table
267
+
268
+ header_text = ['name', 'decimal 1', 'decimal two']
269
+ header_width = [12, 14, 20]
270
+ col_just = [:right, :decimal, :decimal]
271
+ table_header = 'a table of decimal stuff'
272
+
273
+ Table.header(table_header, :padding => 4, :justification => :left)
274
+
275
+ (0..header_text.length - 1).each do |i|
276
+ Table.column(header_text[i], :width => header_width[i], :padding => (col_just == :decimal ? 0 : 2), :justification => col_just[i])
277
+ end
278
+
279
+ Table.row(['jim', 12.12, 0.004])
280
+ Table.row(['george', 1.212, 0.04])
281
+ Table.row(['bob', 121.2, 0.0004])
282
+ Table.row(['joe', 121.2, 0])
283
+ Table.row(['bill', 21.20, 10.1])
284
+
285
+ out = capture_output do
286
+ Table.tabulate
287
+ end
288
+
289
+ table_text = out.string
290
+ table_lines = table_text.split("\n")
291
+
292
+ assert_equal '+--------------------------------------------------+', table_lines[0]
293
+ assert_equal '| a table of decimal stuff |', table_lines[1]
294
+ assert_equal '+--------------------------------------------------+', table_lines[2]
295
+ assert_equal '| name | decimal 1 | decimal two |', table_lines[3]
296
+ assert_equal '|--------------+--------------+--------------------|', table_lines[4]
297
+ assert_equal '| jim | 12.12 | 0.004 |', table_lines[5]
298
+ assert_equal '| george | 1.212 | 0.04 |', table_lines[6]
299
+ assert_equal '| bob | 121.2 | 0.0004 |', table_lines[7]
300
+ assert_equal '| joe | 121.2 | 0 |', table_lines[8]
301
+ assert_equal '| bill | 21.2 | 10.1 |', table_lines[9]
302
+ assert_equal '+--------------------------------------------------+', table_lines[10]
303
+ end
304
+
305
+ def test_table_just_data
306
+
307
+ header_width = [12, 10, 14]
308
+ col_just = [:right, :decimal, :decimal]
309
+
310
+ (0..col_just.length - 1).each do |i|
311
+ Table.column('', :width => header_width[i], :padding => (col_just == :decimal ? 0 : 2), :justification => col_just[i])
312
+ end
313
+
314
+ Table.row(['jim', 12.12, 0.004])
315
+ Table.row(['george', 1.212, 0.04])
316
+ Table.row(['bob', 121.2, 0.0004])
317
+
318
+ out = capture_output do
319
+ Table.tabulate
320
+ end
321
+
322
+ table_text = out.string
323
+ table_lines = table_text.split("\n")
324
+
325
+ assert_equal '+----------------------------------------+', table_lines[0]
326
+ assert_equal '| jim | 12.12 | 0.004 |', table_lines[1]
327
+ assert_equal '| george | 1.212 | 0.04 |', table_lines[2]
328
+ assert_equal '| bob | 121.2 | 0.0004 |', table_lines[3]
329
+ assert_equal '+----------------------------------------+', table_lines[4]
330
+ end
331
+
332
+ def test_table_no_column_headings
333
+
334
+ Table.header("Current User Credentials")
335
+ Table.column('', :width => 16, :padding => 2, :justification => :right)
336
+ Table.column('', :width => 24, :padding => 2, :justification => :left)
337
+
338
+ Table.row ['display', 'geordie']
339
+ Table.row ['password', 'dkdkdkdflsjlj']
340
+ Table.row ['user', 'gspeake']
341
+
342
+ out = capture_output do
343
+ Table.tabulate
344
+ end
345
+
346
+ table_text = out.string
347
+ table_lines = table_text.split("\n")
348
+
349
+ assert_equal '+---------------------------------------------+', table_lines[0]
350
+ assert_equal '| Current User Credentials |', table_lines[1]
351
+ assert_equal '+---------------------------------------------+', table_lines[2]
352
+ assert_equal '| display | geordie |', table_lines[3]
353
+ assert_equal '| password | dkdkdkdflsjlj |', table_lines[4]
354
+ assert_equal '| user | gspeake |', table_lines[5]
355
+ assert_equal '+---------------------------------------------+', table_lines[6]
356
+
357
+ end
358
+
359
+ def test_table__csv_output
360
+
361
+ header_text = ['head A', 'head B', 'head C']
362
+ header_width = [12, 10, 14]
363
+ col_just = [:left, :right, :left]
364
+ table_header = 'a table of stuff'
365
+
366
+ Table.header(table_header, :padding => 4, :justification => :left)
367
+ Table.header('second', :padding => 1, :justification => :right)
368
+
369
+ (0..header_text.length - 1).each do |i|
370
+ Table.column(header_text[i], :width => header_width[i], :padding => 2, :justification => col_just[i])
371
+ end
372
+
373
+ (1..5).each do |i|
374
+ data = [i, (i+32).chr.to_s, i*i]
375
+ Table.row(data)
376
+ end
377
+
378
+ Table.footer("this data is for practice", :padding => 1, :justification => :center)
379
+
380
+ csv_output = Table.csv
381
+
382
+ lines = csv_output.split("\n")
383
+ assert_equal 9, lines.count
384
+
385
+ assert_equal table_header, lines[0]
386
+ assert_equal 'second', lines[1]
387
+ assert_equal header_text.join(','), lines[2]
388
+
389
+ assert_equal '1,!,1', lines[3]
390
+ assert_equal '2,",4', lines[4]
391
+ assert_equal '3,#,9', lines[5]
392
+ assert_equal '4,$,16', lines[6]
393
+ assert_equal '5,%,25', lines[7]
394
+ assert_equal 'this data is for practice', lines[8]
395
+ end
396
+
397
+ def test_table__live_update_data
398
+ header_text = ['head A', 'head B', 'head C']
399
+ header_width = [12, 10, 14]
400
+ col_just = [:left, :right, :left]
401
+ table_header = 'a table of stuff'
402
+
403
+ Table.clear_all
404
+ Table.live_update = true
405
+
406
+ Table.header(table_header, :padding => 4, :justification => :left)
407
+ Table.header('second', :padding => 1, :justification => :right)
408
+
409
+ (0..header_text.length - 1).each do |i|
410
+ Table.column(header_text[i], :width => header_width[i], :padding => 2, :justification => col_just[i])
411
+ end
412
+
413
+ out = capture_output do
414
+ Table.start_live_update
415
+ end
416
+ header_text = out.string
417
+ header_lines = header_text.split("\n")
418
+ assert_equal 6, header_lines.length
419
+
420
+ assert_equal '+--------------------------------------------+', header_lines[0]
421
+ assert_equal '| a table of stuff |', header_lines[1]
422
+ assert_equal '| second |', header_lines[2]
423
+ assert_equal '+--------------------------------------------+', header_lines[3]
424
+ assert_equal '| head A | head B | head C |', header_lines[4]
425
+ assert_equal '|--------------+------------+----------------|', header_lines[5]
426
+
427
+ expected_lines = ['']
428
+ expected_lines << '| 1 | ! | 1 |'
429
+ expected_lines << '| 2 | " | 4 |'
430
+ expected_lines << '| 3 | # | 9 |'
431
+ expected_lines << '| 4 | $ | 16 |'
432
+ expected_lines << '| 5 | % | |'
433
+
434
+ (1..5).each do |i|
435
+ data = [i, (i+32).chr.to_s, i*i]
436
+ data.pop if i == 5
437
+ out = capture_output do
438
+ Table.row(data)
439
+ Table.separator if i == 3
440
+ end
441
+
442
+ line_text = out.string.chomp
443
+ assert_equal expected_lines[i], line_text.split("\n")[0]
444
+ assert_equal '|--------------+------------+----------------|', line_text.split("\n")[1] if i == 3
445
+
446
+ end
447
+
448
+ Table.footer("this data is for practice", :padding => 1, :justification => :center)
449
+
450
+ out = capture_output do
451
+ Table.complete_live_update
452
+ end
453
+
454
+ footer_text = out.string
455
+ footer_lines = footer_text.split("\n")
456
+ assert_equal 3, footer_lines.length
457
+
458
+ assert_equal '+--------------------------------------------+', footer_lines[0]
459
+ assert_equal '| this data is for practice |', footer_lines[1]
460
+ assert_equal '+--------------------------------------------+', footer_lines[2]
461
+ end
462
+
463
+ def test_set_too_much_data
464
+ Table.clear_all
465
+
466
+ Table.header("test header", :padding => 4, :justification => :left)
467
+ Table.column('column 1', :width => 10, :padding => 2, :justification => :left)
468
+ Table.column('column 2', :width => 10, :padding => 2, :justification => :left)
469
+
470
+ Table.row(["r1c1", "r1c2"])
471
+ Table.row(["r2c1", "r2c2"])
472
+ Table.row(["r3c1", "r3c2", "r3c3"])
473
+
474
+ assert_raises TooManyDataPoints do
475
+ out = capture_output do
476
+ Table.tabulate
477
+ end
478
+ end
479
+ end
480
+
481
+ def test_color__single_cell
482
+ Table.clear_all
483
+
484
+ Table.header("test header", :padding => 4, :justification => :left)
485
+ Table.column('column 1', :width => 10, :padding => 2, :justification => :left)
486
+ Table.column('column 2', :width => 10, :padding => 2, :justification => :left)
487
+
488
+ Table.row([{:data => "r1c1", :color => :red}, "r1c2"])
489
+ Table.row(["r2c1", {:data => "r2c1", :color => :yellow}])
490
+ Table.row(["r3c1", {:data => "r3c2", :color => :blue_on_white}])
491
+
492
+ out = capture_output do
493
+ Table.tabulate
494
+ end
495
+
496
+ table_text = out.string
497
+ table_lines = table_text.split("\n")
498
+
499
+ assert_equal "+-------------------------+", table_lines[0]
500
+ assert_equal "| test header |", table_lines[1]
501
+ assert_equal "+-------------------------+", table_lines[2]
502
+ assert_equal "| column 1 | column 2 |", table_lines[3]
503
+ assert_equal "|------------+------------|", table_lines[4]
504
+ assert_equal "| #{"r1c1".red} | r1c2 |", table_lines[5]
505
+ assert_equal "| r2c1 | #{"r2c1".yellow} |", table_lines[6]
506
+ assert_equal "| r3c1 | #{"r3c2".blue_on_white} |", table_lines[7]
507
+ assert_equal "+-------------------------+", table_lines[8]
508
+
509
+ end
510
+
511
+ def test_color__whole_row
512
+ Table.clear_all
513
+
514
+ Table.header("test header", :padding => 4, :justification => :left)
515
+ Table.column('column 1', :width => 10, :padding => 2, :justification => :left)
516
+ Table.column('column 2', :width => 10, :padding => 2, :justification => :left)
517
+
518
+ Table.row(["r1c1", "r1c2"])
519
+ Table.row({:data => ["r2c1", "r2c1"], :color => :yellow})
520
+ Table.row(["r3c1", "r3c2"])
521
+
522
+ out = capture_output do
523
+ Table.tabulate
524
+ end
525
+
526
+ table_text = out.string
527
+ table_lines = table_text.split("\n")
528
+
529
+ assert_equal "+-------------------------+", table_lines[0]
530
+ assert_equal "| test header |", table_lines[1]
531
+ assert_equal "+-------------------------+", table_lines[2]
532
+ assert_equal "| column 1 | column 2 |", table_lines[3]
533
+ assert_equal "|------------+------------|", table_lines[4]
534
+ assert_equal "| r1c1 | r1c2 |", table_lines[5]
535
+ assert_equal "| #{"r2c1".yellow} | #{"r2c1".yellow} |", table_lines[6]
536
+ assert_equal "| r3c1 | r3c2 |", table_lines[7]
537
+ assert_equal "+-------------------------+", table_lines[8]
538
+
539
+ end
540
+
541
+ def test_color__whole_column
542
+ Table.clear_all
543
+
544
+ Table.header("test header", :padding => 4, :justification => :left)
545
+ Table.column('column 1', :width => 10, :padding => 2, :justification => :left)
546
+ Table.column('column 2', :width => 10, :padding => 2, :justification => :left, :color => :blue)
547
+
548
+ Table.row(["r1c1", "r1c2"])
549
+ Table.row(["r2c1", {:data => "r2c1", :color => :red}])
550
+ Table.row(["r3c1", "r3c2"])
551
+
552
+ out = capture_output do
553
+ Table.tabulate
554
+ end
555
+
556
+ table_text = out.string
557
+ table_lines = table_text.split("\n")
558
+
559
+ assert_equal "+-------------------------+", table_lines[0]
560
+ assert_equal "| test header |", table_lines[1]
561
+ assert_equal "+-------------------------+", table_lines[2]
562
+ assert_equal "| column 1 | column 2 |", table_lines[3]
563
+ assert_equal "|------------+------------|", table_lines[4]
564
+ assert_equal "| r1c1 | #{"r1c2".blue} |", table_lines[5]
565
+ assert_equal "| r2c1 | #{"r2c1".red } |", table_lines[6]
566
+ assert_equal "| r3c1 | #{"r3c2".blue} |", table_lines[7]
567
+ assert_equal "+-------------------------+", table_lines[8]
568
+ end
569
+
570
+ def test_color__whole_column__decimal
571
+ Table.clear_all
572
+
573
+ Table.header("test header", :padding => 4, :justification => :left)
574
+ Table.column('column 1', :width => 10, :padding => 2, :justification => :left)
575
+ Table.column('column 2', :width => 14, :padding => 2, :justification => :decimal, :color => :blue)
576
+
577
+ Table.row(["r1c1", "r.1c2"])
578
+ Table.row(["r2c1", "r2.c1"])
579
+ Table.row(["r3c1", "r3c.2"])
580
+ Table.row(["r3c1", ".r3c2"])
581
+
582
+ out = capture_output do
583
+ Table.tabulate
584
+ end
585
+
586
+ table_text = out.string
587
+ table_lines = table_text.split("\n")
588
+
589
+ assert_equal "+---------------------------+", table_lines[0]
590
+ assert_equal "| test header |", table_lines[1]
591
+ assert_equal "+---------------------------+", table_lines[2]
592
+ assert_equal "| column 1 | column 2 |", table_lines[3]
593
+ assert_equal "|------------+--------------|", table_lines[4]
594
+ assert_equal "| r1c1 |\e[34m r.1c2 \e[0m|", table_lines[5]
595
+ assert_equal "| r2c1 |\e[34m r2.c1 \e[0m|", table_lines[6]
596
+ assert_equal "| r3c1 |\e[34m r3c.2 \e[0m|", table_lines[7]
597
+ assert_equal "| r3c1 |\e[34m .r3c2 \e[0m|", table_lines[8]
598
+ assert_equal "+---------------------------+", table_lines[9]
599
+ end
600
+
601
+ def test_string_output
602
+
603
+ header_text = ['head A', 'head B', 'head C']
604
+ header_width = [12, 10, 14]
605
+ col_just = [:left, :right, :left]
606
+ table_header = 'a table of stuff'
607
+
608
+ Table.header(table_header, :padding => 4, :justification => :left)
609
+ Table.header('second', :padding => 1, :justification => :right)
610
+
611
+ (0..header_text.length - 1).each do |i|
612
+ Table.column(header_text[i], :width => header_width[i], :padding => 2, :justification => col_just[i])
613
+ end
614
+
615
+ (1..5).each do |i|
616
+ data = [i, (i+32).chr.to_s, i*i]
617
+ Table.row(data)
618
+ end
619
+
620
+ Table.footer("this data is for practice", :padding => 1, :justification => :center)
621
+
622
+ table_text = Table.tabulate_to_string
623
+
624
+ table_lines = table_text.split("\n")
625
+ assert_equal 14, table_lines.length, 'the number of lines in the table output'
626
+
627
+ assert_equal '+--------------------------------------------+', table_lines[0]
628
+ assert_equal '| a table of stuff |', table_lines[1]
629
+ assert_equal '| second |', table_lines[2]
630
+ assert_equal '+--------------------------------------------+', table_lines[3]
631
+ assert_equal '| head A | head B | head C |', table_lines[4]
632
+ assert_equal '|--------------+------------+----------------|', table_lines[5]
633
+ assert_equal '| 1 | ! | 1 |', table_lines[6]
634
+ assert_equal '| 2 | " | 4 |', table_lines[7]
635
+ assert_equal '| 3 | # | 9 |', table_lines[8]
636
+ assert_equal '| 4 | $ | 16 |', table_lines[9]
637
+ assert_equal '| 5 | % | 25 |', table_lines[10]
638
+ assert_equal '+--------------------------------------------+', table_lines[11]
639
+ assert_equal '| this data is for practice |', table_lines[12]
640
+ assert_equal '+--------------------------------------------+', table_lines[13]
641
+ end
642
+
643
+ def test_html_output
644
+
645
+ header_text = ['a', 'b', 'c', 'd']
646
+
647
+ table_header = 'header'
648
+
649
+ Table.header(table_header, :padding => 4, :justification => :left)
650
+
651
+ (0..header_text.length - 1).each do |i|
652
+ Table.column(header_text[i])
653
+ end
654
+
655
+ Table.row([1, 2, 3, 4])
656
+ Table.row([1.1, 2.1, 3.1, 4.1])
657
+
658
+ table_text = Table.tabulate_to_html
659
+
660
+ #assert_equal "booty", table_text
661
+
662
+ table_lines = table_text.split("\n")
663
+ assert_equal 25, table_lines.length, 'the number of lines in the table output'
664
+
665
+ assert_equal '<table>', table_lines[0]
666
+ assert_equal '<thead>', table_lines[1]
667
+ assert_equal "<tr><th colspan='5'>header</th></tr>", table_lines[2]
668
+ assert_equal '<tr>', table_lines[3]
669
+ assert_equal '<th>a</th>', table_lines[4]
670
+ assert_equal '<th>b</th>', table_lines[5]
671
+ assert_equal '<th>c</th>', table_lines[6]
672
+ assert_equal '<th>d</th>', table_lines[7]
673
+ assert_equal '</tr>', table_lines[8]
674
+ assert_equal '</thead>', table_lines[9]
675
+ assert_equal '<tbody>', table_lines[10]
676
+ assert_equal '<tr>', table_lines[11]
677
+ assert_equal '<td>1</td>', table_lines[12]
678
+ assert_equal '<td>2</td>', table_lines[13]
679
+ assert_equal '<td>3</td>', table_lines[14]
680
+ assert_equal '<td>4</td>', table_lines[15]
681
+ assert_equal '</tr>', table_lines[16]
682
+ assert_equal '<tr>', table_lines[17]
683
+ assert_equal '<td>1.1</td>', table_lines[18]
684
+ assert_equal '<td>2.1</td>', table_lines[19]
685
+ assert_equal '<td>3.1</td>', table_lines[20]
686
+ assert_equal '<td>4.1</td>', table_lines[21]
687
+ assert_equal '</tr>', table_lines[22]
688
+ assert_equal '</tbody>', table_lines[23]
689
+ assert_equal '</table>', table_lines[24]
690
+
691
+
692
+ end
693
+
694
+
695
+ end