Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
335 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
spec/rspec/matchers/differentiate_block_method_types_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.