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
18 changed files
with
281 additions
and
91 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 |
---|---|---|
@@ -1 +0,0 @@ | ||
Remove all strategy classes and define a module with mock/stub/etc methods. | ||
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
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
28 changes: 28 additions & 0 deletions
28
lib/rr/double_definitions/double_injections/any_instance_of.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,28 @@ | ||
module RR | ||
module DoubleDefinitions | ||
module DoubleInjections | ||
class AnyInstanceOf | ||
extend(Module.new do | ||
include RR::Adapters::RRMethods | ||
|
||
def call(subject_class, stubbed_methods=nil, &block) | ||
::RR::DoubleDefinitions::DoubleDefinitionCreate.set_default_double_injection_strategy(lambda do |double_definition_create| | ||
::RR::DoubleDefinitions::Strategies::DoubleInjection::AnyInstanceOf.new(double_definition_create) | ||
end) do | ||
if stubbed_methods | ||
subject_class.class_eval do | ||
stubbed_methods.each do |name, value| | ||
value_proc = value.is_a?(Proc) ? value : lambda {value} | ||
RR.stub(subject_class, name).returns(&value_proc) | ||
end | ||
end | ||
else | ||
block.call(subject_class) | ||
end | ||
end | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module RR | ||
module DoubleDefinitions | ||
module DoubleInjections | ||
class Instance | ||
extend(Module.new do | ||
include ::RR::Adapters::RRMethods | ||
|
||
def call(double_method_name, *args, &definition_eval_block) | ||
double_definition_create = DoubleDefinitions::DoubleDefinitionCreate.new | ||
double_definition_create.send(double_method_name, *args, &definition_eval_block) | ||
end | ||
end) | ||
end | ||
end | ||
end | ||
end |
50 changes: 50 additions & 0 deletions
50
lib/rr/double_definitions/double_injections/new_instance_of.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,50 @@ | ||
module RR | ||
module DoubleDefinitions | ||
module DoubleInjections | ||
class NewInstanceOf | ||
extend(Module.new do | ||
include RR::Adapters::RRMethods | ||
def call(subject, stubbed_methods={}) | ||
double_definition_create = DoubleDefinitionCreate.new.stub | ||
stub(subject).new do |*args| # TODO: Need to stub allocate instead of new | ||
subject = subject.allocate | ||
add_stubbed_methods(subject, stubbed_methods) | ||
add_method_chain_definition(subject, double_definition_create) | ||
yield(subject) if block_given? | ||
initialize_subject_instance(subject, args) | ||
end | ||
DoubleDefinitionCreateBlankSlate.new(double_definition_create) | ||
end | ||
|
||
protected | ||
def add_stubbed_methods(subject_instance, stubbed_methods) | ||
stubbed_methods.each do |name, value| | ||
value_proc = value.is_a?(Proc) ? value : lambda {value} | ||
stub(subject_instance, name).returns(&value_proc) | ||
end | ||
end | ||
|
||
def add_method_chain_definition(subject_instance, double_definition_create) | ||
implementation_strategy = double_definition_create.implementation_strategy | ||
if implementation_strategy.method_name | ||
stub(subject_instance).method_missing( | ||
implementation_strategy.method_name, | ||
*implementation_strategy.args, | ||
&implementation_strategy.handler | ||
) | ||
end | ||
end | ||
|
||
def initialize_subject_instance(subject_instance, args) | ||
if args.last.is_a?(ProcFromBlock) | ||
subject_instance.__send__(:initialize, *args[0..(args.length-2)], &args.last) | ||
else | ||
subject_instance.__send__(:initialize, *args) | ||
end | ||
subject_instance | ||
end | ||
end) | ||
end | ||
end | ||
end | ||
end |
31 changes: 31 additions & 0 deletions
31
lib/rr/double_definitions/strategies/double_injection/any_instance_of.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,31 @@ | ||
module RR | ||
module DoubleDefinitions | ||
module Strategies | ||
module DoubleInjection | ||
# This class is Deprecated. | ||
# Calling instance_of will cause all instances of the passed in Class | ||
# to have the Double defined. | ||
# | ||
# The following example mocks all User's valid? method and return false. | ||
# mock.instance_of(User).valid? {false} | ||
# | ||
# The following example mocks and proxies User#projects and returns the | ||
# first 3 projects. | ||
# mock.instance_of(User).projects do |projects| | ||
# projects[0..2] | ||
# end | ||
class AnyInstanceOf < DoubleInjectionStrategy | ||
protected | ||
def do_call | ||
if !double_definition_create.no_subject? && !double_definition_create.subject.is_a?(Class) | ||
raise ArgumentError, "instance_of only accepts class objects" | ||
end | ||
# subject, method_name | ||
double_injection = Injections::DoubleInjection.find_or_create(subject, method_name) | ||
Double.new(double_injection, definition) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
40 changes: 0 additions & 40 deletions
40
lib/rr/double_definitions/strategies/double_injection/any_instance_of_class.rb
This file was deleted.
Oops, something went wrong.
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
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
Oops, something went wrong.