Skip to content
Browse files

Swap out Rspec::Core.world instead of invading ExampleGroup

  • Loading branch information...
1 parent 6c425ed commit 35e4a6b037b992d5fa88784687bac780d360917d @dchelimsky dchelimsky committed
View
2 lib/rspec/core/example_group.rb
@@ -99,8 +99,6 @@ def self.file_path
end
def self.describe(*args, &example_group_block)
- raise(ArgumentError, "No arguments given. You must a least supply a type or description") if args.empty?
- raise(ArgumentError, "You must supply a block when calling describe") if example_group_block.nil?
@_subclass_count ||= 0
@_subclass_count += 1
args << {} unless args.last.is_a?(Hash)
View
3 rspec-core.gemspec
@@ -9,7 +9,7 @@ Gem::Specification.new do |s|
s.required_rubygems_version = Gem::Requirement.new("> 1.3.1") if s.respond_to? :required_rubygems_version=
s.authors = ["Chad Humphries", "David Chelimsky"]
- s.date = %q{2010-04-17}
+ s.date = %q{2010-04-24}
s.description = %q{Rspec runner and example group classes}
s.email = %q{dchelimsky@gmail.com;chad.humphries@gmail.com}
s.executables = ["rspec", "spec"]
@@ -105,6 +105,7 @@ Gem::Specification.new do |s|
"lib/rspec/core/configuration.rb",
"lib/rspec/core/configuration_options.rb",
"lib/rspec/core/deprecation.rb",
+ "lib/rspec/core/errors.rb",
"lib/rspec/core/example.rb",
"lib/rspec/core/example_group.rb",
"lib/rspec/core/formatters.rb",
View
6 spec/rspec/core/configuration_spec.rb
@@ -136,7 +136,7 @@ def you_call_this_a_blt?
it "includes the given module into each example group" do
config.include(InstanceLevelMethods)
- group = ExampleGroup.create(config, 'does like, stuff and junk', :magic_key => :include) { }
+ group = ExampleGroup.describe(config, 'does like, stuff and junk', :magic_key => :include) { }
group.should_not respond_to(:you_call_this_a_blt?)
group.new.you_call_this_a_blt?.should == "egad man, where's the mayo?!?!?"
end
@@ -147,7 +147,7 @@ def you_call_this_a_blt?
it "includes the given module into each matching example group" do
config.include(InstanceLevelMethods, :magic_key => :include)
- group = ExampleGroup.create(config, 'does like, stuff and junk', :magic_key => :include) { }
+ group = ExampleGroup.describe(config, 'does like, stuff and junk', :magic_key => :include) { }
group.should_not respond_to(:you_call_this_a_blt?)
group.new.you_call_this_a_blt?.should == "egad man, where's the mayo?!?!?"
end
@@ -164,7 +164,7 @@ def that_thing
it "should extend the given module into each matching example group" do
config.extend(ThatThingISentYou, :magic_key => :extend)
- group = ExampleGroup.create(config, ThatThingISentYou, :magic_key => :extend) { }
+ group = ExampleGroup.describe(config, ThatThingISentYou, :magic_key => :extend) { }
group.should respond_to(:that_thing)
end
View
59 spec/rspec/core/example_group_spec.rb
@@ -14,25 +14,12 @@ module Rspec::Core
describe ExampleGroup do
- describe "#describe" do
-
- it "raises an ArgumentError if no type or description is given" do
- lambda { ExampleGroup.describe() {} }.should raise_error(ArgumentError, "No arguments given. You must a least supply a type or description")
- end
-
- it "raises an ArgumentError if no block is given" do
- lambda { ExampleGroup.describe('foo') }.should raise_error(ArgumentError, "You must supply a block when calling describe")
- end
-
- end
-
-
describe '#describes' do
context "with a constant as the first parameter" do
it "is that constant" do
- ExampleGroup.create(Object) { }.describes.should == Object
+ ExampleGroup.describe(Object) { }.describes.should == Object
end
end
@@ -40,7 +27,7 @@ module Rspec::Core
context "with a string as the first parameter" do
it "is nil" do
- ExampleGroup.create("i'm a computer") { }.describes.should be_nil
+ ExampleGroup.describe("i'm a computer") { }.describes.should be_nil
end
end
@@ -50,7 +37,7 @@ module Rspec::Core
describe '#description' do
it "grabs the description from the metadata" do
- group = ExampleGroup.create(Object, "my desc") { }
+ group = ExampleGroup.describe(Object, "my desc") { }
group.description.should == group.metadata[:example_group][:description]
end
@@ -59,25 +46,25 @@ module Rspec::Core
describe '#metadata' do
it "adds the third parameter to the metadata" do
- ExampleGroup.create(Object, nil, 'foo' => 'bar') { }.metadata.should include({ "foo" => 'bar' })
+ ExampleGroup.describe(Object, nil, 'foo' => 'bar') { }.metadata.should include({ "foo" => 'bar' })
end
it "adds the caller to metadata" do
- ExampleGroup.create(Object) { }.metadata[:example_group][:caller].any? {|f|
+ ExampleGroup.describe(Object) { }.metadata[:example_group][:caller].any? {|f|
f =~ /#{__FILE__}/
}.should be_true
end
it "adds the the file_path to metadata" do
- ExampleGroup.create(Object) { }.metadata[:example_group][:file_path].should == __FILE__
+ ExampleGroup.describe(Object) { }.metadata[:example_group][:file_path].should == __FILE__
end
it "has a reader for file_path" do
- ExampleGroup.create(Object) { }.file_path.should == __FILE__
+ ExampleGroup.describe(Object) { }.file_path.should == __FILE__
end
it "adds the line_number to metadata" do
- ExampleGroup.create(Object) { }.metadata[:example_group][:line_number].should == __LINE__
+ ExampleGroup.describe(Object) { }.metadata[:example_group][:line_number].should == __LINE__
end
end
@@ -85,13 +72,13 @@ module Rspec::Core
describe "before, after, and around hooks" do
it "exposes the before each blocks at before_eachs" do
- group = ExampleGroup.create
+ group = ExampleGroup.describe
group.before(:each) { 'foo' }
group.should have(1).before_eachs
end
it "maintains the before each block order" do
- group = ExampleGroup.create
+ group = ExampleGroup.describe
group.before(:each) { 15 }
group.before(:each) { 'A' }
group.before(:each) { 33.5 }
@@ -102,13 +89,13 @@ module Rspec::Core
end
it "exposes the before all blocks at before_alls" do
- group = ExampleGroup.create
+ group = ExampleGroup.describe
group.before(:all) { 'foo' }
group.should have(1).before_alls
end
it "maintains the before all block order" do
- group = ExampleGroup.create
+ group = ExampleGroup.describe
group.before(:all) { 15 }
group.before(:all) { 'A' }
group.before(:all) { 33.5 }
@@ -119,13 +106,13 @@ module Rspec::Core
end
it "exposes the after each blocks at after_eachs" do
- group = ExampleGroup.create
+ group = ExampleGroup.describe
group.after(:each) { 'foo' }
group.should have(1).after_eachs
end
it "maintains the after each block order" do
- group = ExampleGroup.create
+ group = ExampleGroup.describe
group.after(:each) { 15 }
group.after(:each) { 'A' }
group.after(:each) { 33.5 }
@@ -136,13 +123,13 @@ module Rspec::Core
end
it "exposes the after all blocks at after_alls" do
- group = ExampleGroup.create
+ group = ExampleGroup.describe
group.after(:all) { 'foo' }
group.should have(1).after_alls
end
it "maintains the after each block order" do
- group = ExampleGroup.create
+ group = ExampleGroup.describe
group.after(:all) { 15 }
group.after(:all) { 'A' }
group.after(:all) { 33.5 }
@@ -153,7 +140,7 @@ module Rspec::Core
end
it "exposes the around each blocks at after_alls" do
- group = ExampleGroup.create
+ group = ExampleGroup.describe
group.around(:each) { 'foo' }
group.should have(1).around_eachs
end
@@ -163,13 +150,13 @@ module Rspec::Core
describe "adding examples" do
it "allows adding an example using 'it'" do
- group = ExampleGroup.create
+ group = ExampleGroup.describe
group.it("should do something") { }
group.examples.size.should == 1
end
it "exposes all examples at examples" do
- group = ExampleGroup.create
+ group = ExampleGroup.describe
group.it("should do something 1") { }
group.it("should do something 2") { }
group.it("should do something 3") { }
@@ -177,7 +164,7 @@ module Rspec::Core
end
it "maintains the example order" do
- group = ExampleGroup.create
+ group = ExampleGroup.describe
group.it("should 1") { }
group.it("should 2") { }
group.it("should 3") { }
@@ -215,7 +202,7 @@ module Rspec::Core
let(:reporter) { double("reporter").as_null_object }
it "returns true if all examples pass" do
- group = ExampleGroup.create('group') do
+ group = ExampleGroup.describe('group') do
example('ex 1') { 1.should == 1 }
example('ex 2') { 1.should == 1 }
end
@@ -224,7 +211,7 @@ module Rspec::Core
end
it "returns false if any of the examples fail" do
- group = ExampleGroup.create('group') do
+ group = ExampleGroup.describe('group') do
example('ex 1') { 1.should == 1 }
example('ex 2') { 1.should == 2 }
end
@@ -233,7 +220,7 @@ module Rspec::Core
end
it "runs all examples, regardless of any of them failing" do
- group = ExampleGroup.create('group') do
+ group = ExampleGroup.describe('group') do
example('ex 1') { 1.should == 2 }
example('ex 2') { 1.should == 1 }
end
View
12 spec/rspec/core/example_spec.rb
@@ -62,9 +62,9 @@
end
describe "#run" do
- it "should run after(:each) when the example passes" do
+ it "runs after(:each) when the example passes" do
after_run = false
- group = Rspec::Core::ExampleGroup.create do
+ group = Rspec::Core::ExampleGroup.describe do
after(:each) { after_run = true }
example('example') { 1.should == 1 }
end
@@ -72,9 +72,9 @@
after_run.should be_true, "expected after(:each) to be run"
end
- it "should run after(:each) when the example fails" do
+ it "runs after(:each) when the example fails" do
after_run = false
- group = Rspec::Core::ExampleGroup.create do
+ group = Rspec::Core::ExampleGroup.describe do
after(:each) { after_run = true }
example('example') { 1.should == 2 }
end
@@ -82,9 +82,9 @@
after_run.should be_true, "expected after(:each) to be run"
end
- it "should run after(:each) when the example raises an Exception" do
+ it "runs after(:each) when the example raises an Exception" do
after_run = false
- group = Rspec::Core::ExampleGroup.create do
+ group = Rspec::Core::ExampleGroup.describe do
after(:each) { after_run = true }
example('example') { raise "this error" }
end
View
2 spec/rspec/core/mocha_spec.rb
@@ -10,7 +10,7 @@
use_formatter(formatter) do
- group = Rspec::Core::ExampleGroup.create("my favorite pony") do
+ group = Rspec::Core::ExampleGroup.describe("my favorite pony") do
example("showing a double fail") do
foo = "string"
foo.expects(:something)
View
10 spec/rspec/core/pending_example_spec.rb
@@ -13,7 +13,7 @@
describe "an example" do
context "with no block" do
it "is listed as pending with 'Not Yet Implemented'" do
- group = Rspec::Core::ExampleGroup.create('group') do
+ group = Rspec::Core::ExampleGroup.describe('group') do
it "has no block"
end
example = group.examples.first
@@ -24,7 +24,7 @@
context "with no args" do
it "is listed as pending with 'No reason given'" do
- group = Rspec::Core::ExampleGroup.create('group') do
+ group = Rspec::Core::ExampleGroup.describe('group') do
it "does something" do
pending
end
@@ -37,7 +37,7 @@
context "with a message" do
it "is listed as pending with the supplied message" do
- group = Rspec::Core::ExampleGroup.create('group') do
+ group = Rspec::Core::ExampleGroup.describe('group') do
it "does something" do
pending("just because")
end
@@ -51,7 +51,7 @@
context "with a block" do
context "that fails" do
it "is listed as pending with the supplied message" do
- group = Rspec::Core::ExampleGroup.create('group') do
+ group = Rspec::Core::ExampleGroup.describe('group') do
it "does something" do
pending("just because") do
3.should == 4
@@ -66,7 +66,7 @@
context "that passes" do
it "raises a PendingExampleFixedError" do
- group = Rspec::Core::ExampleGroup.create('group') do
+ group = Rspec::Core::ExampleGroup.describe('group') do
it "does something" do
pending("just because") do
3.should == 3
View
163 spec/rspec/core/shared_example_group_spec.rb
@@ -17,7 +17,7 @@ module Rspec::Core
end
it "should raise an ArgumentError when adding a second shared example group with the same name" do
- group = ExampleGroup.create('example group')
+ group = ExampleGroup.describe('example group')
group.share_examples_for('really important business value') { }
lambda do
group.share_examples_for('really important business value') { }
@@ -35,14 +35,8 @@ module Rspec::Core
describe "including shared example_groups using #it_should_behave_like" do
- def cleanup_shared_example_groups
- original_shared_example_groups = Rspec::Core.world.shared_example_groups
- yield if block_given?
- Rspec::Core.world.shared_example_groups.replace(original_shared_example_groups)
- end
-
it "should make any shared example_group available at the correct level", :ruby => 1.8 do
- group = ExampleGroup.create('fake group')
+ group = ExampleGroup.describe('fake group')
block = lambda {
def self.class_helper; end
def extra_helper; end
@@ -54,7 +48,7 @@ def extra_helper; end
end
it "should make any shared example_group available at the correct level", :ruby => 1.9 do
- group = ExampleGroup.create('fake group')
+ group = ExampleGroup.describe('fake group')
block = lambda {
def self.class_helper; end
def extra_helper; end
@@ -66,151 +60,142 @@ def extra_helper; end
end
it "raises when named shared example_group can not be found" do
- cleanup_shared_example_groups do
- group = ExampleGroup.create("example_group")
- lambda do
- group.it_should_behave_like("a group that does not exist")
- end.should raise_error(/Could not find shared example group named/)
- end
+ group = ExampleGroup.describe("example_group")
+ lambda do
+ group.it_should_behave_like("a group that does not exist")
+ end.should raise_error(/Could not find shared example group named/)
end
it "adds examples to current example_group using it_should_behave_like" do
- cleanup_shared_example_groups do
- group = ExampleGroup.create("example_group") do
- it("i was already here") {}
- end
+ group = ExampleGroup.describe("example_group") do
+ it("i was already here") {}
+ end
- group.examples.size.should == 1
+ group.examples.size.should == 1
- group.share_examples_for('shared example_group') do
- it("shared example") {}
- it("shared example 2") {}
- end
+ group.share_examples_for('shared example_group') do
+ it("shared example") {}
+ it("shared example 2") {}
+ end
- group.it_should_behave_like("shared example_group")
+ group.it_should_behave_like("shared example_group")
- group.examples.size.should == 3
- end
+ group.examples.size.should == 3
end
it "adds examples to from two shared groups" do
- cleanup_shared_example_groups do
- group = ExampleGroup.create("example_group") do
- it("i was already here") {}
- end
+ group = ExampleGroup.describe("example_group") do
+ it("i was already here") {}
+ end
- group.examples.size.should == 1
+ group.examples.size.should == 1
- group.share_examples_for('test 2 shared groups') do
- it("shared example") {}
- it("shared example 2") {}
- end
+ group.share_examples_for('test 2 shared groups') do
+ it("shared example") {}
+ it("shared example 2") {}
+ end
- group.share_examples_for('test 2 shared groups 2') do
- it("shared example 3") {}
- end
+ group.share_examples_for('test 2 shared groups 2') do
+ it("shared example 3") {}
+ end
- group.it_should_behave_like("test 2 shared groups")
- group.it_should_behave_like("test 2 shared groups 2")
+ group.it_should_behave_like("test 2 shared groups")
+ group.it_should_behave_like("test 2 shared groups 2")
- group.examples.size.should == 4
- end
+ group.examples.size.should == 4
end
- share_as('Cornucopia') do
- it "is plentiful" do
- 5.should == 4
- end
- end
it "adds examples to current example_group using include", :compat => 'rspec-1.2' do
- group = ExampleGroup.create('group') { include Cornucopia }
+ share_as('Cornucopia') do
+ it "is plentiful" do
+ 5.should == 4
+ end
+ end
+ group = ExampleGroup.describe('group') { include Cornucopia }
group.examples.length.should == 1
group.examples.first.metadata[:description].should == "is plentiful"
end
it "adds examples to current example_group using it_should_behave_like with a module" do
- cleanup_shared_example_groups do
- group = ExampleGroup.create("example_group") {}
+ group = ExampleGroup.describe("example_group") {}
- shared_foo = group.share_as(:FooShared) do
- it("shared example") {}
- end
+ shared_foo = group.share_as(:FooShared) do
+ it("shared example") {}
+ end
- group.it_should_behave_like(::FooShared)
+ group.it_should_behave_like(::FooShared)
- group.examples.size.should == 1
- end
+ group.examples.size.should == 1
end
describe "running shared examples" do
module ::RunningSharedExamplesJustForTesting; end
- share_examples_for("it runs shared examples") do
- include ::RunningSharedExamplesJustForTesting
+ before(:each) do
+ share_examples_for("it runs shared examples") do
+ include ::RunningSharedExamplesJustForTesting
+
+ class << self
+ def magic
+ $magic ||= {}
+ end
+
+ def count(scope)
+ @counters ||= {
+ :before_all => 0,
+ :before_each => 0,
+ :after_each => 0,
+ :after_all => 0
+ }
+ @counters[scope] += 1
+ end
+ end
- class << self
def magic
$magic ||= {}
end
def count(scope)
- @counters ||= {
- :before_all => 0,
- :before_each => 0,
- :after_each => 0,
- :after_all => 0
- }
- @counters[scope] += 1
+ self.class.count(scope)
end
- end
- def magic
- $magic ||= {}
+ before(:all) { magic[:before_all] = "before all #{count(:before_all)}" }
+ before(:each) { magic[:before_each] = "before each #{count(:before_each)}" }
+ after(:each) { magic[:after_each] = "after each #{count(:after_each)}" }
+ after(:all) { magic[:after_all] = "after all #{count(:after_all)}" }
end
-
- def count(scope)
- self.class.count(scope)
- end
-
- before(:all) { magic[:before_all] = "before all #{count(:before_all)}" }
- before(:each) { magic[:before_each] = "before each #{count(:before_each)}" }
- after(:each) { magic[:after_each] = "after each #{count(:after_each)}" }
- after(:all) { magic[:after_all] = "after all #{count(:after_all)}" }
end
let(:group) do
- group = ExampleGroup.create("example group") do
+ group = ExampleGroup.describe("example group") do
it_should_behave_like "it runs shared examples"
it "has one example" do; end
it "has another example" do; end
+ it "includes modules, included into shared example_group, into current example_group", :compat => 'rspec-1.2' do
+ raise "FAIL" unless running_example.example_group.included_modules.include?(RunningSharedExamplesJustForTesting)
+ end
end
end
- before { group.run(stub('reporter').as_null_object) }
+ before { group.run_all }
it "runs before(:all) only once from shared example_group", :compat => 'rspec-1.2' do
group.magic[:before_all].should eq("before all 1")
end
it "runs before(:each) from shared example_group", :compat => 'rspec-1.2' do
- group.magic[:before_each].should eq("before each 2")
+ group.magic[:before_each].should eq("before each 3")
end
it "runs after(:each) from shared example_group", :compat => 'rspec-1.2' do
- group.magic[:after_each].should eq("after each 2")
+ group.magic[:after_each].should eq("after each 3")
end
it "runs after(:all) only once from shared example_group", :pending => true, :compat => 'rspec-1.2' do
group.magic[:after_all].should eq("after all 1")
end
- it_should_behave_like "it runs shared examples"
-
- it "includes modules, included into shared example_group, into current example_group", :compat => 'rspec-1.2' do
- running_example.example_group.included_modules.should include(RunningSharedExamplesJustForTesting)
- end
-
it "makes methods defined in the shared example_group available in consuming example_group", :compat => 'rspec-1.2' do
group.magic.should be_a(Hash)
end
View
12 spec/rspec/core/subject_spec.rb
@@ -7,25 +7,25 @@ module Rspec::Core
describe "implicit subject" do
describe "with a class" do
it "returns an instance of the class" do
- ExampleGroup.create(Array).subject.call.should == []
+ ExampleGroup.describe(Array).subject.call.should == []
end
end
describe "with a Module" do
it "returns the Module" do
- ExampleGroup.create(Enumerable).subject.call.should == Enumerable
+ ExampleGroup.describe(Enumerable).subject.call.should == Enumerable
end
end
describe "with a string" do
it "return the string" do
- ExampleGroup.create("Foo").subject.call.should == 'Foo'
+ ExampleGroup.describe("Foo").subject.call.should == 'Foo'
end
end
describe "with a number" do
it "returns the number" do
- ExampleGroup.create(15).subject.call.should == 15
+ ExampleGroup.describe(15).subject.call.should == 15
end
end
@@ -34,7 +34,7 @@ module Rspec::Core
describe "explicit subject" do
describe "defined in a top level group" do
it "replaces the implicit subject in that group" do
- group = ExampleGroup.create(Array)
+ group = ExampleGroup.describe(Array)
group.subject { [1,2,3] }
group.subject.call.should == [1,2,3]
end
@@ -42,7 +42,7 @@ module Rspec::Core
describe "defined in a top level group" do
let(:group) do
- ExampleGroup.create do
+ ExampleGroup.describe do
subject{ [4,5,6] }
end
end
View
8 spec/rspec/core/world_spec.rb
@@ -87,14 +87,14 @@ module Rspec::Core
it "should find nothing if all describes match the exclusion filter" do
options = { :network_access => true }
- group1 = ExampleGroup.create(Bar, "find group-1", options) do
+ group1 = ExampleGroup.describe(Bar, "find group-1", options) do
it("foo") {}
it("bar") {}
end
@world.apply_exclusion_filters(group1.examples, :network_access => true).should == []
- group2 = ExampleGroup.create(Bar, "find group-1") do
+ group2 = ExampleGroup.describe(Bar, "find group-1") do
it("foo", :network_access => true) {}
it("bar") {}
end
@@ -104,7 +104,7 @@ module Rspec::Core
end
it "should find nothing if a regexp matches the exclusion filter" do
- group = ExampleGroup.create(Bar, "find group-1", :name => "exclude me with a regex", :another => "foo") do
+ group = ExampleGroup.describe(Bar, "find group-1", :name => "exclude me with a regex", :another => "foo") do
it("foo") {}
it("bar") {}
end
@@ -123,7 +123,7 @@ module Rspec::Core
describe "filtering example groups" do
it "should run matches" do
- @group1 = ExampleGroup.create(Bar, "find these examples") do
+ @group1 = ExampleGroup.describe(Bar, "find these examples") do
it('I have no options', :color => :red, :awesome => true) {}
it("I also have no options", :color => :red, :awesome => true) {}
it("not so awesome", :color => :red, :awesome => false) {}
View
15 spec/spec_helper.rb
@@ -9,7 +9,7 @@
begin
require 'autotest'
rescue LoadError
- raise "You must install autotest to use it"
+ raise "Could not load autotest."
end
require 'autotest/rspec2'
@@ -31,12 +31,6 @@ def fail_with(message)
end
class Rspec::Core::ExampleGroup
- def self.create(*args, &example_group_block)
- args.unshift('example group') if args.empty?
- describe(*args, &example_group_block || lambda {})
- Rspec::Core.world.example_groups.pop
- end
-
def self.run_all(reporter=nil)
reporter ||= Rspec::Mocks::Mock.new('reporter').as_null_object
examples_to_run.replace(examples)
@@ -61,4 +55,11 @@ def in_editor?
c.exclusion_filter = { :ruby => lambda {|version|
!(RUBY_VERSION.to_s =~ /^#{version.to_s}/)
}}
+ c.before do
+ @real_world = Rspec::Core.world
+ Rspec::Core.instance_variable_set(:@world, Rspec::Core::World.new)
+ end
+ c.after do
+ Rspec::Core.instance_variable_set(:@world, @real_world)
+ end
end

0 comments on commit 35e4a6b

Please sign in to comment.
Something went wrong with that request. Please try again.