tty 0.0.9 → 0.0.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (125) hide show
  1. data/.rspec +2 -1
  2. data/.travis.yml +3 -6
  3. data/README.md +232 -134
  4. data/lib/tty/plugins/plugin.rb +56 -0
  5. data/lib/tty/plugins.rb +75 -0
  6. data/lib/tty/shell/suggestion.rb +102 -0
  7. data/lib/tty/shell.rb +41 -14
  8. data/lib/tty/system/editor.rb +111 -0
  9. data/lib/tty/system/which.rb +13 -1
  10. data/lib/tty/system.rb +44 -28
  11. data/lib/tty/table/border/null.rb +0 -9
  12. data/lib/tty/table/border/row_line.rb +21 -0
  13. data/lib/tty/table/border.rb +63 -32
  14. data/lib/tty/table/border_dsl.rb +1 -1
  15. data/lib/tty/table/column_set.rb +16 -17
  16. data/lib/tty/table/field.rb +27 -7
  17. data/lib/tty/table/header.rb +18 -9
  18. data/lib/tty/table/operation/alignment_set.rb +20 -25
  19. data/lib/tty/table/operation/escape.rb +30 -0
  20. data/lib/tty/table/operation/filter.rb +36 -0
  21. data/lib/tty/table/operation/truncation.rb +22 -11
  22. data/lib/tty/table/operation/wrapped.rb +21 -10
  23. data/lib/tty/table/operations.rb +10 -8
  24. data/lib/tty/table/orientation/horizontal.rb +1 -1
  25. data/lib/tty/table/renderer/ascii.rb +3 -3
  26. data/lib/tty/table/renderer/basic.rb +135 -65
  27. data/lib/tty/table/renderer/color.rb +1 -4
  28. data/lib/tty/table/renderer/unicode.rb +3 -3
  29. data/lib/tty/table/renderer.rb +48 -61
  30. data/lib/tty/table/row.rb +30 -3
  31. data/lib/tty/table/transformation.rb +38 -0
  32. data/lib/tty/table/validatable.rb +7 -5
  33. data/lib/tty/table.rb +78 -99
  34. data/lib/tty/terminal/color.rb +2 -2
  35. data/lib/tty/terminal/echo.rb +1 -1
  36. data/lib/tty/terminal/pager/basic.rb +52 -0
  37. data/lib/tty/terminal/pager/system.rb +39 -0
  38. data/lib/tty/terminal/pager.rb +95 -0
  39. data/lib/tty/terminal.rb +30 -1
  40. data/lib/tty/version.rb +1 -1
  41. data/lib/tty.rb +41 -1
  42. data/spec/spec_helper.rb +20 -0
  43. data/spec/tty/plugins/find_spec.rb +28 -0
  44. data/spec/tty/plugins/load_spec.rb +20 -0
  45. data/spec/tty/plugins/plugin/load_spec.rb +30 -0
  46. data/spec/tty/plugins/plugin/new_spec.rb +18 -0
  47. data/spec/tty/shell/suggest_spec.rb +50 -0
  48. data/spec/tty/support/conversion_spec.rb +3 -3
  49. data/spec/tty/support/delegatable_spec.rb +1 -1
  50. data/spec/tty/support/equatable_spec.rb +6 -9
  51. data/spec/tty/system/editor/available_spec.rb +40 -0
  52. data/spec/tty/system/editor/build_spec.rb +40 -0
  53. data/spec/tty/system/editor/command_spec.rb +16 -0
  54. data/spec/tty/system/editor/executables_spec.rb +13 -0
  55. data/spec/tty/system/editor/invoke_spec.rb +38 -0
  56. data/spec/tty/system/editor/open_spec.rb +27 -0
  57. data/spec/tty/system/platform_spec.rb +4 -6
  58. data/spec/tty/system/which/which_spec.rb +48 -0
  59. data/spec/tty/system/which_spec.rb +8 -34
  60. data/spec/tty/table/border/ascii/rendering_spec.rb +19 -5
  61. data/spec/tty/table/border/new_spec.rb +1 -1
  62. data/spec/tty/table/border/null/rendering_spec.rb +24 -8
  63. data/spec/tty/table/border/unicode/rendering_spec.rb +19 -5
  64. data/spec/tty/table/column_set/extract_widths_spec.rb +4 -15
  65. data/spec/tty/table/column_set/total_width_spec.rb +15 -0
  66. data/spec/tty/table/data_spec.rb +14 -0
  67. data/spec/tty/table/each_spec.rb +17 -4
  68. data/spec/tty/table/each_with_index_spec.rb +34 -6
  69. data/spec/tty/table/field/length_spec.rb +21 -0
  70. data/spec/tty/table/field/lines_spec.rb +21 -0
  71. data/spec/tty/table/filter_spec.rb +23 -0
  72. data/spec/tty/table/header/call_spec.rb +1 -1
  73. data/spec/tty/table/header/height_spec.rb +27 -0
  74. data/spec/tty/table/initialize_spec.rb +6 -6
  75. data/spec/tty/table/operation/alignment_set/call_spec.rb +39 -0
  76. data/spec/tty/table/operation/escape/call_spec.rb +16 -0
  77. data/spec/tty/table/operation/filter/call_spec.rb +17 -0
  78. data/spec/tty/table/operation/truncation/call_spec.rb +15 -10
  79. data/spec/tty/table/operation/truncation/truncate_spec.rb +1 -1
  80. data/spec/tty/table/operation/wrapped/call_spec.rb +15 -10
  81. data/spec/tty/table/operation/wrapped/wrap_spec.rb +1 -1
  82. data/spec/tty/table/operations/new_spec.rb +4 -4
  83. data/spec/tty/table/options_spec.rb +0 -28
  84. data/spec/tty/table/orientation_spec.rb +5 -6
  85. data/spec/tty/table/properties_spec.rb +1 -4
  86. data/spec/tty/table/render_spec.rb +57 -0
  87. data/spec/tty/table/{renders_with_spec.rb → render_with_spec.rb} +29 -10
  88. data/spec/tty/table/renderer/ascii/render_spec.rb +68 -0
  89. data/spec/tty/table/renderer/ascii/separator_spec.rb +28 -0
  90. data/spec/tty/table/renderer/basic/alignment_spec.rb +18 -16
  91. data/spec/tty/table/renderer/basic/extract_column_widths_spec.rb +17 -12
  92. data/spec/tty/table/renderer/basic/filter_spec.rb +53 -0
  93. data/spec/tty/table/renderer/basic/multiline_content_spec.rb +135 -0
  94. data/spec/tty/table/renderer/basic/new_spec.rb +13 -2
  95. data/spec/tty/table/renderer/basic/options_spec.rb +48 -0
  96. data/spec/tty/table/renderer/basic/render_spec.rb +19 -121
  97. data/spec/tty/table/renderer/basic/separator_spec.rb +14 -48
  98. data/spec/tty/table/renderer/basic/truncation_spec.rb +35 -0
  99. data/spec/tty/table/renderer/basic/wrapping_spec.rb +40 -0
  100. data/spec/tty/table/{border_spec.rb → renderer/border_spec.rb} +17 -20
  101. data/spec/tty/table/renderer/select_spec.rb +22 -0
  102. data/spec/tty/table/{border → renderer}/style_spec.rb +13 -14
  103. data/spec/tty/table/renderer/unicode/render_spec.rb +68 -0
  104. data/spec/tty/table/renderer/unicode/separator_spec.rb +26 -0
  105. data/spec/tty/table/rotate_spec.rb +2 -3
  106. data/spec/tty/table/row/call_spec.rb +1 -1
  107. data/spec/tty/table/row/each_spec.rb +31 -0
  108. data/spec/tty/table/row/height_spec.rb +27 -0
  109. data/spec/tty/table/to_s_spec.rb +3 -3
  110. data/spec/tty/table/transformation/extract_tuples_spec.rb +35 -0
  111. data/spec/tty/table/validatable/validate_options_spec.rb +1 -2
  112. data/spec/tty/terminal/home_spec.rb +3 -3
  113. data/spec/tty/terminal/page_spec.rb +13 -0
  114. data/spec/tty/terminal/pager/available_spec.rb +40 -0
  115. data/spec/tty/terminal/pager/basic/page_spec.rb +54 -0
  116. data/spec/tty/terminal/pager/command_spec.rb +16 -0
  117. data/spec/tty/terminal/pager/executables_spec.rb +13 -0
  118. data/spec/tty/terminal/pager/page_spec.rb +47 -0
  119. data/spec/tty/terminal/pager/system/page_spec.rb +29 -0
  120. data/spec/tty/text/distance/distance_spec.rb +12 -0
  121. data/tty.gemspec +7 -3
  122. metadata +160 -27
  123. data/spec/tty/table/operation/alignment_set/align_rows_spec.rb +0 -53
  124. data/spec/tty/table/renderer/pick_renderer_spec.rb +0 -25
  125. data/spec/tty/table/renderer_spec.rb +0 -49
@@ -5,10 +5,11 @@ require 'spec_helper'
5
5
  describe TTY::Table::Border::Null, '#rendering' do
6
6
  let(:border) { nil }
7
7
 
8
- subject { described_class.new row, border }
8
+ subject { described_class.new column_widths, border }
9
9
 
10
10
  context 'with empty row' do
11
- let(:row) { [] }
11
+ let(:row) { TTY::Table::Row.new([]) }
12
+ let(:column_widths) { [] }
12
13
 
13
14
  it 'draws top line' do
14
15
  subject.top_line.should be_nil
@@ -23,12 +24,13 @@ describe TTY::Table::Border::Null, '#rendering' do
23
24
  end
24
25
 
25
26
  it 'draws row line' do
26
- subject.row_line.should == ''
27
+ subject.row_line(row).should == ''
27
28
  end
28
29
  end
29
30
 
30
31
  context 'with row' do
31
- let(:row) { ['a1', 'a2', 'a3'] }
32
+ let(:row) { TTY::Table::Row.new(['a1', 'a2', 'a3']) }
33
+ let(:column_widths) { [2,2,2] }
32
34
 
33
35
  it 'draws top line' do
34
36
  subject.top_line.should be_nil
@@ -43,12 +45,26 @@ describe TTY::Table::Border::Null, '#rendering' do
43
45
  end
44
46
 
45
47
  it 'draws row line' do
46
- subject.row_line.should == 'a1 a2 a3'
48
+ subject.row_line(row).should == 'a1 a2 a3'
49
+ end
50
+ end
51
+
52
+ context 'with multiline row' do
53
+ let(:row) { TTY::Table::Row.new(["a1\nb1\nc1", 'a2', 'a3']) }
54
+ let(:column_widths) { [2,2,2] }
55
+
56
+ it 'draws row line' do
57
+ subject.row_line(row).should == <<-EOS.normalize
58
+ a1 a2 a3
59
+ b1
60
+ c1
61
+ EOS
47
62
  end
48
63
  end
49
64
 
50
65
  context 'with border' do
51
- let(:row) { ['a1', 'a2', 'a3'] }
66
+ let(:row) { TTY::Table::Row.new(['a1', 'a2', 'a3']) }
67
+ let(:column_widths) { [2,2,2] }
52
68
  let(:border) { { :characters => {
53
69
  'top' => '=',
54
70
  'top_mid' => '=',
@@ -67,6 +83,7 @@ describe TTY::Table::Border::Null, '#rendering' do
67
83
  'right' => '='
68
84
  } } }
69
85
 
86
+
70
87
  it 'draws top line' do
71
88
  expect(subject.top_line).to eql '=========='
72
89
  end
@@ -80,8 +97,7 @@ describe TTY::Table::Border::Null, '#rendering' do
80
97
  end
81
98
 
82
99
  it 'draws row line' do
83
- expect(subject.row_line).to eql '=a1=a2=a3='
100
+ expect(subject.row_line(row)).to eql '=a1=a2=a3='
84
101
  end
85
102
  end
86
-
87
103
  end
@@ -4,10 +4,11 @@ require 'spec_helper'
4
4
 
5
5
  describe TTY::Table::Border::Unicode, '#rendering' do
6
6
 
7
- subject { described_class.new row }
7
+ subject { described_class.new(column_widths) }
8
8
 
9
9
  context 'with empty row' do
10
- let(:row) { [] }
10
+ let(:row) { TTY::Table::Row.new([]) }
11
+ let(:column_widths) { [] }
11
12
 
12
13
  it 'draws top line' do
13
14
  subject.top_line.should == "┌┐"
@@ -22,12 +23,13 @@ describe TTY::Table::Border::Unicode, '#rendering' do
22
23
  end
23
24
 
24
25
  it 'draws row line' do
25
- subject.row_line.should == "││"
26
+ subject.row_line(row).should == "││"
26
27
  end
27
28
  end
28
29
 
29
30
  context 'with row' do
30
- let(:row) { ['a1', 'a2', 'a3'] }
31
+ let(:row) { TTY::Table::Row.new(['a1', 'a2', 'a3']) }
32
+ let(:column_widths) { [2,2,2] }
31
33
 
32
34
  it 'draws top line' do
33
35
  subject.top_line.should == "┌──┬──┬──┐"
@@ -42,8 +44,20 @@ describe TTY::Table::Border::Unicode, '#rendering' do
42
44
  end
43
45
 
44
46
  it 'draws row line' do
45
- subject.row_line.should == "│a1│a2│a3│"
47
+ subject.row_line(row).should == "│a1│a2│a3│"
46
48
  end
47
49
  end
48
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
+ subject.row_line(row).should == <<-EOS.normalize
57
+ │a1│a2│a3│
58
+ │b1│ │ │
59
+ │c1│ │ │
60
+ EOS
61
+ end
62
+ end
49
63
  end
@@ -3,24 +3,13 @@
3
3
  require 'spec_helper'
4
4
 
5
5
  describe TTY::Table::ColumnSet, '#extract_widths!' do
6
-
7
6
  let(:header) { ['h1', 'h2', 'h3'] }
8
- let(:rows) { [['a1', 'a2', 'a3'], ['b1', 'b2', 'b3']] }
9
-
10
- let(:table) {
11
- stub(:table, :column_widths => column_widths,
12
- :header => header, :to_a => rows).as_null_object
13
- }
7
+ let(:rows) { [['a1', 'a2', 'a3'], ['b1', 'b2', 'b3']] }
8
+ let(:table) { TTY::Table.new header, rows }
14
9
 
15
10
  subject { described_class.new table }
16
11
 
17
- context 'with column_widths' do
18
- let(:column_widths) { [5,5,5] }
19
-
20
- it '' do
21
- subject.extract_widths!
22
- subject.column_widths.should == column_widths
23
- end
24
-
12
+ it 'extract widths' do
13
+ expect(subject.extract_widths).to eql([2,2,2])
25
14
  end
26
15
  end
@@ -0,0 +1,15 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'spec_helper'
4
+
5
+ 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,14 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'spec_helper'
4
+
5
+ describe TTY::Table, '#data' do
6
+ let(:header) { ['h1', 'h2', 'h3'] }
7
+ let(:rows) { [['a1', 'a2', 'a3'], ['b1', 'b2', 'b3']] }
8
+
9
+ subject { described_class.new header, rows }
10
+
11
+ it 'gets all table data' do
12
+ expect(subject.data).to eql([header] + rows)
13
+ end
14
+ end
@@ -4,8 +4,9 @@ require 'spec_helper'
4
4
 
5
5
  describe TTY::Table, '#each' do
6
6
  let(:object) { described_class.new header, rows }
7
- let(:header) { ['Header1', 'Header2'] }
8
- let(:rows) { [['a1', 'a2'], ['b1', 'b2']] }
7
+ let(:header) { ['Header1'] }
8
+ let(:rows) { [['a1'], ['b1']] }
9
+ let(:field) { TTY::Table::Field }
9
10
 
10
11
  context 'with no block' do
11
12
  subject { object.each }
@@ -18,13 +19,25 @@ describe TTY::Table, '#each' do
18
19
  end
19
20
 
20
21
  context 'with block' do
21
- subject { object.each { |row| yields << row } }
22
22
  let(:yields) { [] }
23
23
 
24
+ subject { object.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(object.data)
35
+ end
36
+
24
37
  it 'yields each row' do
25
38
  expect { subject }.to change { yields }.
26
39
  from( [] ).
27
- to( rows )
40
+ to( object.data )
28
41
  end
29
42
  end
30
43
  end
@@ -2,10 +2,14 @@
2
2
 
3
3
  require 'spec_helper'
4
4
 
5
+ def to_field(value)
6
+ end
7
+
5
8
  describe TTY::Table, '#each_with_index' do
6
9
  let(:object) { described_class.new header, rows }
7
10
  let(:header) { ['Header1', 'Header2'] }
8
11
  let(:rows) { [['a1', 'a2'], ['b1', 'b2']] }
12
+ let(:field) { TTY::Table::Field }
9
13
 
10
14
  context 'with no block' do
11
15
  subject { object.each_with_index }
@@ -17,14 +21,38 @@ describe TTY::Table, '#each_with_index' do
17
21
  end
18
22
 
19
23
  context 'with block' do
20
- subject { object.each_with_index { |el, row, col| yields << [el, row, col]}}
21
24
  let(:yields) { [] }
22
- let(:expected) { [['a1', 0, 0], ['a2', 0, 1], ['b1', 1, 0], ['b2', 1, 1]] }
23
25
 
24
- it "" do
25
- expect { subject }.to change { yields }.
26
- from( [] ).
27
- to( expected )
26
+ subject { object.each_with_index { |el, row, col| yields << [el, row, col]}}
27
+
28
+ context 'without header' do
29
+ let(:header) { nil }
30
+
31
+ let(:expected) {
32
+ [ [field.new('a1'), 0, 0], [field.new('a2'), 0, 1],
33
+ [field.new('b1'), 1, 0], [field.new('b2'), 1, 1] ]
34
+ }
35
+
36
+ it "yields rows with expected data" do
37
+ expect { subject }.to change { yields }.
38
+ from( [] ).
39
+ to( expected )
40
+ end
41
+ end
42
+
43
+ context 'with header' do
44
+
45
+ let(:expected) {
46
+ [ [field.new('Header1'), 0, 0], [field.new('Header2'), 0, 1],
47
+ [field.new('a1'), 1, 0], [field.new('a2'), 1, 1],
48
+ [field.new('b1'), 2, 0], [field.new('b2'), 2, 1] ]
49
+ }
50
+
51
+ it "yields header and rows with expected data" do
52
+ expect { subject }.to change { yields }.
53
+ from( [] ).
54
+ to( expected )
55
+ end
28
56
  end
29
57
  end
30
58
  end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'spec_helper'
4
+
5
+ describe TTY::Table::Field, '#lines' 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 { should eql(5) }
14
+ end
15
+
16
+ context 'with unescaped value' do
17
+ let(:value) { "Multi\\nLine" }
18
+
19
+ it { should eql(11) }
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'spec_helper'
4
+
5
+ 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 { should eql(["Multi", "Line"]) }
14
+ end
15
+
16
+ context 'with unescaped value' do
17
+ let(:value) { "Multi\\nLine" }
18
+
19
+ it { should eql(["Multi\\nLine"]) }
20
+ end
21
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'spec_helper'
4
+
5
+ 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
+ table.render do |renderer|
16
+ renderer.filter = filter
17
+ end.should == <<-EOS.normalize
18
+ h1 h2 h3
19
+ a1 A2 a3
20
+ b1 B2 b3
21
+ EOS
22
+ end
23
+ end
@@ -6,7 +6,7 @@ describe TTY::Table::Header, '#call' do
6
6
  let(:object) { described_class.new(attributes) }
7
7
  let(:attributes) { [:id, :name, :age] }
8
8
 
9
- subject { object.call(attribute) }
9
+ subject { object[attribute] }
10
10
 
11
11
  context 'with a known attribute' do
12
12
  context 'when symbol' do
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'spec_helper'
4
+
5
+ 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
@@ -15,12 +15,12 @@ describe TTY::Table, '#initialize' do
15
15
  context 'with rows only' do
16
16
  it 'initializes with rows as arguments' do
17
17
  table = TTY::Table[*rows]
18
- table.to_a.should == rows
18
+ expect(table.to_a).to eql rows
19
19
  end
20
20
 
21
21
  it 'initializes with rows' do
22
22
  table = TTY::Table.new rows
23
- table.to_a.should == rows
23
+ expect(table.to_a).to eql(rows)
24
24
  end
25
25
 
26
26
  it 'initializes table rows as an option' do
@@ -69,20 +69,20 @@ describe TTY::Table, '#initialize' do
69
69
  context 'with header and rows' do
70
70
  it 'initializes header as an option' do
71
71
  table = TTY::Table.new :header => header
72
- table.header.should == header
72
+ expect(table.header).to eql header
73
73
  end
74
74
 
75
75
  it 'initializes table rows as an argument' do
76
76
  table = TTY::Table.new header, rows
77
- table.header.should == header
78
- table.to_a.should == rows
77
+ expect(table.header).to eql(header)
78
+ expect(table.rows).to eql(rows)
79
79
  end
80
80
  end
81
81
 
82
82
  context 'coercion' do
83
83
  it 'converts row arguments from hash to array' do
84
84
  table = TTY::Table.new :rows => {:a => 1, :b => 2}
85
- table.to_a.should include [:a, 1 ]
85
+ expect(table.to_a).to include [:a, 1 ]
86
86
  end
87
87
  end
88
88
  end
@@ -0,0 +1,39 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'spec_helper'
4
+
5
+ describe TTY::Table::Operation::AlignmentSet, '#call' do
6
+ let(:object) { described_class.new alignments, widths }
7
+ let(:value) { 'a1' }
8
+ let(:field) { TTY::Table::Field.new(value)}
9
+
10
+ subject { object.call(field, 0, 0) }
11
+
12
+ context 'aligned with column widths and no alignments' do
13
+ let(:alignments) { [] }
14
+ let(:widths) { [4, 4] }
15
+
16
+ it { should == "#{value} " }
17
+ end
18
+
19
+ context 'aligned with column widths and alignments' do
20
+ let(:alignments) { [:right, :left] }
21
+ let(:widths) { [4, 4] }
22
+
23
+ it { should == " #{value}" }
24
+ end
25
+
26
+ context 'aligned with no column widths and no alignments' do
27
+ let(:alignments) { [] }
28
+ let(:widths) { [] }
29
+
30
+ it { should == "#{value}" }
31
+ end
32
+
33
+ context 'aligned with no column widths and alignments' do
34
+ let(:alignments) { [:right, :left] }
35
+ let(:widths) { [] }
36
+
37
+ it { should == "#{value}" }
38
+ end
39
+ end
@@ -0,0 +1,16 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'spec_helper'
4
+
5
+ describe TTY::Table::Operation::Escape, '#call' do
6
+ let(:object) { described_class }
7
+ let(:text) { "太丸\nゴシ\tック体\r" }
8
+ let(:field) { TTY::Table::Field.new(text) }
9
+
10
+ subject { object.new }
11
+
12
+ it 'changes field value' do
13
+ subject.call(field, 0, 0)
14
+ expect(field.value).to eql("太丸\\nゴシ\\tック体\\r")
15
+ end
16
+ end
@@ -0,0 +1,17 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'spec_helper'
4
+
5
+ describe TTY::Table::Operation::Filter, '#call' do
6
+ let(:object) { described_class }
7
+ let(:field) { TTY::Table::Field.new('a1') }
8
+ let(:filter) { Proc.new { |val, row, col| 'new' } }
9
+ let(:value) { 'new' }
10
+
11
+ subject { object.new(filter) }
12
+
13
+ it 'changes field value' do
14
+ subject.call(field, 0, 0)
15
+ expect(field.value).to eql(value)
16
+ end
17
+ end
@@ -3,25 +3,30 @@
3
3
  require 'spec_helper'
4
4
 
5
5
  describe TTY::Table::Operation::Truncation, '#call' do
6
- let(:object) { described_class.new }
7
- let(:text) { '太丸ゴシック体' }
8
- let(:row) { [TTY::Table::Field.new(text), TTY::Table::Field.new(text)] }
6
+ let(:object) { described_class.new column_widths }
7
+ let(:text) { '太丸ゴシック体' }
8
+ let(:field) { TTY::Table::Field.new(text) }
9
9
 
10
10
  context 'without column width' do
11
+ let(:column_widths) { [] }
12
+
11
13
  it "truncates string" do
12
- object.call(row)
13
- expect(row[0]).to eql(text)
14
- expect(row[1]).to eql(text)
14
+ object.call(field, 0, 0)
15
+ expect(field.value).to eql(text)
15
16
  end
16
17
  end
17
18
 
18
19
  context 'with column width ' do
19
20
  let(:column_widths) { [3, 6] }
20
21
 
21
- it "truncates string" do
22
- object.call(row, :column_widths => column_widths)
23
- expect(row[0]).to eql('太丸…')
24
- expect(row[1]).to eql('太丸ゴシッ…')
22
+ it "truncates string for 0 column" do
23
+ object.call(field, 0, 0)
24
+ expect(field.value).to eql('太丸…')
25
+ end
26
+
27
+ it "truncates string for 1 column" do
28
+ object.call(field, 0, 1)
29
+ expect(field.value).to eql('太丸ゴシッ…')
25
30
  end
26
31
  end
27
32
  end
@@ -3,7 +3,7 @@
3
3
  require 'spec_helper'
4
4
 
5
5
  describe TTY::Table::Operation::Truncation, '#truncate' do
6
- let(:instance) { described_class.new }
6
+ let(:instance) { described_class.new [] }
7
7
  let(:text) { '太丸ゴシック体' }
8
8
 
9
9
  subject { instance.truncate(text, width) }
@@ -3,25 +3,30 @@
3
3
  require 'spec_helper'
4
4
 
5
5
  describe TTY::Table::Operation::Wrapped, '#call' do
6
- let(:object) { described_class.new }
7
- let(:text) { 'ラドクリフ、マラソン五輪代表に1万m出場にも含み' }
8
- let(:row) { [TTY::Table::Field.new(text), TTY::Table::Field.new(text)] }
6
+ let(:object) { described_class.new column_widths }
7
+ let(:text) { 'ラドクリフ、マラソン五輪代表に1万m出場にも含み' }
8
+ let(:field) { TTY::Table::Field.new(text) }
9
9
 
10
10
  context 'without column width' do
11
+ let(:column_widths) { [] }
12
+
11
13
  it "doesn't wrap string" do
12
- object.call(row)
13
- expect(row[0]).to eql(text)
14
- expect(row[1]).to eql(text)
14
+ object.call(field, 0, 0)
15
+ expect(field.value).to eql(text)
15
16
  end
16
17
  end
17
18
 
18
19
  context 'with column width' do
19
20
  let(:column_widths) { [12, 14] }
20
21
 
21
- it "truncates string" do
22
- object.call(row, :column_widths => column_widths)
23
- expect(row[0]).to eql("ラドクリフ、マラソン五輪\n代表に1万m出場にも含み")
24
- expect(row[1]).to eql("ラドクリフ、マラソン五輪代表\nに1万m出場にも含み")
22
+ it "wraps string for 0 column" do
23
+ object.call(field, 0, 0)
24
+ expect(field.value).to eql("ラドクリフ、マラソン五輪\n代表に1万m出場にも含み")
25
+ end
26
+
27
+ it "wraps string for 1 column" do
28
+ object.call(field, 0, 1)
29
+ expect(field.value).to eql("ラドクリフ、マラソン五輪代表\nに1万m出場にも含み")
25
30
  end
26
31
  end
27
32
  end
@@ -3,7 +3,7 @@
3
3
  require 'spec_helper'
4
4
 
5
5
  describe TTY::Table::Operation::Wrapped, '#wrap' do
6
- let(:instance) { described_class.new }
6
+ let(:instance) { described_class.new [] }
7
7
  let(:text) { 'ラドクリフ、マラソン五輪代表に1万m出場にも含み' }
8
8
 
9
9
  subject { instance.wrap(text, width) }
@@ -8,8 +8,8 @@ describe TTY::Table::Operations, '#new' do
8
8
  let(:table) { TTY::Table.new :rows => [row] }
9
9
  let(:callable) {
10
10
  Class.new do
11
- def call(row, options)
12
- row.map! { |r| r + 1}
11
+ def call(val, row, col)
12
+ val.value= val.value + 1
13
13
  end
14
14
  end
15
15
  }
@@ -26,7 +26,7 @@ describe TTY::Table::Operations, '#new' do
26
26
  end
27
27
 
28
28
  it 'runs selected operations' do
29
- subject.run_operations(:alignment, row)
30
- expect(row).to eql([2,3,4])
29
+ subject.run_operations(:alignment)
30
+ expect(table.data[0]).to eql([2,3,4])
31
31
  end
32
32
  end
@@ -21,33 +21,5 @@ describe TTY::Table, 'options' do
21
21
 
22
22
  its(:rows) { should == rows }
23
23
 
24
- its(:border) { should be_kind_of TTY::Table::BorderOptions }
25
-
26
24
  its(:orientation) { should be_kind_of TTY::Table::Orientation::Horizontal }
27
-
28
- its(:column_widths) { should be_empty }
29
-
30
- its(:column_aligns) { should eql(aligns) }
31
-
32
- it { subject.column_aligns.to_a.should be_empty }
33
-
34
- context '#column_widths' do
35
- let(:widths) { [10, 10] }
36
-
37
- its(:column_widths) { should == widths }
38
- end
39
-
40
- context '#column_widths empty' do
41
- let(:widths) { [] }
42
-
43
- it { expect { subject }.to raise_error(TTY::InvalidArgument) }
44
- end
45
-
46
- context '#column_aligns' do
47
- let(:aligns) { [:center, :center] }
48
-
49
- it 'unwraps original array' do
50
- subject.column_aligns.to_a.should == aligns
51
- end
52
- end
53
25
  end