Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 84 lines (71 sloc) 2.333 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
require "rubygems"
require "expand_path"
$:.unshift __FILE__.expand_path
require "spec_helper"
require "context"

describe "An object with a method declared in a context" do
  include InContext::WithContext

  before(:each) do
    @klass = Class.new do
      include InContext
      in_context :callable do
        def call
          @called = true
        end
      end
      def called?; @called; end
    end
    @object = @klass.new
    @object.should_not be_called
  end
  
  it "should not respond to the method" do
    lambda { @object.call }.should raise_error(NoMethodError)
  end

  it "should respond to the method in a context" do
    with_context(:callable) { @object.call }
    @object.should be_called
  end

  it "should not respond to the method once the context is complete" do
    with_context(:callable) { @object.call }
    lambda { @object.call }.should raise_error(NoMethodError)
  end

  it "should call the method in context after the instance method is redefined" do
    @klass.send(:define_method, :call) { :noop }
    with_context(:callable) { @object.call }
    @object.should be_called
    lambda { @object.call.should == :noop }
  end

  describe "when the context is opened again" do
    it "should redefine the method" do
      @klass.in_context(:callable) { def call; :redefined; end }
      with_context(:callable) { @object.call.should == :redefined }
    end
    
    it "should define other methods" do
      @klass.in_context(:callable) { def other; :other; end }
      with_context(:callable) {
        @object.call
        @object.other.should == :other
      }
      @object.should be_called
    end
  end
end

describe "An object with an instance method, and the same method declared in a context" do
  include InContext::WithContext
  
  before(:each) do
    @klass = Class.new do
      include InContext
      def the_context(collector); collector << :instance; end
      in_context(:override) do
        def the_context(collector); collector << :singleton; end
      end
    end

    @object = @klass.new
    @collector = []
  end

  it "should call the overridden method in context, and the original method in default context" do
    with_context(:override) { @object.the_context @collector }
    @object.the_context @collector
    @collector.should == [:singleton, :instance]
  end
end
Something went wrong with that request. Please try again.