Skip to content

Commit

Permalink
Registering Verification Strategy methods on DoubleDefininionCreator.
Browse files Browse the repository at this point in the history
git-svn-id: svn+ssh://rubyforge.org/var/svn/pivotalrb/rr/trunk@1917 af276e61-6b34-4dac-905b-574b5f35ef33
  • Loading branch information
btakita committed Oct 8, 2008
1 parent 8882b9e commit 170a81f
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 57 deletions.
1 change: 0 additions & 1 deletion lib/rr.rb
Expand Up @@ -11,7 +11,6 @@
require "#{dir}/rr/hash_with_object_id_key"

require "#{dir}/rr/double_definitions/double_definition_creator_proxy"

require "#{dir}/rr/double_definitions/double_definition_creator"

require "#{dir}/rr/double"
Expand Down
62 changes: 22 additions & 40 deletions lib/rr/double_definitions/double_definition_creator.rb
@@ -1,57 +1,39 @@
module RR
module DoubleDefinitions
class DoubleDefinitionCreator # :nodoc
class << self
def register_verification_strategy_class(strategy_class)
class_eval((<<-CLASS), __FILE__, __LINE__)
def #{strategy_class.domain_name}(subject=NO_SUBJECT, method_name=nil, &definition_eval_block)
add_strategy(subject, method_name, definition_eval_block) do
self.verification_strategy = #{strategy_class.name}.new(self)
end
end
CLASS

class_eval((<<-CLASS), __FILE__, __LINE__)
def #{strategy_class.domain_name}!(method_name=nil, &definition_eval_block)
#{strategy_class.domain_name}(Object.new, method_name, &definition_eval_block)
end
CLASS
end
end

attr_reader :subject, :method_name, :args, :handler, :definition, :verification_strategy, :implementation_strategy, :scope_strategy
NO_SUBJECT = Object.new

include Space::Reader

def initialize
@verification_strategy = nil
@implementation_strategy = Strategies::Implementation::Reimplementation.new
@scope_strategy = Strategies::Scope::Instance.new
@implementation_strategy = Strategies::Implementation::Reimplementation.new(self)
@scope_strategy = Strategies::Scope::Instance.new(self)
end

module StrategySetupMethods
def mock(subject=NO_SUBJECT, method_name=nil, &definition_eval_block) # :nodoc
add_strategy(subject, method_name, definition_eval_block) do
self.verification_strategy = Strategies::Verification::Mock.new
end
end

def mock!(method_name=nil, &definition_eval_block)
mock(Object.new, method_name, &definition_eval_block)
end

def stub(subject=NO_SUBJECT, method_name=nil, &definition_eval_block) # :nodoc
add_strategy(subject, method_name, definition_eval_block) do
self.verification_strategy = Strategies::Verification::Stub.new
end
end

def stub!(method_name=nil, &definition_eval_block)
stub(Object.new, method_name, &definition_eval_block)
end

def dont_allow(subject=NO_SUBJECT, method_name=nil, &definition_eval_block) # :nodoc
add_strategy(subject, method_name, definition_eval_block) do
self.verification_strategy = Strategies::Verification::DontAllow.new
end
end
alias_method :do_not_allow, :dont_allow
alias_method :dont_call, :dont_allow
alias_method :do_not_call, :dont_allow

def dont_allow!(method_name=nil, &definition_eval_block)
dont_allow(Object.new, method_name, &definition_eval_block)
end
alias_method :do_not_allow!, :dont_allow!
alias_method :dont_call!, :dont_allow!
alias_method :do_not_call!, :dont_allow!

def proxy(subject=NO_SUBJECT, method_name=nil, &definition_eval_block) # :nodoc
add_strategy(subject, method_name, definition_eval_block) do
self.implementation_strategy = Strategies::Implementation::Proxy.new
self.implementation_strategy = Strategies::Implementation::Proxy.new(self)
end
end
alias_method :probe, :proxy
Expand All @@ -61,7 +43,7 @@ def instance_of(subject=NO_SUBJECT, method_name=nil, &definition_eval_block) # :
raise ArgumentError, "instance_of only accepts class objects" unless subject.is_a?(Class)
end
add_strategy(subject, method_name, definition_eval_block) do
self.scope_strategy = Strategies::Scope::InstanceOfClass.new
self.scope_strategy = Strategies::Scope::InstanceOfClass.new(self)
end
end

Expand Down
6 changes: 4 additions & 2 deletions lib/rr/double_definitions/strategies/implementation/proxy.rb
Expand Up @@ -3,8 +3,10 @@ module DoubleDefinitions
module Strategies
module Implementation
class Proxy < Strategy
def name
"proxy"
class << self
def domain_name
"proxy"
end
end

protected
Expand Down
Expand Up @@ -3,8 +3,10 @@ module DoubleDefinitions
module Strategies
module Implementation
class Reimplementation < Strategy
def name
"reimplementation"
class << self
def domain_name
"reimplementation"
end
end

protected
Expand Down
6 changes: 4 additions & 2 deletions lib/rr/double_definitions/strategies/scope/instance.rb
Expand Up @@ -3,8 +3,10 @@ module DoubleDefinitions
module Strategies
module Scope
class Instance < Strategy
def name
"instance"
class << self
def domain_name
"instance"
end
end

protected
Expand Down
Expand Up @@ -3,8 +3,10 @@ module DoubleDefinitions
module Strategies
module Scope
class InstanceOfClass < Strategy
def name
"instance_of"
class << self
def domain_name
"instance_of"
end
end

protected
Expand Down
8 changes: 6 additions & 2 deletions lib/rr/double_definitions/strategies/strategy.rb
Expand Up @@ -2,16 +2,20 @@ module RR
module DoubleDefinitions
module Strategies
class Strategy
attr_reader :definition, :method_name, :args, :handler
attr_reader :double_definition_creator, :definition, :method_name, :args, :handler
include Space::Reader

def initialize(double_definition_creator)
@double_definition_creator = double_definition_creator
end

def call(definition, method_name, args, handler)
@definition, @method_name, @args, @handler = definition, method_name, args, handler
do_call
end

def name
raise NotImplementedError
self.class.domain_name
end

protected
Expand Down
16 changes: 14 additions & 2 deletions lib/rr/double_definitions/strategies/verification/dont_allow.rb
Expand Up @@ -3,8 +3,20 @@ module DoubleDefinitions
module Strategies
module Verification
class DontAllow < Strategy
def name
"dont_allow"
class << self
def domain_name
"dont_allow"
end
end
DoubleDefinitionCreator.register_verification_strategy_class(self)
DoubleDefinitionCreator.class_eval do
alias_method :do_not_allow, :dont_allow
alias_method :dont_call, :dont_allow
alias_method :do_not_call, :dont_allow

alias_method :do_not_allow!, :dont_allow!
alias_method :dont_call!, :dont_allow!
alias_method :do_not_call!, :dont_allow!
end

protected
Expand Down
7 changes: 5 additions & 2 deletions lib/rr/double_definitions/strategies/verification/mock.rb
Expand Up @@ -3,9 +3,12 @@ module DoubleDefinitions
module Strategies
module Verification
class Mock < Strategy
def name
"mock"
class << self
def domain_name
"mock"
end
end
DoubleDefinitionCreator.register_verification_strategy_class(self)

protected
def do_call
Expand Down
7 changes: 5 additions & 2 deletions lib/rr/double_definitions/strategies/verification/stub.rb
Expand Up @@ -3,9 +3,12 @@ module DoubleDefinitions
module Strategies
module Verification
class Stub < Strategy
def name
"stub"
class << self
def domain_name
"stub"
end
end
DoubleDefinitionCreator.register_verification_strategy_class(self)

protected
def do_call
Expand Down

0 comments on commit 170a81f

Please sign in to comment.