-
Notifications
You must be signed in to change notification settings - Fork 0
/
take_while_cake_spec.rb
57 lines (41 loc) · 1.62 KB
/
take_while_cake_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
# take_while
# Passes elements to the block until the block returns nil or false, then stops
# iterating and returns an array of all prior elements
# if no block is given, an enumerator is returned instead
describe 'TakeWhileCake' do
subject { ['cheese cake', 'carrot cake', 'white chocolate brownie cake', 'apple pie', 'sad unincluded cake'] }
let(:cakes) { subject.take_while { |m| m.include? 'cake' } }
context 'returns all cakes with the name cake in them' do
specify 'except for the last cake' do
expect(cakes).to eql ['cheese cake', 'carrot cake', 'white chocolate brownie cake']
end
specify 'does not return the sad cake' do
expect(cakes).to_not include 'sad unincluded cake'
end
specify 'does not return apple pie' do
expect(cakes).to_not include 'apple pie'
end
specify 'is an enumerator' do
expect(cakes.is_a? Enumerator).to be false
end
end
context 'when nothing is passed in' do
specify 'returns an enumerator' do
expect(subject.take_while.is_a? Enumerator).to be true
end
specify 'returns first element of array' do # is this called something like an enumerator array?
expect(subject.take_while.to_a).to eql ['cheese cake']
end
end
context 'with an empty array' do
specify 'returns an enumerator' do
expect(subject.take_while.is_a? Enumerator).to be true
end
specify 'returns an empty array' do # is this called something like an enumerator array?
expect([].take_while.to_a).to eql []
end
specify 'with a block' do
expect([].take_while { |m| m.include? 'cake' } ).to eql []
end
end
end