Permalink
Browse files

Merge pull request #128 from mirasrael/master

Fix using execution context methods in nested DSL matchers
  • Loading branch information...
justinko committed Apr 1, 2012
2 parents e5cd722 + 70dd175 commit 5ffc547f683bacf0d0e668f08a20d7982157717c
Showing with 16 additions and 10 deletions.
  1. +4 −3 lib/rspec/matchers/dsl.rb
  2. +6 −5 lib/rspec/matchers/matcher.rb
  3. +6 −2 spec/rspec/matchers/dsl_spec.rb
@@ -4,10 +4,11 @@ module DSL
# Defines a custom matcher.
# @see RSpec::Matchers
def define(name, &declarations)
- matcher = RSpec::Matchers::DSL::Matcher.new(name, &declarations)
+ matcher_template = RSpec::Matchers::DSL::Matcher.new(name, &declarations)
define_method name do |*expected|
- $matcher_execution_context = self
- matcher.for_expected(*expected)
+ matcher = matcher_template.for_expected(*expected)
+ matcher.matcher_execution_context = @matcher_execution_context || self
+ matcher
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 5ffc547

Please sign in to comment.