tbpgr_utils 0.0.23 → 0.0.24
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +41 -2
- data/lib/open_classes/array.rb +349 -312
- data/lib/tbpgr_utils/version.rb +1 -1
- data/spec/open_classes/array_spec.rb +752 -678
- metadata +12 -12
@@ -1,678 +1,752 @@
|
|
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
|
-
|
631
|
-
|
632
|
-
|
633
|
-
|
634
|
-
|
635
|
-
|
636
|
-
|
637
|
-
|
638
|
-
|
639
|
-
|
640
|
-
|
641
|
-
|
642
|
-
|
643
|
-
|
644
|
-
|
645
|
-
|
646
|
-
|
647
|
-
|
648
|
-
|
649
|
-
|
650
|
-
|
651
|
-
|
652
|
-
|
653
|
-
|
654
|
-
|
655
|
-
#
|
656
|
-
|
657
|
-
|
658
|
-
|
659
|
-
|
660
|
-
|
661
|
-
|
662
|
-
expect(
|
663
|
-
|
664
|
-
|
665
|
-
|
666
|
-
|
667
|
-
|
668
|
-
|
669
|
-
|
670
|
-
|
671
|
-
|
672
|
-
|
673
|
-
|
674
|
-
|
675
|
-
|
676
|
-
|
677
|
-
|
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_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
|
+
end
|