tbpgr_utils 0.0.28 → 0.0.29

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,5 +2,5 @@
2
2
 
3
3
  # Tbpgr Utilities
4
4
  module TbpgrUtils
5
- VERSION = '0.0.28'
5
+ VERSION = '0.0.29'
6
6
  end
@@ -1,1008 +1,1088 @@
1
- # encoding: utf-8
2
- require 'spec_helper'
3
- require 'tbpgr_utils'
4
-
5
- describe Array do
6
- context :together do
7
- cases = [
8
- {
9
- case_no: 1,
10
- case_title: 'valid case',
11
- inputs: [[1, 2, 3], %w{one two three}],
12
- expected: ['1:one', '2:two', '3:three'],
13
- },
14
- {
15
- case_no: 2,
16
- case_title: 'contain nil case',
17
- inputs: [[1, 2, 3], %w{one two}],
18
- expected: ['1:one', '2:two', '3:'],
19
- },
20
- ]
21
-
22
- cases.each do |c|
23
- it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
24
- begin
25
- case_before c
26
-
27
- # -- given --
28
- # nothing
29
-
30
- # -- when/then --
31
- actual = []
32
- c[:inputs].together do |first, second|
33
- actual << "#{first}:#{second}"
34
- end
35
-
36
- expect(actual).to eq(c[:expected])
37
- ensure
38
- case_after c
39
- end
40
- end
41
-
42
- def case_before(c)
43
- # implement each case before
44
- end
45
-
46
- def case_after(c)
47
- # implement each case after
48
- end
49
- end
50
- end
51
-
52
- context :together_with_index do
53
- cases = [
54
- {
55
- case_no: 1,
56
- case_title: 'valid case',
57
- inputs: [[1, 2, 3], %w{one two three}],
58
- expected: ['0:1:one', '1:2:two', '2:3:three'],
59
- },
60
- {
61
- case_no: 2,
62
- case_title: 'contain nil case',
63
- inputs: [[1, 2, 3], %w{one two}],
64
- expected: ['0:1:one', '1:2:two', '2:3:'],
65
- },
66
- ]
67
-
68
- cases.each do |c|
69
- it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
70
- begin
71
- case_before c
72
-
73
- # -- given --
74
- # nothing
75
-
76
- # -- when/then --
77
- actual = []
78
- c[:inputs].together_with_index do |first, second, index|
79
- actual << "#{index.to_s}:#{first}:#{second}"
80
- end
81
-
82
- expect(actual).to eq(c[:expected])
83
- ensure
84
- case_after c
85
- end
86
- end
87
-
88
- def case_before(c)
89
- # implement each case before
90
- end
91
-
92
- def case_after(c)
93
- # implement each case after
94
- end
95
- end
96
- end
97
-
98
- context :together_map do
99
- cases = [
100
- {
101
- case_no: 1,
102
- case_title: 'valid case',
103
- method_name: :together_map,
104
- inputs: [[1, 2, 3], %w{one two three}],
105
- expected: ['1:one', '2:two', '3:three'],
106
- },
107
- {
108
- case_no: 2,
109
- case_title: 'contain nil case',
110
- method_name: :together_map,
111
- inputs: [[1, 2, 3], %w{one two}],
112
- expected: ['1:one', '2:two', '3:'],
113
- },
114
- {
115
- case_no: 3,
116
- case_title: 'valid case',
117
- method_name: :together_map,
118
- inputs: [[1, 2, 3], %w{one two three}],
119
- expected: [['1:one', '2:two', '3:three'], ['one:1', 'two:2', 'three:3']],
120
- is_multi: true,
121
- },
122
- {
123
- case_no: 4,
124
- case_title: 'valid case(alias together_collect)',
125
- method_name: :together_collect,
126
- inputs: [[1, 2, 3], %w{one two three}],
127
- expected: ['1:one', '2:two', '3:three'],
128
- },
129
- {
130
- case_no: 5,
131
- case_title: 'valid case(alias tmap)',
132
- method_name: :tmap,
133
- inputs: [[1, 2, 3], %w{one two three}],
134
- expected: ['1:one', '2:two', '3:three'],
135
- },
136
- {
137
- case_no: 6,
138
- case_title: 'valid case(alias tcollect)',
139
- method_name: :tcollect,
140
- inputs: [[1, 2, 3], %w{one two three}],
141
- expected: ['1:one', '2:two', '3:three'],
142
- },
143
- ]
144
-
145
- cases.each do |c|
146
- it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
147
- begin
148
- case_before c
149
-
150
- # -- given --
151
- # nothing
152
-
153
- # -- when/then --
154
- if c[:is_multi]
155
- actual = c[:inputs].method(c[:method_name]).call { |first, second|["#{first}:#{second}", "#{second}:#{first}"] }
156
- else
157
- actual = c[:inputs].method(c[:method_name]).call { |first, second|"#{first}:#{second}" }
158
- end
159
-
160
- expect(actual).to eq(c[:expected])
161
- ensure
162
- case_after c
163
- end
164
- end
165
-
166
- def case_before(c)
167
- # implement each case before
168
- end
169
-
170
- def case_after(c)
171
- # implement each case after
172
- end
173
- end
174
- end
175
-
176
- context :together_map! do
177
- cases = [
178
- {
179
- case_no: 1,
180
- case_title: 'valid case',
181
- method_name: :together_map!,
182
- inputs: [[1, 2, 3], %w{one two three}],
183
- expected_ret: ['1:one', '2:two', '3:three'],
184
- expected_self: ['1:one', '2:two', '3:three'],
185
- },
186
- {
187
- case_no: 2,
188
- case_title: 'contain nil case',
189
- method_name: :together_map!,
190
- inputs: [[1, 2, 3], %w{one two}],
191
- expected_ret: ['1:one', '2:two', '3:'],
192
- expected_self: ['1:one', '2:two', '3:'],
193
- },
194
- {
195
- case_no: 3,
196
- case_title: 'valid case',
197
- method_name: :together_map!,
198
- inputs: [[1, 2, 3], %w{one two three}],
199
- expected_ret: [['1:one', '2:two', '3:three'], ['one:1', 'two:2', 'three:3']],
200
- expected_self: [['1:one', '2:two', '3:three'], ['one:1', 'two:2', 'three:3']],
201
- is_multi: true,
202
- },
203
- {
204
- case_no: 4,
205
- case_title: 'valid case(alias together_collect!)',
206
- method_name: :together_collect!,
207
- inputs: [[1, 2, 3], %w{one two three}],
208
- expected_ret: [['1:one', '2:two', '3:three'], ['one:1', 'two:2', 'three:3']],
209
- expected_self: [['1:one', '2:two', '3:three'], ['one:1', 'two:2', 'three:3']],
210
- is_multi: true,
211
- },
212
- {
213
- case_no: 5,
214
- case_title: 'valid case(alias tmap!)',
215
- method_name: :tmap!,
216
- inputs: [[1, 2, 3], %w{one two three}],
217
- expected_ret: [['1:one', '2:two', '3:three'], ['one:1', 'two:2', 'three:3']],
218
- expected_self: [['1:one', '2:two', '3:three'], ['one:1', 'two:2', 'three:3']],
219
- is_multi: true,
220
- },
221
- {
222
- case_no: 6,
223
- case_title: 'valid case(alias tcollect!)',
224
- method_name: :tcollect!,
225
- inputs: [[1, 2, 3], %w{one two three}],
226
- expected_ret: [['1:one', '2:two', '3:three'], ['one:1', 'two:2', 'three:3']],
227
- expected_self: [['1:one', '2:two', '3:three'], ['one:1', 'two:2', 'three:3']],
228
- is_multi: true,
229
- },
230
- ]
231
-
232
- cases.each do |c|
233
- it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
234
- begin
235
- case_before c
236
-
237
- # -- given --
238
- # nothing
239
-
240
- # -- when/then --
241
- if c[:is_multi]
242
- actual = c[:inputs].method(c[:method_name]).call { |first, second|["#{first}:#{second}", "#{second}:#{first}"] }
243
- else
244
- actual = c[:inputs].method(c[:method_name]).call { |first, second|"#{first}:#{second}" }
245
- end
246
-
247
- expect(actual).to eq(c[:expected_ret])
248
- expect(c[:inputs]).to eq(c[:expected_self])
249
- ensure
250
- case_after c
251
- end
252
- end
253
-
254
- def case_before(c)
255
- # implement each case before
256
- end
257
-
258
- def case_after(c)
259
- # implement each case after
260
- end
261
- end
262
- end
263
-
264
- context :together_select do
265
- cases = [
266
- {
267
- case_no: 1,
268
- case_title: 'single valid case',
269
- inputs: [[1, 2, 3, 4], [4, 2, 3, 1]],
270
- condition: 'first == second',
271
- method_name: :together_select,
272
- expected: [[2, 3], [2, 3]],
273
- },
274
- {
275
- case_no: 2,
276
- case_title: 'multi valid case',
277
- inputs: [[1, 2, 3, 4], [4, 2, 3, 1]],
278
- condition: '[first.odd?, second.even?]',
279
- expected: [[1, 3], [4, 2]],
280
- method_name: :together_select,
281
- },
282
- {
283
- case_no: 3,
284
- case_title: 'multi valid case',
285
- inputs: [[1, 2, 3, 4], [4, 2, 3, 1]],
286
- condition: '[first.odd?, second.even?]',
287
- expected: [[1, 3], [4, 2]],
288
- method_name: :tselect,
289
- },
290
- {
291
- case_no: 4,
292
- case_title: 'multi valid case',
293
- inputs: [[1, 2, 3, 4], [4, 2, 3, 1]],
294
- condition: '[first.odd?, second.even?]',
295
- expected: [[1, 3], [4, 2]],
296
- method_name: :together_find_all,
297
- },
298
- {
299
- case_no: 5,
300
- case_title: 'multi valid case',
301
- inputs: [[1, 2, 3, 4], [4, 2, 3, 1]],
302
- condition: '[first.odd?, second.even?]',
303
- expected: [[1, 3], [4, 2]],
304
- method_name: :tfindall,
305
- },
306
- ]
307
-
308
- cases.each do |c|
309
- it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
310
- begin
311
- case_before c
312
-
313
- # -- given --
314
- # nothing
315
-
316
- # -- when/then --
317
- actual = c[:inputs].method(c[:method_name]).call { |first, second| eval c[:condition], binding }
318
-
319
- expect(actual).to eq(c[:expected])
320
- ensure
321
- case_after c
322
- end
323
- end
324
-
325
- def case_before(c)
326
- # implement each case before
327
- end
328
-
329
- def case_after(c)
330
- # implement each case after
331
- end
332
- end
333
- end
334
-
335
- context :together_reduce do
336
- cases = [
337
- {
338
- case_no: 1,
339
- case_title: 'single valid case',
340
- inputs: [[1, 2, 3, 4], [4, 2, 3, 1]],
341
- init: nil,
342
- logic: 'memo + first + second',
343
- method_name: :together_reduce,
344
- expected: 20,
345
- },
346
- {
347
- case_no: 2,
348
- case_title: 'single with init valid case',
349
- inputs: [[1, 2, 3, 4], [4, 2, 3, 1]],
350
- init: 10,
351
- logic: 'memo + first + second',
352
- method_name: :together_reduce,
353
- expected: 30,
354
- },
355
- {
356
- case_no: 5,
357
- case_title: 'single valid case',
358
- inputs: [[1, 2, 3, 4], [4, 2, 3, 1]],
359
- logic: 'memo + first + second',
360
- expected: 20,
361
- method_name: :treduce,
362
- },
363
- {
364
- case_no: 6,
365
- case_title: 'single valid case',
366
- inputs: [[1, 2, 3, 4], [4, 2, 3, 1]],
367
- logic: 'memo + first + second',
368
- expected: 20,
369
- method_name: :together_inject,
370
- },
371
- {
372
- case_no: 7,
373
- case_title: 'multi valid case',
374
- inputs: [[1, 2, 3, 4], [4, 2, 3, 1]],
375
- logic: 'memo + first + second',
376
- expected: 20,
377
- method_name: :tinject,
378
- },
379
- {
380
- case_no: 8,
381
- case_title: 'single with init valid array case',
382
- inputs: [[1, 2, 3, 4], [4, 2, 3, 1]],
383
- init: [],
384
- logic: 'memo << first + second',
385
- method_name: :together_reduce,
386
- expected: [5, 4, 6, 5],
387
- },
388
- {
389
- case_no: 9,
390
- case_title: 'single with init valid array case',
391
- inputs: [[1, 2, 3, 4], [4, 2, 3, 1]],
392
- init: {},
393
- logic: 'memo[first] = second; memo',
394
- method_name: :together_reduce,
395
- expected: { 1 => 4, 2 => 2, 3 => 3, 4 => 1 },
396
- },
397
- ]
398
-
399
- cases.each do |c|
400
- it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
401
- begin
402
- case_before c
403
-
404
- # -- given --
405
- # nothing
406
-
407
- # -- when/then --
408
- if c[:init]
409
- actual = c[:inputs].method(c[:method_name]).call(c[:init]) { |memo, first, second| eval c[:logic], binding }
410
- else
411
- actual = c[:inputs].method(c[:method_name]).call { |memo, first, second| eval c[:logic], binding }
412
- end
413
-
414
- expect(actual).to eq(c[:expected])
415
- ensure
416
- case_after c
417
- end
418
- end
419
-
420
- def case_before(c)
421
- # implement each case before
422
- end
423
-
424
- def case_after(c)
425
- # implement each case after
426
- end
427
- end
428
- end
429
-
430
- context :together_concat do
431
- cases = [
432
- {
433
- case_no: 1,
434
- case_title: 'valid case',
435
- inputs: [[1, 2, 3], %w{one two three}],
436
- add_list: [4, 5, 6],
437
- method_name: :together_concat,
438
- expected: [[1, 2, 3, 4, 5, 6], ['one', 'two', 'three', 4, 5, 6]],
439
- },
440
- {
441
- case_no: 2,
442
- case_title: 'valid case',
443
- inputs: [[1, 2, 3], %w{one two three}],
444
- add_list: [4, 5, 6],
445
- method_name: :tconcat,
446
- expected: [[1, 2, 3, 4, 5, 6], ['one', 'two', 'three', 4, 5, 6]],
447
- },
448
- ]
449
-
450
- cases.each do |c|
451
- it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
452
- begin
453
- case_before c
454
-
455
- # -- given --
456
- # nothing
457
-
458
- # -- when --
459
- c[:inputs].send c[:method_name], c[:add_list]
460
-
461
- # -- then --
462
- expect(c[:inputs]).to eq(c[:expected])
463
- ensure
464
- case_after c
465
- end
466
- end
467
-
468
- def case_before(c)
469
- # implement each case before
470
- end
471
-
472
- def case_after(c)
473
- # implement each case after
474
- end
475
- end
476
- end
477
-
478
- context :together_at do
479
- cases = [
480
- {
481
- case_no: 1,
482
- case_title: 'valid case',
483
- inputs: [[1, 2, 3], %w{one two three}],
484
- index: 2,
485
- method_name: :together_at,
486
- expected: [3, 'three'],
487
- },
488
- {
489
- case_no: 2,
490
- case_title: 'contain nil case',
491
- inputs: [[1, 2, 3], %w{one two}],
492
- index: 2,
493
- method_name: :together_at,
494
- expected: [3, nil],
495
- },
496
- {
497
- case_no: 3,
498
- case_title: 'valid case',
499
- inputs: [[1, 2, 3], %w{one two three}],
500
- index: 2,
501
- method_name: :tat,
502
- expected: [3, 'three'],
503
- },
504
- ]
505
-
506
- cases.each do |c|
507
- it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
508
- begin
509
- case_before c
510
-
511
- # -- given --
512
- # nothing
513
-
514
- # -- when --
515
- actual = c[:inputs].send c[:method_name], c[:index]
516
-
517
- # -- then --
518
- expect(actual).to eq(c[:expected])
519
- ensure
520
- case_after c
521
- end
522
- end
523
-
524
- def case_before(c)
525
- # implement each case before
526
- end
527
-
528
- def case_after(c)
529
- # implement each case after
530
- end
531
- end
532
- end
533
-
534
- context :together_clear do
535
- cases = [
536
- {
537
- case_no: 1,
538
- case_title: 'valid case',
539
- inputs: [[1, 2, 3], %w{one two three}],
540
- method_name: :together_clear,
541
- expected: [[], []],
542
- },
543
- {
544
- case_no: 2,
545
- case_title: 'valid case',
546
- inputs: [[1, 2, 3], %w{one two three}],
547
- method_name: :tclear,
548
- expected: [[], []],
549
- },
550
- ]
551
-
552
- cases.each do |c|
553
- it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
554
- begin
555
- case_before c
556
-
557
- # -- given --
558
- # nothing
559
-
560
- # -- when --
561
- actual = c[:inputs].send c[:method_name]
562
-
563
- # -- then --
564
- expect(actual).to eq(c[:expected])
565
- ensure
566
- case_after c
567
- end
568
- end
569
-
570
- def case_before(c)
571
- # implement each case before
572
- end
573
-
574
- def case_after(c)
575
- # implement each case after
576
- end
577
- end
578
- end
579
-
580
- context :together_compact do
581
- cases = [
582
- {
583
- case_no: 1,
584
- case_title: 'valid case',
585
- inputs: [[1, 2, nil, 3], ['a', 'b', 'c', nil, 'd']],
586
- method_name: :together_compact,
587
- expected_self: [[1, 2, nil, 3], ['a', 'b', 'c', nil, 'd']],
588
- expected_ret: [[1, 2, 3], ['a', 'b', 'c', 'd']],
589
- },
590
- {
591
- case_no: 2,
592
- case_title: 'valid case(use alias tcompact)',
593
- inputs: [[1, 2, nil, 3], ['a', 'b', 'c', nil, 'd']],
594
- method_name: :tcompact,
595
- expected_self: [[1, 2, nil, 3], ['a', 'b', 'c', nil, 'd']],
596
- expected_ret: [[1, 2, 3], ['a', 'b', 'c', 'd']],
597
- },
598
- ]
599
-
600
- cases.each do |c|
601
- it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
602
- begin
603
- case_before c
604
-
605
- # -- given --
606
- # nothing
607
-
608
- # -- when --
609
- actual = c[:inputs].send c[:method_name]
610
-
611
- # -- then --
612
- expect(c[:inputs]).to eq(c[:expected_self])
613
- expect(actual).to eq(c[:expected_ret])
614
- ensure
615
- case_after c
616
- end
617
- end
618
-
619
- def case_before(c)
620
- # implement each case before
621
- end
622
-
623
- def case_after(c)
624
- # implement each case after
625
- end
626
- end
627
- end
628
-
629
- context :together_compact! do
630
- cases = [
631
- {
632
- case_no: 1,
633
- case_title: 'valid case',
634
- inputs: [[1, 2, nil, 3], ['a', 'b', 'c', nil, 'd']],
635
- method_name: :together_compact!,
636
- expected_self: [[1, 2, 3], ['a', 'b', 'c', 'd']],
637
- expected_ret: [[1, 2, 3], ['a', 'b', 'c', 'd']],
638
- },
639
- {
640
- case_no: 2,
641
- case_title: 'valid case(use alias tcompact)',
642
- inputs: [[1, 2, nil, 3], ['a', 'b', 'c', nil, 'd']],
643
- method_name: :tcompact!,
644
- expected_self: [[1, 2, 3], ['a', 'b', 'c', 'd']],
645
- expected_ret: [[1, 2, 3], ['a', 'b', 'c', 'd']],
646
- },
647
- ]
648
-
649
- cases.each do |c|
650
- it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
651
- begin
652
- case_before c
653
-
654
- # -- given --
655
- # nothing
656
-
657
- # -- when --
658
- actual = c[:inputs].send c[:method_name]
659
-
660
- # -- then --
661
- expect(c[:inputs]).to eq(c[:expected_self])
662
- expect(actual).to eq(c[:expected_ret])
663
- ensure
664
- case_after c
665
- end
666
- end
667
-
668
- def case_before(c)
669
- # implement each case before
670
- end
671
-
672
- def case_after(c)
673
- # implement each case after
674
- end
675
- end
676
- end
677
-
678
- context :together_delete do
679
- cases = [
680
- {
681
- case_no: 1,
682
- case_title: 'valid case',
683
- inputs: [[1, 2, 3, 4], [2, 3, 4, 5]],
684
- delete_value: 2,
685
- method_name: :together_delete,
686
- expected: [[1, 3, 4], [3, 4, 5]],
687
- ret: 2,
688
- },
689
- {
690
- case_no: 2,
691
- case_title: 'valid case(not exist and block use)',
692
- inputs: [[1, 2, 3, 4], [2, 3, 4, 5]],
693
- delete_value: 6,
694
- method_name: :together_delete,
695
- expected: [[1, 2, 3, 4], [2, 3, 4, 5]],
696
- ret: :ret,
697
- has_block: true,
698
- block: :ret,
699
- },
700
- {
701
- case_no: 3,
702
- case_title: 'valid case(not exist and block unuse)',
703
- inputs: [[1, 2, 3, 4], [2, 3, 4, 5]],
704
- delete_value: 6,
705
- method_name: :together_delete,
706
- expected: [[1, 2, 3, 4], [2, 3, 4, 5]],
707
- ret: nil,
708
- },
709
- {
710
- case_no: 4,
711
- case_title: 'valid case(alias tdelete)',
712
- inputs: [[1, 2, 3, 4], [2, 3, 4, 5]],
713
- delete_value: 2,
714
- method_name: :tdelete,
715
- expected: [[1, 3, 4], [3, 4, 5]],
716
- ret: 2,
717
- },
718
- ]
719
-
720
- cases.each do |c|
721
- it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
722
- begin
723
- case_before c
724
-
725
- # -- given --
726
- # nothing
727
-
728
- # -- when --
729
- if c[:has_block]
730
- actual = c[:inputs].send c[:method_name], c[:delete_value] {c[:block] }
731
- else
732
- actual = c[:inputs].send c[:method_name], c[:delete_value]
733
- end
734
-
735
- # -- then --
736
- expect(actual).to eq(c[:ret])
737
- expect(c[:inputs]).to eq(c[:expected])
738
- ensure
739
- case_after c
740
- end
741
- end
742
-
743
- def case_before(c)
744
- # implement each case before
745
- end
746
-
747
- def case_after(c)
748
- # implement each case after
749
- end
750
- end
751
- end
752
-
753
- context :together_delete_at do
754
- cases = [
755
- {
756
- case_no: 1,
757
- case_title: 'valid case',
758
- inputs: [[1, 2, 3, 4], [2, 3, 4, 5]],
759
- delete_value: 2,
760
- method_name: :together_delete_at,
761
- expected: [[1, 2, 4], [2, 3, 5]],
762
- ret: [3, 4],
763
- },
764
- {
765
- case_no: 2,
766
- case_title: 'valid case(not exist)',
767
- inputs: [[1, 2, 3, 4], [2, 3, 4, 5]],
768
- delete_value: 5,
769
- method_name: :together_delete_at,
770
- expected: [[1, 2, 3, 4], [2, 3, 4, 5]],
771
- ret: [nil, nil],
772
- },
773
- {
774
- case_no: 3,
775
- case_title: 'valid case(minus index)',
776
- inputs: [[1, 2, 3, 4], [2, 3, 4, 5]],
777
- delete_value: -3,
778
- method_name: :together_delete_at,
779
- expected: [[1, 3, 4], [2, 4, 5]],
780
- ret: [2, 3],
781
- },
782
- {
783
- case_no: 4,
784
- case_title: 'valid case(use tdelete_at alias)',
785
- inputs: [[1, 2, 3, 4], [2, 3, 4, 5]],
786
- delete_value: 2,
787
- method_name: :tdelete_at,
788
- expected: [[1, 2, 4], [2, 3, 5]],
789
- ret: [3, 4],
790
- },
791
- ]
792
-
793
- cases.each do |c|
794
- it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
795
- begin
796
- case_before c
797
-
798
- # -- given --
799
- # nothing
800
-
801
- # -- when --
802
- if c[:has_block]
803
- actual = c[:inputs].send c[:method_name], c[:delete_value] {c[:block] }
804
- else
805
- actual = c[:inputs].send c[:method_name], c[:delete_value]
806
- end
807
-
808
- # -- then --
809
- expect(actual).to eq(c[:ret])
810
- expect(c[:inputs]).to eq(c[:expected])
811
- ensure
812
- case_after c
813
- end
814
- end
815
-
816
- def case_before(c)
817
- # implement each case before
818
- end
819
-
820
- def case_after(c)
821
- # implement each case after
822
- end
823
- end
824
- end
825
-
826
- context :together_delete_if do
827
- cases = [
828
- {
829
- case_no: 1,
830
- case_title: 'valid case',
831
- inputs: [[1, 2, 3, 4], [6, 4, 6, 8]],
832
- delete_block_code: '(first + second).odd?',
833
- method_name: :together_delete_if,
834
- expected: [[2, 4], [4, 8]],
835
- },
836
- {
837
- case_no: 2,
838
- case_title: 'not have delete target value case',
839
- inputs: [[2, 2, 4, 4], [6, 4, 6, 8]],
840
- delete_block_code: '(first + second).odd?',
841
- method_name: :together_delete_if,
842
- expected: nil,
843
- },
844
- {
845
- case_no: 3,
846
- case_title: 'valid case(alias tdelete_if)',
847
- inputs: [[1, 2, 3, 4], [6, 4, 6, 8]],
848
- delete_block_code: '(first + second).odd?',
849
- method_name: :tdelete_if,
850
- expected: [[2, 4], [4, 8]],
851
- },
852
- ]
853
-
854
- cases.each do |c|
855
- it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
856
- begin
857
- case_before c
858
-
859
- # -- given --
860
- # nothing
861
-
862
- # -- when --
863
- actual = c[:inputs].send c[:method_name] {|first, second|eval c[:delete_block_code], binding }
864
-
865
- # -- then --
866
- expect(actual).to eq(c[:expected])
867
- ensure
868
- case_after c
869
- end
870
- end
871
-
872
- def case_before(c)
873
- # implement each case before
874
- end
875
-
876
- def case_after(c)
877
- # implement each case after
878
- end
879
- end
880
- end
881
-
882
- context :together_empty? do
883
- cases = [
884
- {
885
- case_no: 1,
886
- case_title: 'empty case',
887
- inputs: [[], []],
888
- method_name: :together_empty?,
889
- expected: true,
890
- },
891
- {
892
- case_no: 2,
893
- case_title: 'not empty case',
894
- inputs: [[1], []],
895
- method_name: :together_empty?,
896
- expected: false,
897
- },
898
- {
899
- case_no: 3,
900
- case_title: 'empty case(alias tempty?)',
901
- inputs: [[], []],
902
- method_name: :tempty?,
903
- expected: true,
904
- },
905
- ]
906
-
907
- cases.each do |c|
908
- it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
909
- begin
910
- case_before c
911
-
912
- # -- given --
913
- # nothing
914
-
915
- # -- when --
916
- actual = c[:inputs].send c[:method_name]
917
-
918
- # -- then --
919
- expect(actual).to eq(c[:expected])
920
- ensure
921
- case_after c
922
- end
923
- end
924
-
925
- def case_before(c)
926
- # implement each case before
927
- end
928
-
929
- def case_after(c)
930
- # implement each case after
931
- end
932
- end
933
- end
934
-
935
- context :together_fill do
936
- cases = [
937
- {
938
- case_no: 1,
939
- case_title: 'not use block case',
940
- inputs: [[*1..5], [*6..10]],
941
- fill_value: 99,
942
- method_name: :together_fill,
943
- expected: [[99, 99, 99, 99, 99], [99, 99, 99, 99, 99]],
944
- has_block: false,
945
- },
946
- {
947
- case_no: 2,
948
- case_title: 'use block, no args case',
949
- inputs: [[*1..5], [*6..10]],
950
- fill_value: nil,
951
- method_name: :together_fill,
952
- expected: [[2, 3, 4, 5, 6], [2, 3, 4, 5, 6]],
953
- block_logic: '(i + 1) + 1',
954
- has_block: true,
955
- },
956
- {
957
- case_no: 3,
958
- case_title: 'use block, has args case',
959
- inputs: [[*1..5], [*6..10]],
960
- fill_value: 2,
961
- method_name: :together_fill,
962
- expected: [[1, 2, 4, 5, 6], [6, 7, 4, 5, 6]],
963
- block_logic: '(i + 1) + 1',
964
- has_block: true,
965
- },
966
- {
967
- case_no: 4,
968
- case_title: 'not use block case(alias tfill)',
969
- inputs: [[*1..5], [*6..10]],
970
- fill_value: 99,
971
- method_name: :tfill,
972
- expected: [[99, 99, 99, 99, 99], [99, 99, 99, 99, 99]],
973
- has_block: false,
974
- },
975
- ]
976
-
977
- cases.each do |c|
978
- it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
979
- begin
980
- case_before c
981
-
982
- # -- given --
983
- # nothing
984
-
985
- # -- when --
986
- if c[:has_block]
987
- actual = c[:inputs].send c[:method_name], c[:fill_value] {|i|eval c[:block_logic], binding }
988
- else
989
- actual = c[:inputs].send c[:method_name], c[:fill_value]
990
- end
991
-
992
- # -- then --
993
- expect(actual).to eq(c[:expected])
994
- ensure
995
- case_after c
996
- end
997
- end
998
-
999
- def case_before(c)
1000
- # implement each case before
1001
- end
1002
-
1003
- def case_after(c)
1004
- # implement each case after
1005
- end
1006
- end
1007
- end
1008
- end
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+ require 'tbpgr_utils'
4
+
5
+ describe Array do
6
+ context :together do
7
+ cases = [
8
+ {
9
+ case_no: 1,
10
+ case_title: 'valid case',
11
+ inputs: [[1, 2, 3], %w{one two three}],
12
+ expected: ['1:one', '2:two', '3:three'],
13
+ },
14
+ {
15
+ case_no: 2,
16
+ case_title: 'contain nil case',
17
+ inputs: [[1, 2, 3], %w{one two}],
18
+ expected: ['1:one', '2:two', '3:'],
19
+ },
20
+ ]
21
+
22
+ cases.each do |c|
23
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
24
+ begin
25
+ case_before c
26
+
27
+ # -- given --
28
+ # nothing
29
+
30
+ # -- when/then --
31
+ actual = []
32
+ c[:inputs].together do |first, second|
33
+ actual << "#{first}:#{second}"
34
+ end
35
+
36
+ expect(actual).to eq(c[:expected])
37
+ ensure
38
+ case_after c
39
+ end
40
+ end
41
+
42
+ def case_before(c)
43
+ # implement each case before
44
+ end
45
+
46
+ def case_after(c)
47
+ # implement each case after
48
+ end
49
+ end
50
+ end
51
+
52
+ context :together_with_index do
53
+ cases = [
54
+ {
55
+ case_no: 1,
56
+ case_title: 'valid case',
57
+ inputs: [[1, 2, 3], %w{one two three}],
58
+ expected: ['0:1:one', '1:2:two', '2:3:three'],
59
+ },
60
+ {
61
+ case_no: 2,
62
+ case_title: 'contain nil case',
63
+ inputs: [[1, 2, 3], %w{one two}],
64
+ expected: ['0:1:one', '1:2:two', '2:3:'],
65
+ },
66
+ ]
67
+
68
+ cases.each do |c|
69
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
70
+ begin
71
+ case_before c
72
+
73
+ # -- given --
74
+ # nothing
75
+
76
+ # -- when/then --
77
+ actual = []
78
+ c[:inputs].together_with_index do |first, second, index|
79
+ actual << "#{index.to_s}:#{first}:#{second}"
80
+ end
81
+
82
+ expect(actual).to eq(c[:expected])
83
+ ensure
84
+ case_after c
85
+ end
86
+ end
87
+
88
+ def case_before(c)
89
+ # implement each case before
90
+ end
91
+
92
+ def case_after(c)
93
+ # implement each case after
94
+ end
95
+ end
96
+ end
97
+
98
+ context :together_map do
99
+ cases = [
100
+ {
101
+ case_no: 1,
102
+ case_title: 'valid case',
103
+ method_name: :together_map,
104
+ inputs: [[1, 2, 3], %w{one two three}],
105
+ expected: ['1:one', '2:two', '3:three'],
106
+ },
107
+ {
108
+ case_no: 2,
109
+ case_title: 'contain nil case',
110
+ method_name: :together_map,
111
+ inputs: [[1, 2, 3], %w{one two}],
112
+ expected: ['1:one', '2:two', '3:'],
113
+ },
114
+ {
115
+ case_no: 3,
116
+ case_title: 'valid case',
117
+ method_name: :together_map,
118
+ inputs: [[1, 2, 3], %w{one two three}],
119
+ expected: [['1:one', '2:two', '3:three'], ['one:1', 'two:2', 'three:3']],
120
+ is_multi: true,
121
+ },
122
+ {
123
+ case_no: 4,
124
+ case_title: 'valid case(alias together_collect)',
125
+ method_name: :together_collect,
126
+ inputs: [[1, 2, 3], %w{one two three}],
127
+ expected: ['1:one', '2:two', '3:three'],
128
+ },
129
+ {
130
+ case_no: 5,
131
+ case_title: 'valid case(alias tmap)',
132
+ method_name: :tmap,
133
+ inputs: [[1, 2, 3], %w{one two three}],
134
+ expected: ['1:one', '2:two', '3:three'],
135
+ },
136
+ {
137
+ case_no: 6,
138
+ case_title: 'valid case(alias tcollect)',
139
+ method_name: :tcollect,
140
+ inputs: [[1, 2, 3], %w{one two three}],
141
+ expected: ['1:one', '2:two', '3:three'],
142
+ },
143
+ ]
144
+
145
+ cases.each do |c|
146
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
147
+ begin
148
+ case_before c
149
+
150
+ # -- given --
151
+ # nothing
152
+
153
+ # -- when/then --
154
+ if c[:is_multi]
155
+ actual = c[:inputs].method(c[:method_name]).call { |first, second|["#{first}:#{second}", "#{second}:#{first}"] }
156
+ else
157
+ actual = c[:inputs].method(c[:method_name]).call { |first, second|"#{first}:#{second}" }
158
+ end
159
+
160
+ expect(actual).to eq(c[:expected])
161
+ ensure
162
+ case_after c
163
+ end
164
+ end
165
+
166
+ def case_before(c)
167
+ # implement each case before
168
+ end
169
+
170
+ def case_after(c)
171
+ # implement each case after
172
+ end
173
+ end
174
+ end
175
+
176
+ context :together_map! do
177
+ cases = [
178
+ {
179
+ case_no: 1,
180
+ case_title: 'valid case',
181
+ method_name: :together_map!,
182
+ inputs: [[1, 2, 3], %w{one two three}],
183
+ expected_ret: ['1:one', '2:two', '3:three'],
184
+ expected_self: ['1:one', '2:two', '3:three'],
185
+ },
186
+ {
187
+ case_no: 2,
188
+ case_title: 'contain nil case',
189
+ method_name: :together_map!,
190
+ inputs: [[1, 2, 3], %w{one two}],
191
+ expected_ret: ['1:one', '2:two', '3:'],
192
+ expected_self: ['1:one', '2:two', '3:'],
193
+ },
194
+ {
195
+ case_no: 3,
196
+ case_title: 'valid case',
197
+ method_name: :together_map!,
198
+ inputs: [[1, 2, 3], %w{one two three}],
199
+ expected_ret: [['1:one', '2:two', '3:three'], ['one:1', 'two:2', 'three:3']],
200
+ expected_self: [['1:one', '2:two', '3:three'], ['one:1', 'two:2', 'three:3']],
201
+ is_multi: true,
202
+ },
203
+ {
204
+ case_no: 4,
205
+ case_title: 'valid case(alias together_collect!)',
206
+ method_name: :together_collect!,
207
+ inputs: [[1, 2, 3], %w{one two three}],
208
+ expected_ret: [['1:one', '2:two', '3:three'], ['one:1', 'two:2', 'three:3']],
209
+ expected_self: [['1:one', '2:two', '3:three'], ['one:1', 'two:2', 'three:3']],
210
+ is_multi: true,
211
+ },
212
+ {
213
+ case_no: 5,
214
+ case_title: 'valid case(alias tmap!)',
215
+ method_name: :tmap!,
216
+ inputs: [[1, 2, 3], %w{one two three}],
217
+ expected_ret: [['1:one', '2:two', '3:three'], ['one:1', 'two:2', 'three:3']],
218
+ expected_self: [['1:one', '2:two', '3:three'], ['one:1', 'two:2', 'three:3']],
219
+ is_multi: true,
220
+ },
221
+ {
222
+ case_no: 6,
223
+ case_title: 'valid case(alias tcollect!)',
224
+ method_name: :tcollect!,
225
+ inputs: [[1, 2, 3], %w{one two three}],
226
+ expected_ret: [['1:one', '2:two', '3:three'], ['one:1', 'two:2', 'three:3']],
227
+ expected_self: [['1:one', '2:two', '3:three'], ['one:1', 'two:2', 'three:3']],
228
+ is_multi: true,
229
+ },
230
+ ]
231
+
232
+ cases.each do |c|
233
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
234
+ begin
235
+ case_before c
236
+
237
+ # -- given --
238
+ # nothing
239
+
240
+ # -- when/then --
241
+ if c[:is_multi]
242
+ actual = c[:inputs].method(c[:method_name]).call { |first, second|["#{first}:#{second}", "#{second}:#{first}"] }
243
+ else
244
+ actual = c[:inputs].method(c[:method_name]).call { |first, second|"#{first}:#{second}" }
245
+ end
246
+
247
+ expect(actual).to eq(c[:expected_ret])
248
+ expect(c[:inputs]).to eq(c[:expected_self])
249
+ ensure
250
+ case_after c
251
+ end
252
+ end
253
+
254
+ def case_before(c)
255
+ # implement each case before
256
+ end
257
+
258
+ def case_after(c)
259
+ # implement each case after
260
+ end
261
+ end
262
+ end
263
+
264
+ context :together_select do
265
+ cases = [
266
+ {
267
+ case_no: 1,
268
+ case_title: 'single valid case',
269
+ inputs: [[1, 2, 3, 4], [4, 2, 3, 1]],
270
+ condition: 'first == second',
271
+ method_name: :together_select,
272
+ expected: [[2, 3], [2, 3]],
273
+ },
274
+ {
275
+ case_no: 2,
276
+ case_title: 'multi valid case',
277
+ inputs: [[1, 2, 3, 4], [4, 2, 3, 1]],
278
+ condition: '[first.odd?, second.even?]',
279
+ expected: [[1, 3], [4, 2]],
280
+ method_name: :together_select,
281
+ },
282
+ {
283
+ case_no: 3,
284
+ case_title: 'multi valid case',
285
+ inputs: [[1, 2, 3, 4], [4, 2, 3, 1]],
286
+ condition: '[first.odd?, second.even?]',
287
+ expected: [[1, 3], [4, 2]],
288
+ method_name: :tselect,
289
+ },
290
+ {
291
+ case_no: 4,
292
+ case_title: 'multi valid case',
293
+ inputs: [[1, 2, 3, 4], [4, 2, 3, 1]],
294
+ condition: '[first.odd?, second.even?]',
295
+ expected: [[1, 3], [4, 2]],
296
+ method_name: :together_find_all,
297
+ },
298
+ {
299
+ case_no: 5,
300
+ case_title: 'multi valid case',
301
+ inputs: [[1, 2, 3, 4], [4, 2, 3, 1]],
302
+ condition: '[first.odd?, second.even?]',
303
+ expected: [[1, 3], [4, 2]],
304
+ method_name: :tfindall,
305
+ },
306
+ ]
307
+
308
+ cases.each do |c|
309
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
310
+ begin
311
+ case_before c
312
+
313
+ # -- given --
314
+ # nothing
315
+
316
+ # -- when/then --
317
+ actual = c[:inputs].method(c[:method_name]).call { |first, second| eval c[:condition], binding }
318
+
319
+ expect(actual).to eq(c[:expected])
320
+ ensure
321
+ case_after c
322
+ end
323
+ end
324
+
325
+ def case_before(c)
326
+ # implement each case before
327
+ end
328
+
329
+ def case_after(c)
330
+ # implement each case after
331
+ end
332
+ end
333
+ end
334
+
335
+ context :together_reduce do
336
+ cases = [
337
+ {
338
+ case_no: 1,
339
+ case_title: 'single valid case',
340
+ inputs: [[1, 2, 3, 4], [4, 2, 3, 1]],
341
+ init: nil,
342
+ logic: 'memo + first + second',
343
+ method_name: :together_reduce,
344
+ expected: 20,
345
+ },
346
+ {
347
+ case_no: 2,
348
+ case_title: 'single with init valid case',
349
+ inputs: [[1, 2, 3, 4], [4, 2, 3, 1]],
350
+ init: 10,
351
+ logic: 'memo + first + second',
352
+ method_name: :together_reduce,
353
+ expected: 30,
354
+ },
355
+ {
356
+ case_no: 5,
357
+ case_title: 'single valid case',
358
+ inputs: [[1, 2, 3, 4], [4, 2, 3, 1]],
359
+ logic: 'memo + first + second',
360
+ expected: 20,
361
+ method_name: :treduce,
362
+ },
363
+ {
364
+ case_no: 6,
365
+ case_title: 'single valid case',
366
+ inputs: [[1, 2, 3, 4], [4, 2, 3, 1]],
367
+ logic: 'memo + first + second',
368
+ expected: 20,
369
+ method_name: :together_inject,
370
+ },
371
+ {
372
+ case_no: 7,
373
+ case_title: 'multi valid case',
374
+ inputs: [[1, 2, 3, 4], [4, 2, 3, 1]],
375
+ logic: 'memo + first + second',
376
+ expected: 20,
377
+ method_name: :tinject,
378
+ },
379
+ {
380
+ case_no: 8,
381
+ case_title: 'single with init valid array case',
382
+ inputs: [[1, 2, 3, 4], [4, 2, 3, 1]],
383
+ init: [],
384
+ logic: 'memo << first + second',
385
+ method_name: :together_reduce,
386
+ expected: [5, 4, 6, 5],
387
+ },
388
+ {
389
+ case_no: 9,
390
+ case_title: 'single with init valid array case',
391
+ inputs: [[1, 2, 3, 4], [4, 2, 3, 1]],
392
+ init: {},
393
+ logic: 'memo[first] = second; memo',
394
+ method_name: :together_reduce,
395
+ expected: { 1 => 4, 2 => 2, 3 => 3, 4 => 1 },
396
+ },
397
+ ]
398
+
399
+ cases.each do |c|
400
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
401
+ begin
402
+ case_before c
403
+
404
+ # -- given --
405
+ # nothing
406
+
407
+ # -- when/then --
408
+ if c[:init]
409
+ actual = c[:inputs].method(c[:method_name]).call(c[:init]) { |memo, first, second| eval c[:logic], binding }
410
+ else
411
+ actual = c[:inputs].method(c[:method_name]).call { |memo, first, second| eval c[:logic], binding }
412
+ end
413
+
414
+ expect(actual).to eq(c[:expected])
415
+ ensure
416
+ case_after c
417
+ end
418
+ end
419
+
420
+ def case_before(c)
421
+ # implement each case before
422
+ end
423
+
424
+ def case_after(c)
425
+ # implement each case after
426
+ end
427
+ end
428
+ end
429
+
430
+ context :together_concat do
431
+ cases = [
432
+ {
433
+ case_no: 1,
434
+ case_title: 'valid case',
435
+ inputs: [[1, 2, 3], %w{one two three}],
436
+ add_list: [4, 5, 6],
437
+ method_name: :together_concat,
438
+ expected: [[1, 2, 3, 4, 5, 6], ['one', 'two', 'three', 4, 5, 6]],
439
+ },
440
+ {
441
+ case_no: 2,
442
+ case_title: 'valid case',
443
+ inputs: [[1, 2, 3], %w{one two three}],
444
+ add_list: [4, 5, 6],
445
+ method_name: :tconcat,
446
+ expected: [[1, 2, 3, 4, 5, 6], ['one', 'two', 'three', 4, 5, 6]],
447
+ },
448
+ ]
449
+
450
+ cases.each do |c|
451
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
452
+ begin
453
+ case_before c
454
+
455
+ # -- given --
456
+ # nothing
457
+
458
+ # -- when --
459
+ c[:inputs].send c[:method_name], c[:add_list]
460
+
461
+ # -- then --
462
+ expect(c[:inputs]).to eq(c[:expected])
463
+ ensure
464
+ case_after c
465
+ end
466
+ end
467
+
468
+ def case_before(c)
469
+ # implement each case before
470
+ end
471
+
472
+ def case_after(c)
473
+ # implement each case after
474
+ end
475
+ end
476
+ end
477
+
478
+ context :together_at do
479
+ cases = [
480
+ {
481
+ case_no: 1,
482
+ case_title: 'valid case',
483
+ inputs: [[1, 2, 3], %w{one two three}],
484
+ index: 2,
485
+ method_name: :together_at,
486
+ expected: [3, 'three'],
487
+ },
488
+ {
489
+ case_no: 2,
490
+ case_title: 'contain nil case',
491
+ inputs: [[1, 2, 3], %w{one two}],
492
+ index: 2,
493
+ method_name: :together_at,
494
+ expected: [3, nil],
495
+ },
496
+ {
497
+ case_no: 3,
498
+ case_title: 'valid case',
499
+ inputs: [[1, 2, 3], %w{one two three}],
500
+ index: 2,
501
+ method_name: :tat,
502
+ expected: [3, 'three'],
503
+ },
504
+ ]
505
+
506
+ cases.each do |c|
507
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
508
+ begin
509
+ case_before c
510
+
511
+ # -- given --
512
+ # nothing
513
+
514
+ # -- when --
515
+ actual = c[:inputs].send c[:method_name], c[:index]
516
+
517
+ # -- then --
518
+ expect(actual).to eq(c[:expected])
519
+ ensure
520
+ case_after c
521
+ end
522
+ end
523
+
524
+ def case_before(c)
525
+ # implement each case before
526
+ end
527
+
528
+ def case_after(c)
529
+ # implement each case after
530
+ end
531
+ end
532
+ end
533
+
534
+ context :together_clear do
535
+ cases = [
536
+ {
537
+ case_no: 1,
538
+ case_title: 'valid case',
539
+ inputs: [[1, 2, 3], %w{one two three}],
540
+ method_name: :together_clear,
541
+ expected: [[], []],
542
+ },
543
+ {
544
+ case_no: 2,
545
+ case_title: 'valid case',
546
+ inputs: [[1, 2, 3], %w{one two three}],
547
+ method_name: :tclear,
548
+ expected: [[], []],
549
+ },
550
+ ]
551
+
552
+ cases.each do |c|
553
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
554
+ begin
555
+ case_before c
556
+
557
+ # -- given --
558
+ # nothing
559
+
560
+ # -- when --
561
+ actual = c[:inputs].send c[:method_name]
562
+
563
+ # -- then --
564
+ expect(actual).to eq(c[:expected])
565
+ ensure
566
+ case_after c
567
+ end
568
+ end
569
+
570
+ def case_before(c)
571
+ # implement each case before
572
+ end
573
+
574
+ def case_after(c)
575
+ # implement each case after
576
+ end
577
+ end
578
+ end
579
+
580
+ context :together_compact do
581
+ cases = [
582
+ {
583
+ case_no: 1,
584
+ case_title: 'valid case',
585
+ inputs: [[1, 2, nil, 3], ['a', 'b', 'c', nil, 'd']],
586
+ method_name: :together_compact,
587
+ expected_self: [[1, 2, nil, 3], ['a', 'b', 'c', nil, 'd']],
588
+ expected_ret: [[1, 2, 3], ['a', 'b', 'c', 'd']],
589
+ },
590
+ {
591
+ case_no: 2,
592
+ case_title: 'valid case(use alias tcompact)',
593
+ inputs: [[1, 2, nil, 3], ['a', 'b', 'c', nil, 'd']],
594
+ method_name: :tcompact,
595
+ expected_self: [[1, 2, nil, 3], ['a', 'b', 'c', nil, 'd']],
596
+ expected_ret: [[1, 2, 3], ['a', 'b', 'c', 'd']],
597
+ },
598
+ ]
599
+
600
+ cases.each do |c|
601
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
602
+ begin
603
+ case_before c
604
+
605
+ # -- given --
606
+ # nothing
607
+
608
+ # -- when --
609
+ actual = c[:inputs].send c[:method_name]
610
+
611
+ # -- then --
612
+ expect(c[:inputs]).to eq(c[:expected_self])
613
+ expect(actual).to eq(c[:expected_ret])
614
+ ensure
615
+ case_after c
616
+ end
617
+ end
618
+
619
+ def case_before(c)
620
+ # implement each case before
621
+ end
622
+
623
+ def case_after(c)
624
+ # implement each case after
625
+ end
626
+ end
627
+ end
628
+
629
+ context :together_compact! do
630
+ cases = [
631
+ {
632
+ case_no: 1,
633
+ case_title: 'valid case',
634
+ inputs: [[1, 2, nil, 3], ['a', 'b', 'c', nil, 'd']],
635
+ method_name: :together_compact!,
636
+ expected_self: [[1, 2, 3], ['a', 'b', 'c', 'd']],
637
+ expected_ret: [[1, 2, 3], ['a', 'b', 'c', 'd']],
638
+ },
639
+ {
640
+ case_no: 2,
641
+ case_title: 'valid case(use alias tcompact)',
642
+ inputs: [[1, 2, nil, 3], ['a', 'b', 'c', nil, 'd']],
643
+ method_name: :tcompact!,
644
+ expected_self: [[1, 2, 3], ['a', 'b', 'c', 'd']],
645
+ expected_ret: [[1, 2, 3], ['a', 'b', 'c', 'd']],
646
+ },
647
+ ]
648
+
649
+ cases.each do |c|
650
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
651
+ begin
652
+ case_before c
653
+
654
+ # -- given --
655
+ # nothing
656
+
657
+ # -- when --
658
+ actual = c[:inputs].send c[:method_name]
659
+
660
+ # -- then --
661
+ expect(c[:inputs]).to eq(c[:expected_self])
662
+ expect(actual).to eq(c[:expected_ret])
663
+ ensure
664
+ case_after c
665
+ end
666
+ end
667
+
668
+ def case_before(c)
669
+ # implement each case before
670
+ end
671
+
672
+ def case_after(c)
673
+ # implement each case after
674
+ end
675
+ end
676
+ end
677
+
678
+ context :together_delete do
679
+ cases = [
680
+ {
681
+ case_no: 1,
682
+ case_title: 'valid case',
683
+ inputs: [[1, 2, 3, 4], [2, 3, 4, 5]],
684
+ delete_value: 2,
685
+ method_name: :together_delete,
686
+ expected: [[1, 3, 4], [3, 4, 5]],
687
+ ret: 2,
688
+ },
689
+ {
690
+ case_no: 2,
691
+ case_title: 'valid case(not exist and block use)',
692
+ inputs: [[1, 2, 3, 4], [2, 3, 4, 5]],
693
+ delete_value: 6,
694
+ method_name: :together_delete,
695
+ expected: [[1, 2, 3, 4], [2, 3, 4, 5]],
696
+ ret: :ret,
697
+ has_block: true,
698
+ block: :ret,
699
+ },
700
+ {
701
+ case_no: 3,
702
+ case_title: 'valid case(not exist and block unuse)',
703
+ inputs: [[1, 2, 3, 4], [2, 3, 4, 5]],
704
+ delete_value: 6,
705
+ method_name: :together_delete,
706
+ expected: [[1, 2, 3, 4], [2, 3, 4, 5]],
707
+ ret: nil,
708
+ },
709
+ {
710
+ case_no: 4,
711
+ case_title: 'valid case(alias tdelete)',
712
+ inputs: [[1, 2, 3, 4], [2, 3, 4, 5]],
713
+ delete_value: 2,
714
+ method_name: :tdelete,
715
+ expected: [[1, 3, 4], [3, 4, 5]],
716
+ ret: 2,
717
+ },
718
+ ]
719
+
720
+ cases.each do |c|
721
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
722
+ begin
723
+ case_before c
724
+
725
+ # -- given --
726
+ # nothing
727
+
728
+ # -- when --
729
+ if c[:has_block]
730
+ actual = c[:inputs].send c[:method_name], c[:delete_value] {c[:block] }
731
+ else
732
+ actual = c[:inputs].send c[:method_name], c[:delete_value]
733
+ end
734
+
735
+ # -- then --
736
+ expect(actual).to eq(c[:ret])
737
+ expect(c[:inputs]).to eq(c[:expected])
738
+ ensure
739
+ case_after c
740
+ end
741
+ end
742
+
743
+ def case_before(c)
744
+ # implement each case before
745
+ end
746
+
747
+ def case_after(c)
748
+ # implement each case after
749
+ end
750
+ end
751
+ end
752
+
753
+ context :together_delete_at do
754
+ cases = [
755
+ {
756
+ case_no: 1,
757
+ case_title: 'valid case',
758
+ inputs: [[1, 2, 3, 4], [2, 3, 4, 5]],
759
+ delete_value: 2,
760
+ method_name: :together_delete_at,
761
+ expected: [[1, 2, 4], [2, 3, 5]],
762
+ ret: [3, 4],
763
+ },
764
+ {
765
+ case_no: 2,
766
+ case_title: 'valid case(not exist)',
767
+ inputs: [[1, 2, 3, 4], [2, 3, 4, 5]],
768
+ delete_value: 5,
769
+ method_name: :together_delete_at,
770
+ expected: [[1, 2, 3, 4], [2, 3, 4, 5]],
771
+ ret: [nil, nil],
772
+ },
773
+ {
774
+ case_no: 3,
775
+ case_title: 'valid case(minus index)',
776
+ inputs: [[1, 2, 3, 4], [2, 3, 4, 5]],
777
+ delete_value: -3,
778
+ method_name: :together_delete_at,
779
+ expected: [[1, 3, 4], [2, 4, 5]],
780
+ ret: [2, 3],
781
+ },
782
+ {
783
+ case_no: 4,
784
+ case_title: 'valid case(use tdelete_at alias)',
785
+ inputs: [[1, 2, 3, 4], [2, 3, 4, 5]],
786
+ delete_value: 2,
787
+ method_name: :tdelete_at,
788
+ expected: [[1, 2, 4], [2, 3, 5]],
789
+ ret: [3, 4],
790
+ },
791
+ ]
792
+
793
+ cases.each do |c|
794
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
795
+ begin
796
+ case_before c
797
+
798
+ # -- given --
799
+ # nothing
800
+
801
+ # -- when --
802
+ if c[:has_block]
803
+ actual = c[:inputs].send c[:method_name], c[:delete_value] {c[:block] }
804
+ else
805
+ actual = c[:inputs].send c[:method_name], c[:delete_value]
806
+ end
807
+
808
+ # -- then --
809
+ expect(actual).to eq(c[:ret])
810
+ expect(c[:inputs]).to eq(c[:expected])
811
+ ensure
812
+ case_after c
813
+ end
814
+ end
815
+
816
+ def case_before(c)
817
+ # implement each case before
818
+ end
819
+
820
+ def case_after(c)
821
+ # implement each case after
822
+ end
823
+ end
824
+ end
825
+
826
+ context :together_delete_if do
827
+ cases = [
828
+ {
829
+ case_no: 1,
830
+ case_title: 'valid case',
831
+ inputs: [[1, 2, 3, 4], [6, 4, 6, 8]],
832
+ delete_block_code: '(first + second).odd?',
833
+ method_name: :together_delete_if,
834
+ expected: [[2, 4], [4, 8]],
835
+ },
836
+ {
837
+ case_no: 2,
838
+ case_title: 'not have delete target value case',
839
+ inputs: [[2, 2, 4, 4], [6, 4, 6, 8]],
840
+ delete_block_code: '(first + second).odd?',
841
+ method_name: :together_delete_if,
842
+ expected: nil,
843
+ },
844
+ {
845
+ case_no: 3,
846
+ case_title: 'valid case(alias tdelete_if)',
847
+ inputs: [[1, 2, 3, 4], [6, 4, 6, 8]],
848
+ delete_block_code: '(first + second).odd?',
849
+ method_name: :tdelete_if,
850
+ expected: [[2, 4], [4, 8]],
851
+ },
852
+ ]
853
+
854
+ cases.each do |c|
855
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
856
+ begin
857
+ case_before c
858
+
859
+ # -- given --
860
+ # nothing
861
+
862
+ # -- when --
863
+ actual = c[:inputs].send c[:method_name] {|first, second|eval c[:delete_block_code], binding }
864
+
865
+ # -- then --
866
+ expect(actual).to eq(c[:expected])
867
+ ensure
868
+ case_after c
869
+ end
870
+ end
871
+
872
+ def case_before(c)
873
+ # implement each case before
874
+ end
875
+
876
+ def case_after(c)
877
+ # implement each case after
878
+ end
879
+ end
880
+ end
881
+
882
+ context :together_empty? do
883
+ cases = [
884
+ {
885
+ case_no: 1,
886
+ case_title: 'empty case',
887
+ inputs: [[], []],
888
+ method_name: :together_empty?,
889
+ expected: true,
890
+ },
891
+ {
892
+ case_no: 2,
893
+ case_title: 'not empty case',
894
+ inputs: [[1], []],
895
+ method_name: :together_empty?,
896
+ expected: false,
897
+ },
898
+ {
899
+ case_no: 3,
900
+ case_title: 'empty case(alias tempty?)',
901
+ inputs: [[], []],
902
+ method_name: :tempty?,
903
+ expected: true,
904
+ },
905
+ ]
906
+
907
+ cases.each do |c|
908
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
909
+ begin
910
+ case_before c
911
+
912
+ # -- given --
913
+ # nothing
914
+
915
+ # -- when --
916
+ actual = c[:inputs].send c[:method_name]
917
+
918
+ # -- then --
919
+ expect(actual).to eq(c[:expected])
920
+ ensure
921
+ case_after c
922
+ end
923
+ end
924
+
925
+ def case_before(c)
926
+ # implement each case before
927
+ end
928
+
929
+ def case_after(c)
930
+ # implement each case after
931
+ end
932
+ end
933
+ end
934
+
935
+ context :together_fill do
936
+ cases = [
937
+ {
938
+ case_no: 1,
939
+ case_title: 'not use block case',
940
+ inputs: [[*1..5], [*6..10]],
941
+ fill_value: 99,
942
+ method_name: :together_fill,
943
+ expected: [[99, 99, 99, 99, 99], [99, 99, 99, 99, 99]],
944
+ has_block: false,
945
+ },
946
+ {
947
+ case_no: 2,
948
+ case_title: 'use block, no args case',
949
+ inputs: [[*1..5], [*6..10]],
950
+ fill_value: nil,
951
+ method_name: :together_fill,
952
+ expected: [[2, 3, 4, 5, 6], [2, 3, 4, 5, 6]],
953
+ block_logic: '(i + 1) + 1',
954
+ has_block: true,
955
+ },
956
+ {
957
+ case_no: 3,
958
+ case_title: 'use block, has args case',
959
+ inputs: [[*1..5], [*6..10]],
960
+ fill_value: 2,
961
+ method_name: :together_fill,
962
+ expected: [[1, 2, 4, 5, 6], [6, 7, 4, 5, 6]],
963
+ block_logic: '(i + 1) + 1',
964
+ has_block: true,
965
+ },
966
+ {
967
+ case_no: 4,
968
+ case_title: 'not use block case(alias tfill)',
969
+ inputs: [[*1..5], [*6..10]],
970
+ fill_value: 99,
971
+ method_name: :tfill,
972
+ expected: [[99, 99, 99, 99, 99], [99, 99, 99, 99, 99]],
973
+ has_block: false,
974
+ },
975
+ ]
976
+
977
+ cases.each do |c|
978
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
979
+ begin
980
+ case_before c
981
+
982
+ # -- given --
983
+ # nothing
984
+
985
+ # -- when --
986
+ if c[:has_block]
987
+ actual = c[:inputs].send c[:method_name], c[:fill_value] {|i|eval c[:block_logic], binding }
988
+ else
989
+ actual = c[:inputs].send c[:method_name], c[:fill_value]
990
+ end
991
+
992
+ # -- then --
993
+ expect(actual).to eq(c[:expected])
994
+ ensure
995
+ case_after c
996
+ end
997
+ end
998
+
999
+ def case_before(c)
1000
+ # implement each case before
1001
+ end
1002
+
1003
+ def case_after(c)
1004
+ # implement each case after
1005
+ end
1006
+ end
1007
+ end
1008
+
1009
+
1010
+ context :together_first do
1011
+ cases = [
1012
+ {
1013
+ case_no: 1,
1014
+ case_title: 'not args case',
1015
+ inputs: [[*1..5], [*6..10]],
1016
+ method_name: :together_first,
1017
+ expected: [1, 6],
1018
+ has_args: false,
1019
+ },
1020
+ {
1021
+ case_no: 2,
1022
+ case_title: 'has args 2 case',
1023
+ inputs: [[*1..5], [*6..10]],
1024
+ args: 2,
1025
+ method_name: :together_first,
1026
+ expected: [[1, 2], [6, 7]],
1027
+ has_args: true,
1028
+ },
1029
+ {
1030
+ case_no: 3,
1031
+ case_title: 'has args 0 case',
1032
+ inputs: [[*1..5], [*6..10]],
1033
+ args: 0,
1034
+ method_name: :together_first,
1035
+ expected: [[], []],
1036
+ has_args: true,
1037
+ },
1038
+ {
1039
+ case_no: 4,
1040
+ case_title: 'has args over size case',
1041
+ inputs: [[*1..5], [*6..10]],
1042
+ args: 6,
1043
+ method_name: :together_first,
1044
+ expected: [[*1..5], [*6..10]],
1045
+ has_args: true,
1046
+ },
1047
+ {
1048
+ case_no: 5,
1049
+ case_title: 'not args case(alias tfirst)',
1050
+ inputs: [[*1..5], [*6..10]],
1051
+ method_name: :tfirst,
1052
+ expected: [1, 6],
1053
+ has_args: false,
1054
+ },
1055
+ ]
1056
+
1057
+ cases.each do |c|
1058
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
1059
+ begin
1060
+ case_before c
1061
+
1062
+ # -- given --
1063
+ # nothing
1064
+
1065
+ # -- when --
1066
+ if c[:has_args]
1067
+ actual = c[:inputs].send c[:method_name], c[:args]
1068
+ else
1069
+ actual = c[:inputs].send c[:method_name]
1070
+ end
1071
+
1072
+ # -- then --
1073
+ expect(actual).to eq(c[:expected])
1074
+ ensure
1075
+ case_after c
1076
+ end
1077
+ end
1078
+
1079
+ def case_before(c)
1080
+ # implement each case before
1081
+ end
1082
+
1083
+ def case_after(c)
1084
+ # implement each case after
1085
+ end
1086
+ end
1087
+ end
1088
+ end