1 require File.dirname(__FILE__) + '/spec_helper'
2 require File.dirname(__FILE__) + "/stack"
4 describe "non-empty Stack", :shared => true do
5 # NOTE that this one auto-generates the description "should not be empty"
6 it { @stack.should_not be_empty }
8 it "should return the top item when sent #peek" do
9 @stack.peek.should == @last_item_added
12 it "should NOT remove the top item when sent #peek" do
13 @stack.peek.should == @last_item_added
14 @stack.peek.should == @last_item_added
17 it "should return the top item when sent #pop" do
18 @stack.pop.should == @last_item_added
21 it "should remove the top item when sent #pop" do
22 @stack.pop.should == @last_item_added
24 @stack.pop.should_not == @last_item_added
29 describe "non-full Stack", :shared => true do
30 # NOTE that this one auto-generates the description "should not be full"
31 it { @stack.should_not be_full }
33 it "should add to the top when sent #push" do
34 @stack.push "newly added top item"
35 @stack.peek.should == "newly added top item"
39 describe Stack, " (empty)" do
44 # NOTE that this one auto-generates the description "should be empty"
45 it { @stack.should be_empty }
47 it_should_behave_like "non-full Stack"
49 it "should complain when sent #peek" do
50 lambda { @stack.peek }.should raise_error(StackUnderflowError)
53 it "should complain when sent #pop" do
54 lambda { @stack.pop }.should raise_error(StackUnderflowError)
58 describe Stack, " (with one item)" do
65 it_should_behave_like "non-empty Stack"
66 it_should_behave_like "non-full Stack"
70 describe Stack, " (with one item less than capacity)" do
73 (1..9).each { |i| @stack.push i }
77 it_should_behave_like "non-empty Stack"
78 it_should_behave_like "non-full Stack"
81 describe Stack, " (full)" do
84 (1..10).each { |i| @stack.push i }
88 # NOTE that this one auto-generates the description "should be full"
89 it { @stack.should be_full }
91 it_should_behave_like "non-empty Stack"
93 it "should complain on #push" do
94 lambda { @stack.push Object.new }.should raise_error(StackOverflowError)