tabmani 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/test/helper.rb ADDED
@@ -0,0 +1,33 @@
1
+ require 'simplecov'
2
+
3
+ module SimpleCov::Configuration
4
+ def clean_filters
5
+ @filters = []
6
+ end
7
+ end
8
+
9
+ SimpleCov.configure do
10
+ clean_filters
11
+ load_adapter 'test_frameworks'
12
+ end
13
+
14
+ ENV["COVERAGE"] && SimpleCov.start do
15
+ add_filter "/.rvm/"
16
+ end
17
+ require 'rubygems'
18
+ require 'bundler'
19
+ begin
20
+ Bundler.setup(:default, :development)
21
+ rescue Bundler::BundlerError => e
22
+ $stderr.puts e.message
23
+ $stderr.puts "Run `bundle install` to install missing gems"
24
+ exit e.status_code
25
+ end
26
+ require 'test/unit'
27
+
28
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
29
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
30
+ require 'tabmani'
31
+
32
+ class Test::Unit::TestCase
33
+ end
@@ -0,0 +1,582 @@
1
+ #! /usr/bin/env ruby
2
+ # coding: utf-8
3
+
4
+ require "pp"
5
+ require "helper"
6
+
7
+ TT = Tabmani::Table
8
+
9
+ class TC_Table < Test::Unit::TestCase
10
+
11
+
12
+ def setup
13
+ @t00 = TT.new(matrix: [ %w(a b c), %w(d e f) ])
14
+ @t01 = TT.new(matrix: [ %w(a), %w(d e ) , %w(g h i) ])
15
+ matrix = [
16
+ ['0123', '45', '6789'],
17
+ ['abcd', 'ef', 'ghij'],
18
+ ['a ', 'e ', 'g '],
19
+ ['a cd', ' f', 'gh j'],
20
+ ['abcd', ' ', ' '],
21
+ [' ', ' ', ' hij']
22
+ ]
23
+ @t01 = TT.new(matrix: matrix)
24
+ end
25
+
26
+ def test_print_size
27
+ assert_equal(2, TT.print_size('ab'))
28
+ assert_equal(4, TT.print_size('あい'))
29
+ assert_equal(6, TT.print_size('abあい'))
30
+ end
31
+
32
+ def test_parse_csv
33
+ io = StringIO.new
34
+ io.puts('a,b,c')
35
+ io.puts('d,e,f')
36
+ io.rewind
37
+ results = TT.parse_csv(io)
38
+ corrects = [ %w(a b c), %w(d e f), ]
39
+ assert_equal(corrects, results.matrix)
40
+ end
41
+
42
+ def test_parse_separator
43
+ io = StringIO.new
44
+ io.puts('a | b | c ')
45
+ io.puts('d | e | f ')
46
+ io.rewind
47
+ results = TT.parse_separator(io, '|')
48
+ corrects = [
49
+ ['a ', ' b ', ' c '],
50
+ ['d ', ' e ', ' f ']
51
+ ]
52
+ assert_equal(corrects, results.matrix)
53
+
54
+ io = StringIO.new
55
+ io.puts('a | b | c ')
56
+ io.puts('d | e | f ')
57
+ io.rewind
58
+ results = TT.parse_separator(io, ' ')
59
+ corrects = [ %w(a | b | c), %w(d | e | f), ]
60
+ assert_equal(corrects, results.matrix)
61
+ end
62
+
63
+ def test_parse_blanks_separated_value
64
+ io = StringIO.new
65
+ io.puts('a b c')
66
+ io.puts('d e f')
67
+ io.rewind
68
+ results = TT.parse_blanks_separated_value(io)
69
+ corrects = [ %w(a b c), %w(d e f), ]
70
+ assert_equal(corrects, results.matrix)
71
+
72
+ io = StringIO.new
73
+ io.puts(' a b c')
74
+ io.puts(' d e f')
75
+ io.rewind
76
+ results = TT.parse_blanks_separated_value(io)
77
+ corrects = [ ['a','b','c'], ['d', 'e', 'f'] ]
78
+ assert_equal(corrects, results.matrix)
79
+ assert_equal(2, results.indent)
80
+
81
+ io = StringIO.new
82
+ io.puts('a b c')
83
+ io.puts
84
+ io.puts('d e f')
85
+ io.rewind
86
+ results = TT.parse_blanks_separated_value(io)
87
+ corrects = [ ['a','b','c'], [], ['d', 'e', 'f'] ]
88
+ assert_equal(corrects, results.matrix)
89
+ end
90
+
91
+ def test_parse_column_based_value
92
+ io = StringIO.new
93
+ io.puts('a b c')
94
+ io.puts(' d e f ')
95
+ io.puts()
96
+ io.rewind
97
+ results = TT.parse_column_based_value(io)
98
+ corrects = [ ['a','b','c'], ['d', 'e', 'f'] ]
99
+ assert_equal(corrects, results.matrix)
100
+
101
+ in_io = StringIO.new
102
+ in_io.puts '0123 45 6789'
103
+ in_io.puts 'abcd ef ghij'
104
+ in_io.puts 'a e g '
105
+ in_io.puts 'a cd f gh j'
106
+ in_io.puts 'abcd '
107
+ in_io.puts ' hij'
108
+ in_io.rewind
109
+ c01 = TT.parse_column_based_value(in_io)
110
+ result = c01.matrix
111
+ correct = [
112
+ ['0123', '45', '6789'],
113
+ ['abcd', 'ef', 'ghij'],
114
+ ['a' , 'e' , 'g' ],
115
+ ['a cd', 'f' , 'gh j'],
116
+ ['abcd', '' , '' ],
117
+ ['' , '' , 'hij' ]
118
+ ]
119
+ assert_equal(correct, result)
120
+
121
+ in_io = StringIO.new
122
+ in_io.puts '012'
123
+ in_io.puts 'abcd ef ghij'
124
+ in_io.puts 'a e g '
125
+ in_io.puts 'a cd f gh j'
126
+ in_io.puts 'abcd '
127
+ in_io.puts ' hij'
128
+ in_io.rewind
129
+ c01 = TT.parse_column_based_value(in_io)
130
+ result = c01.matrix
131
+ correct = [
132
+ ['012', '', ''],
133
+ ['abcd', 'ef', 'ghij'],
134
+ ['a' , 'e' , 'g' ],
135
+ ['a cd', 'f' , 'gh j'],
136
+ ['abcd', '' , '' ],
137
+ ['' , '' , 'hij' ]
138
+ ]
139
+ assert_equal(correct, result)
140
+ end
141
+
142
+ def test_parse
143
+ io = StringIO.new
144
+ io.puts('a,b,c')
145
+ io.puts('d,e,f')
146
+ io.rewind
147
+ results = TT.parse(io: io, style: :csv)
148
+ corrects = [ %w(a b c), %w(d e f), ]
149
+ assert_equal(corrects, results.matrix)
150
+
151
+ io = StringIO.new
152
+ io.puts('a b c')
153
+ io.puts('d e f')
154
+ io.rewind
155
+ results = TT.parse(io: io, style: :blank)
156
+ corrects = [ %w(a b c), %w(d e f), ]
157
+ assert_equal(corrects, results.matrix)
158
+
159
+ io = StringIO.new
160
+ io.puts('a b c')
161
+ io.puts(' d e f ')
162
+ io.puts()
163
+ io.rewind
164
+ results = TT.parse(io: io, style: :column)
165
+ corrects = [ ['a','b','c'], ['d', 'e', 'f'] ]
166
+ assert_equal(corrects, results.matrix)
167
+ end
168
+
169
+ def test_dump_csv
170
+ io = StringIO.new
171
+ @t00.dump_csv(io: io)
172
+ io.rewind
173
+ result = io.read
174
+ assert_equal("a,b,c\nd,e,f\n", result)
175
+
176
+ setup
177
+ io = StringIO.new
178
+ @t00.set_title
179
+ @t00.dump_csv(io: io)
180
+ io.rewind
181
+ result = io.read
182
+ assert_equal("a,b,c\nd,e,f\n", result)
183
+ end
184
+
185
+ def test_dump_column_format
186
+ t10 = TT.new(matrix: [ ["a", "ab"], ["abc", "a"], ])
187
+ io = StringIO.new
188
+ t10.dump_column_format(io: io)
189
+ io.rewind
190
+ assert_equal("a ab\nabc a\n", io.read)
191
+
192
+ io = StringIO.new
193
+ t10.dump_column_format(io: io, separator: '|')
194
+ io.rewind
195
+ assert_equal("a |ab\nabc|a\n", io.read)
196
+
197
+ io = StringIO.new
198
+ t10.dump_column_format(io: io, just: :right)
199
+ io.rewind
200
+ assert_equal(" a ab\nabc a\n", io.read)
201
+ io.rewind
202
+
203
+ io = StringIO.new
204
+ @t01 = TT.new(matrix: [ ["abc", "def"], ["あいう", "えおか"], ])
205
+ @t01.dump_column_format(io: io)
206
+ io.rewind
207
+ assert_equal("abc def\nあいう えおか\n", io.read)
208
+
209
+ ## not string
210
+ io = StringIO.new
211
+ t12 = TT.new(matrix: [ [0, 1], [2, 3], ])
212
+ t12.dump_column_format(io: io)
213
+ io.rewind
214
+ assert_equal("0 1\n2 3\n", io.read)
215
+
216
+ ## indent
217
+ t10 = TT.new(matrix: [ ["a", "ab"], ["abc", "a"], ], indent: 2)
218
+ io = StringIO.new
219
+ t10.dump_column_format(io: io)
220
+ io.rewind
221
+ assert_equal(" a ab\n abc a\n", io.read)
222
+
223
+ ## horizontal line
224
+ t10 = TT.new(matrix: [ ["a", "ab"], ["abc", "a"], ])
225
+ t10.add_hline(0)
226
+ t10.add_hline(2)
227
+ io = StringIO.new
228
+ t10.dump_column_format(io: io)
229
+ io.rewind
230
+ assert_equal("------\na ab\nabc a\n------\n", io.read)
231
+
232
+ ## title
233
+ t10 = TT.new(matrix: [ ["a", "ab"], ["abc", "a"], ])
234
+ t10.set_title
235
+ io = StringIO.new
236
+ t10.dump_column_format(io: io)
237
+ io.rewind
238
+ assert_equal("a ab\nabc a\n", io.read)
239
+ end
240
+
241
+ def test_dump_md_simple
242
+ io = StringIO.new
243
+ @t00.dump_md_simple(io: io, just: :left)
244
+ io.rewind
245
+ assert_equal( ["a b c\n", "- - -\n", "d e f\n" ], io.readlines)
246
+ assert_equal( [ %w(a b c), %w(d e f) ], @t00.matrix)
247
+
248
+ setup
249
+ io = StringIO.new
250
+ @t00.set_title
251
+ @t00.dump_md_simple(io: io, just: :left)
252
+ io.rewind
253
+ assert_equal( ["a b c\n", "- - -\n", "d e f\n" ], io.readlines)
254
+ assert_equal( %w(a b c), @t00.titles)
255
+ assert_equal( [ %w(d e f)], @t00.matrix)
256
+ end
257
+
258
+ def test_dump_tex
259
+ io = StringIO.new
260
+ t10 = TT.new(matrix: [ %w(a b c), %w(d e f) , %w(g h i) ])
261
+ t10.dump_tex(io: io, just: :left)
262
+ io.rewind
263
+ corrects = [
264
+ "\\begin{tabular}{lll}\n",
265
+ " \\hline\n",
266
+ " a & b & c \\\\\n",
267
+ " d & e & f \\\\\n",
268
+ " g & h & i \\\\\n",
269
+ " \\hline\n",
270
+ "\\end{tabular}\n"
271
+ ]
272
+ assert_equal( corrects, io.readlines)
273
+ assert_equal( [ %w(a b c), %w(d e f), %w(g h i) ], t10.matrix)
274
+
275
+ io = StringIO.new
276
+ @t01 = TT.new(matrix: [ %w(a), %w(d e ) , %w(g h i) ])
277
+ @t01.dump_tex(io: io, just: :left)
278
+ io.rewind
279
+ corrects = [
280
+ "\\begin{tabular}{lll}\n",
281
+ " \\hline\n",
282
+ " a & & \\\\\n",
283
+ " d & e & \\\\\n",
284
+ " g & h & i \\\\\n",
285
+ " \\hline\n",
286
+ "\\end{tabular}\n"
287
+ ]
288
+ assert_equal( corrects, io.readlines)
289
+ assert_equal( [ %w(a), %w(d e ), %w(g h i) ], @t01.matrix)
290
+
291
+ setup
292
+ io = StringIO.new
293
+ @t01 = TT.new(matrix: [ %w(a), %w(d e ) , %w(g h i) ])
294
+ @t01.set_title
295
+ @t01.dump_tex(io: io, just: :left)
296
+ io.rewind
297
+ corrects = [
298
+ "\\begin{tabular}{lll}\n",
299
+ " \\hline\n",
300
+ " a & & \\\\\n",
301
+ " \\hline\n",
302
+ " d & e & \\\\\n",
303
+ " g & h & i \\\\\n",
304
+ " \\hline\n",
305
+ "\\end{tabular}\n"
306
+ ]
307
+ assert_equal( corrects, io.readlines)
308
+ assert_equal( %w(a), @t01.titles)
309
+ assert_equal( [ %w(d e ), %w(g h i) ], @t01.matrix)
310
+ end
311
+
312
+ def test_padding
313
+ assert_equal( 'abc ', TT.padding(str: 'abc', width: 10, padding: ' '))
314
+ assert_equal( 'abc ', TT.padding(str: 'abc', width: 10, padding: ' ', place: :left))
315
+ assert_equal( ' abc', TT.padding(str: 'abc', width: 10, padding: ' ', place: :right))
316
+ assert_equal( ' abc ', TT.padding(str: 'abc', width: 10, padding: ' ', place: :center))
317
+ end
318
+
319
+ def test_show_keys
320
+ io = StringIO.new
321
+ @t01.show_keys(io: io)
322
+ io.rewind
323
+ result = io.readlines
324
+ assert_equal(
325
+ [ "---- -- ----\n",
326
+ "1 2 3\n"
327
+ ], result
328
+ )
329
+
330
+ t10 = TT.new(matrix: [ %w(a b c), %w(d e f) ], indent: 2)
331
+ io = StringIO.new
332
+ t10.show_keys(io: io)
333
+ io.rewind
334
+ result = io.readlines
335
+ assert_equal(
336
+ [ " - - -\n",
337
+ " 1 2 3\n"
338
+ ], result
339
+ )
340
+ end
341
+
342
+ def test_projection_ary
343
+ text = [
344
+ '0123 45 6789',
345
+ 'abcd ef ghij',
346
+ 'a e g ',
347
+ 'a cd f gh j',
348
+ 'abcd ',
349
+ ' hij',
350
+ ]
351
+
352
+ results = TT.projection_ary(text)
353
+ corrects = [true, true, true, true, false,
354
+ true, true, false,
355
+ true, true, true, true]
356
+ assert_equal(corrects, results)
357
+
358
+ results = TT.projection_ary([])
359
+ corrects = []
360
+ assert_equal(corrects, results)
361
+ end
362
+
363
+ def test_get_ranges
364
+ results = TT.get_ranges([true, true, true, true, false,
365
+ true, true, false,
366
+ true, true, true, true]
367
+ )
368
+ corrects = [ 0..3, 5..6, 8..11 ]
369
+ assert_equal(corrects, results)
370
+ end
371
+
372
+ def test_transpose!
373
+ ##a b
374
+ ##d e f
375
+ ##↓
376
+ ##a d
377
+ ##b e
378
+ ## f
379
+ matrix00 = [ %w(a b), %w(d e f) ]
380
+ t10 = TT.new(matrix: matrix00)
381
+ t10.transpose!
382
+ correct = [ ['a', 'd'], ['b', 'e'], ['', 'f'] ]
383
+ assert_equal(correct, t10.matrix)
384
+ end
385
+
386
+ def test_transpose
387
+ matrix00 = [ %w(a b), %w(d e f) ]
388
+ t10 = TT.new(matrix: matrix00)
389
+ result = t10.transpose
390
+ correct = [ ['a', 'd'], ['b', 'e'], ['', 'f'] ]
391
+ assert_equal(matrix00, t10.matrix)
392
+ assert_equal(correct, result.matrix)
393
+ end
394
+
395
+ def test_filter!
396
+ @t00.filter!('1', 'a')
397
+ assert_equal([ %w(a b c) ], @t00.matrix)
398
+ end
399
+
400
+ def test_filter
401
+ assert_equal([ %w(a b c) ], @t00.filter('1', 'a').matrix)
402
+ assert_equal([ %w(a b c) ], @t00.filter('2', 'b').matrix)
403
+ assert_equal([ %w(a b c) ], @t00.filter('3', 'c').matrix)
404
+ assert_equal([ %w(d e f) ], @t00.filter('1', 'd').matrix)
405
+ assert_equal([ %w(d e f) ], @t00.filter('2', 'e').matrix)
406
+ assert_equal([ %w(d e f) ], @t00.filter('3', 'f').matrix)
407
+ assert_equal([ ], @t00.filter('1', ' ').matrix)
408
+ end
409
+
410
+ def test_analyze
411
+ out_io = StringIO.new
412
+ @t01.analyze(io: out_io, keys: [])
413
+ out_io.rewind
414
+
415
+ corrects = [
416
+ "\n",
417
+ "key head types\n",
418
+ "1 0123 5\n",
419
+ "2 45 5\n",
420
+ "3 6789 6\n",
421
+ ]
422
+ assert_equal(corrects, out_io.readlines)
423
+ end
424
+
425
+ def test_max_lengths
426
+ assert_equal([4, 2, 4], @t01.max_lengths)
427
+
428
+ t10 = TT.new(matrix: [ %w(ABC BC), %w(d e ) , %w(g h ij) ])
429
+ t10.set_title
430
+ assert_equal([3, 2, 2], t10.max_lengths)
431
+ end
432
+
433
+ def test_add_sum
434
+ ## key=1
435
+ io = StringIO.new
436
+ matrix = [
437
+ ['1', 'a', 'a'],
438
+ ['2', 'b', 'a'],
439
+ ['3', 'c', '1'],
440
+ ['4', 'd', '2'],
441
+ ]
442
+ t11 = TT.new(matrix: matrix)
443
+ t11.add_sum('1')
444
+ t11.dump_column_format(io: io)
445
+ io.rewind
446
+ results = io.readlines
447
+ corrects = [
448
+ "1 a a\n",
449
+ "2 b a\n",
450
+ "3 c 1\n",
451
+ "4 d 2\n",
452
+ "------\n",
453
+ "10\n",
454
+ ]
455
+ assert_equal(corrects, results)
456
+
457
+ ## key=2
458
+ io = StringIO.new
459
+ matrix = [
460
+ ['1', 'a', 'a'],
461
+ ['2', 'b', 'a'],
462
+ ['3', 'c', '1'],
463
+ ['4', 'd', '2'],
464
+ ]
465
+ t11 = TT.new(matrix: matrix)
466
+ t11.add_sum('2')
467
+ t11.dump_column_format(io: io)
468
+ io.rewind
469
+ results = io.readlines
470
+ corrects = [
471
+ "1 a a\n",
472
+ "2 b a\n",
473
+ "3 c 1\n",
474
+ "4 d 2\n",
475
+ "-----\n",
476
+ " 0\n",
477
+ ]
478
+ assert_equal(corrects, results)
479
+
480
+ ## key=3
481
+ io = StringIO.new
482
+ matrix = [
483
+ ['1', 'a', 'a'],
484
+ ['2', 'b', 'a'],
485
+ ['3', 'c', '1'],
486
+ ['4', 'd', '2'],
487
+ ]
488
+ t11 = TT.new(matrix: matrix)
489
+ t11.add_sum('3')
490
+ t11.dump_column_format(io: io)
491
+ io.rewind
492
+ results = io.readlines
493
+ corrects = [
494
+ "1 a a\n",
495
+ "2 b a\n",
496
+ "3 c 1\n",
497
+ "4 d 2\n",
498
+ "-----\n",
499
+ " 3\n",
500
+ ]
501
+ assert_equal(corrects, results)
502
+
503
+ ## including float
504
+ io = StringIO.new
505
+ matrix = [
506
+ ['1', 'a', 'a'],
507
+ ['2.0', 'b', 'a'],
508
+ ['3', 'c', '1'],
509
+ ['4', 'd', '2'],
510
+ ]
511
+ t11 = TT.new(matrix: matrix)
512
+ t11.add_sum('1')
513
+ t11.dump_column_format(io: io)
514
+ io.rewind
515
+ results = io.readlines
516
+ corrects = [
517
+ "1 a a\n",
518
+ "2.0 b a\n",
519
+ "3 c 1\n",
520
+ "4 d 2\n",
521
+ "--------\n",
522
+ "10.0\n",
523
+ ]
524
+ assert_equal(corrects, results)
525
+ end
526
+
527
+ def test_strip
528
+ matrix = [
529
+ ['a ', ' b ', ' c '],
530
+ ['d ', ' e ', ' f ']
531
+ ]
532
+ t10 = TT.new(matrix: matrix)
533
+ t10.strip
534
+ corrects = [
535
+ ['a', 'b', 'c'],
536
+ ['d', 'e', 'f']
537
+ ]
538
+ assert_equal(corrects, t10.matrix)
539
+ end
540
+
541
+ def test_reform
542
+ matrix = [
543
+ %w(Ti 1.1 1),
544
+ %w(Ti 2.2 2),
545
+ %w(Zn 3.3 1),
546
+ %w(Zn 4.4 2),
547
+ ]
548
+ t10 = TT.new(matrix: matrix)
549
+ t10.reform('1', '3', '2')
550
+ correct = [
551
+ ['' , 'Ti' , 'Zn' ],
552
+ ['1' , '1.1' , '3.3'],
553
+ ['2' , '2.2' , '4.4'],
554
+ ]
555
+ assert_equal(correct, t10.matrix)
556
+
557
+ matrix = [
558
+ %w(Ti 1.1 1),
559
+ %w(Ti 2.2 2),
560
+ %w(Zn 3.3 1),
561
+ %w(Zn 4.4 2),
562
+ %w(Zn 5.5 2),
563
+ ]
564
+ t10 = TT.new(matrix: matrix)
565
+ assert_raise(TT::DuplicateCellError) { t10.reform('1', '3', '2') }
566
+ end
567
+
568
+ def test_set_title
569
+ @t01.set_title
570
+ assert_equal( ['0123', '45', '6789'], @t01.titles)
571
+ matrix = [
572
+ ['abcd', 'ef', 'ghij'],
573
+ ['a ', 'e ', 'g '],
574
+ ['a cd', ' f', 'gh j'],
575
+ ['abcd', ' ', ' '],
576
+ [' ', ' ', ' hij']
577
+ ]
578
+ @t01 = TT.new(matrix: matrix)
579
+ end
580
+
581
+ end
582
+