Skip to content

Commit

Permalink
Allow any object in the shared spec description.
Browse files Browse the repository at this point in the history
  • Loading branch information
Manfred committed Jan 16, 2012
1 parent d848efe commit 2bae7a4
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 8 deletions.
32 changes: 24 additions & 8 deletions lib/test/spec/share.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
$shared_specs = {}

module Kernel
# Stores the passed in block for inclusion in test cases.
module SharedSpecsInclusionHelper
def self.description(*parts)
parts.map(&:to_s).join(' ')
end

# Include the specified shared specs in this test case.
#
# share "User" do
# it "should authenticate" do
Expand All @@ -18,13 +22,13 @@ module Kernel
# end
#
# 2 tests, 2 assertions, 0 failures, 0 errors
def share(name, &specs_block)
$shared_specs[name] = specs_block
def shared_specs_for(*description)
self.class_eval &$shared_specs[SharedSpecsInclusionHelper.description(*description)]
end
end

module SharedSpecsInclusionHelper
# Include the specified shared specs in this test case.
module Kernel
# Stores the passed in block for inclusion in test cases.
#
# share "User" do
# it "should authenticate" do
Expand All @@ -41,8 +45,20 @@ module SharedSpecsInclusionHelper
# end
#
# 2 tests, 2 assertions, 0 failures, 0 errors
def shared_specs_for(name)
self.class_eval &$shared_specs[name]
#
# You can use anything as a description for the shared specs.
#
# share User, "concerning the use of", 1, :email, "symbol" do
# it "should process" do
# end
# end
#
# describe User do
# shared_specs_for User, "concerning the use of", 1, :email, "symbol"
# end
#
def share(*description, &specs_block)
$shared_specs[SharedSpecsInclusionHelper.description(*description)] = specs_block
end
end

Expand Down
24 changes: 24 additions & 0 deletions test/share_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ def it(name, &block)
@times_called ||= 0
@times_called += 1
end

def reset!
@times_called = nil
end
end
end

Expand All @@ -25,6 +29,10 @@ def it(name, &block)
end

describe "Kernel#share" do
before do
DummyMock.reset!
end

it "should add the shared specs to the global shared modules variable" do
before = $shared_specs.length
share("Bar") {}
Expand All @@ -34,9 +42,25 @@ def it(name, &block)
it "should have stored the proc that holds the specs" do
$shared_specs['Dummy'].should.be.instance_of Proc
end

it "accepts mixed objects as a description" do
before = $shared_specs.length
share(Kernel, "concerning modules", 1) {
it("works") {}
}
$shared_specs.length.should == before + 1
DummyMock.class_eval do
shared_specs_for Kernel, "concerning modules", 1
end
DummyMock.times_called.should.be 1
end
end

describe "SharedSpecsInclusionHelper::shared_specs_for" do
before do
DummyMock.reset!
end

it "should have extended Test::Unit::TestCase" do
Test::Unit::TestCase.should.respond_to :shared_specs_for
end
Expand Down

0 comments on commit 2bae7a4

Please sign in to comment.