-
-
Notifications
You must be signed in to change notification settings - Fork 397
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
Matcher DSL deprecations for 2.99 #337
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
module RSpec | ||
module Matchers | ||
# Evaluates a block in order to determine what methods, if any, | ||
# it defines as instance methods (using `def foo`) vs singleton | ||
# methods (using `def self.foo`). | ||
# | ||
# @api private | ||
class DifferentiateBlockMethodTypes | ||
def initialize(*block_args, &block) | ||
@block_args = block_args | ||
@block = block | ||
|
||
ignore_macro_methods | ||
|
||
capture_added_methods(singletons_singleton_class, singleton_methods) | ||
capture_added_methods(singleton_class, instance_methods) | ||
|
||
singleton_class.class_exec(*block_args, &block) | ||
end | ||
|
||
def singleton_methods | ||
@singleton_methods ||= [] | ||
end | ||
|
||
def instance_methods | ||
@instance_methods ||= [] | ||
end | ||
|
||
private | ||
|
||
def capture_added_methods(object, method_list) | ||
object.__send__(:define_method, :singleton_method_added) do |method_name| | ||
method_list << method_name | ||
end | ||
|
||
method_list.delete(:singleton_method_added) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Assuming this is to remove the method being captured in the second pass? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, it's there because defining
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TIL |
||
end | ||
|
||
unless method_defined?(:singleton_class) | ||
def singleton_class | ||
class << self; self; end | ||
end | ||
end | ||
|
||
def singletons_singleton_class | ||
class << singleton_class; self; end | ||
end | ||
|
||
def ignore_macro_methods | ||
def singleton_class.method_missing(*); self; end | ||
end | ||
end | ||
end | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
require 'spec_helper' | ||
|
||
module RSpec | ||
module Matchers | ||
describe DifferentiateBlockMethodTypes do | ||
let(:differentiator) do | ||
DifferentiateBlockMethodTypes.new do | ||
def some_instance_method_1; end | ||
def self.some_singleton_method_1; end | ||
define_method(:some_instance_method_2) { } | ||
|
||
if RUBY_VERSION.to_f > 1.8 | ||
define_singleton_method(:some_singleton_method_2) { } | ||
else | ||
def self.some_singleton_method_2; end | ||
end | ||
end | ||
end | ||
|
||
it 'differentiates singleton method defs from instance method defs' do | ||
expect(differentiator.instance_methods).to eq([:some_instance_method_1, :some_instance_method_2]) | ||
expect(differentiator.singleton_methods).to eq([:some_singleton_method_1, :some_singleton_method_2]) | ||
end | ||
|
||
it 'passes the given args through to the block' do | ||
expect { |b| | ||
DifferentiateBlockMethodTypes.new(1, 2, &b) | ||
}.to yield_with_args(1, 2) | ||
end | ||
|
||
it 'ignores unrecognized DSL methods called in the block' do | ||
expect { | ||
DifferentiateBlockMethodTypes.new { foo.bar; some_dsl { nested } } | ||
}.not_to raise_error | ||
end | ||
end | ||
end | ||
end | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It could just be me, but
singletons_singleton_class
reads funny... not sure of a better name though.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, it's the singleton class of the singleton class...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe
singleton_class_of_singleton
would have read better...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really don't care either way. Feel free to open a PR changing that if you do.
This code is temporary so other things seem more important than the naming of that method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
More just musing really, I also don't care enough to change it since this is already merged ;)