Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

OperatorMatcher considers ancestor chain #192

Merged
merged 1 commit into from

3 participants

@alindeman
Collaborator
@phiggins phiggins commented on the diff
lib/rspec/matchers/operator_matcher.rb
((8 lines not shown))
def get(klass, operator)
- registry[klass] && registry[klass][operator]
+ klass.ancestors.each { |ancestor|
+ matcher = registry[ancestor] && registry[ancestor][operator]
+ return matcher if matcher
+ }
@phiggins
phiggins added a note

Would klass.ancestors.detect {|ancestor| registry[ancestor] && registry[ancestor][operator] } work?

@alindeman Collaborator

I wish! Unfortunately detect returns the element that matched, and not the result of the block.

@phiggins
phiggins added a note

Ahh, good point. I read too quickly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@myronmarston myronmarston merged commit abefb76 into rspec:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 4, 2012
  1. @alindeman
This page is out of date. Refresh to see the latest.
View
11 lib/rspec/matchers/operator_matcher.rb
@@ -11,8 +11,17 @@ def register(klass, operator, matcher)
registry[klass][operator] = matcher
end
+ def unregister(klass, operator)
+ registry[klass] && registry[klass].delete(operator)
+ end
+
def get(klass, operator)
- registry[klass] && registry[klass][operator]
+ klass.ancestors.each { |ancestor|
+ matcher = registry[ancestor] && registry[ancestor][operator]
+ return matcher if matcher
+ }
@phiggins
phiggins added a note

Would klass.ancestors.detect {|ancestor| registry[ancestor] && registry[ancestor][operator] } work?

@alindeman Collaborator

I wish! Unfortunately detect returns the element that matched, and not the result of the block.

@phiggins
phiggins added a note

Ahh, good point. I read too quickly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+
+ nil
end
end
View
23 spec/rspec/matchers/operator_matcher_spec.rb
@@ -196,6 +196,29 @@
end
+describe "OperatorMatcher registry" do
+ let(:custom_klass) { Class.new }
+ let(:custom_subklass) { Class.new(custom_klass) }
+
+ after {
+ RSpec::Matchers::OperatorMatcher.unregister(custom_klass, "=~")
+ }
+
+ it "allows operator matchers to be registered for types" do
+ RSpec::Matchers::OperatorMatcher.register(custom_klass, "=~", RSpec::Matchers::BuiltIn::Match)
+ expect(RSpec::Matchers::OperatorMatcher.get(custom_klass, "=~")).to eq(RSpec::Matchers::BuiltIn::Match)
+ end
+
+ it "considers ancestors when finding an operator matcher" do
+ RSpec::Matchers::OperatorMatcher.register(custom_klass, "=~", RSpec::Matchers::BuiltIn::Match)
+ expect(RSpec::Matchers::OperatorMatcher.get(custom_subklass, "=~")).to eq(RSpec::Matchers::BuiltIn::Match)
+ end
+
+ it "returns nil if there is no matcher registered for a type" do
+ expect(RSpec::Matchers::OperatorMatcher.get(custom_klass, "=~")).to be_nil
+ end
+end
+
describe RSpec::Matchers::BuiltIn::PositiveOperatorMatcher do
it "works when the target has implemented #send" do
Something went wrong with that request. Please try again.