Skip to content
New issue

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

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OperatorMatcher considers ancestor chain #192

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion lib/rspec/matchers/operator_matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh, good point. I read too quickly.


nil
end
end

Expand Down
23 changes: 23 additions & 0 deletions spec/rspec/matchers/operator_matcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down