text-table 1.2.2 → 1.2.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,113 @@
1
+ require 'integration_helper'
2
+
3
+ describe Text::Table do
4
+ let(:table) { Text::Table.new :rows => @rows, :head => @head, :foot => @foot,
5
+ :horizontal_boundary => horizontal_boundary,
6
+ :vertical_boundary => vertical_boundary,
7
+ :boundary_intersection => boundary_intersection,
8
+ :horizontal_padding => horizontal_padding }
9
+ let(:horizontal_boundary) { nil }
10
+ let(:vertical_boundary) { nil }
11
+ let(:boundary_intersection) { nil }
12
+ let(:horizontal_padding) { nil }
13
+ subject { table.to_s }
14
+
15
+ describe 'horizontal boundaries' do
16
+ context 'when ":"' do
17
+ let(:horizontal_boundary) { ':' }
18
+
19
+ it { should == deindent(%q{
20
+ +------+------+------+------+
21
+ : a : bb : ccc : dddd :
22
+ +------+------+------+------+
23
+ : aa : bbb : cccc : d :
24
+ : aaa : bbbb : c : dd :
25
+ +------+------+------+------+
26
+ : aaaa : b : cc : ddd :
27
+ +------+------+------+------+
28
+ }) }
29
+ end
30
+
31
+ context 'when "||" and boundary intersection is "++"' do
32
+ let(:horizontal_boundary) { '||' }
33
+ let(:boundary_intersection) { '++' }
34
+
35
+ it { should == deindent(%q{
36
+ ++------++------++------++------++
37
+ || a || bb || ccc || dddd ||
38
+ ++------++------++------++------++
39
+ || aa || bbb || cccc || d ||
40
+ || aaa || bbbb || c || dd ||
41
+ ++------++------++------++------++
42
+ || aaaa || b || cc || ddd ||
43
+ ++------++------++------++------++
44
+ }) }
45
+
46
+ context 'with spanned cells' do
47
+ before do
48
+ table.rows << :separator
49
+ table.rows << [{ :value => 'x', :colspan => 2, :align => :right }, 'c', 'd']
50
+ end
51
+
52
+ it { should == deindent(%q{
53
+ ++------++------++------++------++
54
+ || a || bb || ccc || dddd ||
55
+ ++------++------++------++------++
56
+ || aa || bbb || cccc || d ||
57
+ || aaa || bbbb || c || dd ||
58
+ ++------++------++------++------++
59
+ || x || c || d ||
60
+ ++------++------++------++------++
61
+ || aaaa || b || cc || ddd ||
62
+ ++------++------++------++------++
63
+ }) }
64
+ end
65
+
66
+ end
67
+ end
68
+
69
+ describe 'vertical boundaries when "="' do
70
+ let(:vertical_boundary) { '=' }
71
+
72
+ it { should == deindent(%q{
73
+ +======+======+======+======+
74
+ | a | bb | ccc | dddd |
75
+ +======+======+======+======+
76
+ | aa | bbb | cccc | d |
77
+ | aaa | bbbb | c | dd |
78
+ +======+======+======+======+
79
+ | aaaa | b | cc | ddd |
80
+ +======+======+======+======+
81
+ }) }
82
+ end
83
+
84
+ describe 'boundary intersections when "*"' do
85
+ let(:boundary_intersection) { '*' }
86
+
87
+ it { should == deindent(%q{
88
+ *------*------*------*------*
89
+ | a | bb | ccc | dddd |
90
+ *------*------*------*------*
91
+ | aa | bbb | cccc | d |
92
+ | aaa | bbbb | c | dd |
93
+ *------*------*------*------*
94
+ | aaaa | b | cc | ddd |
95
+ *------*------*------*------*
96
+ }) }
97
+ end
98
+
99
+ describe 'horizantal padding when 3 spaces' do
100
+ let(:horizontal_padding) { 3 }
101
+
102
+ it { should == deindent(%q{
103
+ +----------+----------+----------+----------+
104
+ | a | bb | ccc | dddd |
105
+ +----------+----------+----------+----------+
106
+ | aa | bbb | cccc | d |
107
+ | aaa | bbbb | c | dd |
108
+ +----------+----------+----------+----------+
109
+ | aaaa | b | cc | ddd |
110
+ +----------+----------+----------+----------+
111
+ }) }
112
+ end
113
+ end
@@ -0,0 +1,226 @@
1
+ require 'integration_helper'
2
+
3
+ describe Text::Table do
4
+ subject { table.to_s }
5
+ let(:table) { Text::Table.new :rows => @rows, :head => @head, :foot => @foot }
6
+
7
+ describe 'first column spanned' do
8
+ context '2 cells wide' do
9
+ before do
10
+ table.rows << [{ :value => 'x', :colspan => 2, :align => align }, 'c', 'd']
11
+ end
12
+
13
+ context 'aligned to the left' do
14
+ let(:align) { :left }
15
+
16
+ it { should == deindent(%q{
17
+ +------+------+------+------+
18
+ | a | bb | ccc | dddd |
19
+ +------+------+------+------+
20
+ | aa | bbb | cccc | d |
21
+ | aaa | bbbb | c | dd |
22
+ | x | c | d |
23
+ +------+------+------+------+
24
+ | aaaa | b | cc | ddd |
25
+ +------+------+------+------+
26
+ }) }
27
+ end
28
+
29
+ context 'center aligned' do
30
+ let(:align) { :center }
31
+
32
+ it { should == deindent(%q{
33
+ +------+------+------+------+
34
+ | a | bb | ccc | dddd |
35
+ +------+------+------+------+
36
+ | aa | bbb | cccc | d |
37
+ | aaa | bbbb | c | dd |
38
+ | x | c | d |
39
+ +------+------+------+------+
40
+ | aaaa | b | cc | ddd |
41
+ +------+------+------+------+
42
+ }) }
43
+ end
44
+
45
+ context 'aligned to the right' do
46
+ let(:align) { :right }
47
+
48
+ it { should == deindent(%q{
49
+ +------+------+------+------+
50
+ | a | bb | ccc | dddd |
51
+ +------+------+------+------+
52
+ | aa | bbb | cccc | d |
53
+ | aaa | bbbb | c | dd |
54
+ | x | c | d |
55
+ +------+------+------+------+
56
+ | aaaa | b | cc | ddd |
57
+ +------+------+------+------+
58
+ }) }
59
+ end
60
+ end
61
+
62
+ context '3 cells wide' do
63
+ before do
64
+ table.rows << [{ :value => 'x', :colspan => 3, :align => align }, 'd']
65
+ end
66
+
67
+ context 'aligned to the left' do
68
+ let(:align) { :left }
69
+
70
+ it { should == deindent(%q{
71
+ +------+------+------+------+
72
+ | a | bb | ccc | dddd |
73
+ +------+------+------+------+
74
+ | aa | bbb | cccc | d |
75
+ | aaa | bbbb | c | dd |
76
+ | x | d |
77
+ +------+------+------+------+
78
+ | aaaa | b | cc | ddd |
79
+ +------+------+------+------+
80
+ }) }
81
+ end
82
+
83
+ context 'center aligned' do
84
+ let(:align) { :center }
85
+
86
+ it { should == deindent(%q{
87
+ +------+------+------+------+
88
+ | a | bb | ccc | dddd |
89
+ +------+------+------+------+
90
+ | aa | bbb | cccc | d |
91
+ | aaa | bbbb | c | dd |
92
+ | x | d |
93
+ +------+------+------+------+
94
+ | aaaa | b | cc | ddd |
95
+ +------+------+------+------+
96
+ }) }
97
+ end
98
+
99
+ context 'aligned to the right' do
100
+ let(:align) { :right }
101
+
102
+ it { should == deindent(%q{
103
+ +------+------+------+------+
104
+ | a | bb | ccc | dddd |
105
+ +------+------+------+------+
106
+ | aa | bbb | cccc | d |
107
+ | aaa | bbbb | c | dd |
108
+ | x | d |
109
+ +------+------+------+------+
110
+ | aaaa | b | cc | ddd |
111
+ +------+------+------+------+
112
+ }) }
113
+ end
114
+ end
115
+ end
116
+
117
+ describe 'last column spanned' do
118
+ context '2 cells wide' do
119
+ before do
120
+ table.rows << ['a', 'b', { :value => 'x', :colspan => 2, :align => align }]
121
+ end
122
+
123
+ context 'aligned to the left' do
124
+ let(:align) { :left }
125
+
126
+ it { should == deindent(%q{
127
+ +------+------+------+------+
128
+ | a | bb | ccc | dddd |
129
+ +------+------+------+------+
130
+ | aa | bbb | cccc | d |
131
+ | aaa | bbbb | c | dd |
132
+ | a | b | x |
133
+ +------+------+------+------+
134
+ | aaaa | b | cc | ddd |
135
+ +------+------+------+------+
136
+ }) }
137
+ end
138
+
139
+ context 'center aligned' do
140
+ let(:align) { :center }
141
+
142
+ it { should == deindent(%q{
143
+ +------+------+------+------+
144
+ | a | bb | ccc | dddd |
145
+ +------+------+------+------+
146
+ | aa | bbb | cccc | d |
147
+ | aaa | bbbb | c | dd |
148
+ | a | b | x |
149
+ +------+------+------+------+
150
+ | aaaa | b | cc | ddd |
151
+ +------+------+------+------+
152
+ }) }
153
+ end
154
+
155
+ context 'aligned to the right' do
156
+ let(:align) { :right }
157
+
158
+ it { should == deindent(%q{
159
+ +------+------+------+------+
160
+ | a | bb | ccc | dddd |
161
+ +------+------+------+------+
162
+ | aa | bbb | cccc | d |
163
+ | aaa | bbbb | c | dd |
164
+ | a | b | x |
165
+ +------+------+------+------+
166
+ | aaaa | b | cc | ddd |
167
+ +------+------+------+------+
168
+ }) }
169
+ end
170
+ end
171
+
172
+ context '3 cells wide' do
173
+ before do
174
+ table.rows << ['a', { :value => 'x', :colspan => 3, :align => align }]
175
+ end
176
+
177
+ context 'aligned to the left' do
178
+ let(:align) { :left }
179
+
180
+ it { should == deindent(%q{
181
+ +------+------+------+------+
182
+ | a | bb | ccc | dddd |
183
+ +------+------+------+------+
184
+ | aa | bbb | cccc | d |
185
+ | aaa | bbbb | c | dd |
186
+ | a | x |
187
+ +------+------+------+------+
188
+ | aaaa | b | cc | ddd |
189
+ +------+------+------+------+
190
+ }) }
191
+ end
192
+
193
+ context 'center aligned' do
194
+ let(:align) { :center }
195
+
196
+ it { should == deindent(%q{
197
+ +------+------+------+------+
198
+ | a | bb | ccc | dddd |
199
+ +------+------+------+------+
200
+ | aa | bbb | cccc | d |
201
+ | aaa | bbbb | c | dd |
202
+ | a | x |
203
+ +------+------+------+------+
204
+ | aaaa | b | cc | ddd |
205
+ +------+------+------+------+
206
+ }) }
207
+ end
208
+
209
+ context 'aligned to the right' do
210
+ let(:align) { :right }
211
+
212
+ it { should == deindent(%q{
213
+ +------+------+------+------+
214
+ | a | bb | ccc | dddd |
215
+ +------+------+------+------+
216
+ | aa | bbb | cccc | d |
217
+ | aaa | bbbb | c | dd |
218
+ | a | x |
219
+ +------+------+------+------+
220
+ | aaaa | b | cc | ddd |
221
+ +------+------+------+------+
222
+ }) }
223
+ end
224
+ end
225
+ end
226
+ end
@@ -0,0 +1,68 @@
1
+ require 'integration_helper'
2
+
3
+ describe Text::Table do
4
+ let(:table) { Text::Table.new :rows => rows, :head => head, :foot => foot }
5
+ let(:rows) { @rows }
6
+ let(:head) { nil }
7
+ let(:foot) { nil }
8
+ subject { table.to_s }
9
+
10
+ describe 'rows' do
11
+ it { should == deindent(%q{
12
+ +-----+------+------+----+
13
+ | aa | bbb | cccc | d |
14
+ | aaa | bbbb | c | dd |
15
+ +-----+------+------+----+
16
+ }) }
17
+
18
+ context 'when nothing is passed into the contructor' do
19
+ let(:rows) { nil }
20
+
21
+ it 'is an empty array' do
22
+ table.rows.should == []
23
+ end
24
+ end
25
+
26
+ context 'with unequal number of cells' do
27
+ pending
28
+ end
29
+ end
30
+
31
+ describe 'head' do
32
+ let(:head) { @head }
33
+
34
+ it { should == deindent(%q{
35
+ +-----+------+------+------+
36
+ | a | bb | ccc | dddd |
37
+ +-----+------+------+------+
38
+ | aa | bbb | cccc | d |
39
+ | aaa | bbbb | c | dd |
40
+ +-----+------+------+------+
41
+ }) }
42
+ end
43
+
44
+ describe 'foot' do
45
+ let(:foot) { @foot }
46
+
47
+ it { should == deindent(%q{
48
+ +------+------+------+-----+
49
+ | aa | bbb | cccc | d |
50
+ | aaa | bbbb | c | dd |
51
+ +------+------+------+-----+
52
+ | aaaa | b | cc | ddd |
53
+ +------+------+------+-----+
54
+ }) }
55
+ end
56
+
57
+ describe 'separators' do
58
+ let(:rows) { @rows.insert(1, :separator) }
59
+
60
+ it { should == deindent(%q{
61
+ +-----+------+------+----+
62
+ | aa | bbb | cccc | d |
63
+ +-----+------+------+----+
64
+ | aaa | bbbb | c | dd |
65
+ +-----+------+------+----+
66
+ }) }
67
+ end
68
+ end
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+ require 'benchmark'
3
+
4
+ describe Text::Table, 'performance' do
5
+ it 'is linear relative to row count' do
6
+ base = time_to_render_num_of_rows 30
7
+ time = time_to_render_num_of_rows 300
8
+
9
+ time.should_not > base * 12
10
+ end
11
+
12
+ def time_to_render_num_of_rows(num)
13
+ GC.start
14
+
15
+ Benchmark.realtime do
16
+ Text::Table.new(rows: Array.new(num)).to_s
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,13 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.configure do |config|
4
+ config.before do
5
+ @head = %w( a bb ccc dddd )
6
+ @rows = [
7
+ %w( aa bbb cccc d ),
8
+ %w( aaa bbbb c dd ),
9
+ ]
10
+ @foot = %w( aaaa b cc ddd )
11
+ @table = Text::Table.new :rows => @rows, :head => @head, :foot => @foot
12
+ end
13
+ end
@@ -1,15 +1,9 @@
1
1
  $LOAD_PATH.unshift(File.dirname(__FILE__))
2
2
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
3
  require 'text-table'
4
- require 'spec'
5
- require 'spec/autorun'
4
+ require 'rspec'
5
+ require 'rspec/autorun'
6
6
 
7
- Spec::Runner.configure do |config|
8
-
9
- end
10
-
11
- class String
12
- def deindent
13
- gsub /^[ \t]*/, ''
14
- end
7
+ def deindent(table)
8
+ table.gsub(/^\s*/, '')
15
9
  end