Upgraded Rails and RSpec
[monkeycharger.git] / vendor / plugins / rspec / rspec / examples / pure / stack_spec.rb
blob22d8a652b6a3cdefc69d7d88ca02c6b15468f070
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 }
7   
8   it "should return the top item when sent #peek" do
9     @stack.peek.should == @last_item_added
10   end
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
15   end
16   
17   it "should return the top item when sent #pop" do
18     @stack.pop.should == @last_item_added
19   end
20   
21   it "should remove the top item when sent #pop" do
22     @stack.pop.should == @last_item_added
23     unless @stack.empty?
24       @stack.pop.should_not == @last_item_added
25     end
26   end
27 end
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"
36   end
37 end
39 describe Stack, " (empty)" do
40   before(:each) do
41     @stack = Stack.new
42   end
43   
44   # NOTE that this one auto-generates the description "should be empty"
45   it { @stack.should be_empty }
46   
47   it_should_behave_like "non-full Stack"
48   
49   it "should complain when sent #peek" do
50     lambda { @stack.peek }.should raise_error(StackUnderflowError)
51   end
52   
53   it "should complain when sent #pop" do
54     lambda { @stack.pop }.should raise_error(StackUnderflowError)
55   end
56 end
58 describe Stack, " (with one item)" do
59   before(:each) do
60     @stack = Stack.new
61     @stack.push 3
62     @last_item_added = 3
63   end
65   it_should_behave_like "non-empty Stack"
66   it_should_behave_like "non-full Stack"
68 end
70 describe Stack, " (with one item less than capacity)" do
71   before(:each) do
72     @stack = Stack.new
73     (1..9).each { |i| @stack.push i }
74     @last_item_added = 9
75   end
76   
77   it_should_behave_like "non-empty Stack"
78   it_should_behave_like "non-full Stack"
79 end
81 describe Stack, " (full)" do
82   before(:each) do
83     @stack = Stack.new
84     (1..10).each { |i| @stack.push i }
85     @last_item_added = 10
86   end
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)
95   end
96   
97 end