tty-table 0.11.0 → 0.12.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (144) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +13 -0
  3. data/LICENSE.txt +1 -1
  4. data/README.md +104 -98
  5. data/lib/tty-table.rb +1 -1
  6. data/lib/tty/table.rb +70 -34
  7. data/lib/tty/table/alignment_set.rb +1 -3
  8. data/lib/tty/table/border.rb +50 -57
  9. data/lib/tty/table/border/ascii.rb +16 -16
  10. data/lib/tty/table/border/null.rb +19 -22
  11. data/lib/tty/table/border/row_line.rb +3 -2
  12. data/lib/tty/table/border/unicode.rb +16 -16
  13. data/lib/tty/table/border_dsl.rb +24 -22
  14. data/lib/tty/table/border_options.rb +32 -27
  15. data/lib/tty/table/column_constraint.rb +12 -9
  16. data/lib/tty/table/columns.rb +6 -6
  17. data/lib/tty/table/field.rb +46 -28
  18. data/lib/tty/table/header.rb +5 -5
  19. data/lib/tty/table/indentation.rb +7 -4
  20. data/lib/tty/table/operation/alignment.rb +1 -1
  21. data/lib/tty/table/operation/escape.rb +1 -2
  22. data/lib/tty/table/operation/padding.rb +1 -1
  23. data/lib/tty/table/operation/truncation.rb +1 -1
  24. data/lib/tty/table/operation/wrapped.rb +1 -1
  25. data/lib/tty/table/options.rb +1 -1
  26. data/lib/tty/table/orientation.rb +4 -4
  27. data/lib/tty/table/renderer.rb +8 -8
  28. data/lib/tty/table/renderer/ascii.rb +2 -2
  29. data/lib/tty/table/renderer/basic.rb +37 -40
  30. data/lib/tty/table/renderer/unicode.rb +1 -1
  31. data/lib/tty/table/row.rb +7 -7
  32. data/lib/tty/table/validatable.rb +23 -7
  33. data/lib/tty/table/version.rb +1 -1
  34. metadata +23 -272
  35. data/Rakefile +0 -10
  36. data/examples/alignment.rb +0 -10
  37. data/examples/basic.rb +0 -6
  38. data/examples/orientation.rb +0 -13
  39. data/examples/padding.rb +0 -15
  40. data/examples/resize.rb +0 -15
  41. data/spec/spec_helper.rb +0 -50
  42. data/spec/unit/access_spec.rb +0 -84
  43. data/spec/unit/add_row_spec.rb +0 -26
  44. data/spec/unit/alignment_set/each_spec.rb +0 -15
  45. data/spec/unit/alignment_set/new_spec.rb +0 -25
  46. data/spec/unit/alignment_set/to_ary_spec.rb +0 -12
  47. data/spec/unit/alignment_spec.rb +0 -69
  48. data/spec/unit/border/ascii/rendering_spec.rb +0 -88
  49. data/spec/unit/border/new_spec.rb +0 -25
  50. data/spec/unit/border/null/rendering_spec.rb +0 -128
  51. data/spec/unit/border/options/from_spec.rb +0 -36
  52. data/spec/unit/border/options/new_spec.rb +0 -12
  53. data/spec/unit/border/unicode/rendering_spec.rb +0 -61
  54. data/spec/unit/border_options/new_spec.rb +0 -18
  55. data/spec/unit/border_options/update_spec.rb +0 -16
  56. data/spec/unit/column_constraint/enforce_spec.rb +0 -68
  57. data/spec/unit/column_constraint/widths_spec.rb +0 -33
  58. data/spec/unit/columns/extract_widths_spec.rb +0 -41
  59. data/spec/unit/columns/total_width_spec.rb +0 -11
  60. data/spec/unit/columns/widths_from_spec.rb +0 -49
  61. data/spec/unit/data_spec.rb +0 -10
  62. data/spec/unit/each_spec.rb +0 -24
  63. data/spec/unit/each_with_index_spec.rb +0 -49
  64. data/spec/unit/empty_spec.rb +0 -21
  65. data/spec/unit/eql_spec.rb +0 -32
  66. data/spec/unit/field/equality_spec.rb +0 -49
  67. data/spec/unit/field/length_spec.rb +0 -33
  68. data/spec/unit/field/lines_spec.rb +0 -19
  69. data/spec/unit/field/new_spec.rb +0 -27
  70. data/spec/unit/field/width_spec.rb +0 -21
  71. data/spec/unit/filter_spec.rb +0 -20
  72. data/spec/unit/header/call_spec.rb +0 -28
  73. data/spec/unit/header/color_spec.rb +0 -17
  74. data/spec/unit/header/equality_spec.rb +0 -49
  75. data/spec/unit/header/height_spec.rb +0 -25
  76. data/spec/unit/header/new_spec.rb +0 -13
  77. data/spec/unit/header/set_spec.rb +0 -18
  78. data/spec/unit/header/to_ary_spec.rb +0 -12
  79. data/spec/unit/header_spec.rb +0 -11
  80. data/spec/unit/indentation/indent_spec.rb +0 -15
  81. data/spec/unit/new_spec.rb +0 -71
  82. data/spec/unit/operation/alignment/call_spec.rb +0 -37
  83. data/spec/unit/operation/escape/call_spec.rb +0 -13
  84. data/spec/unit/operation/filter/call_spec.rb +0 -14
  85. data/spec/unit/operation/truncation/call_spec.rb +0 -28
  86. data/spec/unit/operation/wrapped/call_spec.rb +0 -38
  87. data/spec/unit/operations/new_spec.rb +0 -28
  88. data/spec/unit/options/access_spec.rb +0 -12
  89. data/spec/unit/options_spec.rb +0 -23
  90. data/spec/unit/orientation_spec.rb +0 -143
  91. data/spec/unit/padding_spec.rb +0 -115
  92. data/spec/unit/properties_spec.rb +0 -23
  93. data/spec/unit/render_repeat_spec.rb +0 -39
  94. data/spec/unit/render_spec.rb +0 -61
  95. data/spec/unit/render_with_spec.rb +0 -104
  96. data/spec/unit/renderer/ascii/coloring_spec.rb +0 -83
  97. data/spec/unit/renderer/ascii/indentation_spec.rb +0 -39
  98. data/spec/unit/renderer/ascii/multiline_spec.rb +0 -99
  99. data/spec/unit/renderer/ascii/padding_spec.rb +0 -115
  100. data/spec/unit/renderer/ascii/render_spec.rb +0 -66
  101. data/spec/unit/renderer/ascii/resizing_spec.rb +0 -112
  102. data/spec/unit/renderer/ascii/separator_spec.rb +0 -38
  103. data/spec/unit/renderer/basic/alignment_spec.rb +0 -86
  104. data/spec/unit/renderer/basic/coloring_spec.rb +0 -59
  105. data/spec/unit/renderer/basic/extract_column_widths_spec.rb +0 -26
  106. data/spec/unit/renderer/basic/filter_spec.rb +0 -51
  107. data/spec/unit/renderer/basic/indentation_spec.rb +0 -46
  108. data/spec/unit/renderer/basic/multiline_spec.rb +0 -72
  109. data/spec/unit/renderer/basic/new_spec.rb +0 -24
  110. data/spec/unit/renderer/basic/options_spec.rb +0 -50
  111. data/spec/unit/renderer/basic/padding_spec.rb +0 -74
  112. data/spec/unit/renderer/basic/render_spec.rb +0 -55
  113. data/spec/unit/renderer/basic/resizing_spec.rb +0 -94
  114. data/spec/unit/renderer/basic/separator_spec.rb +0 -41
  115. data/spec/unit/renderer/basic/single_row_separator_spec.rb +0 -80
  116. data/spec/unit/renderer/basic/truncation_spec.rb +0 -33
  117. data/spec/unit/renderer/basic/wrapping_spec.rb +0 -38
  118. data/spec/unit/renderer/border_spec.rb +0 -102
  119. data/spec/unit/renderer/render_spec.rb +0 -34
  120. data/spec/unit/renderer/select_spec.rb +0 -20
  121. data/spec/unit/renderer/unicode/coloring_spec.rb +0 -68
  122. data/spec/unit/renderer/unicode/indentation_spec.rb +0 -39
  123. data/spec/unit/renderer/unicode/padding_spec.rb +0 -59
  124. data/spec/unit/renderer/unicode/render_spec.rb +0 -66
  125. data/spec/unit/renderer/unicode/separator_spec.rb +0 -36
  126. data/spec/unit/renderer_spec.rb +0 -17
  127. data/spec/unit/rotate_spec.rb +0 -84
  128. data/spec/unit/row/access_spec.rb +0 -23
  129. data/spec/unit/row/call_spec.rb +0 -43
  130. data/spec/unit/row/data_spec.rb +0 -24
  131. data/spec/unit/row/each_spec.rb +0 -29
  132. data/spec/unit/row/equality_spec.rb +0 -71
  133. data/spec/unit/row/height_spec.rb +0 -25
  134. data/spec/unit/row/new_spec.rb +0 -39
  135. data/spec/unit/row/to_ary_spec.rb +0 -12
  136. data/spec/unit/to_s_spec.rb +0 -56
  137. data/spec/unit/transformation/extract_tuples_spec.rb +0 -33
  138. data/spec/unit/utf_spec.rb +0 -31
  139. data/spec/unit/validatable/validate_options_spec.rb +0 -31
  140. data/spec/unit/validatable_spec.rb +0 -30
  141. data/tasks/console.rake +0 -11
  142. data/tasks/coverage.rake +0 -11
  143. data/tasks/spec.rake +0 -29
  144. data/tty-table.gemspec +0 -41
@@ -1,99 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe TTY::Table::Renderer::ASCII, 'multiline content' do
4
- context 'with escaping' do
5
- it "renders multiline as single line" do
6
- rows = [["First", '1'], ["Multiline\nContent", '2'], ["Third", '3']]
7
- table = TTY::Table.new rows
8
- renderer = described_class.new(table, multiline: false)
9
- expect(renderer.render).to eq unindent(<<-EOS)
10
- +------------------+-+
11
- |First |1|
12
- |Multiline\\nContent|2|
13
- |Third |3|
14
- +------------------+-+
15
- EOS
16
- end
17
-
18
- it "truncates multiline content" do
19
- rows = [["First", '1'], ["Multiline\nContent", '2'], ["Third", '3']]
20
- table = TTY::Table.new rows
21
- renderer = described_class.new(table, multiline: false, column_widths: [8,1])
22
- expect(renderer.render).to eq unindent(<<-EOS)
23
- +--------+-+
24
- |First |1|
25
- |Multil… |2|
26
- |Third |3|
27
- +--------+-+
28
- EOS
29
- end
30
-
31
- it "renders correctly multiline header as single line" do
32
- header = ["Multi\nHeader", "header2"]
33
- rows = [["First", '1'], ["Multiline\nContent", '2'], ["Third", '3']]
34
- table = TTY::Table.new header, rows
35
- renderer = described_class.new(table, multiline: false)
36
- expect(renderer.render).to eq unindent(<<-EOS)
37
- +------------------+-------+
38
- |Multi\\nHeader |header2|
39
- +------------------+-------+
40
- |First |1 |
41
- |Multiline\\nContent|2 |
42
- |Third |3 |
43
- +------------------+-------+
44
- EOS
45
- end
46
- end
47
-
48
- context 'without escaping' do
49
- it "renders multiline" do
50
- rows = [["First", '1'], ["Multi\nLine\nContent", '2'], ["Third", '3']]
51
- table = TTY::Table.new rows
52
- renderer = described_class.new(table, multiline: true)
53
- expect(renderer.render).to eq unindent(<<-EOS)
54
- +-------+-+
55
- |First |1|
56
- |Multi |2|
57
- |Line | |
58
- |Content| |
59
- |Third |3|
60
- +-------+-+
61
- EOS
62
- end
63
-
64
- it "wraps multiline" do
65
- rows = [["First", '1'], ["Multi\nLine\nContent", '2'], ["Third", '3']]
66
- table = TTY::Table.new rows
67
- renderer = described_class.new(table, multiline: true, column_widths: [5,1])
68
- expect(renderer.render).to eq unindent(<<-EOS)
69
- +-----+-+
70
- |First|1|
71
- |Multi|2|
72
- |Line | |
73
- |Conte| |
74
- |nt | |
75
- |Third|3|
76
- +-----+-+
77
- EOS
78
- end
79
-
80
- it "renders multilne header" do
81
- header = ["Multi\nHeader", "header2"]
82
- rows = [["First", '1'], ["Multi\nLine\nContent", '2'], ["Third", '3']]
83
- table = TTY::Table.new header, rows
84
- renderer = described_class.new(table, multiline: true)
85
- expect(renderer.render).to eq unindent(<<-EOS)
86
- +-------+-------+
87
- |Multi |header2|
88
- |Header | |
89
- +-------+-------+
90
- |First |1 |
91
- |Multi |2 |
92
- |Line | |
93
- |Content| |
94
- |Third |3 |
95
- +-------+-------+
96
- EOS
97
- end
98
- end
99
- end
@@ -1,115 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe TTY::Table::Renderer::ASCII, 'padding' do
4
- let(:header) { ['Field', 'Type', 'Null', 'Key', 'Default', 'Extra'] }
5
- let(:rows) { [['id', 'int(11)', 'YES', 'nil', 'NULL', '']] }
6
- let(:table) { TTY::Table.new(header, rows) }
7
-
8
- subject(:renderer) { described_class.new(table, options) }
9
-
10
- context 'with left & right padding' do
11
- let(:options) { {padding: [0,1,0,1]} }
12
-
13
- it 'pads each field' do
14
- expect(renderer.render).to eql <<-EOS.chomp
15
- +-------+---------+------+-----+---------+-------+
16
- | Field | Type | Null | Key | Default | Extra |
17
- +-------+---------+------+-----+---------+-------+
18
- | id | int(11) | YES | nil | NULL | |
19
- +-------+---------+------+-----+---------+-------+
20
- EOS
21
- end
22
- end
23
-
24
- context 'with top & bottom padding' do
25
- let(:options) { {padding: [1,0,1,0], multiline: true} }
26
-
27
- it 'pads each field' do
28
- expect(renderer.render).to eql <<-EOS.chomp
29
- +-----+-------+----+---+-------+-----+
30
- | | | | | | |
31
- |Field|Type |Null|Key|Default|Extra|
32
- | | | | | | |
33
- +-----+-------+----+---+-------+-----+
34
- | | | | | | |
35
- |id |int(11)|YES |nil|NULL | |
36
- | | | | | | |
37
- +-----+-------+----+---+-------+-----+
38
- EOS
39
- end
40
- end
41
-
42
- context 'with full padding' do
43
- let(:options) { {padding: [1,1,1,1], multiline: true} }
44
-
45
- it 'pads each field' do
46
- expect(renderer.render).to eql unindent(<<-EOS)
47
- +-------+---------+------+-----+---------+-------+
48
- | | | | | | |
49
- | Field | Type | Null | Key | Default | Extra |
50
- | | | | | | |
51
- +-------+---------+------+-----+---------+-------+
52
- | | | | | | |
53
- | id | int(11) | YES | nil | NULL | |
54
- | | | | | | |
55
- +-------+---------+------+-----+---------+-------+
56
- EOS
57
- end
58
- end
59
-
60
- context "with multiline content padding" do
61
- it "pads around fields" do
62
- padding = [1,2,1,2]
63
- table = TTY::Table.new header: ['header1', 'header2']
64
- table << ["a1\na1\na1",'a2']
65
- table << ["b1","b2\nb2"]
66
- renderer = described_class.new(table, padding: padding, multiline: true)
67
- expect(renderer.render).to eql unindent(<<-EOS)
68
- +-----------+-----------+
69
- | | |
70
- | header1 | header2 |
71
- | | |
72
- +-----------+-----------+
73
- | | |
74
- | a1 | a2 |
75
- | a1 | |
76
- | a1 | |
77
- | | |
78
- | | |
79
- | b1 | b2 |
80
- | | b2 |
81
- | | |
82
- +-----------+-----------+
83
- EOS
84
- end
85
- end
86
-
87
- context "with resize option" do
88
- it "pads fields correctly" do
89
- table = TTY::Table.new(header: [ "Column 1", "Column 2", "Column 3"]) do |t|
90
- t << [ "11", "12", "13" ]
91
- t << [ "21", "22", "23" ]
92
- t << [ "31", "32", "33" ]
93
- end
94
-
95
- renderer = TTY::Table::Renderer::ASCII.new(table, padding: [1,1,1,1], resize: true, width: 50)
96
- expect(renderer.render).to eql unindent(<<-EOS)
97
- +----------------+---------------+---------------+
98
- | | | |
99
- | Column 1 | Column 2 | Column 3 |
100
- | | | |
101
- +----------------+---------------+---------------+
102
- | | | |
103
- | 11 | 12 | 13 |
104
- | | | |
105
- | | | |
106
- | 21 | 22 | 23 |
107
- | | | |
108
- | | | |
109
- | 31 | 32 | 33 |
110
- | | | |
111
- +----------------+---------------+---------------+
112
- EOS
113
- end
114
- end
115
- end
@@ -1,66 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe TTY::Table::Renderer::ASCII, '#render' do
4
- let(:header) { ['h1', 'h2', 'h3'] }
5
- let(:rows) { [['a1', 'a2', 'a3'], ['b1', 'b2', 'b3']] }
6
- let(:table) { TTY::Table.new header, rows }
7
-
8
- subject(:renderer) { described_class.new(table) }
9
-
10
- context 'only rows' do
11
- let(:table) { TTY::Table.new rows }
12
-
13
- it 'display table rows' do
14
- expect(renderer.render).to eq unindent(<<-EOS)
15
- +--+--+--+
16
- |a1|a2|a3|
17
- |b1|b2|b3|
18
- +--+--+--+
19
- EOS
20
- end
21
- end
22
-
23
- context 'with header' do
24
- it 'displays table with header' do
25
- expect(renderer.render).to eq unindent(<<-EOS)
26
- +--+--+--+
27
- |h1|h2|h3|
28
- +--+--+--+
29
- |a1|a2|a3|
30
- |b1|b2|b3|
31
- +--+--+--+
32
- EOS
33
- end
34
- end
35
-
36
- context 'with short header' do
37
- let(:header) { ['h1', 'h2'] }
38
- let(:rows) { [['aaa1', 'a2'], ['b1', 'bb1']] }
39
-
40
- it 'displays table according to widths' do
41
- expect(renderer.render).to eq unindent(<<-EOS)
42
- +----+---+
43
- |h1 |h2 |
44
- +----+---+
45
- |aaa1|a2 |
46
- |b1 |bb1|
47
- +----+---+
48
- EOS
49
- end
50
- end
51
-
52
- context 'with long header' do
53
- let(:header) { ['header1', 'header2', 'header3'] }
54
-
55
- it 'header greater than row sizes' do
56
- expect(renderer.render).to eq unindent(<<-EOS)
57
- +-------+-------+-------+
58
- |header1|header2|header3|
59
- +-------+-------+-------+
60
- |a1 |a2 |a3 |
61
- |b1 |b2 |b3 |
62
- +-------+-------+-------+
63
- EOS
64
- end
65
- end
66
- end
@@ -1,112 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe TTY::Table::Renderer::ASCII, 'resizing' do
4
- let(:header) { ['h1', 'h2', 'h3'] }
5
- let(:rows) { [['a1', 'a2', 'a3'], ['b1', 'b2', 'b3']] }
6
- let(:table) { TTY::Table.new(header, rows) }
7
-
8
- subject(:renderer) { described_class.new(table, options) }
9
-
10
- context 'when expanding' do
11
- context 'even columns' do
12
- let(:options) { {width: 16, resize: true} }
13
-
14
- it 'resizes each column' do
15
- expect(renderer.render).to eql unindent(<<-EOS)
16
- +----+----+----+
17
- |h1 |h2 |h3 |
18
- +----+----+----+
19
- |a1 |a2 |a3 |
20
- |b1 |b2 |b3 |
21
- +----+----+----+
22
- EOS
23
- end
24
- end
25
-
26
- context 'even columns with extra width' do
27
- let(:header) { ['h1', 'h2', 'h3', 'h4'] }
28
- let(:rows) { [['a1','a2','a3','a4'], ['b1','b2','b3','b4']] }
29
- let(:options) { {width: 21, resize: true} }
30
-
31
- it 'resizes each column' do
32
- expect(renderer.render).to eql unindent(<<-EOS)
33
- +----+----+----+----+
34
- |h1 |h2 |h3 |h4 |
35
- +----+----+----+----+
36
- |a1 |a2 |a3 |a4 |
37
- |b1 |b2 |b3 |b4 |
38
- +----+----+----+----+
39
- EOS
40
- end
41
- end
42
-
43
- context 'uneven columns' do
44
- let(:header) { ['h1', 'h2', 'h3'] }
45
- let(:rows) { [['aaa1', 'aa2', 'aaaaaaa3'], ['b1', 'b2', 'b3']] }
46
- let(:options) { {width: 32, resize: true} }
47
-
48
- it 'resizes each column' do
49
- expect(renderer.render).to eql unindent(<<-EOS)
50
- +---------+-------+------------+
51
- |h1 |h2 |h3 |
52
- +---------+-------+------------+
53
- |aaa1 |aa2 |aaaaaaa3 |
54
- |b1 |b2 |b3 |
55
- +---------+-------+------------+
56
- EOS
57
- end
58
- end
59
- end
60
-
61
- context 'when shrinking' do
62
- let(:header) { ['head1', 'head2'] }
63
- let(:rows) { [['aaaa1','aaaa2',], ['bbbb1','bbbb2']] }
64
-
65
- context 'even columns' do
66
- let(:options) { {width: 9, resize: true} }
67
-
68
- it 'resizes each column' do
69
- expect(renderer.render).to eql unindent(<<-EOS)
70
- +---+---+
71
- |h… |h… |
72
- +---+---+
73
- |a… |a… |
74
- |b… |b… |
75
- +---+---+
76
- EOS
77
- end
78
- end
79
-
80
- context 'even columns with extra width' do
81
- let(:options) { {width: 10, resize: true} }
82
-
83
- it 'resizes each column' do
84
- expect(renderer.render).to eql unindent(<<-EOS)
85
- +----+---+
86
- |he… |h… |
87
- +----+---+
88
- |aa… |a… |
89
- |bb… |b… |
90
- +----+---+
91
- EOS
92
- end
93
- end
94
-
95
- context 'uneven columns' do
96
- let(:header) { ['head1', 'head2', 'head3'] }
97
- let(:rows) { [['aaa1', 'aa2', 'aaaaaaa3'], ['b1', 'b2', 'b3']] }
98
- let(:options) { {width: 15, resize: true} }
99
-
100
- it 'resizes each column' do
101
- expect(renderer.render).to eql unindent(<<-EOS)
102
- +---+---+-----+
103
- |h… |h… |head3|
104
- +---+---+-----+
105
- |a… |aa2|aaa… |
106
- |b1 |b2 |b3 |
107
- +---+---+-----+
108
- EOS
109
- end
110
- end
111
- end
112
- end
@@ -1,38 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe TTY::Table::Renderer::ASCII, 'with separator' do
4
- let(:header) { ['h1', 'h2', 'h3'] }
5
- let(:rows) { [['a1', 'a2', 'a3'], ['b1', 'b2', 'b3']] }
6
- let(:table) { TTY::Table.new(header, rows) }
7
-
8
- let(:object) { described_class.new table }
9
-
10
- subject(:renderer) { object }
11
-
12
- context 'when ascii' do
13
- it "renders each row" do
14
- renderer.border.separator = :each_row
15
- expect(renderer.render).to eq unindent(<<-EOS)
16
- +--+--+--+
17
- |h1|h2|h3|
18
- +--+--+--+
19
- |a1|a2|a3|
20
- +--+--+--+
21
- |b1|b2|b3|
22
- +--+--+--+
23
- EOS
24
- end
25
-
26
- it "will not the default separator if individual separators are specified" do
27
- renderer.border.separator = [1]
28
- expect(renderer.render).to eq unindent(<<-EOS)
29
- +--+--+--+
30
- |h1|h2|h3|
31
- |a1|a2|a3|
32
- +--+--+--+
33
- |b1|b2|b3|
34
- +--+--+--+
35
- EOS
36
- end
37
- end
38
- end
@@ -1,86 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe TTY::Table::Renderer::Basic, 'alignment' do
4
- let(:header) { ['h1', 'h2', 'h3'] }
5
- let(:rows) { [['a1', 'a2', 'a3'], ['b1', 'b2', 'b3']] }
6
- let(:options) { { alignments: alignments } }
7
- let(:table) { TTY::Table.new(header, rows) }
8
-
9
- subject(:renderer) { described_class.new table, options }
10
-
11
- context 'with default' do
12
- let(:header) { ['h1', 'h2'] }
13
- let(:rows) { [['aaaaa', 'a'], ['b', 'bbbbb']] }
14
- let(:alignments) { nil }
15
-
16
- it 'aligns left by default' do
17
- expect(renderer.render).to eql unindent(<<-EOS)
18
- h1 h2
19
- aaaaa a
20
- b bbbbb
21
- EOS
22
- end
23
- end
24
-
25
- context 'with different headers' do
26
- let(:header) { ['header1', 'head2', 'h3'] }
27
- let(:alignments) { [:left, :center, :right] }
28
-
29
- it 'aligns headers' do
30
- expect(renderer.render).to eql unindent(<<-EOS)
31
- header1 head2 h3
32
- a1 a2 a3
33
- b1 b2 b3
34
- EOS
35
- end
36
- end
37
-
38
- context 'with different aligns' do
39
- let(:header) { nil }
40
- let(:rows) { [['aaaaa', 'a'], ['b', 'bbbbb']] }
41
- let(:alignments) { [:left, :right] }
42
-
43
- it 'aligns table rows' do
44
- expect(renderer.render.to_s).to eql unindent(<<-EOS)
45
- aaaaa a
46
- b bbbbb
47
- EOS
48
- end
49
- end
50
-
51
- context 'with individual field aligns' do
52
- let(:header) { ['header1', 'header2', 'header3'] }
53
- let(:alignments) { [:left, :center, :right] }
54
- let(:options) { {alignments: alignments} }
55
- let(:table) {
56
- TTY::Table.new header: header do |t|
57
- t << ['a1', 'a2', 'a3']
58
- t << ['b1', {value: 'b2', alignment: :right}, 'b3']
59
- t << ['c1', 'c2', {value: 'c3', alignment: :center}]
60
- end
61
- }
62
-
63
- it "takes individual fields over global aligns" do
64
- expect(renderer.render).to eq unindent(<<-EOS)
65
- header1 header2 header3
66
- a1 a2 a3
67
- b1 b2 b3
68
- c1 c2 c3
69
- EOS
70
- end
71
- end
72
-
73
- context 'with aligned header' do
74
- let(:rows) { [['aaaaa1', 'a2', 'aaa3'], ['b1', 'bbbb2', 'bb3']] }
75
- let(:header) {['h1', {value: 'h2', alignment: :right}, {value: 'h3', alignment: :center}] }
76
- let(:options) { { renderer: :basic } }
77
-
78
- it "aligns headres" do
79
- expect(renderer.render).to eq unindent(<<-EOS)
80
- h1 h2 h3
81
- aaaaa1 a2 aaa3
82
- b1 bbbb2 bb3
83
- EOS
84
- end
85
- end
86
- end