Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

OperatorMatcher considers ancestor chain #192

Merged
merged 1 commit into from Dec 5, 2012
Jump to file or symbol
Failed to load files and symbols.
+33 −1
Split
@@ -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 Dec 5, 2012

Contributor

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

@alindeman

alindeman Dec 5, 2012

Contributor

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

@phiggins

phiggins Dec 5, 2012

Contributor

Ahh, good point. I read too quickly.

+
+ nil
end
end
@@ -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