Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Provide access to running_example in Matcher. Closes #2.

  • Loading branch information...
commit ea852a521dbc53f72865a8029c47370648c76899 1 parent 96ff951
@RipTheJacker RipTheJacker authored dchelimsky committed
View
19 lib/rspec/matchers/matcher.rb
@@ -6,7 +6,6 @@ class Matcher
include Rspec::Matchers
attr_reader :expected, :actual
-
def initialize(name, *expected, &declarations)
@name = name
@expected = expected
@@ -22,7 +21,16 @@ def initialize(name, *expected, &declarations)
instance_exec(*@expected, &declarations)
end
end
-
+
+ # This allows access to running_example
+ alias_method :old_instance_exec, :instance_exec
+ def instance_exec(*args, &block)
+ instance_eval {
+ self.running_example ||= eval("running_example", block.binding) rescue nil
+ old_instance_exec(*args, &block)
+ }
+ end
+
#Used internally by objects returns by +should+ and +should_not+.
def matches?(actual)
@actual = actual
@@ -87,9 +95,12 @@ def chain(method, &block)
end
end
end
-
+
+ protected
+ attr_accessor :running_example
+
private
-
+
def making_declared_methods_public # :nodoc:
# Our home-grown instance_exec in ruby 1.8.6 results in any methods
# declared in the block eval'd by instance_exec in the block to which we
View
4 spec/rspec/matchers/dsl_spec.rb
@@ -15,8 +15,8 @@ module DSL
obj = Object.new
obj.extend mod
- Rspec::Matchers::Matcher.should_receive(:new).with(:foo, 3)
-
+ Rspec::Matchers::Matcher.should_receive(:new).with(:foo, 3) { stub('matcher').as_null_object }
+
obj.foo(3)
end
end
View
34 spec/rspec/matchers/matcher_spec.rb
@@ -284,6 +284,40 @@ def assert_equal(a,b)
matcher.expecting('value').matches?('value').should be_true
matcher.expecting('value').matches?('other value').should be_false
end
+
+ context "can access running_example" do
+ it "works with matcher using dsl" do
+ Rspec::Matchers.define(:something) do
+ match do |actual|
+ actual == running_example
+ end
+ end
+ running_example.should something
+ end
+
+ it "works within matcher using #new" do
+ @matcher = Rspec::Matchers::Matcher.new(:something) {}
+ @matcher.send(:running_example).should == running_example
+ end
+
+ module ::FakeMatcher
+ include Rspec::Matchers
+
+ def do_something
+ Matcher.new(:do_something) do
+ match do |actual|
+ !running_example.nil?
+ end
+ end
+ end
+ end
+
+ include FakeMatcher
+
+ it "works when defined with api" do
+ "this string".should do_something
+ end
+ end
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.