tty-table 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (148) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +14 -0
  3. data/.rspec +3 -0
  4. data/.ruby-version +1 -0
  5. data/.travis.yml +22 -0
  6. data/Gemfile +19 -0
  7. data/LICENSE.txt +22 -0
  8. data/README.md +389 -0
  9. data/Rakefile +8 -0
  10. data/benchmarks/speed.rb +36 -0
  11. data/lib/tty/table/border/ascii.rb +32 -0
  12. data/lib/tty/table/border/null.rb +37 -0
  13. data/lib/tty/table/border/row_line.rb +18 -0
  14. data/lib/tty/table/border/unicode.rb +32 -0
  15. data/lib/tty/table/border.rb +219 -0
  16. data/lib/tty/table/border_dsl.rb +251 -0
  17. data/lib/tty/table/border_options.rb +53 -0
  18. data/lib/tty/table/column_set.rb +121 -0
  19. data/lib/tty/table/columns.rb +170 -0
  20. data/lib/tty/table/empty.rb +26 -0
  21. data/lib/tty/table/error.rb +39 -0
  22. data/lib/tty/table/field.rb +139 -0
  23. data/lib/tty/table/header.rb +163 -0
  24. data/lib/tty/table/indentation.rb +52 -0
  25. data/lib/tty/table/operation/alignment_set.rb +103 -0
  26. data/lib/tty/table/operation/escape.rb +30 -0
  27. data/lib/tty/table/operation/filter.rb +34 -0
  28. data/lib/tty/table/operation/padding.rb +95 -0
  29. data/lib/tty/table/operation/truncation.rb +41 -0
  30. data/lib/tty/table/operation/wrapped.rb +43 -0
  31. data/lib/tty/table/operations.rb +69 -0
  32. data/lib/tty/table/options.rb +30 -0
  33. data/lib/tty/table/orientation/horizontal.rb +48 -0
  34. data/lib/tty/table/orientation/vertical.rb +38 -0
  35. data/lib/tty/table/orientation.rb +57 -0
  36. data/lib/tty/table/padder.rb +180 -0
  37. data/lib/tty/table/renderer/ascii.rb +16 -0
  38. data/lib/tty/table/renderer/basic.rb +294 -0
  39. data/lib/tty/table/renderer/color.rb +12 -0
  40. data/lib/tty/table/renderer/unicode.rb +21 -0
  41. data/lib/tty/table/renderer.rb +101 -0
  42. data/lib/tty/table/row.rb +248 -0
  43. data/lib/tty/table/transformation.rb +39 -0
  44. data/lib/tty/table/validatable.rb +64 -0
  45. data/lib/tty/table/version.rb +7 -0
  46. data/lib/tty/table.rb +469 -0
  47. data/lib/tty-table.rb +48 -0
  48. data/spec/spec_helper.rb +51 -0
  49. data/spec/unit/access_spec.rb +86 -0
  50. data/spec/unit/add_row_spec.rb +28 -0
  51. data/spec/unit/border/ascii/rendering_spec.rb +90 -0
  52. data/spec/unit/border/new_spec.rb +27 -0
  53. data/spec/unit/border/null/rendering_spec.rb +130 -0
  54. data/spec/unit/border/options/from_spec.rb +38 -0
  55. data/spec/unit/border/options/new_spec.rb +14 -0
  56. data/spec/unit/border/unicode/rendering_spec.rb +63 -0
  57. data/spec/unit/border_options/new_spec.rb +20 -0
  58. data/spec/unit/border_options/update_spec.rb +18 -0
  59. data/spec/unit/column_set/extract_widths_spec.rb +15 -0
  60. data/spec/unit/column_set/total_width_spec.rb +15 -0
  61. data/spec/unit/column_set/widths_from_spec.rb +51 -0
  62. data/spec/unit/columns/enforce_spec.rb +67 -0
  63. data/spec/unit/columns/widths_spec.rb +35 -0
  64. data/spec/unit/data_spec.rb +14 -0
  65. data/spec/unit/each_spec.rb +41 -0
  66. data/spec/unit/each_with_index_spec.rb +57 -0
  67. data/spec/unit/empty_spec.rb +23 -0
  68. data/spec/unit/eql_spec.rb +34 -0
  69. data/spec/unit/field/equality_spec.rb +51 -0
  70. data/spec/unit/field/length_spec.rb +21 -0
  71. data/spec/unit/field/lines_spec.rb +21 -0
  72. data/spec/unit/field/new_spec.rb +29 -0
  73. data/spec/unit/field/width_spec.rb +23 -0
  74. data/spec/unit/filter_spec.rb +23 -0
  75. data/spec/unit/header/call_spec.rb +30 -0
  76. data/spec/unit/header/color_spec.rb +19 -0
  77. data/spec/unit/header/equality_spec.rb +51 -0
  78. data/spec/unit/header/height_spec.rb +27 -0
  79. data/spec/unit/header/new_spec.rb +25 -0
  80. data/spec/unit/header/set_spec.rb +20 -0
  81. data/spec/unit/header/to_ary_spec.rb +14 -0
  82. data/spec/unit/header_spec.rb +13 -0
  83. data/spec/unit/indentation/insert_indent_spec.rb +27 -0
  84. data/spec/unit/initialize_spec.rb +88 -0
  85. data/spec/unit/operation/alignment_set/call_spec.rb +39 -0
  86. data/spec/unit/operation/alignment_set/each_spec.rb +17 -0
  87. data/spec/unit/operation/alignment_set/new_spec.rb +27 -0
  88. data/spec/unit/operation/alignment_set/to_ary_spec.rb +14 -0
  89. data/spec/unit/operation/escape/call_spec.rb +16 -0
  90. data/spec/unit/operation/filter/call_spec.rb +17 -0
  91. data/spec/unit/operation/truncation/call_spec.rb +32 -0
  92. data/spec/unit/operation/wrapped/call_spec.rb +33 -0
  93. data/spec/unit/operations/new_spec.rb +30 -0
  94. data/spec/unit/options/access_spec.rb +14 -0
  95. data/spec/unit/options_spec.rb +25 -0
  96. data/spec/unit/orientation_spec.rb +145 -0
  97. data/spec/unit/padder/parse_spec.rb +45 -0
  98. data/spec/unit/padder/to_s_spec.rb +14 -0
  99. data/spec/unit/padding_spec.rb +120 -0
  100. data/spec/unit/properties_spec.rb +25 -0
  101. data/spec/unit/render_spec.rb +63 -0
  102. data/spec/unit/render_with_spec.rb +106 -0
  103. data/spec/unit/renderer/ascii/indentation_spec.rb +41 -0
  104. data/spec/unit/renderer/ascii/padding_spec.rb +61 -0
  105. data/spec/unit/renderer/ascii/render_spec.rb +68 -0
  106. data/spec/unit/renderer/ascii/resizing_spec.rb +114 -0
  107. data/spec/unit/renderer/ascii/separator_spec.rb +28 -0
  108. data/spec/unit/renderer/basic/alignment_spec.rb +88 -0
  109. data/spec/unit/renderer/basic/coloring_spec.rb +46 -0
  110. data/spec/unit/renderer/basic/extract_column_widths_spec.rb +28 -0
  111. data/spec/unit/renderer/basic/filter_spec.rb +53 -0
  112. data/spec/unit/renderer/basic/indentation_spec.rb +48 -0
  113. data/spec/unit/renderer/basic/multiline_content_spec.rb +135 -0
  114. data/spec/unit/renderer/basic/new_spec.rb +26 -0
  115. data/spec/unit/renderer/basic/options_spec.rb +52 -0
  116. data/spec/unit/renderer/basic/padding_spec.rb +52 -0
  117. data/spec/unit/renderer/basic/render_spec.rb +57 -0
  118. data/spec/unit/renderer/basic/resizing_spec.rb +96 -0
  119. data/spec/unit/renderer/basic/separator_spec.rb +43 -0
  120. data/spec/unit/renderer/basic/truncation_spec.rb +35 -0
  121. data/spec/unit/renderer/basic/wrapping_spec.rb +40 -0
  122. data/spec/unit/renderer/border_spec.rb +104 -0
  123. data/spec/unit/renderer/render_spec.rb +36 -0
  124. data/spec/unit/renderer/select_spec.rb +22 -0
  125. data/spec/unit/renderer/style_spec.rb +72 -0
  126. data/spec/unit/renderer/unicode/indentation_spec.rb +41 -0
  127. data/spec/unit/renderer/unicode/padding_spec.rb +61 -0
  128. data/spec/unit/renderer/unicode/render_spec.rb +68 -0
  129. data/spec/unit/renderer/unicode/separator_spec.rb +26 -0
  130. data/spec/unit/renderer_spec.rb +19 -0
  131. data/spec/unit/rotate_spec.rb +86 -0
  132. data/spec/unit/row/access_spec.rb +25 -0
  133. data/spec/unit/row/call_spec.rb +45 -0
  134. data/spec/unit/row/data_spec.rb +26 -0
  135. data/spec/unit/row/each_spec.rb +31 -0
  136. data/spec/unit/row/equality_spec.rb +73 -0
  137. data/spec/unit/row/height_spec.rb +27 -0
  138. data/spec/unit/row/new_spec.rb +41 -0
  139. data/spec/unit/row/to_ary_spec.rb +14 -0
  140. data/spec/unit/to_s_spec.rb +63 -0
  141. data/spec/unit/transformation/extract_tuples_spec.rb +35 -0
  142. data/spec/unit/validatable/validate_options_spec.rb +33 -0
  143. data/spec/unit/validatable_spec.rb +32 -0
  144. data/tasks/console.rake +10 -0
  145. data/tasks/coverage.rake +11 -0
  146. data/tasks/spec.rake +29 -0
  147. data/tty-table.gemspec +28 -0
  148. metadata +371 -0
@@ -0,0 +1,53 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe TTY::Table::Renderer::Basic, 'filter' do
6
+ let(:header) { ['h1', 'h2', 'h3'] }
7
+ let(:rows) { [['a1', 'a2', 'a3'], ['b1', 'b2', 'b3']] }
8
+ let(:table) { TTY::Table.new(header, rows) }
9
+
10
+ subject(:renderer) { described_class.new(table, {filter: filter}) }
11
+
12
+ context 'with header' do
13
+ context 'filtering only rows' do
14
+ let(:filter) { Proc.new { |val, row, col|
15
+ (col == 1 and row > 0) ? val.capitalize : val
16
+ }
17
+ }
18
+
19
+ it 'filters only rows' do
20
+ expect(renderer.render).to eq <<-EOS.normalize
21
+ h1 h2 h3
22
+ a1 A2 a3
23
+ b1 B2 b3
24
+ EOS
25
+ end
26
+ end
27
+
28
+ context 'filtering header and rows' do
29
+ let(:filter) { Proc.new { |val, row, col| col == 1 ? val.capitalize : val }}
30
+
31
+ it 'filters only rows' do
32
+ expect(renderer.render).to eq <<-EOS.normalize
33
+ h1 H2 h3
34
+ a1 A2 a3
35
+ b1 B2 b3
36
+ EOS
37
+ end
38
+ end
39
+ end
40
+
41
+ context 'without header' do
42
+ let(:header) { nil }
43
+
44
+ let(:filter) { Proc.new { |val, row, col| col == 1 ? val.capitalize : val } }
45
+
46
+ it 'filters only rows' do
47
+ expect(renderer.render).to eq <<-EOS.normalize
48
+ a1 A2 a3
49
+ b1 B2 b3
50
+ EOS
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,48 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe TTY::Table::Renderer::Basic, 'indentation' do
6
+ let(:header) { ['h1', 'h2', 'h3'] }
7
+ let(:rows) { [['a1', 'a2', 'a3'], ['b1', 'b2', 'b3']] }
8
+ let(:table) { TTY::Table.new(header, rows) }
9
+ let(:options) { {indent: indent } }
10
+
11
+ subject(:renderer) { described_class.new(table, options)}
12
+
13
+ context 'when default' do
14
+ let(:indent) { 0 }
15
+
16
+ it 'indents by value' do
17
+ expect(renderer.render).to eql <<-EOS.chomp
18
+ h1 h2 h3
19
+ a1 a2 a3
20
+ b1 b2 b3
21
+ EOS
22
+ end
23
+ end
24
+
25
+ context 'when custom' do
26
+ let(:indent) { 2 }
27
+
28
+ it 'indents by value' do
29
+ expect(renderer.render).to eql <<-EOS.chomp
30
+ h1 h2 h3
31
+ a1 a2 a3
32
+ b1 b2 b3
33
+ EOS
34
+ end
35
+ end
36
+
37
+ context 'when changed' do
38
+ let(:indent) { 2 }
39
+ let(:header) { ['h1', 'h2'] }
40
+ let(:rows) { [['a1', 'a2']] }
41
+
42
+ it 'changes indentation and reuses renderer' do
43
+ expect(renderer.render).to eq(" h1 h2\n a1 a2")
44
+ renderer.indent = 1
45
+ expect(renderer.render).to eq(" h1 h2\n a1 a2")
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,135 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe TTY::Table::Renderer::Basic, 'with multiline content' do
6
+ let(:header) { nil }
7
+ let(:object) { described_class }
8
+ let(:table) { TTY::Table.new header, rows }
9
+
10
+ subject(:renderer) { object.new table }
11
+
12
+ context 'with escaping' do
13
+ let(:rows) { [ ["First", '1'], ["Multiline\nContent", '2'], ["Third", '3']] }
14
+
15
+ context 'without border' do
16
+ it "renders single line" do
17
+ expect(table.render(multiline: false)).to eq <<-EOS.normalize
18
+ First 1
19
+ Multiline\\nContent 2
20
+ Third 3
21
+ EOS
22
+ end
23
+ end
24
+
25
+ context 'with column widths' do
26
+ it "renders single line" do
27
+ expect(table.render(multiline: false, column_widths: [8,1])).to eq <<-EOS.normalize
28
+ First 1
29
+ Multili… 2
30
+ Third 3
31
+ EOS
32
+ end
33
+ end
34
+
35
+ context 'with border' do
36
+ it "renders single line" do
37
+ expect(table.render(:ascii, multiline: false)).to eq <<-EOS.normalize
38
+ +------------------+-+
39
+ |First |1|
40
+ |Multiline\\nContent|2|
41
+ |Third |3|
42
+ +------------------+-+
43
+ EOS
44
+ end
45
+ end
46
+
47
+ context 'with header' do
48
+ let(:header) { ["Multi\nHeader", "header2"] }
49
+
50
+ it "renders header" do
51
+ expect(table.render(:ascii, multiline: false)).to eq <<-EOS.normalize
52
+ +------------------+-------+
53
+ |Multi\\nHeader |header2|
54
+ +------------------+-------+
55
+ |First |1 |
56
+ |Multiline\\nContent|2 |
57
+ |Third |3 |
58
+ +------------------+-------+
59
+ EOS
60
+ end
61
+ end
62
+ end
63
+
64
+ context 'without escaping' do
65
+ let(:rows) { [ ["First", '1'], ["Multi\nLine\nContent", '2'], ["Third", '3']] }
66
+
67
+ context 'without border' do
68
+ it "renders every line" do
69
+ expect(table.render(multiline: true)).to eq <<-EOS.normalize
70
+ First 1
71
+ Multi 2
72
+ Line
73
+ Content
74
+ Third 3
75
+ EOS
76
+ end
77
+ end
78
+
79
+ context 'with column widths' do
80
+ it "renders multiline" do
81
+ expect(table.render(multiline: true, column_widths: [8,1])).to eq <<-EOS.normalize
82
+ First 1
83
+ Multi 2
84
+ Line
85
+ Content
86
+ Third 3
87
+ EOS
88
+ end
89
+
90
+ it 'wraps multi line' do
91
+ expect(table.render(multiline: true, column_widths: [5,1])).to eq <<-EOS.normalize
92
+ First 1
93
+ Multi 2
94
+ Line
95
+ Conte
96
+ nt
97
+ Third 3
98
+ EOS
99
+ end
100
+ end
101
+
102
+ context 'with border' do
103
+ it "renders every line" do
104
+ expect(table.render(:ascii, multiline: true)).to eq <<-EOS.normalize
105
+ +-------+-+
106
+ |First |1|
107
+ |Multi |2|
108
+ |Line | |
109
+ |Content| |
110
+ |Third |3|
111
+ +-------+-+
112
+ EOS
113
+ end
114
+ end
115
+
116
+ context 'with header' do
117
+ let(:header) { ["Multi\nHeader", "header2"] }
118
+
119
+ it "renders header" do
120
+ expect(table.render(:ascii, multiline: true)).to eq <<-EOS.normalize
121
+ +-------+-------+
122
+ |Multi |header2|
123
+ |Header | |
124
+ +-------+-------+
125
+ |First |1 |
126
+ |Multi |2 |
127
+ |Line | |
128
+ |Content| |
129
+ |Third |3 |
130
+ +-------+-------+
131
+ EOS
132
+ end
133
+ end
134
+ end
135
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe TTY::Table::Renderer::Basic, '.new' do
6
+ let(:header) { ['h1'] }
7
+ let(:rows) { [['a1']] }
8
+
9
+ subject(:renderer) { described_class }
10
+
11
+ context 'without table' do
12
+ let(:table) { nil }
13
+
14
+ it {
15
+ expect {
16
+ renderer.new(table)
17
+ }.to raise_error(TTY::Table::ArgumentRequired)
18
+ }
19
+ end
20
+
21
+ context 'with table' do
22
+ let(:table) { TTY::Table.new(header, rows) }
23
+
24
+ it { expect { renderer.new(table) }.not_to raise_error }
25
+ end
26
+ end
@@ -0,0 +1,52 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe TTY::Table::Renderer::Basic, 'options' do
6
+ let(:rows) { [['a1', 'a2'], ['b1', 'b2']] }
7
+ let(:object) { described_class }
8
+ let(:table) { TTY::Table.new(rows) }
9
+ let(:widths) { nil }
10
+ let(:aligns) { [] }
11
+ let(:options) {
12
+ {
13
+ :column_widths => widths,
14
+ :column_aligns => aligns,
15
+ :renderer => :basic
16
+ }
17
+ }
18
+
19
+ subject(:renderer) { object.new table, options }
20
+
21
+ it { expect(renderer.border).to be_kind_of(TTY::Table::BorderOptions) }
22
+
23
+ it { expect(renderer.column_widths).to eql([2,2]) }
24
+
25
+ it { expect(renderer.column_aligns).to eql(aligns) }
26
+
27
+ it { expect(renderer.column_aligns.to_a).to be_empty }
28
+
29
+ context '#column_widths' do
30
+ let(:widths) { [10, 10] }
31
+
32
+ it { expect(renderer.column_widths).to eq(widths) }
33
+ end
34
+
35
+ context '#column_widths empty' do
36
+ let(:widths) { [] }
37
+
38
+ it {
39
+ expect {
40
+ renderer.column_widths
41
+ }.to raise_error(TTY::Table::InvalidArgument)
42
+ }
43
+ end
44
+
45
+ context '#column_aligns' do
46
+ let(:aligns) { [:center, :center] }
47
+
48
+ it 'unwraps original array' do
49
+ expect(renderer.column_aligns.to_a).to eq(aligns)
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,52 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe TTY::Table::Renderer::Basic, 'padding' do
6
+ let(:header) { ['Field', 'Type', 'Null', 'Key', 'Default', 'Extra'] }
7
+ let(:rows) { [['id', 'int(11)', 'YES', 'nil', 'NULL', '']] }
8
+ let(:table) { TTY::Table.new(header, rows) }
9
+
10
+ subject(:renderer) { described_class.new(table, options) }
11
+
12
+ context 'with left & right padding' do
13
+ let(:options) { {padding: [0,1,0,1]} }
14
+
15
+ it 'pads each field' do
16
+ expect(renderer.render).to eql <<-EOS.chomp
17
+ Field Type Null Key Default Extra
18
+ id int(11) YES nil NULL
19
+ EOS
20
+ end
21
+ end
22
+
23
+ context 'with top & bottom padding' do
24
+ let(:options) { {padding: [1,0,1,0], multiline: true} }
25
+
26
+ it 'pads each field' do
27
+ expect(renderer.render).to eql <<-EOS.chomp
28
+
29
+ Field Type Null Key Default Extra
30
+
31
+
32
+ id int(11) YES nil NULL
33
+
34
+ EOS
35
+ end
36
+ end
37
+
38
+ context 'with full padding' do
39
+ let(:options) { {padding: [1,1,1,1], multiline: true} }
40
+
41
+ it 'pads each field' do
42
+ expect(renderer.render).to eql <<-EOS.chomp
43
+
44
+ Field Type Null Key Default Extra
45
+
46
+
47
+ id int(11) YES nil NULL
48
+
49
+ EOS
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,57 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe TTY::Table::Renderer::Basic, '#render' do
6
+ let(:header) { ['h1', 'h2', 'h3'] }
7
+ let(:rows) { [['a1', 'a2', 'a3'], ['b1', 'b2', 'b3']] }
8
+ let(:table) { TTY::Table.new header, rows }
9
+
10
+ subject(:renderer) { described_class.new(table) }
11
+
12
+ context 'with rows' do
13
+ let(:table) { TTY::Table.new rows }
14
+
15
+ it 'displays table without styling' do
16
+ expect(renderer.render).to eq <<-EOS.normalize
17
+ a1 a2 a3
18
+ b1 b2 b3
19
+ EOS
20
+ end
21
+ end
22
+
23
+ context 'with header and rows' do
24
+ it 'displays table with header' do
25
+ expect(renderer.render).to eq <<-EOS.normalize
26
+ h1 h2 h3
27
+ a1 a2 a3
28
+ b1 b2 b3
29
+ EOS
30
+ end
31
+ end
32
+
33
+ context 'with short header' do
34
+ let(:header) { ['h1', 'h2'] }
35
+ let(:rows) { [['aaa1', 'a2'], ['b1', 'bb1']] }
36
+
37
+ it 'displays table according to widths' do
38
+ expect(renderer.render).to eq <<-EOS.normalize
39
+ h1 h2
40
+ aaa1 a2
41
+ b1 bb1
42
+ EOS
43
+ end
44
+ end
45
+
46
+ context 'with long header' do
47
+ let(:header) { ['header1', 'header2', 'header3'] }
48
+
49
+ it 'header greater than row sizes' do
50
+ expect(renderer.render).to eq <<-EOS.normalize
51
+ header1 header2 header3
52
+ a1 a2 a3
53
+ b1 b2 b3
54
+ EOS
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,96 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe TTY::Table::Renderer::Basic, 'resizing' do
6
+ let(:header) { ['h1', 'h2', 'h3'] }
7
+ let(:rows) { [['a1', 'a2', 'a3'], ['b1', 'b2', 'b3']] }
8
+ let(:table) { TTY::Table.new(header, rows) }
9
+
10
+ subject(:renderer) { described_class.new(table, options) }
11
+
12
+ context 'when expanding' do
13
+ context 'even columns' do
14
+ let(:options) { {width: 16, resize: true} }
15
+
16
+ it 'resizes each column' do
17
+ expect(renderer.render).to eql <<-EOS.normalize
18
+ h1 h2 h3
19
+ a1 a2 a3
20
+ b1 b2 b3
21
+ EOS
22
+ end
23
+ end
24
+
25
+ context 'even columns with extra width' do
26
+ let(:header) { ['h1', 'h2', 'h3', 'h4'] }
27
+ let(:rows) { [['a1','a2','a3','a4'], ['b1','b2','b3','b4']] }
28
+ let(:options) { {width: 21, resize: true} }
29
+
30
+ it 'resizes each column' do
31
+ expect(renderer.render).to eql <<-EOS.normalize
32
+ h1 h2 h3 h4
33
+ a1 a2 a3 a4
34
+ b1 b2 b3 b4
35
+ EOS
36
+ end
37
+ end
38
+
39
+ context 'uneven columns' do
40
+ let(:header) { ['h1', 'h2', 'h3'] }
41
+ let(:rows) { [['aaa1', 'aa2', 'aaaaaaa3'], ['b1', 'b2', 'b3']] }
42
+ let(:options) { {width: 32, resize: true} }
43
+
44
+ it 'resizes each column' do
45
+ expect(renderer.render).to eql <<-EOS.normalize
46
+ h1 h2 h3
47
+ aaa1 aa2 aaaaaaa3
48
+ b1 b2 b3
49
+ EOS
50
+ end
51
+ end
52
+ end
53
+
54
+ context 'when shrinking' do
55
+ let(:header) { ['head1', 'head2'] }
56
+ let(:rows) { [['aaaa1','aaaa2',], ['bbbb1','bbbb2']] }
57
+
58
+ context 'even columns' do
59
+ let(:options) { {width: 7, resize: true} }
60
+
61
+ it 'resizes each column' do
62
+ expect(renderer.render).to eql <<-EOS.normalize
63
+ he… he…
64
+ aa… aa…
65
+ bb… bb…
66
+ EOS
67
+ end
68
+ end
69
+
70
+ context 'even columns with extra width' do
71
+ let(:options) { {width: 8, resize: true} }
72
+
73
+ it 'resizes each column' do
74
+ expect(renderer.render).to eql <<-EOS.normalize
75
+ hea… he…
76
+ aaa… aa…
77
+ bbb… bb…
78
+ EOS
79
+ end
80
+ end
81
+
82
+ context 'uneven columns' do
83
+ let(:header) { ['head1', 'head2', 'head3'] }
84
+ let(:rows) { [['aaa1', 'aa2', 'aaaaaaa3'], ['b1', 'b2', 'b3']] }
85
+ let(:options) { {width: 15, resize: true} }
86
+
87
+ it 'resizes each column' do
88
+ expect(renderer.render).to eql <<-EOS.normalize
89
+ hea… he… head3
90
+ aaa1 aa2 aaaaa…
91
+ b1 b2 b3
92
+ EOS
93
+ end
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,43 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe TTY::Table::Renderer::Basic, 'with separator' do
6
+ let(:header) { ['h1', 'h2', 'h3'] }
7
+ let(:rows) { [['a1', 'a2', 'a3'], ['b1', 'b2', 'b3']] }
8
+ let(:table) { TTY::Table.new(header, rows) }
9
+
10
+ let(:object) { described_class.new table }
11
+
12
+ subject(:renderer) { object }
13
+
14
+ context 'when default' do
15
+ it "sets through hash" do
16
+ renderer.border :separator => :each_row
17
+ expect(renderer.border.separator).to eql(:each_row)
18
+ end
19
+
20
+ it "sets through attribute" do
21
+ renderer.border.separator= :each_row
22
+ expect(renderer.border.separator).to eql(:each_row)
23
+ end
24
+
25
+ it "sets through block" do
26
+ renderer.border do
27
+ separator :each_row
28
+ end
29
+ expect(renderer.border.separator).to eql(:each_row)
30
+ end
31
+
32
+ it "renders each row" do
33
+ renderer.border.separator= :each_row
34
+ expect(renderer.render).to eq <<-EOS.normalize
35
+ h1 h2 h3
36
+
37
+ a1 a2 a3
38
+
39
+ b1 b2 b3
40
+ EOS
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,35 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe TTY::Table::Renderer::Basic, 'truncation' do
6
+ let(:header) { ['header1', 'head2', 'h3'] }
7
+ let(:rows) { [['a1111111', 'a222', 'a3333333'], ['b111', 'b2222222', 'b333333']]}
8
+ let(:table) { TTY::Table.new header, rows }
9
+
10
+ subject(:renderer) { described_class.new(table, options) }
11
+
12
+ context 'without column widths' do
13
+ let(:options) { {} }
14
+
15
+ it "doesn't shorten the fields" do
16
+ expect(renderer.render).to eq <<-EOS.normalize
17
+ header1 head2 h3
18
+ a1111111 a222 a3333333
19
+ b111 b2222222 b333333
20
+ EOS
21
+ end
22
+ end
23
+
24
+ context 'with column widths' do
25
+ let(:options) { { column_widths: [3, 5, 7] } }
26
+
27
+ it 'shortens the fields' do
28
+ expect(renderer.render).to eq <<-EOS.normalize
29
+ he… head2 h3
30
+ a1… a222 a33333…
31
+ b1… b222… b333333
32
+ EOS
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,40 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe TTY::Table::Renderer::Basic, 'wrapping' do
6
+ let(:header) { ['header1', 'head2', 'h3'] }
7
+ let(:rows) { [['a1111111', 'a222', 'a3333333'], ['b111', 'b2222222', 'b333333']]}
8
+ let(:table) { TTY::Table.new header, rows }
9
+
10
+ subject(:renderer) { described_class.new(table, options) }
11
+
12
+ context 'without column widths' do
13
+ let(:options) { {multiline: true} }
14
+
15
+ it 'doesn\'t wrap the fields' do
16
+ expect(renderer.render).to eq <<-EOS.normalize
17
+ header1 head2 h3
18
+ a1111111 a222 a3333333
19
+ b111 b2222222 b333333
20
+ EOS
21
+ end
22
+ end
23
+
24
+ context 'with column widths' do
25
+ let(:options) { { column_widths: [3, 5, 7], multiline: true} }
26
+
27
+ it 'wraps the fields' do
28
+ expect(renderer.render).to eq <<-EOS.normalize
29
+ hea head2 h3
30
+ der
31
+ 1
32
+ a11 a222 a333333
33
+ 111 3
34
+ 11
35
+ b11 b2222 b333333
36
+ 1 222
37
+ EOS
38
+ end
39
+ end
40
+ end