tbpgr_utils 0.0.22 → 0.0.23

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,590 +1,678 @@
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_select do
177
- cases = [
178
- {
179
- case_no: 1,
180
- case_title: 'single valid case',
181
- inputs: [[1, 2, 3, 4], [4, 2, 3, 1]],
182
- condition: 'first == second',
183
- method_name: :together_select,
184
- expected: [[2, 3], [2, 3]],
185
- },
186
- {
187
- case_no: 2,
188
- case_title: 'multi valid case',
189
- inputs: [[1, 2, 3, 4], [4, 2, 3, 1]],
190
- condition: '[first.odd?, second.even?]',
191
- expected: [[1, 3], [4, 2]],
192
- method_name: :together_select,
193
- },
194
- {
195
- case_no: 3,
196
- case_title: 'multi valid case',
197
- inputs: [[1, 2, 3, 4], [4, 2, 3, 1]],
198
- condition: '[first.odd?, second.even?]',
199
- expected: [[1, 3], [4, 2]],
200
- method_name: :tselect,
201
- },
202
- {
203
- case_no: 4,
204
- case_title: 'multi valid case',
205
- inputs: [[1, 2, 3, 4], [4, 2, 3, 1]],
206
- condition: '[first.odd?, second.even?]',
207
- expected: [[1, 3], [4, 2]],
208
- method_name: :together_find_all,
209
- },
210
- {
211
- case_no: 5,
212
- case_title: 'multi valid case',
213
- inputs: [[1, 2, 3, 4], [4, 2, 3, 1]],
214
- condition: '[first.odd?, second.even?]',
215
- expected: [[1, 3], [4, 2]],
216
- method_name: :tfindall,
217
- },
218
- ]
219
-
220
- cases.each do |c|
221
- it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
222
- begin
223
- case_before c
224
-
225
- # -- given --
226
- # nothing
227
-
228
- # -- when/then --
229
- actual = c[:inputs].method(c[:method_name]).call { |first, second| eval c[:condition], binding }
230
-
231
- expect(actual).to eq(c[:expected])
232
- ensure
233
- case_after c
234
- end
235
- end
236
-
237
- def case_before(c)
238
- # implement each case before
239
- end
240
-
241
- def case_after(c)
242
- # implement each case after
243
- end
244
- end
245
- end
246
-
247
- context :together_reduce do
248
- cases = [
249
- {
250
- case_no: 1,
251
- case_title: 'single valid case',
252
- inputs: [[1, 2, 3, 4], [4, 2, 3, 1]],
253
- init: nil,
254
- logic: 'memo + first + second',
255
- method_name: :together_reduce,
256
- expected: 20,
257
- },
258
- {
259
- case_no: 2,
260
- case_title: 'single with init valid case',
261
- inputs: [[1, 2, 3, 4], [4, 2, 3, 1]],
262
- init: 10,
263
- logic: 'memo + first + second',
264
- method_name: :together_reduce,
265
- expected: 30,
266
- },
267
- {
268
- case_no: 5,
269
- case_title: 'single valid case',
270
- inputs: [[1, 2, 3, 4], [4, 2, 3, 1]],
271
- logic: 'memo + first + second',
272
- expected: 20,
273
- method_name: :treduce,
274
- },
275
- {
276
- case_no: 6,
277
- case_title: 'single valid case',
278
- inputs: [[1, 2, 3, 4], [4, 2, 3, 1]],
279
- logic: 'memo + first + second',
280
- expected: 20,
281
- method_name: :together_inject,
282
- },
283
- {
284
- case_no: 7,
285
- case_title: 'multi valid case',
286
- inputs: [[1, 2, 3, 4], [4, 2, 3, 1]],
287
- logic: 'memo + first + second',
288
- expected: 20,
289
- method_name: :tinject,
290
- },
291
- {
292
- case_no: 8,
293
- case_title: 'single with init valid array case',
294
- inputs: [[1, 2, 3, 4], [4, 2, 3, 1]],
295
- init: [],
296
- logic: 'memo << first + second',
297
- method_name: :together_reduce,
298
- expected: [5, 4, 6, 5],
299
- },
300
- {
301
- case_no: 9,
302
- case_title: 'single with init valid array case',
303
- inputs: [[1, 2, 3, 4], [4, 2, 3, 1]],
304
- init: {},
305
- logic: 'memo[first] = second; memo',
306
- method_name: :together_reduce,
307
- expected: { 1 => 4, 2 => 2, 3 => 3, 4 => 1 },
308
- },
309
- ]
310
-
311
- cases.each do |c|
312
- it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
313
- begin
314
- case_before c
315
-
316
- # -- given --
317
- # nothing
318
-
319
- # -- when/then --
320
- if c[:init]
321
- actual = c[:inputs].method(c[:method_name]).call(c[:init]) { |memo, first, second| eval c[:logic], binding }
322
- else
323
- actual = c[:inputs].method(c[:method_name]).call { |memo, first, second| eval c[:logic], binding }
324
- end
325
-
326
- expect(actual).to eq(c[:expected])
327
- ensure
328
- case_after c
329
- end
330
- end
331
-
332
- def case_before(c)
333
- # implement each case before
334
- end
335
-
336
- def case_after(c)
337
- # implement each case after
338
- end
339
- end
340
- end
341
-
342
- context :together_concat do
343
- cases = [
344
- {
345
- case_no: 1,
346
- case_title: 'valid case',
347
- inputs: [[1, 2, 3], %w{one two three}],
348
- add_list: [4, 5, 6],
349
- method_name: :together_concat,
350
- expected: [[1, 2, 3, 4, 5, 6], ['one', 'two', 'three', 4, 5, 6]],
351
- },
352
- {
353
- case_no: 2,
354
- case_title: 'valid case',
355
- inputs: [[1, 2, 3], %w{one two three}],
356
- add_list: [4, 5, 6],
357
- method_name: :tconcat,
358
- expected: [[1, 2, 3, 4, 5, 6], ['one', 'two', 'three', 4, 5, 6]],
359
- },
360
- ]
361
-
362
- cases.each do |c|
363
- it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
364
- begin
365
- case_before c
366
-
367
- # -- given --
368
- # nothing
369
-
370
- # -- when --
371
- c[:inputs].send c[:method_name], c[:add_list]
372
-
373
- # -- then --
374
- expect(c[:inputs]).to eq(c[:expected])
375
- ensure
376
- case_after c
377
- end
378
- end
379
-
380
- def case_before(c)
381
- # implement each case before
382
- end
383
-
384
- def case_after(c)
385
- # implement each case after
386
- end
387
- end
388
- end
389
-
390
- context :together_at do
391
- cases = [
392
- {
393
- case_no: 1,
394
- case_title: 'valid case',
395
- inputs: [[1, 2, 3], %w{one two three}],
396
- index: 2,
397
- method_name: :together_at,
398
- expected: [3, 'three'],
399
- },
400
- {
401
- case_no: 2,
402
- case_title: 'contain nil case',
403
- inputs: [[1, 2, 3], %w{one two}],
404
- index: 2,
405
- method_name: :together_at,
406
- expected: [3, nil],
407
- },
408
- {
409
- case_no: 3,
410
- case_title: 'valid case',
411
- inputs: [[1, 2, 3], %w{one two three}],
412
- index: 2,
413
- method_name: :tat,
414
- expected: [3, 'three'],
415
- },
416
- ]
417
-
418
- cases.each do |c|
419
- it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
420
- begin
421
- case_before c
422
-
423
- # -- given --
424
- # nothing
425
-
426
- # -- when --
427
- actual = c[:inputs].send c[:method_name], c[:index]
428
-
429
- # -- then --
430
- expect(actual).to eq(c[:expected])
431
- ensure
432
- case_after c
433
- end
434
- end
435
-
436
- def case_before(c)
437
- # implement each case before
438
- end
439
-
440
- def case_after(c)
441
- # implement each case after
442
- end
443
- end
444
- end
445
-
446
- context :together_clear do
447
- cases = [
448
- {
449
- case_no: 1,
450
- case_title: 'valid case',
451
- inputs: [[1, 2, 3], %w{one two three}],
452
- method_name: :together_clear,
453
- expected: [[], []],
454
- },
455
- {
456
- case_no: 2,
457
- case_title: 'valid case',
458
- inputs: [[1, 2, 3], %w{one two three}],
459
- method_name: :tclear,
460
- expected: [[], []],
461
- },
462
- ]
463
-
464
- cases.each do |c|
465
- it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
466
- begin
467
- case_before c
468
-
469
- # -- given --
470
- # nothing
471
-
472
- # -- when --
473
- actual = c[:inputs].send c[:method_name]
474
-
475
- # -- then --
476
- expect(actual).to eq(c[:expected])
477
- ensure
478
- case_after c
479
- end
480
- end
481
-
482
- def case_before(c)
483
- # implement each case before
484
- end
485
-
486
- def case_after(c)
487
- # implement each case after
488
- end
489
- end
490
- end
491
-
492
- context :together_compact do
493
- cases = [
494
- {
495
- case_no: 1,
496
- case_title: 'valid case',
497
- inputs: [[1, 2, nil, 3], ['a', 'b', 'c', nil, 'd']],
498
- method_name: :together_compact,
499
- expected_self: [[1, 2, nil, 3], ['a', 'b', 'c', nil, 'd']],
500
- expected_ret: [[1, 2, 3], ['a', 'b', 'c', 'd']],
501
- },
502
- {
503
- case_no: 2,
504
- case_title: 'valid case(use alias tcompact)',
505
- inputs: [[1, 2, nil, 3], ['a', 'b', 'c', nil, 'd']],
506
- method_name: :tcompact,
507
- expected_self: [[1, 2, nil, 3], ['a', 'b', 'c', nil, 'd']],
508
- expected_ret: [[1, 2, 3], ['a', 'b', 'c', 'd']],
509
- },
510
- ]
511
-
512
- cases.each do |c|
513
- it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
514
- begin
515
- case_before c
516
-
517
- # -- given --
518
- # nothing
519
-
520
- # -- when --
521
- actual = c[:inputs].send c[:method_name]
522
-
523
- # -- then --
524
- expect(c[:inputs]).to eq(c[:expected_self])
525
- expect(actual).to eq(c[:expected_ret])
526
- ensure
527
- case_after c
528
- end
529
- end
530
-
531
- def case_before(c)
532
- # implement each case before
533
- end
534
-
535
- def case_after(c)
536
- # implement each case after
537
- end
538
- end
539
- end
540
-
541
-
542
- context :together_compact! do
543
- cases = [
544
- {
545
- case_no: 1,
546
- case_title: 'valid case',
547
- inputs: [[1, 2, nil, 3], ['a', 'b', 'c', nil, 'd']],
548
- method_name: :together_compact!,
549
- expected_self: [[1, 2, 3], ['a', 'b', 'c', 'd']],
550
- expected_ret: [[1, 2, 3], ['a', 'b', 'c', 'd']],
551
- },
552
- {
553
- case_no: 2,
554
- case_title: 'valid case(use alias tcompact)',
555
- inputs: [[1, 2, nil, 3], ['a', 'b', 'c', nil, 'd']],
556
- method_name: :tcompact!,
557
- expected_self: [[1, 2, 3], ['a', 'b', 'c', 'd']],
558
- expected_ret: [[1, 2, 3], ['a', 'b', 'c', 'd']],
559
- },
560
- ]
561
-
562
- cases.each do |c|
563
- it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
564
- begin
565
- case_before c
566
-
567
- # -- given --
568
- # nothing
569
-
570
- # -- when --
571
- actual = c[:inputs].send c[:method_name]
572
-
573
- # -- then --
574
- expect(c[:inputs]).to eq(c[:expected_self])
575
- expect(actual).to eq(c[:expected_ret])
576
- ensure
577
- case_after c
578
- end
579
- end
580
-
581
- def case_before(c)
582
- # implement each case before
583
- end
584
-
585
- def case_after(c)
586
- # implement each case after
587
- end
588
- end
589
- end
590
- 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
+
630
+ context :together_compact! do
631
+ cases = [
632
+ {
633
+ case_no: 1,
634
+ case_title: 'valid case',
635
+ inputs: [[1, 2, nil, 3], ['a', 'b', 'c', nil, 'd']],
636
+ method_name: :together_compact!,
637
+ expected_self: [[1, 2, 3], ['a', 'b', 'c', 'd']],
638
+ expected_ret: [[1, 2, 3], ['a', 'b', 'c', 'd']],
639
+ },
640
+ {
641
+ case_no: 2,
642
+ case_title: 'valid case(use alias tcompact)',
643
+ inputs: [[1, 2, nil, 3], ['a', 'b', 'c', nil, 'd']],
644
+ method_name: :tcompact!,
645
+ expected_self: [[1, 2, 3], ['a', 'b', 'c', 'd']],
646
+ expected_ret: [[1, 2, 3], ['a', 'b', 'c', 'd']],
647
+ },
648
+ ]
649
+
650
+ cases.each do |c|
651
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
652
+ begin
653
+ case_before c
654
+
655
+ # -- given --
656
+ # nothing
657
+
658
+ # -- when --
659
+ actual = c[:inputs].send c[:method_name]
660
+
661
+ # -- then --
662
+ expect(c[:inputs]).to eq(c[:expected_self])
663
+ expect(actual).to eq(c[:expected_ret])
664
+ ensure
665
+ case_after c
666
+ end
667
+ end
668
+
669
+ def case_before(c)
670
+ # implement each case before
671
+ end
672
+
673
+ def case_after(c)
674
+ # implement each case after
675
+ end
676
+ end
677
+ end
678
+ end