This repository has been archived by the owner on Oct 27, 2021. It is now read-only.
/
stack_spec.rb
64 lines (49 loc) · 1.51 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
61
62
63
64
require File.dirname(__FILE__) + '/spec_helper'
require File.dirname(__FILE__) + "/stack"
require File.dirname(__FILE__) + '/shared_stack_examples'
describe Stack, " (empty)" do
before(:each) do
@stack = Stack.new
end
# This uses @stack (because the described class is Stack) auto-generates the
# description "should be empty"
it { should be_empty }
it_should_behave_like "non-full Stack"
it "should complain when sent #peek" do
lambda { @stack.peek }.should raise_error(StackUnderflowError)
end
it "should complain when sent #pop" do
lambda { @stack.pop }.should raise_error(StackUnderflowError)
end
end
describe Stack, " (with one item)" do
before(:each) do
@stack = Stack.new
@stack.push 3
@last_item_added = 3
end
it_should_behave_like "non-empty Stack"
it_should_behave_like "non-full Stack"
end
describe Stack, " (with one item less than capacity)" do
before(:each) do
@stack = Stack.new
(1..9).each { |i| @stack.push i }
@last_item_added = 9
end
it_should_behave_like "non-empty Stack"
it_should_behave_like "non-full Stack"
end
describe Stack, " (full)" do
before(:each) do
@stack = Stack.new
(1..10).each { |i| @stack.push i }
@last_item_added = 10
end
# NOTE that this one auto-generates the description "should be full"
it { @stack.should be_full }
it_should_behave_like "non-empty Stack"
it "should complain on #push" do
lambda { @stack.push Object.new }.should raise_error(StackOverflowError)
end
end