From 4bd8f7f165ba8113870a6be9ee4f11474aea892d Mon Sep 17 00:00:00 2001 From: Christian Neukirchen Date: Wed, 5 Dec 2007 01:41:56 +0100 Subject: Add shared contexts darcs-hash:20071205004156-4fc50-50fb9e4bd448a79706dff072ecf7efe06df41d3e.gz --- lib/bacon.rb | 17 ++++++++++++++--- test/spec_bacon.rb | 30 ++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/lib/bacon.rb b/lib/bacon.rb index 0288987..627f79d 100644 --- a/lib/bacon.rb +++ b/lib/bacon.rb @@ -6,6 +6,9 @@ module Bacon Counter = Hash.new(0) ErrorLog = "" + Shared = Hash.new { |_, name| + raise NameError, "no such context: #{name.inspect}" + } module SpecDoxOutput def handle_specification(name) @@ -104,15 +107,19 @@ module Bacon instance_eval(&block) end end - + def before(&block); @before << block; end def after(&block); @after << block; end - + + def behaves_like(name) + instance_eval &Bacon::Shared[name] + end + def it(description, &block) Bacon::Counter[:specifications] += 1 run_requirement description, block end - + def run_requirement(description, spec) Bacon.handle_requirement description do begin @@ -191,6 +198,10 @@ module Kernel def describe(name, &block) Bacon::Context.new(name, &block) end + + def shared(name, &block) + Bacon::Shared[name] = block + end end diff --git a/test/spec_bacon.rb b/test/spec_bacon.rb index f599a7b..cfbb7bb 100644 --- a/test/spec_bacon.rb +++ b/test/spec_bacon.rb @@ -258,3 +258,33 @@ describe "before/after" do @b.should.equal 2 end end + +shared "a shared context" do + it "gets called where it is included" do + true.should.be.true + end +end + +shared "another shared context" do + it "can access data" do + @magic.should.be.equal 42 + end +end + +describe "shared/behaves_like" do + behaves_like "a shared context" + + ctx = self + it "raises NameError when the context is not found" do + lambda { + ctx.behaves_like "whoops" + }.should.raise NameError + end + + behaves_like "a shared context" + + before { + @magic = 42 + } + behaves_like "another shared context" +end -- cgit 1.4.1