command_kit 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ruby.yml +4 -6
  3. data/.rubocop.yml +13 -0
  4. data/ChangeLog.md +18 -0
  5. data/Gemfile +2 -0
  6. data/LICENSE.txt +1 -1
  7. data/README.md +18 -9
  8. data/command_kit.gemspec +0 -1
  9. data/examples/printing/tables.rb +141 -0
  10. data/gemspec.yml +3 -3
  11. data/lib/command_kit/bug_report.rb +105 -0
  12. data/lib/command_kit/colors.rb +4 -4
  13. data/lib/command_kit/edit.rb +54 -0
  14. data/lib/command_kit/env.rb +1 -1
  15. data/lib/command_kit/options/option.rb +5 -1
  16. data/lib/command_kit/options/option_value.rb +2 -2
  17. data/lib/command_kit/options/parser.rb +1 -1
  18. data/lib/command_kit/options/quiet.rb +1 -1
  19. data/lib/command_kit/options/verbose.rb +2 -2
  20. data/lib/command_kit/options/version.rb +10 -0
  21. data/lib/command_kit/options.rb +1 -1
  22. data/lib/command_kit/os.rb +1 -1
  23. data/lib/command_kit/printing/fields.rb +56 -0
  24. data/lib/command_kit/printing/indent.rb +1 -1
  25. data/lib/command_kit/printing/lists.rb +91 -0
  26. data/lib/command_kit/printing/tables/border_style.rb +169 -0
  27. data/lib/command_kit/printing/tables/cell_builder.rb +93 -0
  28. data/lib/command_kit/printing/tables/row_builder.rb +111 -0
  29. data/lib/command_kit/printing/tables/style.rb +198 -0
  30. data/lib/command_kit/printing/tables/table_builder.rb +145 -0
  31. data/lib/command_kit/printing/tables/table_formatter.rb +254 -0
  32. data/lib/command_kit/printing/tables.rb +208 -0
  33. data/lib/command_kit/stdio.rb +5 -1
  34. data/lib/command_kit/version.rb +1 -1
  35. data/spec/bug_report_spec.rb +266 -0
  36. data/spec/colors_spec.rb +6 -0
  37. data/spec/command_name_spec.rb +1 -1
  38. data/spec/edit_spec.rb +72 -0
  39. data/spec/options/option_spec.rb +12 -2
  40. data/spec/options/quiet_spec.rb +51 -0
  41. data/spec/options/verbose_spec.rb +51 -0
  42. data/spec/options/version_spec.rb +146 -0
  43. data/spec/pager_spec.rb +1 -1
  44. data/spec/printing/fields_spec.rb +167 -0
  45. data/spec/printing/lists_spec.rb +99 -0
  46. data/spec/printing/tables/border_style.rb +43 -0
  47. data/spec/printing/tables/cell_builer_spec.rb +135 -0
  48. data/spec/printing/tables/row_builder_spec.rb +165 -0
  49. data/spec/printing/tables/style_spec.rb +377 -0
  50. data/spec/printing/tables/table_builder_spec.rb +252 -0
  51. data/spec/printing/tables/table_formatter_spec.rb +1180 -0
  52. data/spec/printing/tables_spec.rb +1069 -0
  53. metadata +33 -7
@@ -0,0 +1,1069 @@
1
+ require 'spec_helper'
2
+ require 'command_kit/printing/tables'
3
+
4
+ require 'stringio'
5
+
6
+ describe CommandKit::Printing::Tables do
7
+ module TestPrintingTables
8
+ class TestCmd
9
+
10
+ include CommandKit::Printing::Tables
11
+
12
+ end
13
+ end
14
+
15
+ let(:command_class) { TestPrintingTables::TestCmd }
16
+ subject { command_class.new }
17
+
18
+ describe "#print_table" do
19
+ let(:header) { ['A', 'B', 'C'] }
20
+ let(:table) do
21
+ [
22
+ ['AAAA', 'BBBB', 'CCCC'],
23
+ ['DDDD', 'EEEE', 'FFFF'],
24
+ ['GGGG', 'HHHH', 'IIII']
25
+ ]
26
+ end
27
+ let(:multiline_table) do
28
+ [
29
+ ["AAAA\nAA", "BBBB", "CCCC"],
30
+ ["DDDD", "EEEE\nEE", "FFFF"],
31
+ ["GGGG", "HHHH", "IIII\nII"]
32
+ ]
33
+ end
34
+ let(:table_with_empty_cells) do
35
+ [
36
+ [nil, 'BBBB', 'CCCC'],
37
+ ['DDDD', nil, 'FFFF'],
38
+ ['GGGG', 'HHHH', nil ]
39
+ ]
40
+ end
41
+ let(:table_with_diff_row_lengths) do
42
+ [
43
+ ['AAAA'],
44
+ ['DDDD', 'EEEE'],
45
+ ['GGGG', 'HHHH', 'IIII']
46
+ ]
47
+ end
48
+ let(:unjustified_table) do
49
+ [
50
+ ['AAAA', 'BBBB', 'CCCC'],
51
+ ['DD', 'EE', 'FF' ],
52
+ ['G', 'H', 'I' ]
53
+ ]
54
+ end
55
+
56
+ context "when no keyword arguments are given" do
57
+ it "must print the table with 1 space padding and no borders" do
58
+ expect {
59
+ subject.print_table(table)
60
+ }.to output(
61
+ [
62
+ " AAAA BBBB CCCC ",
63
+ " DDDD EEEE FFFF ",
64
+ " GGGG HHHH IIII ",
65
+ ''
66
+ ].join($/)
67
+ ).to_stdout
68
+ end
69
+
70
+ context "but when the table contains multi-line cells" do
71
+ it "must print each line of each row" do
72
+ expect {
73
+ subject.print_table(multiline_table)
74
+ }.to output(
75
+ [
76
+ " AAAA BBBB CCCC ",
77
+ " AA ",
78
+ " DDDD EEEE FFFF ",
79
+ " EE ",
80
+ " GGGG HHHH IIII ",
81
+ " II ",
82
+ ''
83
+ ].join($/)
84
+ ).to_stdout
85
+ end
86
+ end
87
+
88
+ context "but when the table contains empty cells" do
89
+ it "must replace the empty cells with spaces" do
90
+ expect {
91
+ subject.print_table(table_with_empty_cells)
92
+ }.to output(
93
+ [
94
+ " BBBB CCCC ",
95
+ " DDDD FFFF ",
96
+ " GGGG HHHH ",
97
+ ''
98
+ ].join($/)
99
+ ).to_stdout
100
+ end
101
+ end
102
+
103
+ context "but when the table contains empty cells" do
104
+ it "must pad the columns with empty cells" do
105
+ expect {
106
+ subject.print_table(table_with_diff_row_lengths)
107
+ }.to output(
108
+ [
109
+ " AAAA ",
110
+ " DDDD EEEE ",
111
+ " GGGG HHHH IIII ",
112
+ ''
113
+ ].join($/)
114
+ ).to_stdout
115
+ end
116
+ end
117
+ end
118
+
119
+ context "and when `separate_rows: true` is given" do
120
+ it "must print each row with blank lines between each row" do
121
+ expect {
122
+ subject.print_table(table, separate_rows: true)
123
+ }.to output(
124
+ [
125
+ " AAAA BBBB CCCC ",
126
+ "",
127
+ " DDDD EEEE FFFF ",
128
+ "",
129
+ " GGGG HHHH IIII ",
130
+ ''
131
+ ].join($/)
132
+ ).to_stdout
133
+ end
134
+ end
135
+
136
+ context "and when `justify: :left` is given" do
137
+ it "must left justify each cell" do
138
+ expect {
139
+ subject.print_table(unjustified_table, justify: :left)
140
+ }.to output(
141
+ [
142
+ " AAAA BBBB CCCC ",
143
+ " DD EE FF ",
144
+ " G H I ",
145
+ ''
146
+ ].join($/)
147
+ ).to_stdout
148
+ end
149
+ end
150
+
151
+ context "and when `justify: :right` is given" do
152
+ it "must right justify each cell" do
153
+ expect {
154
+ subject.print_table(unjustified_table, justify: :right)
155
+ }.to output(
156
+ [
157
+ " AAAA BBBB CCCC ",
158
+ " DD EE FF ",
159
+ " G H I ",
160
+ ''
161
+ ].join($/)
162
+ ).to_stdout
163
+ end
164
+ end
165
+
166
+ context "and when `justify: :center` is given" do
167
+ it "must center justify each cell" do
168
+ expect {
169
+ subject.print_table(unjustified_table, justify: :center)
170
+ }.to output(
171
+ [
172
+ " AAAA BBBB CCCC ",
173
+ " DD EE FF ",
174
+ " G H I ",
175
+ ''
176
+ ].join($/)
177
+ ).to_stdout
178
+ end
179
+ end
180
+
181
+ context "and when the header: keyword argument is given" do
182
+ it "must print the center justified header row, then a blank line, then the table" do
183
+ expect {
184
+ subject.print_table(table, header: header)
185
+ }.to output(
186
+ [
187
+ " A B C ",
188
+ "",
189
+ " AAAA BBBB CCCC ",
190
+ " DDDD EEEE FFFF ",
191
+ " GGGG HHHH IIII ",
192
+ ''
193
+ ].join($/)
194
+ ).to_stdout
195
+ end
196
+
197
+ context "and when `separate_rows: true` is given" do
198
+ it "must print the header and add blank lines between each row" do
199
+ expect {
200
+ subject.print_table(table, header: header, separate_rows: true)
201
+ }.to output(
202
+ [
203
+ " A B C ",
204
+ "",
205
+ " AAAA BBBB CCCC ",
206
+ "",
207
+ " DDDD EEEE FFFF ",
208
+ "",
209
+ " GGGG HHHH IIII ",
210
+ ''
211
+ ].join($/)
212
+ ).to_stdout
213
+ end
214
+ end
215
+
216
+ context "and when `justify_header: :left` is given" do
217
+ it "must left justify the header cells" do
218
+ expect {
219
+ subject.print_table(table, header: header, justify_header: :left)
220
+ }.to output(
221
+ [
222
+ " A B C ",
223
+ "",
224
+ " AAAA BBBB CCCC ",
225
+ " DDDD EEEE FFFF ",
226
+ " GGGG HHHH IIII ",
227
+ ''
228
+ ].join($/)
229
+ ).to_stdout
230
+ end
231
+ end
232
+
233
+ context "and when `justify_header: :right` is given" do
234
+ it "must right justify the header cells" do
235
+ expect {
236
+ subject.print_table(table, header: header, justify_header: :right)
237
+ }.to output(
238
+ [
239
+ " A B C ",
240
+ "",
241
+ " AAAA BBBB CCCC ",
242
+ " DDDD EEEE FFFF ",
243
+ " GGGG HHHH IIII ",
244
+ ''
245
+ ].join($/)
246
+ ).to_stdout
247
+ end
248
+ end
249
+
250
+ context "and when `justify_header: :center` is given" do
251
+ it "must center justify the header cells" do
252
+ expect {
253
+ subject.print_table(table, header: header, justify_header: :center)
254
+ }.to output(
255
+ [
256
+ " A B C ",
257
+ "",
258
+ " AAAA BBBB CCCC ",
259
+ " DDDD EEEE FFFF ",
260
+ " GGGG HHHH IIII ",
261
+ ''
262
+ ].join($/)
263
+ ).to_stdout
264
+ end
265
+ end
266
+
267
+ context "and when `justify: :left` is given" do
268
+ it "must left justify each cell" do
269
+ expect {
270
+ subject.print_table(unjustified_table, header: header,
271
+ justify: :left)
272
+ }.to output(
273
+ [
274
+ " A B C ",
275
+ "",
276
+ " AAAA BBBB CCCC ",
277
+ " DD EE FF ",
278
+ " G H I ",
279
+ ''
280
+ ].join($/)
281
+ ).to_stdout
282
+ end
283
+ end
284
+
285
+ context "and when `justify: :right` is given" do
286
+ it "must right justify each cell" do
287
+ expect {
288
+ subject.print_table(unjustified_table, header: header,
289
+ justify: :right)
290
+ }.to output(
291
+ [
292
+ " A B C ",
293
+ "",
294
+ " AAAA BBBB CCCC ",
295
+ " DD EE FF ",
296
+ " G H I ",
297
+ ''
298
+ ].join($/)
299
+ ).to_stdout
300
+ end
301
+ end
302
+
303
+ context "and when `justify: :center` is given" do
304
+ it "must center justify each cell" do
305
+ expect {
306
+ subject.print_table(unjustified_table, header: header,
307
+ justify: :center)
308
+ }.to output(
309
+ [
310
+ " A B C ",
311
+ "",
312
+ " AAAA BBBB CCCC ",
313
+ " DD EE FF ",
314
+ " G H I ",
315
+ ''
316
+ ].join($/)
317
+ ).to_stdout
318
+ end
319
+ end
320
+ end
321
+
322
+ context "when given `border: :ascii`" do
323
+ it "must print the table with an ASCII border" do
324
+ expect {
325
+ subject.print_table(table, border: :ascii)
326
+ }.to output(
327
+ [
328
+ "+------+------+------+",
329
+ "| AAAA | BBBB | CCCC |",
330
+ "| DDDD | EEEE | FFFF |",
331
+ "| GGGG | HHHH | IIII |",
332
+ "+------+------+------+",
333
+ ''
334
+ ].join($/)
335
+ ).to_stdout
336
+ end
337
+
338
+ context "but when the table contains multi-line cells" do
339
+ it "must print each line of each row" do
340
+ expect {
341
+ subject.print_table(multiline_table, border: :ascii)
342
+ }.to output(
343
+ [
344
+ "+------+------+------+",
345
+ "| AAAA | BBBB | CCCC |",
346
+ "| AA | | |",
347
+ "| DDDD | EEEE | FFFF |",
348
+ "| | EE | |",
349
+ "| GGGG | HHHH | IIII |",
350
+ "| | | II |",
351
+ "+------+------+------+",
352
+ ''
353
+ ].join($/)
354
+ ).to_stdout
355
+ end
356
+ end
357
+
358
+ context "but when the table contains empty cells" do
359
+ it "must replace the empty cells with spaces" do
360
+ expect {
361
+ subject.print_table(table_with_empty_cells, border: :ascii)
362
+ }.to output(
363
+ [
364
+ "+------+------+------+",
365
+ "| | BBBB | CCCC |",
366
+ "| DDDD | | FFFF |",
367
+ "| GGGG | HHHH | |",
368
+ "+------+------+------+",
369
+ ''
370
+ ].join($/)
371
+ ).to_stdout
372
+ end
373
+ end
374
+
375
+ context "but when the table contains empty cells" do
376
+ it "must pad the columns with empty cells" do
377
+ expect {
378
+ subject.print_table(table_with_diff_row_lengths, border: :ascii)
379
+ }.to output(
380
+ [
381
+ "+------+------+------+",
382
+ "| AAAA | | |",
383
+ "| DDDD | EEEE | |",
384
+ "| GGGG | HHHH | IIII |",
385
+ "+------+------+------+",
386
+ ''
387
+ ].join($/)
388
+ ).to_stdout
389
+ end
390
+ end
391
+
392
+ context "and when `separate_rows: true` is given" do
393
+ it "must add separator lines between each row" do
394
+ expect {
395
+ subject.print_table(table, border: :ascii, separate_rows: true)
396
+ }.to output(
397
+ [
398
+ "+------+------+------+",
399
+ "| AAAA | BBBB | CCCC |",
400
+ "+------+------+------+",
401
+ "| DDDD | EEEE | FFFF |",
402
+ "+------+------+------+",
403
+ "| GGGG | HHHH | IIII |",
404
+ "+------+------+------+",
405
+ ''
406
+ ].join($/)
407
+ ).to_stdout
408
+ end
409
+ end
410
+
411
+ context "and when `justify: :left` is given" do
412
+ it "must left justify each cell" do
413
+ expect {
414
+ subject.print_table(unjustified_table, border: :ascii,
415
+ justify: :left)
416
+ }.to output(
417
+ [
418
+ "+------+------+------+",
419
+ "| AAAA | BBBB | CCCC |",
420
+ "| DD | EE | FF |",
421
+ "| G | H | I |",
422
+ "+------+------+------+",
423
+ ''
424
+ ].join($/)
425
+ ).to_stdout
426
+ end
427
+ end
428
+
429
+ context "and when `justify: :right` is given" do
430
+ it "must right justify each cell" do
431
+ expect {
432
+ subject.print_table(unjustified_table, border: :ascii,
433
+ justify: :right)
434
+ }.to output(
435
+ [
436
+ "+------+------+------+",
437
+ "| AAAA | BBBB | CCCC |",
438
+ "| DD | EE | FF |",
439
+ "| G | H | I |",
440
+ "+------+------+------+",
441
+ ''
442
+ ].join($/)
443
+ ).to_stdout
444
+ end
445
+ end
446
+
447
+ context "and when `justify: :center` is given" do
448
+ it "must center justify each cell" do
449
+ expect {
450
+ subject.print_table(unjustified_table, border: :ascii,
451
+ justify: :center)
452
+ }.to output(
453
+ [
454
+ "+------+------+------+",
455
+ "| AAAA | BBBB | CCCC |",
456
+ "| DD | EE | FF |",
457
+ "| G | H | I |",
458
+ "+------+------+------+",
459
+ ''
460
+ ].join($/)
461
+ ).to_stdout
462
+ end
463
+ end
464
+
465
+ context "and when `header: true` is given" do
466
+ it "must print the center justified header row, then a separator line, then the table" do
467
+ expect {
468
+ subject.print_table(table, border: :ascii, header: header)
469
+ }.to output(
470
+ [
471
+ "+------+------+------+",
472
+ "| A | B | C |",
473
+ "+------+------+------+",
474
+ "| AAAA | BBBB | CCCC |",
475
+ "| DDDD | EEEE | FFFF |",
476
+ "| GGGG | HHHH | IIII |",
477
+ "+------+------+------+",
478
+ ''
479
+ ].join($/)
480
+ ).to_stdout
481
+ end
482
+
483
+ context "and when `separate_rows: true` is given" do
484
+ it "must print the header and add separator lines between each row" do
485
+ expect {
486
+ subject.print_table(table, border: :ascii,
487
+ header: header,
488
+ separate_rows: true)
489
+ }.to output(
490
+ [
491
+ "+------+------+------+",
492
+ "| A | B | C |",
493
+ "+------+------+------+",
494
+ "| AAAA | BBBB | CCCC |",
495
+ "+------+------+------+",
496
+ "| DDDD | EEEE | FFFF |",
497
+ "+------+------+------+",
498
+ "| GGGG | HHHH | IIII |",
499
+ "+------+------+------+",
500
+ ''
501
+ ].join($/)
502
+ ).to_stdout
503
+ end
504
+ end
505
+
506
+ context "and when `justify_header: :left` is given" do
507
+ it "must left justify the header cells" do
508
+ expect {
509
+ subject.print_table(table, border: :ascii,
510
+ header: header,
511
+ justify_header: :left)
512
+ }.to output(
513
+ [
514
+ "+------+------+------+",
515
+ "| A | B | C |",
516
+ "+------+------+------+",
517
+ "| AAAA | BBBB | CCCC |",
518
+ "| DDDD | EEEE | FFFF |",
519
+ "| GGGG | HHHH | IIII |",
520
+ "+------+------+------+",
521
+ ''
522
+ ].join($/)
523
+ ).to_stdout
524
+ end
525
+ end
526
+
527
+ context "and when `justify_header: :right` is given" do
528
+ it "must right justify the header cells" do
529
+ expect {
530
+ subject.print_table(table, border: :ascii,
531
+ header: header,
532
+ justify_header: :right)
533
+ }.to output(
534
+ [
535
+ "+------+------+------+",
536
+ "| A | B | C |",
537
+ "+------+------+------+",
538
+ "| AAAA | BBBB | CCCC |",
539
+ "| DDDD | EEEE | FFFF |",
540
+ "| GGGG | HHHH | IIII |",
541
+ "+------+------+------+",
542
+ ''
543
+ ].join($/)
544
+ ).to_stdout
545
+ end
546
+ end
547
+
548
+ context "and when `justify_header: :center` is given" do
549
+ it "must center justify the header cells" do
550
+ expect {
551
+ subject.print_table(table, border: :ascii,
552
+ header: header,
553
+ justify_header: :center)
554
+ }.to output(
555
+ [
556
+ "+------+------+------+",
557
+ "| A | B | C |",
558
+ "+------+------+------+",
559
+ "| AAAA | BBBB | CCCC |",
560
+ "| DDDD | EEEE | FFFF |",
561
+ "| GGGG | HHHH | IIII |",
562
+ "+------+------+------+",
563
+ ''
564
+ ].join($/)
565
+ ).to_stdout
566
+ end
567
+ end
568
+ end
569
+ end
570
+
571
+ context "when given `border: :line`" do
572
+ it "must print the table with an ANSI line border" do
573
+ expect {
574
+ subject.print_table(table, border: :line)
575
+ }.to output(
576
+ [
577
+ "┌──────┬──────┬──────┐",
578
+ "│ AAAA │ BBBB │ CCCC │",
579
+ "│ DDDD │ EEEE │ FFFF │",
580
+ "│ GGGG │ HHHH │ IIII │",
581
+ "└──────┴──────┴──────┘",
582
+ ''
583
+ ].join($/)
584
+ ).to_stdout
585
+ end
586
+
587
+ context "but when the table contains multi-line cells" do
588
+ it "must print each line of each row" do
589
+ expect {
590
+ subject.print_table(multiline_table, border: :line)
591
+ }.to output(
592
+ [
593
+ "┌──────┬──────┬──────┐",
594
+ "│ AAAA │ BBBB │ CCCC │",
595
+ "│ AA │ │ │",
596
+ "│ DDDD │ EEEE │ FFFF │",
597
+ "│ │ EE │ │",
598
+ "│ GGGG │ HHHH │ IIII │",
599
+ "│ │ │ II │",
600
+ "└──────┴──────┴──────┘",
601
+ ''
602
+ ].join($/)
603
+ ).to_stdout
604
+ end
605
+ end
606
+
607
+ context "but when the table contains empty cells" do
608
+ it "must replace the empty cells with spaces" do
609
+ expect {
610
+ subject.print_table(table_with_empty_cells, border: :line)
611
+ }.to output(
612
+ [
613
+ "┌──────┬──────┬──────┐",
614
+ "│ │ BBBB │ CCCC │",
615
+ "│ DDDD │ │ FFFF │",
616
+ "│ GGGG │ HHHH │ │",
617
+ "└──────┴──────┴──────┘",
618
+ ''
619
+ ].join($/)
620
+ ).to_stdout
621
+ end
622
+ end
623
+
624
+ context "but when the table contains empty cells" do
625
+ it "must pad the columns with empty cells" do
626
+ expect {
627
+ subject.print_table(table_with_diff_row_lengths, border: :line)
628
+ }.to output(
629
+ [
630
+ "┌──────┬──────┬──────┐",
631
+ "│ AAAA │ │ │",
632
+ "│ DDDD │ EEEE │ │",
633
+ "│ GGGG │ HHHH │ IIII │",
634
+ "└──────┴──────┴──────┘",
635
+ ''
636
+ ].join($/)
637
+ ).to_stdout
638
+ end
639
+ end
640
+
641
+ context "and when `separate_rows: true` is given" do
642
+ it "must add separator lines between each row" do
643
+ expect {
644
+ subject.print_table(table, border: :line, separate_rows: true)
645
+ }.to output(
646
+ [
647
+ "┌──────┬──────┬──────┐",
648
+ "│ AAAA │ BBBB │ CCCC │",
649
+ "├──────┼──────┼──────┤",
650
+ "│ DDDD │ EEEE │ FFFF │",
651
+ "├──────┼──────┼──────┤",
652
+ "│ GGGG │ HHHH │ IIII │",
653
+ "└──────┴──────┴──────┘",
654
+ ''
655
+ ].join($/)
656
+ ).to_stdout
657
+ end
658
+ end
659
+
660
+ context "and when `justify: :left` is given" do
661
+ it "must left justify each cell" do
662
+ expect {
663
+ subject.print_table(unjustified_table, border: :line,
664
+ justify: :left)
665
+ }.to output(
666
+ [
667
+ "┌──────┬──────┬──────┐",
668
+ "│ AAAA │ BBBB │ CCCC │",
669
+ "│ DD │ EE │ FF │",
670
+ "│ G │ H │ I │",
671
+ "└──────┴──────┴──────┘",
672
+ ''
673
+ ].join($/)
674
+ ).to_stdout
675
+ end
676
+ end
677
+
678
+ context "and when `justify: :right` is given" do
679
+ it "must right justify each cell" do
680
+ expect {
681
+ subject.print_table(unjustified_table, border: :line,
682
+ justify: :right)
683
+ }.to output(
684
+ [
685
+ "┌──────┬──────┬──────┐",
686
+ "│ AAAA │ BBBB │ CCCC │",
687
+ "│ DD │ EE │ FF │",
688
+ "│ G │ H │ I │",
689
+ "└──────┴──────┴──────┘",
690
+ ''
691
+ ].join($/)
692
+ ).to_stdout
693
+ end
694
+ end
695
+
696
+ context "and when `justify: :center` is given" do
697
+ it "must center justify each cell" do
698
+ expect {
699
+ subject.print_table(unjustified_table, border: :line,
700
+ justify: :center)
701
+ }.to output(
702
+ [
703
+ "┌──────┬──────┬──────┐",
704
+ "│ AAAA │ BBBB │ CCCC │",
705
+ "│ DD │ EE │ FF │",
706
+ "│ G │ H │ I │",
707
+ "└──────┴──────┴──────┘",
708
+ ''
709
+ ].join($/)
710
+ ).to_stdout
711
+ end
712
+ end
713
+
714
+ context "and when `header: true` is given" do
715
+ it "must print the center justified header row, then a separator line, then the table" do
716
+ expect {
717
+ subject.print_table(table, border: :line, header: header)
718
+ }.to output(
719
+ [
720
+ "┌──────┬──────┬──────┐",
721
+ "│ A │ B │ C │",
722
+ "├──────┼──────┼──────┤",
723
+ "│ AAAA │ BBBB │ CCCC │",
724
+ "│ DDDD │ EEEE │ FFFF │",
725
+ "│ GGGG │ HHHH │ IIII │",
726
+ "└──────┴──────┴──────┘",
727
+ ''
728
+ ].join($/)
729
+ ).to_stdout
730
+ end
731
+
732
+ context "and when `separate_rows: true` is given" do
733
+ it "must print the header and add separator lines between each row" do
734
+ expect {
735
+ subject.print_table(table, border: :line,
736
+ header: header,
737
+ separate_rows: true)
738
+ }.to output(
739
+ [
740
+ "┌──────┬──────┬──────┐",
741
+ "│ A │ B │ C │",
742
+ "├──────┼──────┼──────┤",
743
+ "│ AAAA │ BBBB │ CCCC │",
744
+ "├──────┼──────┼──────┤",
745
+ "│ DDDD │ EEEE │ FFFF │",
746
+ "├──────┼──────┼──────┤",
747
+ "│ GGGG │ HHHH │ IIII │",
748
+ "└──────┴──────┴──────┘",
749
+ ''
750
+ ].join($/)
751
+ ).to_stdout
752
+ end
753
+ end
754
+
755
+ context "and when `justify_header: :left` is given" do
756
+ it "must left justify the header cells" do
757
+ expect {
758
+ subject.print_table(table, border: :line,
759
+ header: header,
760
+ justify_header: :left)
761
+ }.to output(
762
+ [
763
+ "┌──────┬──────┬──────┐",
764
+ "│ A │ B │ C │",
765
+ "├──────┼──────┼──────┤",
766
+ "│ AAAA │ BBBB │ CCCC │",
767
+ "│ DDDD │ EEEE │ FFFF │",
768
+ "│ GGGG │ HHHH │ IIII │",
769
+ "└──────┴──────┴──────┘",
770
+ ''
771
+ ].join($/)
772
+ ).to_stdout
773
+ end
774
+ end
775
+
776
+ context "and when `justify_header: :right` is given" do
777
+ it "must right justify the header cells" do
778
+ expect {
779
+ subject.print_table(table, border: :line,
780
+ header: header,
781
+ justify_header: :right)
782
+ }.to output(
783
+ [
784
+ "┌──────┬──────┬──────┐",
785
+ "│ A │ B │ C │",
786
+ "├──────┼──────┼──────┤",
787
+ "│ AAAA │ BBBB │ CCCC │",
788
+ "│ DDDD │ EEEE │ FFFF │",
789
+ "│ GGGG │ HHHH │ IIII │",
790
+ "└──────┴──────┴──────┘",
791
+ ''
792
+ ].join($/)
793
+ ).to_stdout
794
+ end
795
+ end
796
+
797
+ context "and when `justify_header: :center` is given" do
798
+ it "must center justify the header cells" do
799
+ expect {
800
+ subject.print_table(table, border: :line,
801
+ header: header,
802
+ justify_header: :center)
803
+ }.to output(
804
+ [
805
+ "┌──────┬──────┬──────┐",
806
+ "│ A │ B │ C │",
807
+ "├──────┼──────┼──────┤",
808
+ "│ AAAA │ BBBB │ CCCC │",
809
+ "│ DDDD │ EEEE │ FFFF │",
810
+ "│ GGGG │ HHHH │ IIII │",
811
+ "└──────┴──────┴──────┘",
812
+ ''
813
+ ].join($/)
814
+ ).to_stdout
815
+ end
816
+ end
817
+ end
818
+ end
819
+
820
+ context "when given `border: :double_line`" do
821
+ it "must print the table with an ANSI double-line border" do
822
+ expect {
823
+ subject.print_table(table, border: :double_line)
824
+ }.to output(
825
+ [
826
+ "╔══════╦══════╦══════╗",
827
+ "║ AAAA ║ BBBB ║ CCCC ║",
828
+ "║ DDDD ║ EEEE ║ FFFF ║",
829
+ "║ GGGG ║ HHHH ║ IIII ║",
830
+ "╚══════╩══════╩══════╝",
831
+ ''
832
+ ].join($/)
833
+ ).to_stdout
834
+ end
835
+
836
+ context "but when the table contains multi-line cells" do
837
+ it "must print each line of each row" do
838
+ expect {
839
+ subject.print_table(multiline_table, border: :double_line)
840
+ }.to output(
841
+ [
842
+ "╔══════╦══════╦══════╗",
843
+ "║ AAAA ║ BBBB ║ CCCC ║",
844
+ "║ AA ║ ║ ║",
845
+ "║ DDDD ║ EEEE ║ FFFF ║",
846
+ "║ ║ EE ║ ║",
847
+ "║ GGGG ║ HHHH ║ IIII ║",
848
+ "║ ║ ║ II ║",
849
+ "╚══════╩══════╩══════╝",
850
+ ''
851
+ ].join($/)
852
+ ).to_stdout
853
+ end
854
+ end
855
+
856
+ context "but when the table contains empty cells" do
857
+ it "must replace the empty cells with spaces" do
858
+ expect {
859
+ subject.print_table(table_with_empty_cells, border: :double_line)
860
+ }.to output(
861
+ [
862
+ "╔══════╦══════╦══════╗",
863
+ "║ ║ BBBB ║ CCCC ║",
864
+ "║ DDDD ║ ║ FFFF ║",
865
+ "║ GGGG ║ HHHH ║ ║",
866
+ "╚══════╩══════╩══════╝",
867
+ ''
868
+ ].join($/)
869
+ ).to_stdout
870
+ end
871
+ end
872
+
873
+ context "but when the table contains empty cells" do
874
+ it "must pad the columns with empty cells" do
875
+ expect {
876
+ subject.print_table(table_with_diff_row_lengths, border: :double_line)
877
+ }.to output(
878
+ [
879
+ "╔══════╦══════╦══════╗",
880
+ "║ AAAA ║ ║ ║",
881
+ "║ DDDD ║ EEEE ║ ║",
882
+ "║ GGGG ║ HHHH ║ IIII ║",
883
+ "╚══════╩══════╩══════╝",
884
+ ''
885
+ ].join($/)
886
+ ).to_stdout
887
+ end
888
+ end
889
+
890
+ context "and when `separate_rows: true` is given" do
891
+ it "must add separator lines between each row" do
892
+ expect {
893
+ subject.print_table(table, border: :double_line, separate_rows: true)
894
+ }.to output(
895
+ [
896
+ "╔══════╦══════╦══════╗",
897
+ "║ AAAA ║ BBBB ║ CCCC ║",
898
+ "╠══════╬══════╬══════╣",
899
+ "║ DDDD ║ EEEE ║ FFFF ║",
900
+ "╠══════╬══════╬══════╣",
901
+ "║ GGGG ║ HHHH ║ IIII ║",
902
+ "╚══════╩══════╩══════╝",
903
+ ''
904
+ ].join($/)
905
+ ).to_stdout
906
+ end
907
+ end
908
+
909
+ context "and when `justify: :left` is given" do
910
+ it "must left justify each cell" do
911
+ expect {
912
+ subject.print_table(unjustified_table, border: :double_line,
913
+ justify: :left)
914
+ }.to output(
915
+ [
916
+ "╔══════╦══════╦══════╗",
917
+ "║ AAAA ║ BBBB ║ CCCC ║",
918
+ "║ DD ║ EE ║ FF ║",
919
+ "║ G ║ H ║ I ║",
920
+ "╚══════╩══════╩══════╝",
921
+ ''
922
+ ].join($/)
923
+ ).to_stdout
924
+ end
925
+ end
926
+
927
+ context "and when `justify: :right` is given" do
928
+ it "must right justify each cell" do
929
+ expect {
930
+ subject.print_table(unjustified_table, border: :double_line,
931
+ justify: :right)
932
+ }.to output(
933
+ [
934
+ "╔══════╦══════╦══════╗",
935
+ "║ AAAA ║ BBBB ║ CCCC ║",
936
+ "║ DD ║ EE ║ FF ║",
937
+ "║ G ║ H ║ I ║",
938
+ "╚══════╩══════╩══════╝",
939
+ ''
940
+ ].join($/)
941
+ ).to_stdout
942
+ end
943
+ end
944
+
945
+ context "and when `justify: :center` is given" do
946
+ it "must center justify each cell" do
947
+ expect {
948
+ subject.print_table(unjustified_table, border: :double_line,
949
+ justify: :center)
950
+ }.to output(
951
+ [
952
+ "╔══════╦══════╦══════╗",
953
+ "║ AAAA ║ BBBB ║ CCCC ║",
954
+ "║ DD ║ EE ║ FF ║",
955
+ "║ G ║ H ║ I ║",
956
+ "╚══════╩══════╩══════╝",
957
+ ''
958
+ ].join($/)
959
+ ).to_stdout
960
+ end
961
+ end
962
+
963
+ context "and when `header: true` is given" do
964
+ it "must print the center justified header row, then a separator line, then the table" do
965
+ expect {
966
+ subject.print_table(table, border: :double_line, header: header)
967
+ }.to output(
968
+ [
969
+ "╔══════╦══════╦══════╗",
970
+ "║ A ║ B ║ C ║",
971
+ "╠══════╬══════╬══════╣",
972
+ "║ AAAA ║ BBBB ║ CCCC ║",
973
+ "║ DDDD ║ EEEE ║ FFFF ║",
974
+ "║ GGGG ║ HHHH ║ IIII ║",
975
+ "╚══════╩══════╩══════╝",
976
+ ''
977
+ ].join($/)
978
+ ).to_stdout
979
+ end
980
+
981
+ context "and when `separate_rows: true` is given" do
982
+ it "must print the header and add separator line between each row" do
983
+ expect {
984
+ subject.print_table(table, border: :double_line,
985
+ header: header,
986
+ separate_rows: true)
987
+ }.to output(
988
+ [
989
+ "╔══════╦══════╦══════╗",
990
+ "║ A ║ B ║ C ║",
991
+ "╠══════╬══════╬══════╣",
992
+ "║ AAAA ║ BBBB ║ CCCC ║",
993
+ "╠══════╬══════╬══════╣",
994
+ "║ DDDD ║ EEEE ║ FFFF ║",
995
+ "╠══════╬══════╬══════╣",
996
+ "║ GGGG ║ HHHH ║ IIII ║",
997
+ "╚══════╩══════╩══════╝",
998
+ ''
999
+ ].join($/)
1000
+ ).to_stdout
1001
+ end
1002
+ end
1003
+
1004
+ context "and when `justify_header: :left` is given" do
1005
+ it "must left justify the header cells" do
1006
+ expect {
1007
+ subject.print_table(table, border: :double_line,
1008
+ header: header,
1009
+ justify_header: :left)
1010
+ }.to output(
1011
+ [
1012
+ "╔══════╦══════╦══════╗",
1013
+ "║ A ║ B ║ C ║",
1014
+ "╠══════╬══════╬══════╣",
1015
+ "║ AAAA ║ BBBB ║ CCCC ║",
1016
+ "║ DDDD ║ EEEE ║ FFFF ║",
1017
+ "║ GGGG ║ HHHH ║ IIII ║",
1018
+ "╚══════╩══════╩══════╝",
1019
+ ''
1020
+ ].join($/)
1021
+ ).to_stdout
1022
+ end
1023
+ end
1024
+
1025
+ context "and when `justify_header: :right` is given" do
1026
+ it "must right justify the header cells" do
1027
+ expect {
1028
+ subject.print_table(table, border: :double_line,
1029
+ header: header,
1030
+ justify_header: :right)
1031
+ }.to output(
1032
+ [
1033
+ "╔══════╦══════╦══════╗",
1034
+ "║ A ║ B ║ C ║",
1035
+ "╠══════╬══════╬══════╣",
1036
+ "║ AAAA ║ BBBB ║ CCCC ║",
1037
+ "║ DDDD ║ EEEE ║ FFFF ║",
1038
+ "║ GGGG ║ HHHH ║ IIII ║",
1039
+ "╚══════╩══════╩══════╝",
1040
+ ''
1041
+ ].join($/)
1042
+ ).to_stdout
1043
+ end
1044
+ end
1045
+
1046
+ context "and when `justify_header: :center` is given" do
1047
+ it "must center justify the header cells" do
1048
+ expect {
1049
+ subject.print_table(table, border: :double_line,
1050
+ header: header,
1051
+ justify_header: :center)
1052
+ }.to output(
1053
+ [
1054
+ "╔══════╦══════╦══════╗",
1055
+ "║ A ║ B ║ C ║",
1056
+ "╠══════╬══════╬══════╣",
1057
+ "║ AAAA ║ BBBB ║ CCCC ║",
1058
+ "║ DDDD ║ EEEE ║ FFFF ║",
1059
+ "║ GGGG ║ HHHH ║ IIII ║",
1060
+ "╚══════╩══════╩══════╝",
1061
+ ''
1062
+ ].join($/)
1063
+ ).to_stdout
1064
+ end
1065
+ end
1066
+ end
1067
+ end
1068
+ end
1069
+ end