Permalink
Browse files

Fix using execution context methods in nested DSL matchers

- Fixes #127.
  • Loading branch information...
1 parent e5cd722 commit abba43932a821b3c7f60edfe247ee1917dbf1dac mirasrael committed Mar 30, 2012
Showing with 13 additions and 9 deletions.
  1. +1 −2 lib/rspec/matchers/dsl.rb
  2. +6 −5 lib/rspec/matchers/matcher.rb
  3. +6 −2 spec/rspec/matchers/dsl_spec.rb
@@ -6,8 +6,7 @@ module DSL
def define(name, &declarations)
matcher = RSpec::Matchers::DSL::Matcher.new(name, &declarations)
define_method name do |*expected|
- $matcher_execution_context = self
- matcher.for_expected(*expected)
+ matcher.for_expected(*expected).tap { |m| m.matcher_execution_context = @matcher_execution_context || self }
end
end
@@ -11,6 +11,7 @@ class Matcher
include RSpec::Matchers
attr_reader :expected, :actual, :rescued_exception
+ attr_accessor :matcher_execution_context
# @api private
def initialize(name, &declarations)
@@ -23,7 +24,7 @@ def initialize(name, &declarations)
@messages = {}
end
- PERSISENT_INSTANCE_VARIABLES = [
+ PERSISTENT_INSTANCE_VARIABLES = [
:@name, :@declarations, :@diffable, :@messages,
:@match_block, :@match_for_should_not_block,
:@expected_exception
@@ -34,7 +35,7 @@ def for_expected(*expected)
@expected = expected
dup.instance_eval do
instance_variables.map {|ivar| ivar.intern}.each do |ivar|
- instance_variable_set(ivar, nil) unless (PERSISENT_INSTANCE_VARIABLES + [:@expected]).include?(ivar)
+ instance_variable_set(ivar, nil) unless (PERSISTENT_INSTANCE_VARIABLES + [:@expected]).include?(ivar)
end
making_declared_methods_public do
instance_eval_with_args(*@expected, &@declarations)
@@ -222,14 +223,14 @@ def does_not_match?(actual)
end
def respond_to?(method, include_private=false)
- $matcher_execution_context != self && $matcher_execution_context.respond_to?(method, include_private) || super
+ super || @matcher_execution_context.respond_to?(method, include_private)
end
private
def method_missing(method, *args, &block)
- if $matcher_execution_context.respond_to?(method)
- $matcher_execution_context.send method, *args, &block
+ if @matcher_execution_context.respond_to?(method)
+ @matcher_execution_context.send method, *args, &block
else
super(method, *args, &block)
end
@@ -5,16 +5,20 @@ def question?
:answer
end
+ def ok
+ "ok"
+ end
+
it "supports calling custom matchers from within other custom matchers" do
RSpec::Matchers.define :be_ok do
- match { |actual| actual.should == "ok" }
+ match { |actual| actual == ok }
end
RSpec::Matchers.define :be_well do
match { |actual| actual.should be_ok }
end
- "ok".should be_well
+ ok.should be_well
end
it "has access to methods available in the scope of the example" do

0 comments on commit abba439

Please sign in to comment.