-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathstack_spec.rb
60 lines (45 loc) · 1.38 KB
/
stack_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
require 'example_helper'
require 'stack'
Given.use_natural_assertions
describe Stack do
Given(:stack) { Stack.new }
Given(:initial_contents) { [] }
Given { initial_contents.each do |item| stack.push(item) end }
Invariant { stack.empty? == (stack.depth == 0) }
context "with an empty stack" do
Given(:initial_contents) { [] }
Then { stack.depth == 0 }
context "when pushing" do
When { stack.push(:an_item) }
Then { stack.depth == 1 }
Then { stack.top == :an_item }
end
context "when popping" do
When(:result) { stack.pop }
Then { result == Failure(Stack::UnderflowError, /empty/) }
end
end
context "with one item" do
Given(:initial_contents) { [:an_item] }
context "when popping" do
When(:pop_result) { stack.pop }
Then { pop_result == :an_item }
Then { stack.depth == 0 }
end
end
context "with several items" do
Given(:initial_contents) { [:second_item, :top_item] }
Given!(:original_depth) { stack.depth }
context "when pushing" do
When { stack.push(:new_item) }
Then { stack.top == :new_item }
Then { stack.depth == original_depth + 1 }
end
context "when popping" do
When(:pop_result) { stack.pop }
Then { pop_result == :top_item }
Then { stack.top == :second_item }
Then { stack.depth == original_depth - 1 }
end
end
end