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,86 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe TTY::Table, 'access' do
6
+ let(:header) { [:head1, :head2] }
7
+ let(:rows) { [['a1', 'a2'], ['b1', 'b2']] }
8
+
9
+ subject(:table) { TTY::Table.new rows: rows, header: header }
10
+
11
+ it { is_expected.to respond_to(:element) }
12
+
13
+ it { is_expected.to respond_to(:component) }
14
+
15
+ it { is_expected.to respond_to(:at) }
16
+
17
+ context 'when array like access' do
18
+ it { expect(table[0,0]).to eq('a1') }
19
+
20
+ it { expect(table[0]).to eq(rows[0]) }
21
+
22
+ it { expect(table[5]).to eq(nil) }
23
+
24
+ it { expect(table[-1]).to eq(rows[-1]) }
25
+
26
+ it { expect(table[5,5]).to eq(nil) }
27
+
28
+ it 'raises error for negative indices' do
29
+ expect { table[-5,-5] }.to raise_error(IndexError)
30
+ end
31
+ end
32
+
33
+ context '#row' do
34
+ it 'returns nil for wrong index' do
35
+ expect(table.row(11)).to be_nil
36
+ end
37
+
38
+ it 'gets row at index' do
39
+ expect(table.row(1)).to eq(rows[1])
40
+ end
41
+
42
+ it 'yields self for wrong index' do
43
+ block = lambda { |el| [] << el }
44
+ expect(table.row(11, &block)).to eq(table)
45
+ end
46
+
47
+ it 'yields row at index' do
48
+ yields = []
49
+ expect { table.row(1).each { |el| yields << el } }.to change { yields }.
50
+ from( [] ).
51
+ to( rows[1] )
52
+ end
53
+ end
54
+
55
+ context '#column' do
56
+ it "gets based on header name" do
57
+ expect(table.column(:head1)).to eq(['a1', 'b1'])
58
+ end
59
+
60
+ it "yields based on header name" do
61
+ yielded = []
62
+ table.column(:head1) { |el| yielded << el }
63
+ expect(yielded).to eql(['a1', 'b1'])
64
+ end
65
+
66
+ it 'returns nil for wrong index' do
67
+ expect(table.column(11)).to be_nil
68
+ end
69
+
70
+ it 'gets column at index' do
71
+ expect(table.column(0)).to eq(['a1', 'b1'])
72
+ end
73
+
74
+ it 'yields self for wrong index' do
75
+ block = lambda { |el| [] << el }
76
+ expect(table.column(11, &block)).to eq(table)
77
+ end
78
+
79
+ it 'yields column at index' do
80
+ yields = []
81
+ expect { table.column(1).each { |el| yields << el } }.to change { yields }.
82
+ from( [] ).
83
+ to( ['a2', 'b2'])
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe TTY::Table, '#<<' do
6
+ let(:rows) { ['a', 'b', 'c'] }
7
+ let(:object) { described_class }
8
+
9
+ subject(:table) { object[rows] }
10
+
11
+ context 'with primitive values' do
12
+ let(:row) { [1, 2, 3] }
13
+
14
+ it 'extracts values correctly' do
15
+ table << row
16
+ expect(table.to_a.last).to eql(row)
17
+ end
18
+ end
19
+
20
+ context 'with complex values' do
21
+ let(:row) { [1, { value: 2 }, 3] }
22
+
23
+ it 'extracts values correctly' do
24
+ table << row
25
+ expect(table.to_a.last).to eql([1,2,3])
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,90 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe TTY::Table::Border::ASCII, '#rendering' do
6
+
7
+ subject { described_class.new(column_widths) }
8
+
9
+ context 'with empty row' do
10
+ let(:row) { TTY::Table::Row.new([]) }
11
+ let(:column_widths) { [] }
12
+
13
+ it 'draws top line' do
14
+ expect(subject.top_line).to eq("++")
15
+ end
16
+
17
+ it 'draws middle line' do
18
+ expect(subject.separator).to eq("++")
19
+ end
20
+
21
+ it 'draw bottom line' do
22
+ expect(subject.bottom_line).to eq("++")
23
+ end
24
+
25
+ it 'draws row line' do
26
+ expect(subject.row_line(row)).to eq("||")
27
+ end
28
+ end
29
+
30
+ context 'with row' do
31
+ let(:column_widths) { [2,2,2] }
32
+ let(:row) { TTY::Table::Row.new(['a1', 'a2', 'a3']) }
33
+
34
+ it 'draws top line' do
35
+ expect(subject.top_line).to eq("+--+--+--+")
36
+ end
37
+
38
+ it 'draw middle line' do
39
+ expect(subject.separator).to eq("+--+--+--+")
40
+ end
41
+
42
+ it 'draw bottom line' do
43
+ expect(subject.bottom_line).to eq("+--+--+--+")
44
+ end
45
+
46
+ it 'draws row line' do
47
+ expect(subject.row_line(row)).to eq("|a1|a2|a3|")
48
+ end
49
+ end
50
+
51
+ context 'with multiline row' do
52
+ let(:column_widths) { [2,2,2]}
53
+
54
+ context 'with mixed data' do
55
+ let(:row) { TTY::Table::Row.new(["a1\nb1\nc1", 'a2', 'a3']) }
56
+
57
+ it 'draws row line' do
58
+ expect(subject.row_line(row)).to eq <<-EOS.normalize
59
+ |a1|a2|a3|
60
+ |b1| | |
61
+ |c1| | |
62
+ EOS
63
+ end
64
+ end
65
+
66
+ context 'with sparse data' do
67
+ let(:row) { TTY::Table::Row.new(["a1\n\n", "\na2\n", "\n\na3"]) }
68
+
69
+ it 'draws row line' do
70
+ expect(subject.row_line(row)).to eq <<-EOS.normalize
71
+ |a1| | |
72
+ | |a2| |
73
+ | | |a3|
74
+ EOS
75
+ end
76
+ end
77
+
78
+ context 'with empty data' do
79
+ let(:row) { TTY::Table::Row.new(["\na1\n", "\na2\n", "\na3\n"]) }
80
+
81
+ it 'draws row line' do
82
+ expect(subject.row_line(row)).to eq <<-EOS.normalize
83
+ | | | |
84
+ |a1|a2|a3|
85
+ | | | |
86
+ EOS
87
+ end
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe TTY::Table::Border, '#new' do
6
+ let(:row) { [] }
7
+
8
+ subject(:instance) { klass.new row }
9
+
10
+ context 'when abstract' do
11
+ let(:klass) { described_class }
12
+
13
+ it { expect { instance }.to raise_error(NotImplementedError) }
14
+ end
15
+
16
+ context 'when concrete' do
17
+ let(:klass) {
18
+ Class.new do
19
+ def initialize(row); end
20
+ end
21
+ }
22
+
23
+ it { expect { instance }.to_not raise_error() }
24
+
25
+ it { is_expected.to be_instance_of klass }
26
+ end
27
+ end
@@ -0,0 +1,130 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe TTY::Table::Border::Null, '#rendering' do
6
+ let(:border) { nil }
7
+
8
+ subject { described_class.new column_widths, border }
9
+
10
+ context 'with empty row' do
11
+ let(:row) { TTY::Table::Row.new([]) }
12
+ let(:column_widths) { [] }
13
+
14
+ it 'draws top line' do
15
+ expect(subject.top_line).to be_nil
16
+ end
17
+
18
+ it 'draws middle line' do
19
+ expect(subject.separator).to be_nil
20
+ end
21
+
22
+ it 'draw bottom line' do
23
+ expect(subject.bottom_line).to be_nil
24
+ end
25
+
26
+ it 'draws row line' do
27
+ expect(subject.row_line(row)).to eq('')
28
+ end
29
+ end
30
+
31
+ context 'with row' do
32
+ let(:row) { TTY::Table::Row.new(['a1', 'a2', 'a3']) }
33
+ let(:column_widths) { [2,2,2] }
34
+
35
+ it 'draws top line' do
36
+ expect(subject.top_line).to be_nil
37
+ end
38
+
39
+ it 'draw middle line' do
40
+ expect(subject.separator).to be_nil
41
+ end
42
+
43
+ it 'draw bottom line' do
44
+ expect(subject.bottom_line).to be_nil
45
+ end
46
+
47
+ it 'draws row line' do
48
+ expect(subject.row_line(row)).to eq('a1 a2 a3')
49
+ end
50
+ end
51
+
52
+ context 'with multiline row' do
53
+ let(:column_widths) { [2,2,2] }
54
+
55
+ context 'with mixed data' do
56
+ let(:row) { TTY::Table::Row.new(["a1\nb1\nc1", 'a2', 'a3']) }
57
+
58
+ it 'draws row line' do
59
+ expect(subject.row_line(row)).to eq <<-EOS.normalize
60
+ a1 a2 a3
61
+ b1
62
+ c1
63
+ EOS
64
+ end
65
+ end
66
+
67
+ context 'with sparse data' do
68
+ let(:row) { TTY::Table::Row.new(["a1\n\n", "\na2\n", "\n\na3"]) }
69
+
70
+ it 'draws row line' do
71
+ expect(subject.row_line(row)).to eq <<-EOS.chomp
72
+ a1
73
+ a2
74
+ a3
75
+ EOS
76
+ end
77
+ end
78
+
79
+ context 'with empty data' do
80
+ let(:row) { TTY::Table::Row.new(["\na1\n", "\na2\n", "\na3\n"]) }
81
+
82
+ it 'draws row line' do
83
+ expect(subject.row_line(row)).to eq <<-EOS.chomp
84
+
85
+ a1 a2 a3
86
+
87
+ EOS
88
+ end
89
+ end
90
+ end
91
+
92
+ context 'with border' do
93
+ let(:row) { TTY::Table::Row.new(['a1', 'a2', 'a3']) }
94
+ let(:column_widths) { [2,2,2] }
95
+ let(:border) { { :characters => {
96
+ 'top' => '=',
97
+ 'top_mid' => '=',
98
+ 'top_left' => '=',
99
+ 'top_right' => '=',
100
+ 'bottom' => '=',
101
+ 'bottom_mid' => '=',
102
+ 'bottom_left' => '=',
103
+ 'bottom_right' => '=',
104
+ 'mid' => '=',
105
+ 'mid_mid' => '=',
106
+ 'mid_left' => '=',
107
+ 'mid_right' => '=',
108
+ 'left' => '=',
109
+ 'center' => '=',
110
+ 'right' => '='
111
+ } } }
112
+
113
+
114
+ it 'draws top line' do
115
+ expect(subject.top_line).to eql '=========='
116
+ end
117
+
118
+ it 'draws separator line' do
119
+ expect(subject.separator).to eql '=========='
120
+ end
121
+
122
+ it 'draws bottom line' do
123
+ expect(subject.bottom_line).to eql '=========='
124
+ end
125
+
126
+ it 'draws row line' do
127
+ expect(subject.row_line(row)).to eql '=a1=a2=a3='
128
+ end
129
+ end
130
+ end
@@ -0,0 +1,38 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe TTY::Table::BorderOptions, '#from' do
6
+
7
+ subject(:options) { described_class.from object }
8
+
9
+ context 'when empty hash' do
10
+ let(:object) { {} }
11
+
12
+ it { expect(options.style).to be_nil }
13
+
14
+ it { expect(options.separator).to be_nil }
15
+ end
16
+
17
+ context 'when hash' do
18
+ let(:object) { { style: :red, separator: :none } }
19
+
20
+ it { expect(options).to be_kind_of(described_class) }
21
+
22
+ it { expect(options.style).to eql :red }
23
+
24
+ it { expect(options.separator).to eql :none }
25
+
26
+ it { expect(options.characters).to eql({}) }
27
+ end
28
+
29
+ context 'when other BorderOptions' do
30
+ let(:object) { described_class.new(nil, nil, :blue) }
31
+
32
+ it { expect(options).to eql object }
33
+
34
+ it { expect(options.characters).to eql({}) }
35
+
36
+ it { expect(options.style).to eql :blue }
37
+ end
38
+ end # from
@@ -0,0 +1,14 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe TTY::Table::BorderOptions, '.new' do
6
+
7
+ subject(:options) { described_class.new }
8
+
9
+ it { expect(subject.characters).to eql({}) }
10
+
11
+ it { expect(subject.separator).to be_nil }
12
+
13
+ it { expect(subject.style).to be_nil }
14
+ end # new
@@ -0,0 +1,63 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe TTY::Table::Border::Unicode, '#rendering' do
6
+
7
+ subject { described_class.new(column_widths) }
8
+
9
+ context 'with empty row' do
10
+ let(:row) { TTY::Table::Row.new([]) }
11
+ let(:column_widths) { [] }
12
+
13
+ it 'draws top line' do
14
+ expect(subject.top_line).to eq("┌┐")
15
+ end
16
+
17
+ it 'draws middle line' do
18
+ expect(subject.separator).to eq("├┤")
19
+ end
20
+
21
+ it 'draw bottom line' do
22
+ expect(subject.bottom_line).to eq("└┘")
23
+ end
24
+
25
+ it 'draws row line' do
26
+ expect(subject.row_line(row)).to eq("││")
27
+ end
28
+ end
29
+
30
+ context 'with row' do
31
+ let(:row) { TTY::Table::Row.new(['a1', 'a2', 'a3']) }
32
+ let(:column_widths) { [2,2,2] }
33
+
34
+ it 'draws top line' do
35
+ expect(subject.top_line).to eq("┌──┬──┬──┐")
36
+ end
37
+
38
+ it 'draw middle line' do
39
+ expect(subject.separator).to eq("├──┼──┼──┤")
40
+ end
41
+
42
+ it 'draw bottom line' do
43
+ expect(subject.bottom_line).to eq("└──┴──┴──┘")
44
+ end
45
+
46
+ it 'draws row line' do
47
+ expect(subject.row_line(row)).to eq("│a1│a2│a3│")
48
+ end
49
+ end
50
+
51
+ context 'with multiline row' do
52
+ let(:row) { TTY::Table::Row.new(["a1\nb1\nc1", 'a2', 'a3']) }
53
+ let(:column_widths) { [2,2,2] }
54
+
55
+ it 'draws row line' do
56
+ expect(subject.row_line(row)).to eq <<-EOS.normalize
57
+ │a1│a2│a3│
58
+ │b1│ │ │
59
+ │c1│ │ │
60
+ EOS
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,20 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe TTY::Table::BorderOptions do
6
+
7
+ subject { described_class.new(options) }
8
+
9
+ context 'with characters' do
10
+ let(:options) { {top: '**'} }
11
+
12
+ it { expect(subject.characters).to eq({top:'**'}) }
13
+ end
14
+
15
+ context 'without characters' do
16
+ let(:options) { nil }
17
+
18
+ it { expect(subject.characters).to eq({}) }
19
+ end
20
+ end
@@ -0,0 +1,18 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe TTY::Table::BorderOptions, '.update' do
6
+
7
+ let(:object) { described_class.new }
8
+
9
+ let(:params) { {characters: {top: '*'}, separator: '|', style: :red} }
10
+
11
+ subject { object.update(params) }
12
+
13
+ it 'sets properties from hash object' do
14
+ expect(subject.characters).to eq({top: '*'})
15
+ expect(subject.separator).to eq('|')
16
+ expect(subject.style).to eq(:red)
17
+ end
18
+ end
@@ -0,0 +1,15 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe TTY::Table::ColumnSet, '#extract_widths' 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 { described_class.new table }
11
+
12
+ it 'extract widths' do
13
+ expect(subject.extract_widths).to eql([2,2,2])
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe TTY::Table::ColumnSet, '#extract_widths!' 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 { described_class.new table }
11
+
12
+ it 'extract widths' do
13
+ expect(subject.total_width).to eql(6)
14
+ end
15
+ end
@@ -0,0 +1,51 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe TTY::Table::ColumnSet, '#widths_from' 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 { described_class.widths_from(table, column_widths) }
11
+
12
+ context 'when empty array' do
13
+ let(:column_widths) { [] }
14
+
15
+ it 'raises an error' do
16
+ expect { subject }.to raise_error(TTY::Table::InvalidArgument)
17
+ end
18
+ end
19
+
20
+ context 'when invalid size array' do
21
+ let(:column_widths) { [3,3] }
22
+
23
+ it 'raises an error' do
24
+ expect { subject }.to raise_error(TTY::Table::InvalidArgument)
25
+ end
26
+ end
27
+
28
+ context 'when valid array' do
29
+ let(:column_widths) { [3,3,3] }
30
+
31
+ it 'converts into numbers' do
32
+ expect(subject).to eql(column_widths)
33
+ end
34
+ end
35
+
36
+ context 'when nil' do
37
+ let(:column_widths) { nil }
38
+
39
+ it 'extracts widths' do
40
+ expect(subject).to eql([2,2,2])
41
+ end
42
+ end
43
+
44
+ context 'when numeric' do
45
+ let(:column_widths) { 5 }
46
+
47
+ it 'generates widths' do
48
+ expect(subject).to eql([5,5,5])
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,67 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe TTY::Table::Columns, '#enforce' do
6
+ let(:header) { ['h1', 'h2', 'h3', 'h4'] }
7
+ let(:rows) { [['a1', 'a2', 'a3', 'a4'], ['b1', 'b2', 'b3', 'b4']] }
8
+ let(:table) { TTY::Table.new(header, rows) }
9
+ let(:object) { described_class.new(renderer) }
10
+
11
+ subject { object.enforce }
12
+
13
+ context 'with width contraint' do
14
+ let(:renderer) { TTY::Table::Renderer::Basic.new(table, options) }
15
+ let(:options) { { width: 5 }}
16
+
17
+ it 'raises error when table width is too small' do
18
+ expect { subject }.to raise_error(TTY::Table::ResizeError)
19
+ end
20
+ end
21
+
22
+ context 'with width contraint matching natural width' do
23
+ let(:renderer) { TTY::Table::Renderer::Basic.new(table, options) }
24
+ let(:options) { { width: 11, resize: true }}
25
+
26
+ it 'raises error when table width is too small' do
27
+ expect(object).to receive(:expand)
28
+ subject
29
+ end
30
+ end
31
+
32
+ context 'with table larger than allowed width' do
33
+ let(:renderer) { TTY::Table::Renderer::Basic.new(table, options) }
34
+
35
+ context 'with resize' do
36
+ let(:options) { { width: 8, resize: true } }
37
+
38
+ it 'calls shrink' do
39
+ expect(object).to receive(:shrink)
40
+ subject
41
+ end
42
+ end
43
+
44
+ context 'without resize' do
45
+ let(:options) { { width: 8, resize: false }}
46
+
47
+ it 'changes table orientation to vertical' do
48
+ allow(Kernel).to receive(:warn)
49
+ expect(renderer.column_widths).to eql([2,2,2,2])
50
+ expect(renderer.table.orientation.name).to eql(:horizontal)
51
+ subject
52
+ expect(renderer.column_widths).to eq([2,2])
53
+ expect(renderer.table.orientation.name).to eql(:vertical)
54
+ end
55
+ end
56
+ end
57
+
58
+ context 'with table less than allowed width' do
59
+ let(:renderer) { TTY::Table::Renderer::Basic.new(table, options) }
60
+ let(:options) { { width: 15 }}
61
+
62
+ it "doesn't change original widths" do
63
+ allow(Kernel).to receive(:warn)
64
+ expect(renderer.column_widths).to eq([2,2,2,2])
65
+ end
66
+ end
67
+ end