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,35 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe TTY::Table::Columns, 'column widths' 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
+
10
+ subject { described_class.new(renderer) }
11
+
12
+ context 'with basic renderer' do
13
+ let(:renderer) { TTY::Table::Renderer::Basic.new(table) }
14
+
15
+ it 'calculates columns natural width' do
16
+ expect(subject.natural_width).to eq(11)
17
+ end
18
+
19
+ it 'calculates miminimum columns width' do
20
+ expect(subject.minimum_width).to eq(7)
21
+ end
22
+ end
23
+
24
+ context 'with ascii renderer' do
25
+ let(:renderer) { TTY::Table::Renderer::ASCII.new(table) }
26
+
27
+ it 'calculates columns natural width' do
28
+ expect(subject.natural_width).to eq(13)
29
+ end
30
+
31
+ it 'calculates miminimum columns width' do
32
+ expect(subject.minimum_width).to eq(9)
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,14 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe TTY::Table, '#data' do
6
+ let(:header) { ['h1', 'h2', 'h3'] }
7
+ let(:rows) { [['a1', 'a2', 'a3'], ['b1', 'b2', 'b3']] }
8
+
9
+ subject(:table) { described_class.new header, rows }
10
+
11
+ it 'gets all table data' do
12
+ expect(table.data).to eql([header] + rows)
13
+ end
14
+ end
@@ -0,0 +1,41 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe TTY::Table, '#each' do
6
+ let(:table) { described_class.new header, rows }
7
+ let(:header) { ['Header1'] }
8
+ let(:rows) { [['a1'], ['b1']] }
9
+ let(:field) { TTY::Table::Field }
10
+
11
+ context 'with no block' do
12
+ subject { table.each }
13
+
14
+ it { is_expected.to be_instance_of(to_enum.class) }
15
+
16
+ it 'yields the expected values' do
17
+ expect(subject.to_a).to eql(table.to_a)
18
+ end
19
+ end
20
+
21
+ context 'with block' do
22
+ let(:yields) { [] }
23
+
24
+ subject { table.each { |row| yields << row } }
25
+
26
+ it 'yields header and rows' do
27
+ subject
28
+ expect(yields.first).to be_instance_of(TTY::Table::Header)
29
+ expect(yields.last).to be_instance_of(TTY::Table::Row)
30
+ end
31
+
32
+ it 'yields header and rows with expected attributes' do
33
+ subject
34
+ expect(yields).to eql(table.data)
35
+ end
36
+
37
+ xit 'yields each row' do
38
+ expect { subject }.to change { yields }.from([]).to(table.data)
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,57 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe TTY::Table, '.each_with_index' do
6
+ let(:header) { ['Header1', 'Header2'] }
7
+ let(:rows) { [['a1', 'a2'], ['b1', 'b2']] }
8
+ let(:field) { TTY::Table::Field }
9
+
10
+ let(:object) { described_class.new header, rows }
11
+
12
+ context 'with no block' do
13
+ subject { object.each_with_index }
14
+
15
+ it { is_expected.to be_instance_of(to_enum.class) }
16
+
17
+ it 'yields the expected values' do
18
+ expect(subject.to_a).to eql(object.to_a)
19
+ end
20
+ end
21
+
22
+ context 'with block' do
23
+ let(:yields) { [] }
24
+
25
+ subject { object.each_with_index { |el, row, col| yields << [el, row, col]}}
26
+
27
+ context 'without header' do
28
+ let(:header) { nil }
29
+
30
+ let(:expected) {
31
+ [ [field.new('a1'), 0, 0], [field.new('a2'), 0, 1],
32
+ [field.new('b1'), 1, 0], [field.new('b2'), 1, 1] ]
33
+ }
34
+
35
+ it "yields rows with expected data" do
36
+ expect { subject }.to change { yields }.
37
+ from( [] ).
38
+ to( expected )
39
+ end
40
+ end
41
+
42
+ context 'with header' do
43
+
44
+ let(:expected) {
45
+ [ [field.new('Header1'), 0, 0], [field.new('Header2'), 0, 1],
46
+ [field.new('a1'), 1, 0], [field.new('a2'), 1, 1],
47
+ [field.new('b1'), 2, 0], [field.new('b2'), 2, 1] ]
48
+ }
49
+
50
+ it "yields header and rows with expected data" do
51
+ expect { subject }.to change { yields }.
52
+ from( [] ).
53
+ to( expected )
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe TTY::Table, '#empty?' do
6
+ let(:header) { ['Header1', 'Header2'] }
7
+ let(:object) { described_class.new header, rows }
8
+
9
+ subject { object.empty? }
10
+
11
+ context 'with rows containing no entries' do
12
+ let(:rows) { [] }
13
+
14
+ it { is_expected.to eq(true) }
15
+ end
16
+
17
+ context 'with rows containing an entry' do
18
+ let(:rows) { [['a1']] }
19
+
20
+ it { is_expected.to eq(false) }
21
+ end
22
+ end
23
+
@@ -0,0 +1,34 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe TTY::Table, '#eql?' do
6
+ let(:rows) { [['a1', 'a2'], ['b1', 'b2']] }
7
+ let(:object) { described_class.new rows }
8
+
9
+ subject { object.eql?(other) }
10
+
11
+ describe '#inspect' do
12
+ it { expect(object.inspect).to match /#<TTY::Table/ }
13
+ end
14
+
15
+ context 'with the same object' do
16
+ let(:other) { object }
17
+
18
+ it { is_expected.to eql(true) }
19
+
20
+ it 'is symmetric' do
21
+ is_expected.to eql(other.eql?(object))
22
+ end
23
+ end
24
+
25
+ context 'with an equivalent object' do
26
+ let(:other) { object.dup }
27
+
28
+ it { is_expected.to eql(true) }
29
+
30
+ it 'is symmetric' do
31
+ is_expected.to eql(other.eql?(object))
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,51 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe TTY::Table::Field, '#==' do
6
+ let(:value) { '1' }
7
+ let(:object) { described_class.new(value) }
8
+
9
+ subject { object == other }
10
+
11
+ context 'with the same object' do
12
+ let(:other) { object }
13
+
14
+ it { is_expected.to eql(true) }
15
+
16
+ it 'is symmetric' do
17
+ is_expected.to eql(other == object)
18
+ end
19
+ end
20
+
21
+ context 'with an equivalent object' do
22
+ let(:other) { object.dup }
23
+
24
+ it { is_expected.to eql(true) }
25
+
26
+ it 'is symmetric' do
27
+ is_expected.to eql(other == object)
28
+ end
29
+ end
30
+
31
+ context 'with an equivalent object of subclass' do
32
+ let(:other) { Class.new(described_class).new(value) }
33
+
34
+ it { is_expected.to eq(true) }
35
+
36
+ it 'is symmetric' do
37
+ is_expected.not_to eql(other == object)
38
+ end
39
+ end
40
+
41
+ context 'with an object having a different value' do
42
+ let(:other_value) { '2' }
43
+ let(:other) { described_class.new(other_value) }
44
+
45
+ it { is_expected.to eql(false) }
46
+
47
+ it 'is symmetric' do
48
+ is_expected.to eql(other == object)
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,21 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe TTY::Table::Field, '#length' do
6
+ let(:object) { described_class.new value }
7
+
8
+ subject { object.length }
9
+
10
+ context 'with escaped value' do
11
+ let(:value) { "Multi\nLine" }
12
+
13
+ it { is_expected.to eql(5) }
14
+ end
15
+
16
+ context 'with unescaped value' do
17
+ let(:value) { "Multi\\nLine" }
18
+
19
+ it { is_expected.to eql(11) }
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe TTY::Table::Field, '#lines' do
6
+ let(:object) { described_class.new value }
7
+
8
+ subject { object.lines }
9
+
10
+ context 'with escaped value' do
11
+ let(:value) { "Multi\nLine" }
12
+
13
+ it { is_expected.to eql(["Multi", "Line"]) }
14
+ end
15
+
16
+ context 'with unescaped value' do
17
+ let(:value) { "Multi\\nLine" }
18
+
19
+ it { is_expected.to eql(["Multi\\nLine"]) }
20
+ end
21
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe TTY::Table::Field, '#new' do
6
+ let(:object) { described_class }
7
+
8
+ subject { object.new value }
9
+
10
+ context 'with only value' do
11
+ let(:value) { 'foo' }
12
+
13
+ it { is_expected.to be_instance_of(object) }
14
+
15
+ it { expect(subject.value).to eql(value) }
16
+
17
+ it { expect(subject.height).to eql(1) }
18
+ end
19
+
20
+ context 'with hash value' do
21
+ let(:value) { { :value => 'foo' } }
22
+
23
+ it { is_expected.to be_instance_of(object) }
24
+
25
+ it { expect(subject.value).to eql('foo') }
26
+
27
+ it { expect(subject.height).to eql(1) }
28
+ end
29
+ end
@@ -0,0 +1,23 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe TTY::Table::Field, '#width' do
6
+ let(:object) { described_class }
7
+
8
+ let(:instance) { object.new(value) }
9
+
10
+ subject { instance.width }
11
+
12
+ context 'with only value' do
13
+ let(:value) { 'foo' }
14
+
15
+ it { is_expected.to eql(3) }
16
+ end
17
+
18
+ context 'with hash value' do
19
+ let(:value) { "foo\nbaar" }
20
+
21
+ it { is_expected.to eql(8) }
22
+ end
23
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe TTY::Table, '#filter' do
6
+ let(:header) { ['h1', 'h2', 'h3'] }
7
+ let(:rows) { [['a1', 'a2', 'a3'], ['b1', 'b2', 'b3']] }
8
+ let(:table) { described_class.new(header, rows) }
9
+ let(:filter) { Proc.new { |val, row, col|
10
+ (col == 1 and row > 0) ? val.capitalize : val
11
+ }
12
+ }
13
+
14
+ it 'filters rows' do
15
+ expect(table.render do |renderer|
16
+ renderer.filter = filter
17
+ end).to eq <<-EOS.normalize
18
+ h1 h2 h3
19
+ a1 A2 a3
20
+ b1 B2 b3
21
+ EOS
22
+ end
23
+ end
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe TTY::Table::Header, '#call' do
6
+ let(:object) { described_class.new(attributes) }
7
+ let(:attributes) { [:id, :name, :age] }
8
+
9
+ subject { object[attribute] }
10
+
11
+ context 'with a known attribute' do
12
+ context 'when symbol' do
13
+ let(:attribute) { :age }
14
+
15
+ it { is_expected.to eq(2) }
16
+ end
17
+
18
+ context 'when integer' do
19
+ let(:attribute) { 1 }
20
+
21
+ it { is_expected.to eq(:name) }
22
+ end
23
+ end
24
+
25
+ context 'with an unknown attribute' do
26
+ let(:attribute) { :mine }
27
+
28
+ it { expect { subject }.to raise_error(TTY::Table::UnknownAttributeError, "the header 'mine' is unknown")}
29
+ end
30
+ end
@@ -0,0 +1,19 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe TTY::Table::Header, 'color' do
6
+
7
+ context 'when default' do
8
+
9
+ end
10
+
11
+ context 'when ascii' do
12
+ let(:renderer) { :ascii }
13
+ let(:red) { "\e[31m" }
14
+ let(:clear) { "\e[0m" }
15
+
16
+ xit 'renders header background in color' do
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,51 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe TTY::Table::Header, '#==' do
6
+ let(:attributes) { [:id, :name] }
7
+ let(:object) { described_class.new(attributes) }
8
+
9
+ subject { object == other }
10
+
11
+ context 'with the same object' do
12
+ let(:other) { object }
13
+
14
+ it { is_expected.to eql(true) }
15
+
16
+ it 'is symmetric' do
17
+ is_expected.to eql(other == object)
18
+ end
19
+ end
20
+
21
+ context 'with an equivalent object' do
22
+ let(:other) { object.dup }
23
+
24
+ it { is_expected.to eql(true) }
25
+
26
+ it 'is symmetric' do
27
+ is_expected.to eql(other == object)
28
+ end
29
+ end
30
+
31
+ context 'with an equivalent object of subclass' do
32
+ let(:other) { Class.new(described_class).new(attributes) }
33
+
34
+ it { is_expected.to eql(true) }
35
+
36
+ it 'is symmetric' do
37
+ is_expected.to eql(other == object)
38
+ end
39
+ end
40
+
41
+ context 'with an object having different attributes' do
42
+ let(:other_attributes) { [:text] }
43
+ let(:other) { described_class.new(other_attributes) }
44
+
45
+ it { is_expected.to eql(false) }
46
+
47
+ it 'is symmetric' do
48
+ is_expected.to eql(other == object)
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe TTY::Table::Header, '#height' do
6
+ let(:object) { described_class.new header }
7
+
8
+ subject { object.height }
9
+
10
+ context 'single row' do
11
+ let(:header) { ['h1', 'h1'] }
12
+
13
+ it { expect(subject).to eql(1) }
14
+ end
15
+
16
+ context 'non escaped multiline' do
17
+ let(:header) { ["h1\nh1\nh1", 'h2'] }
18
+
19
+ it { expect(subject).to eql(3)}
20
+ end
21
+
22
+ context 'escaped multiline' do
23
+ let(:header) { ["h1\\h1\\h1", 'h2'] }
24
+
25
+ it { expect(subject).to eql(1) }
26
+ end
27
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe TTY::Table::Header, '#new' do
6
+ let(:object) { described_class }
7
+
8
+ context 'with no arguments' do
9
+ subject { object.new }
10
+
11
+ it { is_expected.to be_instance_of(object) }
12
+
13
+ it { is_expected.to be_empty }
14
+ end
15
+
16
+ context 'with attributes' do
17
+ subject { object.new(attributes) }
18
+
19
+ let(:attributes) { ['id', 'name', 'age'] }
20
+
21
+ it { is_expected.to be_instance_of(object) }
22
+
23
+ it { is_expected == attributes }
24
+ end
25
+ end
@@ -0,0 +1,20 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe TTY::Table::Header, '#set' do
6
+ let(:object) { described_class }
7
+ let(:attributes) { [:id, :name, :age] }
8
+
9
+ subject(:header) { object.new }
10
+
11
+ it 'sets the value' do
12
+ header[0] = :id
13
+ expect(header[0]).to eql(:id)
14
+ end
15
+
16
+ it 'gets the value' do
17
+ head = object.new [{value: :id}]
18
+ expect(head[0]).to eq(:id)
19
+ end
20
+ end
@@ -0,0 +1,14 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe TTY::Table::Header, '#to_ary' do
6
+ let(:object) { described_class.new(attributes) }
7
+ let(:attributes) { [:id, :name, :age] }
8
+
9
+ subject { object.to_ary }
10
+
11
+ it { is_expected.to be_instance_of(Array) }
12
+
13
+ it { is_expected.to eq(attributes) }
14
+ end
@@ -0,0 +1,13 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe TTY::Table, '#header' do
6
+ let(:header) { [:header1, :header2] }
7
+ let(:rows) { [['a1', 'a2'], ['b1', 'b2']] }
8
+ let(:object) { described_class }
9
+
10
+ subject(:table) { object.new header, rows }
11
+
12
+ it { expect(table.header).to be_instance_of(TTY::Table::Header) }
13
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe TTY::Table::Indentation, '#insert_indent' do
6
+ let(:indent) { 2 }
7
+ let(:renderer) { double(:renderer, indent: indent) }
8
+ let(:object) { described_class.new(renderer) }
9
+
10
+ subject { object.insert_indent(part) }
11
+
12
+ context 'when enumerable' do
13
+ let(:part) { ['line1'] }
14
+
15
+ it 'inserts indentation for each element' do
16
+ expect(subject[0]).to eql(' line1')
17
+ end
18
+ end
19
+
20
+ context 'when string' do
21
+ let(:part) { 'line1' }
22
+
23
+ it 'inserts indentation' do
24
+ expect(subject).to eql(' line1')
25
+ end
26
+ end
27
+ end