summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--lib/bacon.rb17
-rw-r--r--test/spec_bacon.rb30
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