rr 0.4.10 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGES +14 -0
- data/README.rdoc +67 -13
- data/Rakefile +1 -1
- data/lib/rr.rb +29 -9
- data/lib/rr/adapters/rr_methods.rb +38 -158
- data/lib/rr/double.rb +46 -41
- data/lib/rr/double_definitions/child_double_definition_creator.rb +23 -0
- data/lib/rr/double_definitions/double_definition.rb +212 -0
- data/lib/rr/double_definitions/double_definition_creator.rb +153 -0
- data/lib/rr/double_definitions/double_definition_creator_proxy.rb +25 -0
- data/lib/rr/double_definitions/strategies/implementation/implementation_strategy.rb +15 -0
- data/lib/rr/double_definitions/strategies/implementation/proxy.rb +62 -0
- data/lib/rr/double_definitions/strategies/implementation/reimplementation.rb +14 -0
- data/lib/rr/double_definitions/strategies/scope/instance.rb +15 -0
- data/lib/rr/double_definitions/strategies/scope/instance_of_class.rb +43 -0
- data/lib/rr/double_definitions/strategies/scope/scope_strategy.rb +15 -0
- data/lib/rr/double_definitions/strategies/strategy.rb +70 -0
- data/lib/rr/double_definitions/strategies/verification/dont_allow.rb +34 -0
- data/lib/rr/double_definitions/strategies/verification/mock.rb +44 -0
- data/lib/rr/double_definitions/strategies/verification/stub.rb +45 -0
- data/lib/rr/double_definitions/strategies/verification/verification_strategy.rb +15 -0
- data/lib/rr/double_injection.rb +21 -15
- data/lib/rr/expectations/argument_equality_expectation.rb +2 -1
- data/lib/rr/space.rb +23 -22
- data/lib/rr/wildcard_matchers/hash_including.rb +29 -0
- data/lib/rr/wildcard_matchers/satisfy.rb +26 -0
- data/spec/high_level_spec.rb +111 -64
- data/spec/rr/adapters/rr_methods_argument_matcher_spec.rb +1 -1
- data/spec/rr/adapters/rr_methods_creator_spec.rb +99 -315
- data/spec/rr/adapters/rr_methods_space_spec.rb +90 -109
- data/spec/rr/adapters/rr_methods_spec_helper.rb +1 -1
- data/spec/rr/adapters/rr_methods_times_matcher_spec.rb +1 -1
- data/spec/rr/double_definitions/child_double_definition_creator_spec.rb +103 -0
- data/spec/rr/double_definitions/double_definition_creator_proxy_spec.rb +83 -0
- data/spec/rr/double_definitions/double_definition_creator_spec.rb +495 -0
- data/spec/rr/double_definitions/double_definition_spec.rb +1116 -0
- data/spec/rr/double_injection/double_injection_bind_spec.rb +111 -0
- data/spec/rr/double_injection/double_injection_dispatching_spec.rb +245 -0
- data/spec/rr/{double → double_injection}/double_injection_has_original_method_spec.rb +9 -9
- data/spec/rr/double_injection/double_injection_reset_spec.rb +90 -0
- data/spec/rr/double_injection/double_injection_spec.rb +77 -0
- data/spec/rr/double_injection/double_injection_verify_spec.rb +29 -0
- data/spec/rr/double_spec.rb +156 -136
- data/spec/rr/errors/rr_error_spec.rb +1 -1
- data/spec/rr/expectations/any_argument_expectation_spec.rb +1 -1
- data/spec/rr/expectations/anything_argument_equality_expectation_spec.rb +6 -30
- data/spec/rr/expectations/argument_equality_expectation_spec.rb +35 -18
- data/spec/rr/expectations/boolean_argument_equality_expectation_spec.rb +22 -41
- data/spec/rr/expectations/hash_including_argument_equality_expectation_spec.rb +82 -0
- data/spec/rr/expectations/hash_including_spec.rb +17 -0
- data/spec/rr/expectations/satisfy_argument_equality_expectation_spec.rb +59 -0
- data/spec/rr/expectations/satisfy_spec.rb +14 -0
- data/spec/rr/expectations/times_called_expectation/times_called_expectation_any_times_spec.rb +30 -28
- data/spec/rr/expectations/times_called_expectation/times_called_expectation_at_least_spec.rb +55 -54
- data/spec/rr/expectations/times_called_expectation/times_called_expectation_at_most_spec.rb +49 -48
- data/spec/rr/expectations/times_called_expectation/times_called_expectation_helper.rb +9 -7
- data/spec/rr/expectations/times_called_expectation/times_called_expectation_integer_spec.rb +77 -76
- data/spec/rr/expectations/times_called_expectation/times_called_expectation_proc_spec.rb +58 -57
- data/spec/rr/expectations/times_called_expectation/times_called_expectation_range_spec.rb +59 -58
- data/spec/rr/expectations/times_called_expectation/times_called_expectation_spec.rb +25 -24
- data/spec/rr/rspec/rspec_adapter_spec.rb +12 -11
- data/spec/rr/rspec/rspec_backtrace_tweaking_spec.rb +10 -8
- data/spec/rr/rspec/rspec_usage_spec.rb +1 -1
- data/spec/rr/space/hash_with_object_id_key_spec.rb +1 -1
- data/spec/rr/space/space_spec.rb +330 -192
- data/spec/rr/test_unit/test_helper.rb +1 -2
- data/spec/rr/test_unit/test_unit_backtrace_test.rb +1 -2
- data/spec/rr/test_unit/test_unit_integration_test.rb +1 -2
- data/spec/rr/times_called_matchers/any_times_matcher_spec.rb +1 -1
- data/spec/rr/times_called_matchers/at_least_matcher_spec.rb +1 -1
- data/spec/rr/times_called_matchers/at_most_matcher_spec.rb +1 -1
- data/spec/rr/times_called_matchers/integer_matcher_spec.rb +1 -1
- data/spec/rr/times_called_matchers/proc_matcher_spec.rb +1 -1
- data/spec/rr/times_called_matchers/range_matcher_spec.rb +1 -1
- data/spec/rr/times_called_matchers/times_called_matcher_spec.rb +1 -1
- data/spec/rr/wildcard_matchers/anything_spec.rb +24 -0
- data/spec/rr/wildcard_matchers/boolean_spec.rb +36 -0
- data/spec/rr/wildcard_matchers/duck_type_spec.rb +52 -0
- data/spec/rr/wildcard_matchers/is_a_spec.rb +32 -0
- data/spec/rr/wildcard_matchers/numeric_spec.rb +32 -0
- data/spec/rr/wildcard_matchers/range_spec.rb +35 -0
- data/spec/rr/wildcard_matchers/regexp_spec.rb +43 -0
- data/spec/rr_spec.rb +28 -0
- data/spec/spec_helper.rb +84 -0
- metadata +43 -29
- data/lib/rr/double_creator.rb +0 -271
- data/lib/rr/double_definition.rb +0 -179
- data/lib/rr/double_definition_builder.rb +0 -44
- data/lib/rr/double_definition_creator.rb +0 -156
- data/lib/rr/double_definition_creator_proxy.rb +0 -20
- data/spec/rr/double/double_injection_bind_spec.rb +0 -105
- data/spec/rr/double/double_injection_dispatching_spec.rb +0 -228
- data/spec/rr/double/double_injection_reset_spec.rb +0 -86
- data/spec/rr/double/double_injection_spec.rb +0 -72
- data/spec/rr/double/double_injection_verify_spec.rb +0 -24
- data/spec/rr/double_definition_creator_proxy_spec.rb +0 -85
- data/spec/rr/double_definition_creator_spec.rb +0 -496
- data/spec/rr/double_definition_spec.rb +0 -815
- data/spec/rr/expectations/anything_spec.rb +0 -14
- data/spec/rr/expectations/boolean_spec.rb +0 -14
- data/spec/rr/expectations/duck_type_argument_equality_expectation_spec.rb +0 -71
- data/spec/rr/expectations/duck_type_spec.rb +0 -14
- data/spec/rr/expectations/is_a_argument_equality_expectation_spec.rb +0 -51
- data/spec/rr/expectations/is_a_spec.rb +0 -14
- data/spec/rr/expectations/numeric_argument_equality_expectation_spec.rb +0 -47
- data/spec/rr/expectations/numeric_spec.rb +0 -14
- data/spec/rr/expectations/range_argument_equality_expectation_spec.rb +0 -59
- data/spec/rr/expectations/range_spec.rb +0 -10
- data/spec/rr/expectations/regexp_argument_equality_expectation_spec.rb +0 -72
- data/spec/rr/expectations/regexp_spec.rb +0 -10
@@ -1,44 +0,0 @@
|
|
1
|
-
module RR
|
2
|
-
class DoubleDefinitionBuilder #:nodoc:
|
3
|
-
attr_reader :definition
|
4
|
-
|
5
|
-
def initialize(definition, args, handler)
|
6
|
-
@definition = definition
|
7
|
-
@args = args
|
8
|
-
@handler = handler
|
9
|
-
end
|
10
|
-
|
11
|
-
def mock
|
12
|
-
@definition.with(*@args).once
|
13
|
-
end
|
14
|
-
|
15
|
-
def stub
|
16
|
-
@definition.any_number_of_times
|
17
|
-
permissive_argument
|
18
|
-
end
|
19
|
-
|
20
|
-
def dont_allow
|
21
|
-
@definition.never
|
22
|
-
permissive_argument
|
23
|
-
reimplementation
|
24
|
-
end
|
25
|
-
|
26
|
-
def permissive_argument
|
27
|
-
if @args.empty?
|
28
|
-
@definition.with_any_args
|
29
|
-
else
|
30
|
-
@definition.with(*@args)
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
def reimplementation
|
35
|
-
@definition.returns(&@handler)
|
36
|
-
end
|
37
|
-
|
38
|
-
def proxy
|
39
|
-
@definition.after_call_block_callback_strategy
|
40
|
-
@definition.proxy
|
41
|
-
@definition.after_call(&@handler) if @handler
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
@@ -1,156 +0,0 @@
|
|
1
|
-
module RR
|
2
|
-
class DoubleDefinitionCreator # :nodoc
|
3
|
-
NO_SUBJECT_ARG = Object.new
|
4
|
-
|
5
|
-
include Space::Reader
|
6
|
-
include Errors
|
7
|
-
|
8
|
-
def initialize
|
9
|
-
@core_strategy = nil
|
10
|
-
@using_proxy_strategy = false
|
11
|
-
@using_instance_of_strategy = nil
|
12
|
-
end
|
13
|
-
|
14
|
-
def mock(subject=NO_SUBJECT_ARG, method_name=nil, &definition) # :nodoc
|
15
|
-
add_strategy(subject, method_name, definition) do
|
16
|
-
set_core_strategy :mock
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
def stub(subject=NO_SUBJECT_ARG, method_name=nil, &definition) # :nodoc
|
21
|
-
add_strategy(subject, method_name, definition) do
|
22
|
-
set_core_strategy :stub
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
def dont_allow(subject=NO_SUBJECT_ARG, method_name=nil, &definition) # :nodoc
|
27
|
-
add_strategy(subject, method_name, definition) do
|
28
|
-
set_core_strategy :dont_allow
|
29
|
-
end
|
30
|
-
end
|
31
|
-
alias_method :do_not_allow, :dont_allow
|
32
|
-
alias_method :dont_call, :dont_allow
|
33
|
-
alias_method :do_not_call, :dont_allow
|
34
|
-
|
35
|
-
def proxy(subject=NO_SUBJECT_ARG, method_name=nil, &definition) # :nodoc
|
36
|
-
add_strategy(subject, method_name, definition) do
|
37
|
-
proxy_when_dont_allow_error if @core_strategy == :dont_allow
|
38
|
-
@using_proxy_strategy = true
|
39
|
-
end
|
40
|
-
end
|
41
|
-
alias_method :probe, :proxy
|
42
|
-
|
43
|
-
def instance_of(subject=NO_SUBJECT_ARG, method_name=nil, &definition) # :nodoc
|
44
|
-
if subject != NO_SUBJECT_ARG && !subject.is_a?(Class)
|
45
|
-
raise ArgumentError, "instance_of only accepts class objects" unless subject.is_a?(Class)
|
46
|
-
end
|
47
|
-
add_strategy(subject, method_name, definition) do
|
48
|
-
@using_instance_of_strategy = true
|
49
|
-
return self if subject === NO_SUBJECT_ARG
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
def create(subject, method_name, *args, &handler)
|
54
|
-
@args = args
|
55
|
-
@handler = handler
|
56
|
-
if @using_instance_of_strategy
|
57
|
-
setup_doubles_for_class_instances(subject, method_name)
|
58
|
-
else
|
59
|
-
setup_double(subject, method_name)
|
60
|
-
end
|
61
|
-
transform
|
62
|
-
@definition
|
63
|
-
end
|
64
|
-
|
65
|
-
protected
|
66
|
-
def add_strategy(subject, method_name, definition)
|
67
|
-
if method_name && definition
|
68
|
-
raise ArgumentError, "Cannot pass in a method name and a block"
|
69
|
-
end
|
70
|
-
yield
|
71
|
-
if subject.__id__ === NO_SUBJECT_ARG.__id__
|
72
|
-
self
|
73
|
-
elsif method_name
|
74
|
-
create subject, method_name, &definition
|
75
|
-
else
|
76
|
-
DoubleDefinitionCreatorProxy.new(self, subject, &definition)
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
def set_core_strategy(strategy)
|
81
|
-
verify_no_core_strategy
|
82
|
-
@core_strategy = strategy
|
83
|
-
proxy_when_dont_allow_error if strategy == :dont_allow && @using_proxy_strategy
|
84
|
-
end
|
85
|
-
|
86
|
-
def setup_double(subject, method_name)
|
87
|
-
@double_injection = space.double_injection(subject, method_name)
|
88
|
-
@double = Double.new(@double_injection)
|
89
|
-
@definition = @double.definition
|
90
|
-
end
|
91
|
-
|
92
|
-
def setup_doubles_for_class_instances(subject, method_name)
|
93
|
-
class_double = space.double_injection(subject, :new)
|
94
|
-
class_double = Double.new(class_double)
|
95
|
-
|
96
|
-
instance_method_name = method_name
|
97
|
-
|
98
|
-
@definition = DoubleDefinition.new
|
99
|
-
class_handler = lambda do |return_value|
|
100
|
-
double_injection = space.double_injection(return_value, instance_method_name)
|
101
|
-
Double.new(double_injection, @definition)
|
102
|
-
return_value
|
103
|
-
end
|
104
|
-
|
105
|
-
builder = DoubleDefinitionBuilder.new(
|
106
|
-
class_double.definition,
|
107
|
-
[],
|
108
|
-
class_handler
|
109
|
-
)
|
110
|
-
builder.stub
|
111
|
-
builder.proxy
|
112
|
-
end
|
113
|
-
|
114
|
-
def transform
|
115
|
-
builder = DoubleDefinitionBuilder.new(@definition, @args, @handler)
|
116
|
-
|
117
|
-
verify_strategy
|
118
|
-
builder.__send__(@core_strategy)
|
119
|
-
|
120
|
-
if @using_proxy_strategy
|
121
|
-
builder.proxy
|
122
|
-
else
|
123
|
-
builder.reimplementation
|
124
|
-
end
|
125
|
-
end
|
126
|
-
|
127
|
-
def verify_no_core_strategy
|
128
|
-
strategy_already_defined_error if @core_strategy
|
129
|
-
end
|
130
|
-
|
131
|
-
def strategy_already_defined_error
|
132
|
-
raise(
|
133
|
-
DoubleDefinitionError,
|
134
|
-
"This Double already has a #{@core_strategy} strategy"
|
135
|
-
)
|
136
|
-
end
|
137
|
-
|
138
|
-
def verify_strategy
|
139
|
-
no_strategy_error unless @core_strategy
|
140
|
-
end
|
141
|
-
|
142
|
-
def no_strategy_error
|
143
|
-
raise(
|
144
|
-
DoubleDefinitionError,
|
145
|
-
"This Double has no strategy"
|
146
|
-
)
|
147
|
-
end
|
148
|
-
|
149
|
-
def proxy_when_dont_allow_error
|
150
|
-
raise(
|
151
|
-
DoubleDefinitionError,
|
152
|
-
"Doubles cannot be proxied when using dont_allow strategy"
|
153
|
-
)
|
154
|
-
end
|
155
|
-
end
|
156
|
-
end
|
@@ -1,20 +0,0 @@
|
|
1
|
-
module RR
|
2
|
-
class DoubleDefinitionCreatorProxy
|
3
|
-
def initialize(creator, object, &block) #:nodoc:
|
4
|
-
@creator = creator
|
5
|
-
@object = object
|
6
|
-
class << self
|
7
|
-
instance_methods.each do |m|
|
8
|
-
unless m =~ /^_/ || m.to_s == 'object_id'
|
9
|
-
undef_method m
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
def method_missing(method_name, *args, &block)
|
14
|
-
@creator.create(@object, method_name, *args, &block)
|
15
|
-
end
|
16
|
-
end
|
17
|
-
yield(self) if block_given?
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
@@ -1,105 +0,0 @@
|
|
1
|
-
require "spec/spec_helper"
|
2
|
-
|
3
|
-
module RR
|
4
|
-
describe DoubleInjection, "#bind with an existing method" do
|
5
|
-
before do
|
6
|
-
@object = Object.new
|
7
|
-
@method_name = :foobar
|
8
|
-
def @object.foobar;
|
9
|
-
:original_foobar;
|
10
|
-
end
|
11
|
-
@original_method = @object.method(@method_name)
|
12
|
-
@object.methods.should include(@method_name.to_s)
|
13
|
-
@double_injection = DoubleInjection.new(@object, @method_name)
|
14
|
-
end
|
15
|
-
|
16
|
-
it "overrides the original method with the double_injection's dispatching methods" do
|
17
|
-
@object.respond_to?(:__rr__foobar).should == false
|
18
|
-
@double_injection.bind
|
19
|
-
@object.respond_to?(:__rr__foobar).should == true
|
20
|
-
|
21
|
-
rr_foobar_called = false
|
22
|
-
(class << @object; self; end).class_eval do
|
23
|
-
define_method :__rr__foobar do
|
24
|
-
rr_foobar_called = true
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
rr_foobar_called.should == false
|
29
|
-
@object.foobar
|
30
|
-
rr_foobar_called.should == true
|
31
|
-
end
|
32
|
-
|
33
|
-
it "stores original method in __rr__original_method_name" do
|
34
|
-
@double_injection.bind
|
35
|
-
@object.respond_to?(:__rr__original_foobar).should == true
|
36
|
-
@object.method(:__rr__original_foobar).should == @original_method
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
describe DoubleInjection, "#bind without an existing method" do
|
41
|
-
before do
|
42
|
-
@object = Object.new
|
43
|
-
@method_name = :foobar
|
44
|
-
@object.methods.should_not include(@method_name.to_s)
|
45
|
-
@double_injection = DoubleInjection.new(@object, @method_name)
|
46
|
-
end
|
47
|
-
|
48
|
-
it "creates a new method with the double_injection's dispatching methods" do
|
49
|
-
@object.respond_to?(:__rr__foobar).should == false
|
50
|
-
@double_injection.bind
|
51
|
-
@object.respond_to?(:__rr__foobar).should == true
|
52
|
-
|
53
|
-
rr_foobar_called = false
|
54
|
-
(class << @object; self; end).class_eval do
|
55
|
-
define_method :__rr__foobar do
|
56
|
-
rr_foobar_called = true
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
rr_foobar_called.should == false
|
61
|
-
@object.foobar
|
62
|
-
rr_foobar_called.should == true
|
63
|
-
end
|
64
|
-
|
65
|
-
it "does not create method __rr__original_method_name" do
|
66
|
-
@double_injection.bind
|
67
|
-
@object.respond_to?(:__rr__original_foobar).should == false
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
describe DoubleInjection, "#bind with ==" do
|
72
|
-
before do
|
73
|
-
@object = Object.new
|
74
|
-
@method_name = :'=='
|
75
|
-
@object.should respond_to(@method_name)
|
76
|
-
@original_method = @object.method(@method_name)
|
77
|
-
@object.methods.should include(@method_name.to_s)
|
78
|
-
@double_injection = DoubleInjection.new(@object, @method_name)
|
79
|
-
end
|
80
|
-
|
81
|
-
it "overrides the original method with the double_injection's dispatching methods" do
|
82
|
-
@object.respond_to?(:"__rr__#{@method_name}").should == false
|
83
|
-
@double_injection.bind
|
84
|
-
@object.respond_to?(:"__rr__#{@method_name}").should == true
|
85
|
-
|
86
|
-
override_called = false
|
87
|
-
method_name = @method_name
|
88
|
-
(class << @object; self; end).class_eval do
|
89
|
-
define_method :"__rr__#{method_name}" do
|
90
|
-
override_called = true
|
91
|
-
end
|
92
|
-
end
|
93
|
-
|
94
|
-
override_called.should == false
|
95
|
-
@object == 1
|
96
|
-
override_called.should == true
|
97
|
-
end
|
98
|
-
|
99
|
-
it "stores original method in __rr__original_method_name" do
|
100
|
-
@double_injection.bind
|
101
|
-
@object.respond_to?(:"__rr__original_#{@method_name}").should == true
|
102
|
-
@object.method(:"__rr__original_#{@method_name}").should == @original_method
|
103
|
-
end
|
104
|
-
end
|
105
|
-
end
|
@@ -1,228 +0,0 @@
|
|
1
|
-
require "spec/spec_helper"
|
2
|
-
|
3
|
-
module RR
|
4
|
-
describe DoubleInjection do
|
5
|
-
before do
|
6
|
-
@space = Space.new
|
7
|
-
@object = Object.new
|
8
|
-
@object.methods.should_not include(method_name.to_s)
|
9
|
-
@double_injection = @space.double_injection(@object, method_name)
|
10
|
-
end
|
11
|
-
|
12
|
-
describe "normal methods" do
|
13
|
-
def method_name
|
14
|
-
:foobar
|
15
|
-
end
|
16
|
-
|
17
|
-
describe "where the double takes a block" do
|
18
|
-
it "executes the block" do
|
19
|
-
method_fixture = Object.new
|
20
|
-
class << method_fixture
|
21
|
-
def method_with_block(a, b)
|
22
|
-
yield(a, b)
|
23
|
-
end
|
24
|
-
end
|
25
|
-
double = Double.new(@double_injection)
|
26
|
-
double.with(1, 2).implemented_by(method_fixture.method(:method_with_block))
|
27
|
-
@object.foobar(1, 2) {|a, b| [b, a]}.should == [2, 1]
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
describe "where there are no doubles with duplicate ArgumentExpectations" do
|
32
|
-
it "dispatches to Double that have an exact match" do
|
33
|
-
double1_with_exact_match = Double.new(@double_injection)
|
34
|
-
double1_with_exact_match.with(:exact_match_1).returns {:return_1}
|
35
|
-
double_with_no_match = Double.new(@double_injection)
|
36
|
-
double_with_no_match.with("nothing that matches").returns {:no_match}
|
37
|
-
double2_with_exact_match = Double.new(@double_injection)
|
38
|
-
double2_with_exact_match.with(:exact_match_2).returns {:return_2}
|
39
|
-
|
40
|
-
@object.foobar(:exact_match_1).should == :return_1
|
41
|
-
@object.foobar(:exact_match_2).should == :return_2
|
42
|
-
end
|
43
|
-
|
44
|
-
it "dispatches to Double that have a wildcard match" do
|
45
|
-
double_with_wildcard_match = Double.new(@double_injection)
|
46
|
-
double_with_wildcard_match.with_any_args.returns {:wild_card_value}
|
47
|
-
double_with_no_match = Double.new(@double_injection)
|
48
|
-
double_with_no_match.with("nothing that matches").returns {:no_match}
|
49
|
-
|
50
|
-
@object.foobar(:wildcard_match_1).should == :wild_card_value
|
51
|
-
@object.foobar(:wildcard_match_2, 3).should == :wild_card_value
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
describe "where there are doubles" do
|
56
|
-
it "raises DoubleNotFoundError error when arguments do not match a double" do
|
57
|
-
double_1 = Double.new(@double_injection)
|
58
|
-
double_1.with(1, 2)
|
59
|
-
|
60
|
-
double_2 = Double.new(@double_injection)
|
61
|
-
double_2.with(3)
|
62
|
-
|
63
|
-
error = nil
|
64
|
-
begin
|
65
|
-
@object.foobar(:arg1, :arg2)
|
66
|
-
viotated "Error should have been raised"
|
67
|
-
rescue Errors::DoubleNotFoundError => e
|
68
|
-
error = e
|
69
|
-
end
|
70
|
-
error.message.should include("On object #<Object")
|
71
|
-
expected_double_message_part = "unexpected method invocation in the next line followed by the expected invocations\n" <<
|
72
|
-
" foobar(:arg1, :arg2)\n"
|
73
|
-
"- foobar(1, 2)\n" <<
|
74
|
-
"- foobar(3)"
|
75
|
-
error.message.should include(expected_double_message_part)
|
76
|
-
end
|
77
|
-
end
|
78
|
-
|
79
|
-
describe "where there are Doubles with NonTerminal TimesCalledMatchers" do
|
80
|
-
it "dispatches to Double with exact match" do
|
81
|
-
double = double(1, 2) {:return_value}
|
82
|
-
@object.foobar(1, 2).should == :return_value
|
83
|
-
end
|
84
|
-
|
85
|
-
it "matches to the last Double that was registered with an exact match" do
|
86
|
-
double_1 = double(1, 2) {:value_1}
|
87
|
-
double_2 = double(1, 2) {:value_2}
|
88
|
-
|
89
|
-
@object.foobar(1, 2).should == :value_2
|
90
|
-
end
|
91
|
-
|
92
|
-
it "dispatches to Double with wildcard match" do
|
93
|
-
double = double(anything) {:return_value}
|
94
|
-
@object.foobar(:dont_care).should == :return_value
|
95
|
-
end
|
96
|
-
|
97
|
-
it "matches to the last Double that was registered with a wildcard match" do
|
98
|
-
double_1 = double(anything) {:value_1}
|
99
|
-
double_2 = double(anything) {:value_2}
|
100
|
-
|
101
|
-
@object.foobar(:dont_care).should == :value_2
|
102
|
-
end
|
103
|
-
|
104
|
-
it "matches to Double with exact match Double even when a Double with wildcard match was registered later" do
|
105
|
-
exact_double_registered_first = double(1, 2) {:exact_first}
|
106
|
-
wildcard_double_registered_last = double(anything, anything) {:wildcard_last}
|
107
|
-
|
108
|
-
@object.foobar(1, 2).should == :exact_first
|
109
|
-
end
|
110
|
-
|
111
|
-
def double(*arguments, &return_value)
|
112
|
-
double = Double.new(@double_injection)
|
113
|
-
double.with(*arguments).any_number_of_times.returns(&return_value)
|
114
|
-
double.should_not be_terminal
|
115
|
-
double
|
116
|
-
end
|
117
|
-
end
|
118
|
-
|
119
|
-
describe "where there are Terminal Doubles with duplicate Exact Match ArgumentExpectations" do
|
120
|
-
it "dispatches to Double that have an exact match" do
|
121
|
-
double1_with_exact_match = double(:exact_match) {:return_1}
|
122
|
-
|
123
|
-
@object.foobar(:exact_match).should == :return_1
|
124
|
-
end
|
125
|
-
|
126
|
-
it "dispatches to the first Double that have an exact match" do
|
127
|
-
double1_with_exact_match = double(:exact_match) {:return_1}
|
128
|
-
double2_with_exact_match = double(:exact_match) {:return_2}
|
129
|
-
|
130
|
-
@object.foobar(:exact_match).should == :return_1
|
131
|
-
end
|
132
|
-
|
133
|
-
it "dispatches the second Double with an exact match
|
134
|
-
when the first double's Times Called expectation is satisfied" do
|
135
|
-
double1_with_exact_match = double(:exact_match) {:return_1}
|
136
|
-
double2_with_exact_match = double(:exact_match) {:return_2}
|
137
|
-
|
138
|
-
@object.foobar(:exact_match)
|
139
|
-
@object.foobar(:exact_match).should == :return_2
|
140
|
-
end
|
141
|
-
|
142
|
-
it "raises TimesCalledError when all of the doubles Times Called expectation is satisfied" do
|
143
|
-
double1_with_exact_match = double(:exact_match) {:return_1}
|
144
|
-
double2_with_exact_match = double(:exact_match) {:return_2}
|
145
|
-
|
146
|
-
@object.foobar(:exact_match)
|
147
|
-
@object.foobar(:exact_match)
|
148
|
-
lambda do
|
149
|
-
@object.foobar(:exact_match)
|
150
|
-
end.should raise_error(Errors::TimesCalledError)
|
151
|
-
end
|
152
|
-
|
153
|
-
def double(*arguments, &return_value)
|
154
|
-
double = Double.new(@double_injection)
|
155
|
-
double.with(*arguments).once.returns(&return_value)
|
156
|
-
double.should be_terminal
|
157
|
-
double
|
158
|
-
end
|
159
|
-
end
|
160
|
-
|
161
|
-
describe "where there are doubles with duplicate Wildcard Match ArgumentExpectations" do
|
162
|
-
it "dispatches to Double that have a wildcard match" do
|
163
|
-
double_1 = double {:return_1}
|
164
|
-
|
165
|
-
@object.foobar(:anything).should == :return_1
|
166
|
-
end
|
167
|
-
|
168
|
-
it "dispatches to the first Double that has a wildcard match" do
|
169
|
-
double_1 = double {:return_1}
|
170
|
-
double_2 = double {:return_2}
|
171
|
-
|
172
|
-
@object.foobar(:anything).should == :return_1
|
173
|
-
end
|
174
|
-
|
175
|
-
it "dispatches the second Double with a wildcard match
|
176
|
-
when the first double's Times Called expectation is satisfied" do
|
177
|
-
double_1 = double {:return_1}
|
178
|
-
double_2 = double {:return_2}
|
179
|
-
|
180
|
-
@object.foobar(:anything)
|
181
|
-
@object.foobar(:anything).should == :return_2
|
182
|
-
end
|
183
|
-
|
184
|
-
it "raises TimesCalledError when all of the doubles Times Called expectation is satisfied" do
|
185
|
-
double_1 = double {:return_1}
|
186
|
-
double_2 = double {:return_2}
|
187
|
-
|
188
|
-
@object.foobar(:anything)
|
189
|
-
@object.foobar(:anything)
|
190
|
-
lambda do
|
191
|
-
@object.foobar(:anything)
|
192
|
-
end.should raise_error(Errors::TimesCalledError)
|
193
|
-
end
|
194
|
-
|
195
|
-
def double(&return_value)
|
196
|
-
double = Double.new(@double_injection)
|
197
|
-
double.with_any_args.once.returns(&return_value)
|
198
|
-
double.should be_terminal
|
199
|
-
double
|
200
|
-
end
|
201
|
-
end
|
202
|
-
end
|
203
|
-
|
204
|
-
describe "method names with !" do
|
205
|
-
def method_name
|
206
|
-
:foobar!
|
207
|
-
end
|
208
|
-
|
209
|
-
it "executes the block" do
|
210
|
-
double = Double.new(@double_injection)
|
211
|
-
double.with(1, 2) {:return_value}
|
212
|
-
@object.foobar!(1, 2).should == :return_value
|
213
|
-
end
|
214
|
-
end
|
215
|
-
|
216
|
-
describe "method names with ?" do
|
217
|
-
def method_name
|
218
|
-
:foobar?
|
219
|
-
end
|
220
|
-
|
221
|
-
it "executes the block" do
|
222
|
-
double = Double.new(@double_injection)
|
223
|
-
double.with(1, 2) {:return_value}
|
224
|
-
@object.foobar?(1, 2).should == :return_value
|
225
|
-
end
|
226
|
-
end
|
227
|
-
end
|
228
|
-
end
|