tablegen 0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,87 @@
1
+ # Instances of this class are created automatically by {TableGen#column TableGen#column}.
2
+ class TableGen::Column
3
+ # The column's index.
4
+ #
5
+ # @return [Fixnum]
6
+ attr_reader :index
7
+
8
+ # The alignment of the row fields.
9
+ # Possible values:
10
+ # - :left
11
+ # - :center
12
+ # - :right
13
+ #
14
+ # (Defaults to *:left*)
15
+ #
16
+ # @return [Symbol]
17
+ # @see #header_alignment
18
+ attr_accessor :alignment
19
+
20
+ # Whether the column can be hidden to respect the table's width constraint.
21
+ # (Defaults to *false*)
22
+ #
23
+ # @return [Boolean]
24
+ attr_accessor :collapse
25
+
26
+ # The row formatter. The default block converts the original data to a {String}.
27
+ #
28
+ # @example Progress Bar
29
+ # # formats 0.4 to [#### ]
30
+ # column.format = proc {|fraction, width_hint|
31
+ # fill_width = width_hint - 2 # bar borders
32
+ # repeat = fraction * fill_width
33
+ # "[%-#{fill_width}s]" % ['#' * repeat]
34
+ # }
35
+ # # works best with:
36
+ # column.min_width = 12
37
+ # column.stretch = true
38
+ #
39
+ # @param [Object] data whatever you passed to {TableGen#row}
40
+ # @param [Fixnum] width_hint
41
+ # @return [Proc]
42
+ attr_accessor :format
43
+
44
+ # The alignment of the header fields. Possible values:
45
+ # - :auto (row alignment)
46
+ # - :left
47
+ # - :center
48
+ # - :right
49
+ #
50
+ # (Defaults to *:auto*)
51
+ #
52
+ # @return [Symbol]
53
+ #
54
+ # @see #alignment
55
+ attr_accessor :header_alignment
56
+
57
+ # The column's minimum width (in characters).
58
+ # (Defaults to *0*)
59
+ #
60
+ # @return [Fixnum]
61
+ attr_accessor :min_width
62
+
63
+ # The field padding character.
64
+ # (Defaults to a space)
65
+ #
66
+ # @return [String]
67
+ attr_accessor :padding
68
+
69
+ # Whether to stretch the column to fill the table's width constraint.
70
+ # (Defaults to *false*)
71
+ #
72
+ # @return [Boolean]
73
+ attr_accessor :stretch
74
+
75
+ # @api private
76
+ def initialize(index)
77
+ @index = index
78
+
79
+ @alignment = :left
80
+ @collapse = false
81
+ @format = proc {|data| data.to_s }
82
+ @header_alignment = :auto
83
+ @min_width = 0
84
+ @padding = "\x20"
85
+ @stretch = false
86
+ end
87
+ end
@@ -0,0 +1,3 @@
1
+ class TableGen
2
+ VERSION = "0.1"
3
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path '../lib', __FILE__
3
+ $LOAD_PATH.unshift lib unless $LOAD_PATH.include? lib
4
+
5
+ require 'tablegen/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "tablegen"
9
+ spec.version = TableGen::VERSION
10
+ spec.authors = ["cfi30"]
11
+ spec.email = ["tablegen@cfillion.tk"]
12
+ spec.summary = %q{plain text table generator}
13
+ spec.homepage = "https://bitbucket.org/cfi30/tablegen"
14
+ spec.license = "LGPL-3.0+"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency 'minitest', '~> 5.4'
23
+ spec.add_development_dependency "rake"
24
+ end
@@ -0,0 +1,2 @@
1
+ require 'tablegen'
2
+ require 'minitest/autorun'
@@ -0,0 +1,516 @@
1
+ require File.expand_path '../helper', __FILE__
2
+
3
+ class TestTable < MiniTest::Test
4
+ def setup
5
+ @gen = TableGen.new
6
+ end
7
+
8
+ def test_row
9
+ assert_equal 0, @gen.height
10
+ assert_equal 0, @gen.real_height
11
+ assert_empty @gen.to_s
12
+
13
+ @gen.row 'test1', 'test2'
14
+ assert_equal 1, @gen.height
15
+ assert_equal 1, @gen.real_height
16
+
17
+ @gen.row 'test3', 'test4'
18
+ assert_equal 2, @gen.height
19
+ assert_equal 2, @gen.real_height
20
+
21
+ assert_equal \
22
+ 'test1 test2' + $/ +
23
+ 'test3 test4', @gen.to_s
24
+ end
25
+
26
+ def test_width
27
+ assert_equal 0, @gen.width
28
+ assert_equal 0, @gen.real_width
29
+
30
+ @gen.row 'test'
31
+
32
+ assert_equal 4, @gen.width
33
+ assert_equal 4, @gen.real_width
34
+
35
+ @gen.width = 100
36
+
37
+ assert_equal 100, @gen.width
38
+ assert_equal 4, @gen.real_width
39
+ end
40
+
41
+ def test_no_row
42
+ error = assert_raises ArgumentError do
43
+ @gen.row
44
+ end
45
+
46
+ assert_equal 'wrong number of arguments (0 for 1+)', error.message
47
+ end
48
+
49
+ def test_column
50
+ col = @gen.column 0
51
+ assert_equal :left, col.alignment
52
+ assert_equal false, col.collapse
53
+ assert_equal "\x20", col.padding
54
+ assert_equal :auto, col.header_alignment
55
+ assert_equal 0, col.min_width
56
+ assert_equal false, col.stretch
57
+ assert_equal 0, col.index
58
+
59
+ block_param = nil
60
+ @gen.column 0 do |col|
61
+ block_param = col
62
+ end
63
+ assert_equal col, block_param
64
+ end
65
+
66
+ def test_default_format
67
+ col = @gen.column 0
68
+
69
+ input = 'test'
70
+ assert_same input, col.format[input]
71
+
72
+ assert_equal '42', col.format[42]
73
+ end
74
+
75
+ def test_columns
76
+ block_params = []
77
+ @gen.columns 0, 1, 2, 3 do |col|
78
+ col.alignment = :left
79
+ block_params << col
80
+ end
81
+
82
+ cols = []
83
+ 4.times {|i| cols << @gen.column(i) }
84
+
85
+ assert_equal cols, block_params
86
+ end
87
+
88
+ def test_clear
89
+ @gen.row 'test'
90
+
91
+ @gen.column 0 do |col|
92
+ col.padding = 'not cleared'
93
+ end
94
+
95
+ refute_equal 0, @gen.height
96
+ assert_equal 'not cleared', @gen.column(0).padding
97
+
98
+ @gen.clear
99
+
100
+ assert_equal 0, @gen.height
101
+ assert_equal 'not cleared', @gen.column(0).padding
102
+ assert_empty @gen.to_s
103
+ end
104
+
105
+ def test_clear!
106
+ @gen.row 'test'
107
+
108
+ @gen.column 0 do |col|
109
+ col.padding = 'cleared'
110
+ end
111
+
112
+ refute_equal 0, @gen.height
113
+ assert_equal 'cleared', @gen.column(0).padding
114
+
115
+ @gen.clear!
116
+
117
+ assert_empty @gen.to_s
118
+ assert_equal 0, @gen.height
119
+ refute_equal 'cleared', @gen.column(0).padding
120
+ end
121
+
122
+ def test_align_left
123
+ @gen.row 'long_text', 'short'
124
+ @gen.row 'short', 'long_text'
125
+
126
+ assert_equal \
127
+ 'long_text short' + $/ +
128
+ 'short long_text', @gen.to_s
129
+ end
130
+
131
+ def test_align_right
132
+ @gen.row 'long_text', 'short'
133
+ @gen.row 'short', 'long_text'
134
+
135
+ col = @gen.column 1 do |col|
136
+ col.alignment = :right
137
+ end
138
+
139
+ assert_equal \
140
+ 'long_text short' + $/ +
141
+ 'short long_text', @gen.to_s
142
+ end
143
+
144
+ def test_align_center
145
+ @gen.row 'long_text', 'test'
146
+ @gen.row 'short', 'test'
147
+ @gen.row 'test', 'test'
148
+
149
+ col = @gen.column 0 do |col|
150
+ col.alignment = :center
151
+ end
152
+
153
+ assert_equal \
154
+ 'long_text test' + $/ +
155
+ ' short test' + $/ +
156
+ ' test test', @gen.to_s
157
+ end
158
+
159
+ def test_align_other
160
+ @gen.row 'test'
161
+
162
+ col = @gen.column 0 do |col|
163
+ col.alignment = :top
164
+ end
165
+
166
+ error = assert_raises TableGen::Error do
167
+ @gen.to_s
168
+ end
169
+
170
+ assert_equal "invalid alignment 'top'", error.message
171
+ end
172
+
173
+ def test_align_cjk
174
+ @gen.row '新世界より', 'from'
175
+ @gen.row 'the', 'new world'
176
+
177
+ assert_equal \
178
+ '新世界より from' + $/ +
179
+ 'the new world', @gen.to_s
180
+ end
181
+
182
+ def test_custom_padding
183
+ @gen.row 'long_text', 'short'
184
+ @gen.row 'short', 'long_text'
185
+
186
+ @gen.column 0 do |col|
187
+ col.padding = '_'
188
+ end
189
+
190
+ @gen.column 1 do |col|
191
+ col.padding = '-='
192
+ end
193
+
194
+ assert_equal \
195
+ 'long_text short----' + $/ +
196
+ 'short____ long_text', @gen.to_s
197
+ end
198
+
199
+ def test_holes
200
+ @gen.row 'long_text', 'short'
201
+ @gen.row 'short'
202
+
203
+ assert_equal \
204
+ 'long_text short' + $/ +
205
+ 'short', @gen.to_s
206
+ end
207
+
208
+ def test_separator
209
+ @gen.separator '='
210
+ assert_equal '', @gen.to_s
211
+
212
+ @gen.row 'long_text', 'long_text'
213
+ @gen.row'short', 'short'
214
+ @gen.separator '-='
215
+
216
+ assert_equal \
217
+ '===================' + $/ +
218
+ 'long_text long_text' + $/ +
219
+ 'short short' + $/ +
220
+ '-------------------', @gen.to_s
221
+ end
222
+
223
+ def test_separator_fixed_width
224
+ @gen.width = 100
225
+
226
+ assert_equal 0, @gen.height
227
+ @gen.separator '='
228
+ assert_equal 1, @gen.height
229
+
230
+ assert_equal '='*100, @gen.to_s
231
+ end
232
+
233
+ def test_custom_border
234
+ @gen.border = '-|-'
235
+ @gen.row 'long_text', 'short'
236
+ @gen.row 'short', 'long_text'
237
+
238
+ assert_equal \
239
+ 'long_text-|-short' + $/ +
240
+ 'short -|-long_text', @gen.to_s
241
+ end
242
+
243
+ def test_custom_format
244
+ @gen.row 'test', 0.42
245
+ @gen.row 'test', 0.5678
246
+
247
+ sizes = []
248
+ @gen.column 1 do |col|
249
+ col.format = proc {|data, width|
250
+ sizes << width
251
+ "%d%%" % [data * 100]
252
+ }
253
+ end
254
+
255
+ assert_equal \
256
+ 'test 42%' + $/ +
257
+ 'test 56%', @gen.to_s
258
+
259
+ assert_equal [0, 3], sizes.uniq
260
+ end
261
+
262
+ def test_stretch
263
+ @gen.row 'test1', 'test2'
264
+ @gen.row 'test3', 'test4'
265
+
266
+ @gen.column 0 do |col|
267
+ col.stretch = true
268
+ end
269
+
270
+ assert_equal \
271
+ 'test1 test2' + $/ +
272
+ 'test3 test4', @gen.to_s
273
+
274
+ @gen.width = 20
275
+
276
+ assert_equal \
277
+ 'test1 test2' + $/ +
278
+ 'test3 test4', @gen.to_s
279
+ end
280
+
281
+ def test_stretch_long_border
282
+ @gen.row 'test1', 'test2'
283
+ @gen.row 'test3', 'test4'
284
+
285
+ @gen.column 0 do |col|
286
+ col.stretch = true
287
+ end
288
+
289
+ @gen.width = 20
290
+ @gen.border = '-||-'
291
+
292
+ assert_equal \
293
+ 'test1 -||-test2' + $/ +
294
+ 'test3 -||-test4', @gen.to_s
295
+ end
296
+
297
+ def test_stretch_format
298
+ @gen.row '-'
299
+ @gen.row '-', 'test'
300
+
301
+ sizes = []
302
+ @gen.column 0 do |col|
303
+ col.stretch = true
304
+ col.format = proc {|data, width|
305
+ sizes << width
306
+ data * width
307
+ }
308
+ end
309
+
310
+ @gen.width = 20
311
+
312
+ assert_equal \
313
+ '---------------' + $/ +
314
+ '--------------- test', @gen.to_s
315
+
316
+ assert_equal [0, 15], sizes.uniq
317
+ end
318
+
319
+ def test_multi_stretch
320
+ @gen.row 'test', 'test'
321
+
322
+ @gen.column 0 do |col|
323
+ col.stretch = true
324
+ end
325
+
326
+ @gen.column 1 do |col|
327
+ col.stretch = true
328
+ end
329
+
330
+ @gen.width = 20
331
+
332
+ assert_equal 'test test', @gen.to_s
333
+ end
334
+
335
+ def test_table_outgrow
336
+ @gen.width = 2
337
+ @gen.row 'too long'
338
+
339
+ error = assert_raises TableGen::WidthError do
340
+ @gen.to_s
341
+ end
342
+
343
+ assert_equal 'insufficient width to generate the table', error.message
344
+ end
345
+
346
+ def test_stretch_outgrow
347
+ @gen.width = 2
348
+ @gen.row 'too long'
349
+
350
+ @gen.column 0 do |col|
351
+ col.stretch = true
352
+ end
353
+
354
+ error = assert_raises TableGen::WidthError do
355
+ @gen.to_s
356
+ end
357
+
358
+ assert_equal 'insufficient width to generate the table', error.message
359
+ end
360
+
361
+ def test_minimum_width
362
+ @gen.row 'long_text', 'short'
363
+ @gen.row 'short', 'long_text'
364
+
365
+ sizes = []
366
+ @gen.column 0 do |col|
367
+ col.min_width = 15
368
+ col.format = proc {|data, width|
369
+ sizes << width
370
+ data
371
+ }
372
+ end
373
+
374
+ assert_equal \
375
+ 'long_text short' + $/ +
376
+ 'short long_text', @gen.to_s
377
+ assert_equal [15], sizes.uniq
378
+ end
379
+
380
+ def test_collapse
381
+ @gen.row 'column1', 'col2', 'col3'
382
+
383
+ @gen.column 0 do |col|
384
+ col.collapse = true
385
+ end
386
+
387
+ @gen.column 2 do |col|
388
+ col.collapse = true
389
+ end
390
+
391
+ assert_equal 'column1 col2 col3', @gen.to_s
392
+
393
+ @gen.width = 4
394
+ assert_equal 'col2', @gen.to_s
395
+
396
+ @gen.width = 12
397
+ assert_equal 'column1 col2', @gen.to_s
398
+
399
+ @gen.width = 10
400
+ assert_equal 'col2 col3', @gen.to_s
401
+ end
402
+
403
+ def test_collapse_stretch
404
+ @gen.row 'col1', 'col2', 'col3'
405
+ @gen.column 1 do |col|
406
+ col.collapse = true
407
+ col.stretch = true
408
+ end
409
+
410
+ @gen.column 2 do |col|
411
+ col.stretch = true
412
+ col.alignment = :right
413
+ end
414
+
415
+ @gen.width = 12
416
+ assert_equal 'col1 col3', @gen.to_s
417
+ end
418
+
419
+ def test_unused_column
420
+ @gen.column 42
421
+ @gen.row 'test'
422
+ assert_equal 'test', @gen.to_s
423
+ end
424
+
425
+ def test_stretch_empty_column
426
+ @gen.column 0 do |col|
427
+ col.stretch = true
428
+ end
429
+
430
+ @gen.column 42
431
+ @gen.width = 15
432
+ @gen.row 'test1', 'test2'
433
+ assert_equal 'test1 test2', @gen.to_s
434
+ end
435
+
436
+ def test_header
437
+ @gen.column 0 do |col|
438
+ col.alignment = :right
439
+ col.format = proc {|data|
440
+ if data == 'long_text'
441
+ data
442
+ else
443
+ flunk 'format called'
444
+ end
445
+ }
446
+ end
447
+
448
+ @gen.header 'head'
449
+ @gen.row 'long_text'
450
+
451
+ assert_equal \
452
+ ' head' + $/ +
453
+ 'long_text', @gen.to_s
454
+ end
455
+
456
+ def test_header_alignment
457
+ @gen.column 0 do |col|
458
+ col.alignment = :right
459
+ col.header_alignment = :left
460
+ end
461
+
462
+ @gen.header 'test1'
463
+ @gen.row 'long_text'
464
+ @gen.row 'test2'
465
+
466
+ assert_equal \
467
+ 'test1' + $/ +
468
+ 'long_text' + $/ +
469
+ ' test2', @gen.to_s
470
+ end
471
+
472
+ def test_text
473
+ assert_equal 0, @gen.height
474
+ @gen.text "Hello World!\x20\x20"
475
+
476
+ assert_equal 1, @gen.height
477
+ assert_equal 1, @gen.real_height
478
+ assert_equal 0, @gen.width
479
+ assert_equal 12, @gen.real_width
480
+ assert_equal 'Hello World!', @gen.to_s
481
+ end
482
+
483
+ def test_text_chunks
484
+ @gen.text 'long text!'
485
+ @gen.row '123'
486
+
487
+ assert_equal \
488
+ ['lon', 'g t', 'ext', '!'].join($/) + $/ +
489
+ '123', @gen.to_s
490
+
491
+ assert_equal 2, @gen.height
492
+ assert_equal 5, @gen.real_height
493
+ end
494
+
495
+ def test_mulitiline_text_chunks
496
+ @gen.text "long\ntext!"
497
+ @gen.width = 3
498
+
499
+ assert_equal ['lon', 'g', 'tex', 't!'].join($/), @gen.to_s
500
+
501
+ assert_equal 1, @gen.height
502
+ assert_equal 4, @gen.real_height
503
+ end
504
+
505
+ def test_empty_text
506
+ @gen.text ''
507
+ @gen.text '' # last line break is stripped from output
508
+
509
+ assert_equal 2, @gen.height
510
+ assert_equal 1, @gen.real_height
511
+ assert_equal 0, @gen.width
512
+ assert_equal 0, @gen.real_width
513
+
514
+ assert_equal $/, @gen.to_s
515
+ end
516
+ end