Skip to content

Commit

Permalink
Reduce redundancy in example group isolation techniques
Browse files Browse the repository at this point in the history
- for rspec's own specs
  • Loading branch information
dchelimsky committed Feb 2, 2010
1 parent 9a8854f commit e5d88ee
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 120 deletions.
12 changes: 5 additions & 7 deletions spec/rspec/core/configuration_spec.rb
Expand Up @@ -130,14 +130,12 @@ def you_call_this_a_blt?
end
end

it "should include the given module into each matching behaviour" do
it "should include the given module into each matching example group" do
Rspec::Core.configuration.include(InstanceLevelMethods, :magic_key => :include)

isolate_example_group do
group = Rspec::Core::ExampleGroup.describe(Object, '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
group = isolated_example_group('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

end
Expand All @@ -156,7 +154,7 @@ def that_thing
group = Rspec::Core::ExampleGroup.describe(ThatThingISentYou, :magic_key => :extend) { }

group.should respond_to(:that_thing)
remove_last_describe_from_world
remove_last_example_group_from_world
end

end
Expand Down
72 changes: 31 additions & 41 deletions spec/rspec/core/example_group_spec.rb
@@ -1,12 +1,5 @@
require 'spec_helper'

def empty_example_group(name='Empty ExampleGroup Group')
group = Rspec::Core::ExampleGroup.describe(Object, name) {}
remove_last_describe_from_world
yield group if block_given?
group
end

describe Rspec::Core::ExampleGroup do

describe "#describe" do
Expand All @@ -24,29 +17,26 @@ def empty_example_group(name='Empty ExampleGroup Group')
describe '#name' do

it "uses the first parameter as name" do
isolate_example_group do
disconnect_from_world do
Rspec::Core::ExampleGroup.describe("my favorite pony") { }.name.should == 'my favorite pony'
end
end

it "accepts a constant as the first parameter" do
isolate_example_group do
disconnect_from_world do
Rspec::Core::ExampleGroup.describe(Object) { }.name.should == 'Object'
end
end

it "concats nested names" do
isolate_example_group do
behaviour_to_test = nil
group = empty_example_group('test')
group.name.should == 'Object test'
group = isolated_example_group(Object, 'test')
group.name.should == 'Object test'

nested_group_one = group.describe('nested one') { }
nested_group_one.name.should == 'Object test nested one'
nested_group_one = group.describe('nested one') { }
nested_group_one.name.should == 'Object test nested one'

nested_group_two = nested_group_one.describe('nested two') { }
nested_group_two.name.should == 'Object test nested one nested two'
end
nested_group_two = nested_group_one.describe('nested two') { }
nested_group_two.name.should == 'Object test nested one nested two'
end

end
Expand All @@ -56,7 +46,7 @@ def empty_example_group(name='Empty ExampleGroup Group')
context "with a constant as the first parameter" do

it "is that constant" do
isolate_example_group do
disconnect_from_world do
Rspec::Core::ExampleGroup.describe(Object) { }.describes.should == Object
end
end
Expand All @@ -66,7 +56,7 @@ def empty_example_group(name='Empty ExampleGroup Group')
context "with a string as the first parameter" do

it "is nil" do
isolate_example_group do
disconnect_from_world do
Rspec::Core::ExampleGroup.describe("i'm a computer") { }.describes.should be_nil
end
end
Expand All @@ -78,14 +68,14 @@ def empty_example_group(name='Empty ExampleGroup Group')
describe '#description' do

it "exposes the second parameter as description" do
isolate_example_group do
disconnect_from_world do
Rspec::Core::ExampleGroup.describe(Object, "my desc") { }.description.should == 'my desc'
end
end

it "allows the second parameter to be nil" do
isolate_example_group do
Rspec::Core::ExampleGroup.describe(Object, nil) { }.description.size.should == 0
disconnect_from_world do
Rspec::Core::ExampleGroup.describe(Object, nil) { }.description.should == ""
end
end

Expand All @@ -94,33 +84,33 @@ def empty_example_group(name='Empty ExampleGroup Group')
describe '#metadata' do

it "adds the third parameter to the metadata" do
isolate_example_group do
disconnect_from_world do
Rspec::Core::ExampleGroup.describe(Object, nil, 'foo' => 'bar') { }.metadata.should include({ "foo" => 'bar' })
end
end

it "adds the caller to metadata" do
isolate_example_group do
disconnect_from_world do
Rspec::Core::ExampleGroup.describe(Object) { }.metadata[:behaviour][:caller].any? {|f|
f =~ /#{__FILE__}/
}.should be_true
end
end

it "adds the the file_path to metadata" do
isolate_example_group do
disconnect_from_world do
Rspec::Core::ExampleGroup.describe(Object) { }.metadata[:behaviour][:file_path].should == __FILE__
end
end

it "has a reader for file_path" do
isolate_example_group do
disconnect_from_world do
Rspec::Core::ExampleGroup.describe(Object) { }.file_path.should == __FILE__
end
end

it "adds the line_number to metadata" do
isolate_example_group do
disconnect_from_world do
Rspec::Core::ExampleGroup.describe(Object) { }.metadata[:behaviour][:line_number].should == __LINE__
end
end
Expand All @@ -133,21 +123,21 @@ def empty_example_group(name='Empty ExampleGroup Group')
end
end

4.times { remove_last_describe_from_world }
4.times { remove_last_example_group_from_world }
end

end

describe "adding before, after, and around hooks" do

it "should expose the before each blocks at before_eachs" do
group = empty_example_group
group = isolated_example_group
group.before(:each) { 'foo' }
group.should have(1).before_eachs
end

it "should maintain the before each block order" do
group = empty_example_group
group = isolated_example_group
group.before(:each) { 15 }
group.before(:each) { 'A' }
group.before(:each) { 33.5 }
Expand All @@ -158,13 +148,13 @@ def empty_example_group(name='Empty ExampleGroup Group')
end

it "should expose the before all blocks at before_alls" do
group = empty_example_group
group = isolated_example_group
group.before(:all) { 'foo' }
group.should have(1).before_alls
end

it "should maintain the before all block order" do
group = empty_example_group
group = isolated_example_group
group.before(:all) { 15 }
group.before(:all) { 'A' }
group.before(:all) { 33.5 }
Expand All @@ -175,13 +165,13 @@ def empty_example_group(name='Empty ExampleGroup Group')
end

it "should expose the after each blocks at after_eachs" do
group = empty_example_group
group = isolated_example_group
group.after(:each) { 'foo' }
group.should have(1).after_eachs
end

it "should maintain the after each block order" do
group = empty_example_group
group = isolated_example_group
group.after(:each) { 15 }
group.after(:each) { 'A' }
group.after(:each) { 33.5 }
Expand All @@ -192,13 +182,13 @@ def empty_example_group(name='Empty ExampleGroup Group')
end

it "should expose the after all blocks at after_alls" do
group = empty_example_group
group = isolated_example_group
group.after(:all) { 'foo' }
group.should have(1).after_alls
end

it "should maintain the after each block order" do
group = empty_example_group
group = isolated_example_group
group.after(:all) { 15 }
group.after(:all) { 'A' }
group.after(:all) { 33.5 }
Expand All @@ -209,7 +199,7 @@ def empty_example_group(name='Empty ExampleGroup Group')
end

it "should expose the around each blocks at after_alls" do
group = empty_example_group
group = isolated_example_group
group.around(:each) { 'foo' }
group.should have(1).around_eachs
end
Expand All @@ -219,21 +209,21 @@ def empty_example_group(name='Empty ExampleGroup Group')
describe "adding examples" do

it "should allow adding an example using 'it'" do
group = empty_example_group
group = isolated_example_group
group.it("should do something") { }
group.examples.size.should == 1
end

it "should expose all examples at examples" do
group = empty_example_group
group = isolated_example_group
group.it("should do something 1") { }
group.it("should do something 2") { }
group.it("should do something 3") { }
group.examples.size.should == 3
end

it "should maintain the example order" do
group = empty_example_group
group = isolated_example_group
group.it("should 1") { }
group.it("should 2") { }
group.it("should 3") { }
Expand Down
2 changes: 1 addition & 1 deletion spec/rspec/core/example_group_subject_spec.rb
Expand Up @@ -2,7 +2,7 @@

def describe_double(describes=Object)
group = Rspec::Core::ExampleGroup.describe(describes) {}
remove_last_describe_from_world
remove_last_example_group_from_world
yield group if block_given?
group
end
Expand Down
8 changes: 4 additions & 4 deletions spec/rspec/core/mocha_spec.rb
Expand Up @@ -10,15 +10,15 @@

use_formatter(formatter) do

isolate_example_group do
desc = Rspec::Core::ExampleGroup.describe("my favorite pony") do
disconnect_from_world do
group = Rspec::Core::ExampleGroup.describe("my favorite pony") do
example("showing a double fail") do
foo = "string"
foo.expects(:something)
end
end
desc.examples_to_run.replace(desc.examples)
desc.run(formatter)
group.examples_to_run.replace(group.examples)
group.run(formatter)
end

end
Expand Down
22 changes: 11 additions & 11 deletions spec/rspec/core/shared_behaviour_spec.rb
Expand Up @@ -15,7 +15,7 @@
end

it "should raise an ArgumentError when adding a second shared behaviour with the same name" do
group = double_describe('example group')
group = isolated_example_group('example group')
group.share_examples_for('really important business value') { }
lambda do
group.share_examples_for('really important business value') { }
Expand All @@ -40,7 +40,7 @@ def cleanup_shared_behaviours
end

it "should module_eval any found shared behaviours" do
group = double_describe('fake group')
group = isolated_example_group('fake group')
block1 = lambda {}
block2 = lambda {
def extra_helper
Expand All @@ -53,7 +53,7 @@ def extra_helper
end

it "should make any shared behaviour available at the correct level" do
group = double_describe('fake group')
group = isolated_example_group('fake group')
block = lambda {
def self.class_helper; end
def extra_helper; end
Expand All @@ -74,8 +74,8 @@ def extra_helper; end

it "adds examples to current example_group using it_should_behave_like" do
cleanup_shared_behaviours do
group = double_describe("example_group") do |g|
g.it("i was already here") {}
group = isolated_example_group("example_group") do
it("i was already here") {}
end

group.examples.size.should == 1
Expand All @@ -93,7 +93,7 @@ def extra_helper; end

it "adds examples to from two shared groups" do
cleanup_shared_behaviours do
group = double_describe("example_group") do |g|
group = isolated_example_group("example_group") do |g|
g.it("i was already here") {}
end

Expand All @@ -116,17 +116,17 @@ def extra_helper; end
end

share_as('Cornucopia') do
it "should do foo"
it "should do foo" do; end
end

it "adds examples to current example_group using include", :compat => 'rspec-1.2' do
group = double_describe('group') { include Cornucopia }
group.should have(1).example
pending "adds examples to current example_group using include", :compat => 'rspec-1.2' do
group = isolated_example_group('group') { include Cornucopia }
group.examples.length.should == 1
end

it "adds examples to current example_group using it_should_behave_like with a module" do
cleanup_shared_behaviours do
group = double_describe("example_group") {}
group = isolated_example_group("example_group") {}

shared_foo = group.share_as(:FooShared) do
it("shared example") {}
Expand Down

0 comments on commit e5d88ee

Please sign in to comment.