-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplit_up_spec.rb
83 lines (68 loc) · 3.69 KB
/
split_up_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
require 'spec_helper'
describe "Enumerable#split_up" do
let(:numbers_list) { 1.upto(10).to_a }
let(:numbers_hash) { { one: 1, two: 2, three: 3, four: 4 } }
let(:pad) { [:a, :b, :c] }
it "iterates a block (if given) for each slice" do
slices = []
numbers_list.split_up(length: 2, step: 3, pad: pad) { |slice| slices << slice }
slices.should eq [[1, 2], [4, 5], [7, 8], [10, :a]]
end
it "returns the processed input when a block is passed" do
numbers_list.split_up(length: 2, step: 3, pad: pad) {}.should eq [[1, 2],
[4, 5],
[7, 8],
[10, :a]]
end
it "requires the length: keyword argument" do
expect { numbers_list.split_up }.to raise_error(ArgumentError)
end
it "does not mutate the input enumerable" do
numbers_list.split_up(length: 2, step: 3, pad: [:a])
numbers_list.should eq [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
end
it "splits up a collection in slices" do
numbers_list.split_up(length: 3).should eq [[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[10]]
numbers_list.split_up(length: 2).should eq [[1, 2],
[3, 4],
[5, 6],
[7, 8],
[9, 10]]
numbers_hash.split_up(length: 2).should eq [[[:one, 1], [:two, 2]],
[[:three, 3], [:four, 4]]]
end
it "splits up a collection in slices with step" do
numbers_list.split_up(length: 2, step: 3).should eq [[1, 2],
[4, 5],
[7, 8],
[10]]
numbers_hash.split_up(length: 2, step: 3).should eq [[[:one, 1], [:two, 2]],
[[:four, 4]]]
end
it "splits up a collection in slices with pad" do
numbers_list.split_up(length: 3, pad: pad).should eq [[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[10, :a, :b]]
numbers_hash.split_up(length: 3, pad: pad).should eq [[[:one, 1], [:two, 2], [:three, 3]],
[[:four, 4], :a, :b]]
end
it "splits up a collection in slices with step and pad" do
numbers_list.split_up(length: 2, step: 3, pad: pad).should eq [[1, 2],
[4, 5],
[7, 8],
[10, :a]]
numbers_hash.split_up(length: 2, step: 3, pad: pad).should eq [[[:one, 1], [:two, 2]],
[[:four, 4], :a]]
end
it "leaves the last partition with less than n items if not enough padding elements" do
not_enough = [:not_enough]
numbers_list.split_up(length: 3, pad: not_enough).should eq [[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[10, :not_enough]]
end
end