rspec-core 2.0.0.a1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.document +5 -0
- data/.gitignore +7 -0
- data/.treasure_map.rb +23 -0
- data/License.txt +22 -0
- data/README.markdown +9 -0
- data/Rakefile +76 -0
- data/TODO.markdown +16 -0
- data/VERSION +1 -0
- data/VERSION.yml +5 -0
- data/bin/rspec +12 -0
- data/cucumber.yml +2 -0
- data/example_specs/failing/README.txt +7 -0
- data/example_specs/failing/diffing_spec.rb +36 -0
- data/example_specs/failing/failing_implicit_docstrings_example.rb +19 -0
- data/example_specs/failing/failure_in_after.rb +10 -0
- data/example_specs/failing/failure_in_before.rb +10 -0
- data/example_specs/failing/mocking_example.rb +40 -0
- data/example_specs/failing/mocking_with_flexmock.rb +26 -0
- data/example_specs/failing/mocking_with_mocha.rb +25 -0
- data/example_specs/failing/mocking_with_rr.rb +27 -0
- data/example_specs/failing/partial_mock_example.rb +20 -0
- data/example_specs/failing/pending_example.rb +9 -0
- data/example_specs/failing/predicate_example.rb +34 -0
- data/example_specs/failing/raising_example.rb +47 -0
- data/example_specs/failing/spec_helper.rb +3 -0
- data/example_specs/failing/syntax_error_example.rb +7 -0
- data/example_specs/failing/team_spec.rb +44 -0
- data/example_specs/failing/timeout_behaviour.rb +7 -0
- data/example_specs/passing/custom_formatter.rb +12 -0
- data/example_specs/passing/custom_matchers.rb +54 -0
- data/example_specs/passing/dynamic_spec.rb +9 -0
- data/example_specs/passing/file_accessor.rb +19 -0
- data/example_specs/passing/file_accessor_spec.rb +38 -0
- data/example_specs/passing/filtered_formatter.rb +18 -0
- data/example_specs/passing/filtered_formatter_example.rb +31 -0
- data/example_specs/passing/greeter_spec.rb +31 -0
- data/example_specs/passing/helper_method_example.rb +14 -0
- data/example_specs/passing/implicit_docstrings_example.rb +18 -0
- data/example_specs/passing/io_processor.rb +8 -0
- data/example_specs/passing/io_processor_spec.rb +21 -0
- data/example_specs/passing/mocking_example.rb +27 -0
- data/example_specs/passing/multi_threaded_example_group_runner.rb +26 -0
- data/example_specs/passing/nested_classes_example.rb +36 -0
- data/example_specs/passing/options_example.rb +31 -0
- data/example_specs/passing/options_formatter.rb +20 -0
- data/example_specs/passing/partial_mock_example.rb +29 -0
- data/example_specs/passing/pending_example.rb +20 -0
- data/example_specs/passing/predicate_example.rb +27 -0
- data/example_specs/passing/shared_example_group_example.rb +81 -0
- data/example_specs/passing/shared_stack_examples.rb +36 -0
- data/example_specs/passing/simple_matcher_example.rb +31 -0
- data/example_specs/passing/spec_helper.rb +14 -0
- data/example_specs/passing/stack.rb +36 -0
- data/example_specs/passing/stack_spec.rb +64 -0
- data/example_specs/passing/stack_spec_with_nested_example_groups.rb +67 -0
- data/example_specs/passing/stubbing_example.rb +69 -0
- data/example_specs/passing/yielding_example.rb +33 -0
- data/example_specs/ruby1.9.compatibility/access_to_constants_spec.rb +85 -0
- data/features-pending/command_line/line_number_option.feature +56 -0
- data/features-pending/command_line/line_number_option_with_example_with_no_name.feature +22 -0
- data/features-pending/example_groups/example_group_with_should_methods.feature +29 -0
- data/features-pending/example_groups/implicit_docstrings.feature +59 -0
- data/features-pending/example_groups/nested_groups.feature +32 -0
- data/features-pending/expectations/expect_change.feature +65 -0
- data/features-pending/expectations/expect_error.feature +44 -0
- data/features-pending/extensions/custom_example_group.feature +19 -0
- data/features-pending/formatters/custom_formatter.feature +30 -0
- data/features-pending/heckle/heckle.feature +56 -0
- data/features-pending/interop/examples_and_tests_together.feature +80 -0
- data/features-pending/interop/rspec_output.feature +25 -0
- data/features-pending/interop/test_but_not_test_unit.feature +26 -0
- data/features-pending/interop/test_case_with_should_methods.feature +46 -0
- data/features-pending/matchers/define_diffable_matcher.feature +26 -0
- data/features-pending/matchers/define_matcher.feature +179 -0
- data/features-pending/matchers/define_matcher_with_fluent_interface.feature +27 -0
- data/features-pending/mocks/mix_stubs_and_mocks.feature +22 -0
- data/features-pending/mocks/stub_implementation.feature +26 -0
- data/features-pending/pending/pending_examples.feature +81 -0
- data/features-pending/runner/specify_line_number.feature +32 -0
- data/features/before_and_after_blocks/before_and_after_blocks.feature +169 -0
- data/features/expectations/customized_message.feature +54 -0
- data/features/matchers/define_matcher_outside_rspec.feature +39 -0
- data/features/mock_framework_integration/use_flexmock.feature +23 -0
- data/features/mock_framework_integration/use_mocha.feature +23 -0
- data/features/mock_framework_integration/use_rr.feature +23 -0
- data/features/mock_framework_integration/use_rspec.feature +23 -0
- data/features/step_definitions/running_rspec_steps.rb +35 -0
- data/features/subject/explicit_subject.feature +31 -0
- data/features/subject/implicit_subject.feature +31 -0
- data/features/support/env.rb +82 -0
- data/features/support/matchers/smart_match.rb +14 -0
- data/lib/rspec/autorun.rb +2 -0
- data/lib/rspec/core.rb +38 -0
- data/lib/rspec/core/backward_compatibility.rb +9 -0
- data/lib/rspec/core/command_line_options.rb +60 -0
- data/lib/rspec/core/configuration.rb +222 -0
- data/lib/rspec/core/deprecation.rb +47 -0
- data/lib/rspec/core/example.rb +113 -0
- data/lib/rspec/core/example_group.rb +239 -0
- data/lib/rspec/core/example_group_subject.rb +77 -0
- data/lib/rspec/core/formatters.rb +16 -0
- data/lib/rspec/core/formatters/base_formatter.rb +123 -0
- data/lib/rspec/core/formatters/base_text_formatter.rb +139 -0
- data/lib/rspec/core/formatters/documentation_formatter.rb +84 -0
- data/lib/rspec/core/formatters/progress_formatter.rb +36 -0
- data/lib/rspec/core/kernel_extensions.rb +15 -0
- data/lib/rspec/core/mocking/with_absolutely_nothing.rb +13 -0
- data/lib/rspec/core/mocking/with_flexmock.rb +25 -0
- data/lib/rspec/core/mocking/with_mocha.rb +22 -0
- data/lib/rspec/core/mocking/with_rr.rb +26 -0
- data/lib/rspec/core/mocking/with_rspec.rb +21 -0
- data/lib/rspec/core/rake_task.rb +88 -0
- data/lib/rspec/core/runner.rb +66 -0
- data/lib/rspec/core/shared_behaviour.rb +41 -0
- data/lib/rspec/core/shared_behaviour_kernel_extensions.rb +31 -0
- data/lib/rspec/core/version.rb +16 -0
- data/lib/rspec/core/world.rb +105 -0
- data/rspec-core.gemspec +204 -0
- data/script/console +8 -0
- data/spec/resources/example_classes.rb +67 -0
- data/spec/rspec/core/command_line_options_spec.rb +63 -0
- data/spec/rspec/core/configuration_spec.rb +171 -0
- data/spec/rspec/core/example_group_spec.rb +351 -0
- data/spec/rspec/core/example_group_subject_spec.rb +67 -0
- data/spec/rspec/core/example_spec.rb +67 -0
- data/spec/rspec/core/formatters/base_formatter_spec.rb +105 -0
- data/spec/rspec/core/formatters/documentation_formatter_spec.rb +5 -0
- data/spec/rspec/core/formatters/progress_formatter_spec.rb +29 -0
- data/spec/rspec/core/kernel_extensions_spec.rb +13 -0
- data/spec/rspec/core/mocha_spec.rb +29 -0
- data/spec/rspec/core/resources/a_bar.rb +0 -0
- data/spec/rspec/core/resources/a_foo.rb +0 -0
- data/spec/rspec/core/resources/a_spec.rb +1 -0
- data/spec/rspec/core/resources/custom_example_group_runner.rb +14 -0
- data/spec/rspec/core/resources/example_classes.rb +67 -0
- data/spec/rspec/core/resources/utf8_encoded.rb +8 -0
- data/spec/rspec/core/runner_spec.rb +34 -0
- data/spec/rspec/core/shared_behaviour_spec.rb +185 -0
- data/spec/rspec/core/world_spec.rb +167 -0
- data/spec/rspec/core_spec.rb +35 -0
- data/spec/ruby_forker.rb +13 -0
- data/spec/spec_helper.rb +72 -0
- metadata +219 -0
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
|
2
|
+
require File.dirname(__FILE__) + '/stack'
|
|
3
|
+
|
|
4
|
+
class StackExamples < Spec::ExampleGroup
|
|
5
|
+
describe(Stack)
|
|
6
|
+
before(:each) do
|
|
7
|
+
@stack = Stack.new
|
|
8
|
+
end
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
class EmptyStackExamples < StackExamples
|
|
12
|
+
describe("when empty")
|
|
13
|
+
it "should be empty" do
|
|
14
|
+
@stack.should be_empty
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
class AlmostFullStackExamples < StackExamples
|
|
19
|
+
describe("when almost full")
|
|
20
|
+
before(:each) do
|
|
21
|
+
(1..9).each {|n| @stack.push n}
|
|
22
|
+
end
|
|
23
|
+
it "should be full" do
|
|
24
|
+
@stack.should_not be_full
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
class FullStackExamples < StackExamples
|
|
29
|
+
describe("when full")
|
|
30
|
+
before(:each) do
|
|
31
|
+
(1..10).each {|n| @stack.push n}
|
|
32
|
+
end
|
|
33
|
+
it "should be full" do
|
|
34
|
+
@stack.should be_full
|
|
35
|
+
end
|
|
36
|
+
end
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# This demonstrates the use of the options hash to support custom reporting.
|
|
2
|
+
# To see the result, run this command from the project root:
|
|
3
|
+
#
|
|
4
|
+
# bin/spec --require examples/passing/options_formatter.rb examples/passing/options_example.rb \
|
|
5
|
+
# --format OptionsFormatter
|
|
6
|
+
|
|
7
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
|
8
|
+
|
|
9
|
+
describe "this group will be reported", :report => true do
|
|
10
|
+
it "this example will be reported", :report => true do
|
|
11
|
+
# no-op
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
it "this example will not be reported", :report => false do
|
|
15
|
+
# no-op
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
it "this example will also not be reported", :foo => 'bar' do
|
|
19
|
+
# no-op
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
it "this example will also also not be reported" do
|
|
23
|
+
# no-op
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
describe "this group will not be reported", :report => false do
|
|
28
|
+
it "though this example will", :report => true do
|
|
29
|
+
# no-op
|
|
30
|
+
end
|
|
31
|
+
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# This is an example of how you can use a custom formatter to do custom
|
|
2
|
+
# reporting. This formatter will only report example groups and examples that
|
|
3
|
+
# have :report => true (or anything truthy) in the declaration. See
|
|
4
|
+
# options_example.rb in this directory.
|
|
5
|
+
|
|
6
|
+
require 'spec/runner/formatter/base_text_formatter'
|
|
7
|
+
|
|
8
|
+
class OptionsFormatter < Spec::Runner::Formatter::BaseTextFormatter
|
|
9
|
+
def example_started(proxy)
|
|
10
|
+
if proxy.options[:report]
|
|
11
|
+
puts proxy.description
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def example_group_started(proxy)
|
|
16
|
+
if proxy.options[:report]
|
|
17
|
+
puts proxy.description
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
|
2
|
+
|
|
3
|
+
class MockableClass
|
|
4
|
+
def self.find id
|
|
5
|
+
return :original_return
|
|
6
|
+
end
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
describe "A partial mock" do
|
|
10
|
+
|
|
11
|
+
it "should work at the class level" do
|
|
12
|
+
MockableClass.should_receive(:find).with(1).and_return {:stub_return}
|
|
13
|
+
MockableClass.find(1).should equal(:stub_return)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
it "should revert to the original after each spec" do
|
|
17
|
+
MockableClass.find(1).should equal(:original_return)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
it "can be mocked w/ ordering" do
|
|
21
|
+
MockableClass.should_receive(:msg_1).ordered
|
|
22
|
+
MockableClass.should_receive(:msg_2).ordered
|
|
23
|
+
MockableClass.should_receive(:msg_3).ordered
|
|
24
|
+
MockableClass.msg_1
|
|
25
|
+
MockableClass.msg_2
|
|
26
|
+
MockableClass.msg_3
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
|
2
|
+
|
|
3
|
+
describe "pending example (using pending method)" do
|
|
4
|
+
it %Q|should be reported as "PENDING: for some reason"| do
|
|
5
|
+
pending("for some reason")
|
|
6
|
+
end
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
describe "pending example (with no block)" do
|
|
10
|
+
it %Q|should be reported as "PENDING: Not Yet Implemented"|
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
describe "pending example (with block for pending)" do
|
|
14
|
+
it %Q|should have a failing block, passed to pending, reported as "PENDING: for some reason"| do
|
|
15
|
+
pending("for some reason") do
|
|
16
|
+
raise "some reason"
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
|
2
|
+
|
|
3
|
+
class BddFramework
|
|
4
|
+
def intuitive?
|
|
5
|
+
true
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
def adopted_quickly?
|
|
9
|
+
true
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
describe "BDD framework" do
|
|
14
|
+
|
|
15
|
+
before(:each) do
|
|
16
|
+
@bdd_framework = BddFramework.new
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
it "should be adopted quickly" do
|
|
20
|
+
@bdd_framework.should be_adopted_quickly
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
it "should be intuitive" do
|
|
24
|
+
@bdd_framework.should be_intuitive
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
end
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
|
2
|
+
|
|
3
|
+
module SharedExampleGroupExample
|
|
4
|
+
class OneThing
|
|
5
|
+
def what_things_do
|
|
6
|
+
"stuff"
|
|
7
|
+
end
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
class AnotherThing
|
|
11
|
+
def what_things_do
|
|
12
|
+
"stuff"
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
class YetAnotherThing
|
|
17
|
+
def what_things_do
|
|
18
|
+
"stuff"
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# A SharedExampleGroup is an example group that doesn't get run.
|
|
23
|
+
# You can create one like this:
|
|
24
|
+
share_examples_for "most things" do
|
|
25
|
+
def helper_method
|
|
26
|
+
"helper method"
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
it "should do what things do" do
|
|
30
|
+
@thing.what_things_do.should == "stuff"
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# A SharedExampleGroup is also a module. If you create one like this it gets
|
|
35
|
+
# assigned to the constant MostThings
|
|
36
|
+
share_as :MostThings do
|
|
37
|
+
def helper_method
|
|
38
|
+
"helper method"
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
it "should do what things do" do
|
|
42
|
+
@thing.what_things_do.should == "stuff"
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
describe OneThing do
|
|
47
|
+
# Now you can include the shared example group like this, which
|
|
48
|
+
# feels more like what you might say ...
|
|
49
|
+
it_should_behave_like "most things"
|
|
50
|
+
|
|
51
|
+
before(:each) { @thing = OneThing.new }
|
|
52
|
+
|
|
53
|
+
it "should have access to helper methods defined in the shared example group" do
|
|
54
|
+
helper_method.should == "helper method"
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
describe AnotherThing do
|
|
59
|
+
# ... or you can include the example group like this, which
|
|
60
|
+
# feels more like the programming language we love.
|
|
61
|
+
it_should_behave_like MostThings
|
|
62
|
+
|
|
63
|
+
before(:each) { @thing = AnotherThing.new }
|
|
64
|
+
|
|
65
|
+
it "should have access to helper methods defined in the shared example group" do
|
|
66
|
+
helper_method.should == "helper method"
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
describe YetAnotherThing do
|
|
71
|
+
#... or you can include the example group like this, which
|
|
72
|
+
#feels more like the programming language we love.
|
|
73
|
+
include MostThings
|
|
74
|
+
|
|
75
|
+
before(:each) { @thing = AnotherThing.new }
|
|
76
|
+
|
|
77
|
+
it "should have access to helper methods defined in the shared example group" do
|
|
78
|
+
helper_method.should == "helper method"
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
shared_examples_for "non-empty Stack" do
|
|
2
|
+
|
|
3
|
+
it { @stack.should_not be_empty }
|
|
4
|
+
|
|
5
|
+
it "should return the top item when sent #peek" do
|
|
6
|
+
@stack.peek.should == @last_item_added
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
it "should NOT remove the top item when sent #peek" do
|
|
10
|
+
@stack.peek.should == @last_item_added
|
|
11
|
+
@stack.peek.should == @last_item_added
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
it "should return the top item when sent #pop" do
|
|
15
|
+
@stack.pop.should == @last_item_added
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
it "should remove the top item when sent #pop" do
|
|
19
|
+
@stack.pop.should == @last_item_added
|
|
20
|
+
unless @stack.empty?
|
|
21
|
+
@stack.pop.should_not == @last_item_added
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
shared_examples_for "non-full Stack" do
|
|
28
|
+
|
|
29
|
+
it { @stack.should_not be_full }
|
|
30
|
+
|
|
31
|
+
it "should add to the top when sent #push" do
|
|
32
|
+
@stack.push "newly added top item"
|
|
33
|
+
@stack.peek.should == "newly added top item"
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
end
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
|
2
|
+
|
|
3
|
+
describe "arrays" do
|
|
4
|
+
def contain_same_elements_as(expected)
|
|
5
|
+
simple_matcher "array with same elements in any order as #{expected.inspect}" do |actual|
|
|
6
|
+
if actual.size == expected.size
|
|
7
|
+
a, e = actual.dup, expected.dup
|
|
8
|
+
until e.empty? do
|
|
9
|
+
if i = a.index(e.pop) then a.delete_at(i) end
|
|
10
|
+
end
|
|
11
|
+
a.empty?
|
|
12
|
+
else
|
|
13
|
+
false
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
describe "can be matched by their contents disregarding order" do
|
|
19
|
+
subject { [1,2,2,3] }
|
|
20
|
+
it { should contain_same_elements_as([1,2,2,3]) }
|
|
21
|
+
it { should contain_same_elements_as([2,3,2,1]) }
|
|
22
|
+
it { should_not contain_same_elements_as([3,3,2,1]) }
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
describe "fail the match with different contents" do
|
|
26
|
+
subject { [1,2,3] }
|
|
27
|
+
it { should_not contain_same_elements_as([2,3,4])}
|
|
28
|
+
it { should_not contain_same_elements_as([1,2,2,3])}
|
|
29
|
+
it { should_not contain_same_elements_as([1,2])}
|
|
30
|
+
end
|
|
31
|
+
end
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
lib_path = File.expand_path("#{File.dirname(__FILE__)}/../../lib")
|
|
2
|
+
$LOAD_PATH.unshift lib_path unless $LOAD_PATH.include?(lib_path)
|
|
3
|
+
require 'rspec/autorun'
|
|
4
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '/../../../expectations/lib'))
|
|
5
|
+
require 'rspec/expectations'
|
|
6
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '/../../../mocks/lib'))
|
|
7
|
+
require 'rspec/mocks'
|
|
8
|
+
|
|
9
|
+
Rspec::Core::ExampleGroup.send(:include, Rspec::Matchers)
|
|
10
|
+
|
|
11
|
+
Rspec::Core.configure do |c|
|
|
12
|
+
c.mock_framework = :rspec
|
|
13
|
+
c.color_enabled = true
|
|
14
|
+
end
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
class StackUnderflowError < RuntimeError
|
|
2
|
+
end
|
|
3
|
+
|
|
4
|
+
class StackOverflowError < RuntimeError
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
class Stack
|
|
8
|
+
|
|
9
|
+
def initialize
|
|
10
|
+
@items = []
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def push object
|
|
14
|
+
raise StackOverflowError if @items.length == 10
|
|
15
|
+
@items.push object
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def pop
|
|
19
|
+
raise StackUnderflowError if @items.empty?
|
|
20
|
+
@items.delete @items.last
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def peek
|
|
24
|
+
raise StackUnderflowError if @items.empty?
|
|
25
|
+
@items.last
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def empty?
|
|
29
|
+
@items.empty?
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def full?
|
|
33
|
+
@items.length == 10
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
end
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
|
2
|
+
require File.dirname(__FILE__) + "/stack"
|
|
3
|
+
require File.dirname(__FILE__) + '/shared_stack_examples'
|
|
4
|
+
|
|
5
|
+
describe Stack, " (empty)" do
|
|
6
|
+
before(:each) do
|
|
7
|
+
@stack = Stack.new
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
# This uses @stack (because the described class is Stack) auto-generates the
|
|
11
|
+
# description "should be empty"
|
|
12
|
+
it { should be_empty }
|
|
13
|
+
|
|
14
|
+
it_should_behave_like "non-full Stack"
|
|
15
|
+
|
|
16
|
+
it "should complain when sent #peek" do
|
|
17
|
+
lambda { @stack.peek }.should raise_error(StackUnderflowError)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
it "should complain when sent #pop" do
|
|
21
|
+
lambda { @stack.pop }.should raise_error(StackUnderflowError)
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
describe Stack, " (with one item)" do
|
|
26
|
+
before(:each) do
|
|
27
|
+
@stack = Stack.new
|
|
28
|
+
@stack.push 3
|
|
29
|
+
@last_item_added = 3
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
it_should_behave_like "non-empty Stack"
|
|
33
|
+
it_should_behave_like "non-full Stack"
|
|
34
|
+
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
describe Stack, " (with one item less than capacity)" do
|
|
38
|
+
before(:each) do
|
|
39
|
+
@stack = Stack.new
|
|
40
|
+
(1..9).each { |i| @stack.push i }
|
|
41
|
+
@last_item_added = 9
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
it_should_behave_like "non-empty Stack"
|
|
45
|
+
it_should_behave_like "non-full Stack"
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
describe Stack, " (full)" do
|
|
49
|
+
before(:each) do
|
|
50
|
+
@stack = Stack.new
|
|
51
|
+
(1..10).each { |i| @stack.push i }
|
|
52
|
+
@last_item_added = 10
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# NOTE that this one auto-generates the description "should be full"
|
|
56
|
+
it { @stack.should be_full }
|
|
57
|
+
|
|
58
|
+
it_should_behave_like "non-empty Stack"
|
|
59
|
+
|
|
60
|
+
it "should complain on #push" do
|
|
61
|
+
lambda { @stack.push Object.new }.should raise_error(StackOverflowError)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
end
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
|
2
|
+
require File.dirname(__FILE__) + '/stack'
|
|
3
|
+
require File.dirname(__FILE__) + '/shared_stack_examples'
|
|
4
|
+
|
|
5
|
+
describe Stack do
|
|
6
|
+
|
|
7
|
+
before(:each) do
|
|
8
|
+
@stack = Stack.new
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
describe "(empty)" do
|
|
12
|
+
|
|
13
|
+
it { @stack.should be_empty }
|
|
14
|
+
|
|
15
|
+
it_should_behave_like "non-full Stack"
|
|
16
|
+
|
|
17
|
+
it "should complain when sent #peek" do
|
|
18
|
+
lambda { @stack.peek }.should raise_error(StackUnderflowError)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
it "should complain when sent #pop" do
|
|
22
|
+
lambda { @stack.pop }.should raise_error(StackUnderflowError)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
describe "(with one item)" do
|
|
28
|
+
|
|
29
|
+
before(:each) do
|
|
30
|
+
@stack.push 3
|
|
31
|
+
@last_item_added = 3
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
it_should_behave_like "non-empty Stack"
|
|
35
|
+
it_should_behave_like "non-full Stack"
|
|
36
|
+
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
describe "(with one item less than capacity)" do
|
|
40
|
+
|
|
41
|
+
before(:each) do
|
|
42
|
+
(1..9).each { |i| @stack.push i }
|
|
43
|
+
@last_item_added = 9
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
it_should_behave_like "non-empty Stack"
|
|
47
|
+
it_should_behave_like "non-full Stack"
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
describe "(full)" do
|
|
51
|
+
|
|
52
|
+
before(:each) do
|
|
53
|
+
(1..10).each { |i| @stack.push i }
|
|
54
|
+
@last_item_added = 10
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
it { @stack.should be_full }
|
|
58
|
+
|
|
59
|
+
it_should_behave_like "non-empty Stack"
|
|
60
|
+
|
|
61
|
+
it "should complain on #push" do
|
|
62
|
+
lambda { @stack.push Object.new }.should raise_error(StackOverflowError)
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
end
|