mcmire-rr 1.0.5.rc1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES +269 -0
- data/Gemfile +16 -0
- data/Gemfile.lock +47 -0
- data/LICENSE +22 -0
- data/README.rdoc +390 -0
- data/Rakefile +49 -0
- data/VERSION +1 -0
- data/lib/rr.rb +101 -0
- data/lib/rr/adapters/minitest.rb +31 -0
- data/lib/rr/adapters/rr_methods.rb +146 -0
- data/lib/rr/adapters/rspec.rb +61 -0
- data/lib/rr/adapters/rspec2.rb +22 -0
- data/lib/rr/adapters/test_unit.rb +31 -0
- data/lib/rr/blank_slate.rb +17 -0
- data/lib/rr/class_instance_method_defined.rb +9 -0
- data/lib/rr/double.rb +154 -0
- data/lib/rr/double_definitions/child_double_definition_create.rb +27 -0
- data/lib/rr/double_definitions/double_definition.rb +365 -0
- data/lib/rr/double_definitions/double_definition_create.rb +139 -0
- data/lib/rr/double_definitions/double_definition_create_blank_slate.rb +26 -0
- data/lib/rr/double_definitions/double_injections/any_instance_of.rb +28 -0
- data/lib/rr/double_definitions/double_injections/instance.rb +16 -0
- data/lib/rr/double_definitions/strategies/double_injection/any_instance_of.rb +30 -0
- data/lib/rr/double_definitions/strategies/double_injection/double_injection_strategy.rb +10 -0
- data/lib/rr/double_definitions/strategies/double_injection/instance.rb +17 -0
- data/lib/rr/double_definitions/strategies/implementation/implementation_strategy.rb +10 -0
- data/lib/rr/double_definitions/strategies/implementation/proxy.rb +60 -0
- data/lib/rr/double_definitions/strategies/implementation/reimplementation.rb +14 -0
- data/lib/rr/double_definitions/strategies/implementation/strongly_typed_reimplementation.rb +15 -0
- data/lib/rr/double_definitions/strategies/strategy.rb +43 -0
- data/lib/rr/double_definitions/strategies/strategy_methods.rb +53 -0
- data/lib/rr/double_definitions/strategies/verification/dont_allow.rb +31 -0
- data/lib/rr/double_definitions/strategies/verification/mock.rb +42 -0
- data/lib/rr/double_definitions/strategies/verification/stub.rb +43 -0
- data/lib/rr/double_definitions/strategies/verification/verification_strategy.rb +10 -0
- data/lib/rr/double_matches.rb +42 -0
- data/lib/rr/errors/argument_equality_error.rb +6 -0
- data/lib/rr/errors/double_definition_error.rb +6 -0
- data/lib/rr/errors/double_not_found_error.rb +6 -0
- data/lib/rr/errors/double_order_error.rb +6 -0
- data/lib/rr/errors/rr_error.rb +20 -0
- data/lib/rr/errors/spy_verification_errors/double_injection_not_found_error.rb +8 -0
- data/lib/rr/errors/spy_verification_errors/invocation_count_error.rb +8 -0
- data/lib/rr/errors/spy_verification_errors/spy_verification_error.rb +8 -0
- data/lib/rr/errors/subject_does_not_implement_method_error.rb +6 -0
- data/lib/rr/errors/subject_has_different_arity_error.rb +6 -0
- data/lib/rr/errors/times_called_error.rb +6 -0
- data/lib/rr/expectations/any_argument_expectation.rb +21 -0
- data/lib/rr/expectations/argument_equality_expectation.rb +41 -0
- data/lib/rr/expectations/times_called_expectation.rb +57 -0
- data/lib/rr/hash_with_object_id_key.rb +46 -0
- data/lib/rr/injections/double_injection.rb +220 -0
- data/lib/rr/injections/injection.rb +33 -0
- data/lib/rr/injections/method_missing_injection.rb +73 -0
- data/lib/rr/injections/singleton_method_added_injection.rb +72 -0
- data/lib/rr/method_dispatches/base_method_dispatch.rb +84 -0
- data/lib/rr/method_dispatches/method_dispatch.rb +59 -0
- data/lib/rr/method_dispatches/method_missing_dispatch.rb +61 -0
- data/lib/rr/proc_from_block.rb +7 -0
- data/lib/rr/recorded_calls.rb +103 -0
- data/lib/rr/space.rb +119 -0
- data/lib/rr/spy_verification.rb +48 -0
- data/lib/rr/spy_verification_proxy.rb +13 -0
- data/lib/rr/times_called_matchers/any_times_matcher.rb +18 -0
- data/lib/rr/times_called_matchers/at_least_matcher.rb +15 -0
- data/lib/rr/times_called_matchers/at_most_matcher.rb +23 -0
- data/lib/rr/times_called_matchers/integer_matcher.rb +19 -0
- data/lib/rr/times_called_matchers/never_matcher.rb +23 -0
- data/lib/rr/times_called_matchers/non_terminal.rb +27 -0
- data/lib/rr/times_called_matchers/proc_matcher.rb +11 -0
- data/lib/rr/times_called_matchers/range_matcher.rb +21 -0
- data/lib/rr/times_called_matchers/terminal.rb +20 -0
- data/lib/rr/times_called_matchers/times_called_matcher.rb +44 -0
- data/lib/rr/wildcard_matchers.rb +158 -0
- data/lib/rr/wildcard_matchers/anything.rb +18 -0
- data/lib/rr/wildcard_matchers/boolean.rb +23 -0
- data/lib/rr/wildcard_matchers/duck_type.rb +32 -0
- data/lib/rr/wildcard_matchers/hash_including.rb +29 -0
- data/lib/rr/wildcard_matchers/is_a.rb +25 -0
- data/lib/rr/wildcard_matchers/numeric.rb +13 -0
- data/lib/rr/wildcard_matchers/range.rb +7 -0
- data/lib/rr/wildcard_matchers/regexp.rb +7 -0
- data/lib/rr/wildcard_matchers/satisfy.rb +26 -0
- data/spec/api/any_instance_of/all_instances_of_spec.rb +12 -0
- data/spec/api/any_instance_of/any_instance_of_spec.rb +47 -0
- data/spec/api/any_instance_of/instance_of_spec.rb +12 -0
- data/spec/api/dont_allow/dont_allow_after_stub_spec.rb +14 -0
- data/spec/api/mock/mock_spec.rb +193 -0
- data/spec/api/proxy/proxy_spec.rb +86 -0
- data/spec/api/spy/spy_spec.rb +49 -0
- data/spec/api/strong/strong_spec.rb +87 -0
- data/spec/api/stub/stub_spec.rb +152 -0
- data/spec/core_spec_suite.rb +18 -0
- data/spec/environment_fixture_setup.rb +7 -0
- data/spec/minitest_spec_suite.rb +21 -0
- data/spec/proc_from_block_spec.rb +14 -0
- data/spec/rr/adapters/rr_methods_argument_matcher_spec.rb +67 -0
- data/spec/rr/adapters/rr_methods_creator_spec.rb +137 -0
- data/spec/rr/adapters/rr_methods_space_spec.rb +98 -0
- data/spec/rr/adapters/rr_methods_spec_helper.rb +7 -0
- data/spec/rr/adapters/rr_methods_times_matcher_spec.rb +13 -0
- data/spec/rr/double_definitions/child_double_definition_creator_spec.rb +112 -0
- data/spec/rr/double_definitions/double_definition_create_blank_slate_spec.rb +91 -0
- data/spec/rr/double_definitions/double_definition_create_spec.rb +443 -0
- data/spec/rr/double_injection/double_injection_spec.rb +546 -0
- data/spec/rr/double_injection/double_injection_verify_spec.rb +29 -0
- data/spec/rr/errors/rr_error_spec.rb +67 -0
- data/spec/rr/expectations/any_argument_expectation_spec.rb +47 -0
- data/spec/rr/expectations/anything_argument_equality_expectation_spec.rb +14 -0
- data/spec/rr/expectations/argument_equality_expectation_spec.rb +135 -0
- data/spec/rr/expectations/boolean_argument_equality_expectation_spec.rb +34 -0
- 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 +22 -0
- data/spec/rr/expectations/times_called_expectation/times_called_expectation_at_least_spec.rb +37 -0
- data/spec/rr/expectations/times_called_expectation/times_called_expectation_at_most_spec.rb +43 -0
- data/spec/rr/expectations/times_called_expectation/times_called_expectation_helper.rb +11 -0
- data/spec/rr/expectations/times_called_expectation/times_called_expectation_integer_spec.rb +58 -0
- data/spec/rr/expectations/times_called_expectation/times_called_expectation_proc_spec.rb +35 -0
- data/spec/rr/expectations/times_called_expectation/times_called_expectation_range_spec.rb +39 -0
- data/spec/rr/minitest/minitest_integration_test.rb +59 -0
- data/spec/rr/minitest/test_helper.rb +7 -0
- data/spec/rr/rspec/invocation_matcher_spec.rb +279 -0
- data/spec/rr/rspec/rspec_adapter_spec.rb +63 -0
- data/spec/rr/rspec/rspec_backtrace_tweaking_spec.rb +21 -0
- data/spec/rr/rspec/rspec_backtrace_tweaking_spec_fixture.rb +11 -0
- data/spec/rr/rspec/rspec_usage_spec.rb +86 -0
- data/spec/rr/space/hash_with_object_id_key_spec.rb +88 -0
- data/spec/rr/space/space_spec.rb +596 -0
- data/spec/rr/test_unit/test_helper.rb +7 -0
- data/spec/rr/test_unit/test_unit_backtrace_test.rb +36 -0
- data/spec/rr/test_unit/test_unit_integration_test.rb +59 -0
- data/spec/rr/times_called_matchers/any_times_matcher_spec.rb +47 -0
- data/spec/rr/times_called_matchers/at_least_matcher_spec.rb +55 -0
- data/spec/rr/times_called_matchers/at_most_matcher_spec.rb +70 -0
- data/spec/rr/times_called_matchers/integer_matcher_spec.rb +70 -0
- data/spec/rr/times_called_matchers/proc_matcher_spec.rb +55 -0
- data/spec/rr/times_called_matchers/range_matcher_spec.rb +76 -0
- data/spec/rr/times_called_matchers/times_called_matcher_spec.rb +118 -0
- 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/rspec_spec_suite.rb +16 -0
- data/spec/spec_helper.rb +40 -0
- data/spec/spec_suite.rb +50 -0
- data/spec/spy_verification_spec.rb +129 -0
- data/spec/test_unit_spec_suite.rb +20 -0
- metadata +220 -0
@@ -0,0 +1,17 @@
|
|
1
|
+
module RR
|
2
|
+
module BlankSlate
|
3
|
+
extend(Module.new do
|
4
|
+
def call(klass)
|
5
|
+
klass.instance_eval do
|
6
|
+
instance_methods.each do |unformatted_method_name|
|
7
|
+
method_name = unformatted_method_name.to_s
|
8
|
+
unless method_name =~ /^_/ || Space.blank_slate_whitelist.any? {|whitelisted_method_name| method_name == whitelisted_method_name}
|
9
|
+
alias_method "__blank_slated_#{method_name}", method_name
|
10
|
+
undef_method method_name
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end)
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
module RR
|
2
|
+
module ClassInstanceMethodDefined
|
3
|
+
def class_instance_method_defined(klass, instance_method, include_super=true)
|
4
|
+
klass.instance_methods(include_super).detect {|method_name| method_name.to_sym == instance_method.to_sym} ||
|
5
|
+
klass.protected_instance_methods(include_super).detect {|method_name| method_name.to_sym == instance_method.to_sym} ||
|
6
|
+
klass.private_instance_methods(include_super).detect {|method_name| method_name.to_sym == instance_method.to_sym}
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
data/lib/rr/double.rb
ADDED
@@ -0,0 +1,154 @@
|
|
1
|
+
module RR
|
2
|
+
# RR::Double is the use case for a method call.
|
3
|
+
# It has the ArgumentEqualityExpectation, TimesCalledExpectation,
|
4
|
+
# and the implementation.
|
5
|
+
class Double
|
6
|
+
extend(Module.new do
|
7
|
+
def formatted_name(method_name, args)
|
8
|
+
formatted_errors = args.collect {|arg| arg.inspect}.join(', ')
|
9
|
+
"#{method_name}(#{formatted_errors})"
|
10
|
+
end
|
11
|
+
|
12
|
+
def list_message_part(doubles)
|
13
|
+
doubles.collect do |double|
|
14
|
+
"- #{formatted_name(double.method_name, double.expected_arguments)}"
|
15
|
+
end.join("\n")
|
16
|
+
end
|
17
|
+
end)
|
18
|
+
|
19
|
+
attr_reader :times_called, :double_injection, :definition, :times_called_expectation
|
20
|
+
include Space::Reader
|
21
|
+
|
22
|
+
def initialize(double_injection, definition)
|
23
|
+
@double_injection = double_injection
|
24
|
+
@definition = definition
|
25
|
+
@times_called = 0
|
26
|
+
@times_called_expectation = Expectations::TimesCalledExpectation.new(self)
|
27
|
+
definition.double = self
|
28
|
+
verify_method_signature if definition.verify_method_signature?
|
29
|
+
double_injection.register_double self
|
30
|
+
end
|
31
|
+
|
32
|
+
# Double#exact_match? returns true when the passed in arguments
|
33
|
+
# exactly match the ArgumentEqualityExpectation arguments.
|
34
|
+
def exact_match?(*arguments)
|
35
|
+
definition.exact_match?(*arguments)
|
36
|
+
end
|
37
|
+
|
38
|
+
# Double#wildcard_match? returns true when the passed in arguments
|
39
|
+
# wildcard match the ArgumentEqualityExpectation arguments.
|
40
|
+
def wildcard_match?(*arguments)
|
41
|
+
definition.wildcard_match?(*arguments)
|
42
|
+
end
|
43
|
+
|
44
|
+
# Double#attempt? returns true when the
|
45
|
+
# TimesCalledExpectation is satisfied.
|
46
|
+
def attempt?
|
47
|
+
verify_times_matcher_is_set
|
48
|
+
times_called_expectation.attempt?
|
49
|
+
end
|
50
|
+
|
51
|
+
# Double#verify verifies the the TimesCalledExpectation
|
52
|
+
# is satisfied for this double. A TimesCalledError
|
53
|
+
# is raised if the TimesCalledExpectation is not met.
|
54
|
+
def verify
|
55
|
+
verify_times_matcher_is_set
|
56
|
+
times_called_expectation.verify!
|
57
|
+
true
|
58
|
+
end
|
59
|
+
|
60
|
+
def terminal?
|
61
|
+
verify_times_matcher_is_set
|
62
|
+
times_called_expectation.terminal?
|
63
|
+
end
|
64
|
+
|
65
|
+
# The method name that this Double is attatched to
|
66
|
+
def method_name
|
67
|
+
double_injection.method_name
|
68
|
+
end
|
69
|
+
|
70
|
+
# The Arguments that this Double expects
|
71
|
+
def expected_arguments
|
72
|
+
verify_argument_expectation_is_set
|
73
|
+
argument_expectation.expected_arguments
|
74
|
+
end
|
75
|
+
|
76
|
+
# The TimesCalledMatcher for the TimesCalledExpectation
|
77
|
+
def times_matcher
|
78
|
+
definition.times_matcher
|
79
|
+
end
|
80
|
+
|
81
|
+
def formatted_name
|
82
|
+
self.class.formatted_name(method_name, expected_arguments)
|
83
|
+
end
|
84
|
+
|
85
|
+
def method_call(args)
|
86
|
+
if verbose?
|
87
|
+
puts Double.formatted_name(method_name, args)
|
88
|
+
end
|
89
|
+
times_called_expectation.attempt if definition.times_matcher
|
90
|
+
space.verify_ordered_double(self) if ordered?
|
91
|
+
end
|
92
|
+
|
93
|
+
def implementation_is_original_method?
|
94
|
+
definition.implementation_is_original_method?
|
95
|
+
end
|
96
|
+
|
97
|
+
protected
|
98
|
+
def ordered?
|
99
|
+
definition.ordered?
|
100
|
+
end
|
101
|
+
|
102
|
+
def verbose?
|
103
|
+
definition.verbose?
|
104
|
+
end
|
105
|
+
|
106
|
+
def verify_times_matcher_is_set
|
107
|
+
unless definition.times_matcher
|
108
|
+
raise RR::Errors::DoubleDefinitionError, "#definition.times_matcher is not set"
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
def verify_argument_expectation_is_set
|
113
|
+
unless definition.argument_expectation
|
114
|
+
raise RR::Errors::DoubleDefinitionError, "#definition.argument_expectation is not set"
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
def verify_method_signature
|
119
|
+
unless double_injection.subject_has_original_method?
|
120
|
+
raise RR::Errors::SubjectDoesNotImplementMethodError
|
121
|
+
end
|
122
|
+
raise RR::Errors::SubjectHasDifferentArityError unless arity_matches?
|
123
|
+
end
|
124
|
+
|
125
|
+
def subject_arity
|
126
|
+
double_injection.original_method.arity
|
127
|
+
end
|
128
|
+
|
129
|
+
def subject_accepts_only_varargs?
|
130
|
+
subject_arity == -1
|
131
|
+
end
|
132
|
+
|
133
|
+
def subject_accepts_varargs?
|
134
|
+
subject_arity < 0
|
135
|
+
end
|
136
|
+
|
137
|
+
def arity_matches?
|
138
|
+
return true if subject_accepts_only_varargs?
|
139
|
+
if subject_accepts_varargs?
|
140
|
+
return ((subject_arity * -1) - 1) <= args.size
|
141
|
+
else
|
142
|
+
return subject_arity == args.size
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
def args
|
147
|
+
definition.argument_expectation.expected_arguments
|
148
|
+
end
|
149
|
+
|
150
|
+
def argument_expectation
|
151
|
+
definition.argument_expectation
|
152
|
+
end
|
153
|
+
end
|
154
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module RR
|
2
|
+
module DoubleDefinitions
|
3
|
+
class ChildDoubleDefinitionCreate < DoubleDefinitionCreate # :nodoc
|
4
|
+
attr_reader :parent_double_definition
|
5
|
+
def initialize(parent_double_definition)
|
6
|
+
@parent_double_definition = parent_double_definition
|
7
|
+
super()
|
8
|
+
end
|
9
|
+
|
10
|
+
def root_subject
|
11
|
+
parent_double_definition.root_subject
|
12
|
+
end
|
13
|
+
|
14
|
+
def instance_of(*args)
|
15
|
+
raise NoMethodError
|
16
|
+
end
|
17
|
+
|
18
|
+
protected
|
19
|
+
def add_strategy(subject, method_name, definition_eval_block, &block)
|
20
|
+
super do
|
21
|
+
block.call
|
22
|
+
parent_double_definition.implemented_by(lambda {|*args|subject})
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,365 @@
|
|
1
|
+
module RR
|
2
|
+
module DoubleDefinitions
|
3
|
+
class DoubleDefinition #:nodoc:
|
4
|
+
ORIGINAL_METHOD = Object.new
|
5
|
+
attr_accessor(
|
6
|
+
:argument_expectation,
|
7
|
+
:times_matcher,
|
8
|
+
:implementation,
|
9
|
+
:after_call_proc,
|
10
|
+
:yields_value,
|
11
|
+
:double,
|
12
|
+
:double_definition_create
|
13
|
+
)
|
14
|
+
|
15
|
+
include Space::Reader
|
16
|
+
|
17
|
+
def initialize(double_definition_create)
|
18
|
+
@implementation = nil
|
19
|
+
@argument_expectation = nil
|
20
|
+
@times_matcher = nil
|
21
|
+
@after_call_proc = nil
|
22
|
+
@yields_value = nil
|
23
|
+
@double_definition_create = double_definition_create
|
24
|
+
@ordered = false
|
25
|
+
@verbose = false
|
26
|
+
@verify_method_signature = false
|
27
|
+
end
|
28
|
+
|
29
|
+
def subject
|
30
|
+
double_definition_create.subject
|
31
|
+
end
|
32
|
+
|
33
|
+
def root_subject
|
34
|
+
double_definition_create.root_subject
|
35
|
+
end
|
36
|
+
|
37
|
+
module ArgumentDefinitionConstructionMethods
|
38
|
+
# Double#with sets the expectation that the Double will receive
|
39
|
+
# the passed in arguments.
|
40
|
+
#
|
41
|
+
# Passing in a block sets the return value.
|
42
|
+
#
|
43
|
+
# mock(subject).method_name.with(1, 2) {:return_value}
|
44
|
+
def with(*args, &return_value_block)
|
45
|
+
@argument_expectation = Expectations::ArgumentEqualityExpectation.new(*args)
|
46
|
+
install_method_callback return_value_block
|
47
|
+
self
|
48
|
+
end
|
49
|
+
|
50
|
+
# Double#with_any_args sets the expectation that the Double can receive
|
51
|
+
# any arguments.
|
52
|
+
#
|
53
|
+
# Passing in a block sets the return value.
|
54
|
+
#
|
55
|
+
# mock(subject).method_name.with_any_args {:return_value}
|
56
|
+
def with_any_args(&return_value_block)
|
57
|
+
@argument_expectation = Expectations::AnyArgumentExpectation.new
|
58
|
+
install_method_callback return_value_block
|
59
|
+
self
|
60
|
+
end
|
61
|
+
|
62
|
+
# Double#with_no_args sets the expectation that the Double will receive
|
63
|
+
# no arguments.
|
64
|
+
#
|
65
|
+
# Passing in a block sets the return value.
|
66
|
+
#
|
67
|
+
# mock(subject).method_name.with_no_args {:return_value}
|
68
|
+
def with_no_args(&return_value_block)
|
69
|
+
@argument_expectation = Expectations::ArgumentEqualityExpectation.new()
|
70
|
+
install_method_callback return_value_block
|
71
|
+
self
|
72
|
+
end
|
73
|
+
end
|
74
|
+
include ArgumentDefinitionConstructionMethods
|
75
|
+
|
76
|
+
module TimesDefinitionConstructionMethods
|
77
|
+
# Double#never sets the expectation that the Double will never be
|
78
|
+
# called.
|
79
|
+
#
|
80
|
+
# This method does not accept a block because it will never be called.
|
81
|
+
#
|
82
|
+
# mock(subject).method_name.never
|
83
|
+
def never
|
84
|
+
@times_matcher = TimesCalledMatchers::NeverMatcher.new
|
85
|
+
self
|
86
|
+
end
|
87
|
+
|
88
|
+
# Double#once sets the expectation that the Double will be called
|
89
|
+
# 1 time.
|
90
|
+
#
|
91
|
+
# Passing in a block sets the return value.
|
92
|
+
#
|
93
|
+
# mock(subject).method_name.once {:return_value}
|
94
|
+
def once(&return_value_block)
|
95
|
+
@times_matcher = TimesCalledMatchers::IntegerMatcher.new(1)
|
96
|
+
install_method_callback return_value_block
|
97
|
+
self
|
98
|
+
end
|
99
|
+
|
100
|
+
# Double#twice sets the expectation that the Double will be called
|
101
|
+
# 2 times.
|
102
|
+
#
|
103
|
+
# Passing in a block sets the return value.
|
104
|
+
#
|
105
|
+
# mock(subject).method_name.twice {:return_value}
|
106
|
+
def twice(&return_value_block)
|
107
|
+
@times_matcher = TimesCalledMatchers::IntegerMatcher.new(2)
|
108
|
+
install_method_callback return_value_block
|
109
|
+
self
|
110
|
+
end
|
111
|
+
|
112
|
+
# Double#at_least sets the expectation that the Double
|
113
|
+
# will be called at least n times.
|
114
|
+
# It works by creating a TimesCalledExpectation.
|
115
|
+
#
|
116
|
+
# Passing in a block sets the return value.
|
117
|
+
#
|
118
|
+
# mock(subject).method_name.at_least(4) {:return_value}
|
119
|
+
def at_least(number, &return_value_block)
|
120
|
+
@times_matcher = TimesCalledMatchers::AtLeastMatcher.new(number)
|
121
|
+
install_method_callback return_value_block
|
122
|
+
self
|
123
|
+
end
|
124
|
+
|
125
|
+
# Double#at_most allows sets the expectation that the Double
|
126
|
+
# will be called at most n times.
|
127
|
+
# It works by creating a TimesCalledExpectation.
|
128
|
+
#
|
129
|
+
# Passing in a block sets the return value.
|
130
|
+
#
|
131
|
+
# mock(subject).method_name.at_most(4) {:return_value}
|
132
|
+
def at_most(number, &return_value_block)
|
133
|
+
@times_matcher = TimesCalledMatchers::AtMostMatcher.new(number)
|
134
|
+
install_method_callback return_value_block
|
135
|
+
self
|
136
|
+
end
|
137
|
+
|
138
|
+
# Double#any_number_of_times sets an that the Double will be called
|
139
|
+
# any number of times. This effectively removes the times called expectation
|
140
|
+
# from the Doublen
|
141
|
+
#
|
142
|
+
# Passing in a block sets the return value.
|
143
|
+
#
|
144
|
+
# mock(subject).method_name.any_number_of_times
|
145
|
+
def any_number_of_times(&return_value_block)
|
146
|
+
@times_matcher = TimesCalledMatchers::AnyTimesMatcher.new
|
147
|
+
install_method_callback return_value_block
|
148
|
+
self
|
149
|
+
end
|
150
|
+
alias_method :any_times, :any_number_of_times
|
151
|
+
|
152
|
+
# Double#times creates an TimesCalledExpectation of the passed
|
153
|
+
# in number.
|
154
|
+
#
|
155
|
+
# Passing in a block sets the return value.
|
156
|
+
#
|
157
|
+
# mock(subject).method_name.times(4) {:return_value}
|
158
|
+
def times(matcher_value, &return_value_block)
|
159
|
+
@times_matcher = TimesCalledMatchers::TimesCalledMatcher.create(matcher_value)
|
160
|
+
install_method_callback return_value_block
|
161
|
+
self
|
162
|
+
end
|
163
|
+
end
|
164
|
+
include TimesDefinitionConstructionMethods
|
165
|
+
|
166
|
+
module DefinitionConstructionMethods
|
167
|
+
# Double#ordered sets the Double to have an ordered
|
168
|
+
# expectation.
|
169
|
+
#
|
170
|
+
# Passing in a block sets the return value.
|
171
|
+
#
|
172
|
+
# mock(subject).method_name.ordered {return_value}
|
173
|
+
def ordered(&return_value_block)
|
174
|
+
raise(
|
175
|
+
Errors::DoubleDefinitionError,
|
176
|
+
"Double Definitions must have a dedicated Double to be ordered. " <<
|
177
|
+
"For example, using instance_of does not allow ordered to be used. " <<
|
178
|
+
"proxy the class's #new method instead."
|
179
|
+
) unless @double
|
180
|
+
@ordered = true
|
181
|
+
space.register_ordered_double(@double)
|
182
|
+
install_method_callback return_value_block
|
183
|
+
DoubleDefinitionCreateBlankSlate.new(double_definition_create)
|
184
|
+
end
|
185
|
+
alias_method :then, :ordered
|
186
|
+
|
187
|
+
# Double#yields sets the Double to invoke a passed in block when
|
188
|
+
# the Double is called.
|
189
|
+
# An Expection will be raised if no block is passed in when the
|
190
|
+
# Double is called.
|
191
|
+
#
|
192
|
+
# Passing in a block sets the return value.
|
193
|
+
#
|
194
|
+
# mock(subject).method_name.yields(yield_arg1, yield_arg2) {return_value}
|
195
|
+
# subject.method_name {|yield_arg1, yield_arg2|}
|
196
|
+
def yields(*args, &return_value_block)
|
197
|
+
@yields_value = args
|
198
|
+
install_method_callback return_value_block
|
199
|
+
self
|
200
|
+
end
|
201
|
+
|
202
|
+
# Double#after_call creates a callback that occurs after call
|
203
|
+
# is called. The passed in block receives the return value of
|
204
|
+
# the Double being called.
|
205
|
+
# An Expection will be raised if no block is passed in.
|
206
|
+
#
|
207
|
+
# mock(subject).method_name {return_value}.after_call {|return_value|}
|
208
|
+
# subject.method_name # return_value
|
209
|
+
#
|
210
|
+
# This feature is built into proxies.
|
211
|
+
# mock.proxy(User).find('1') {|user| mock(user).valid? {false}}
|
212
|
+
def after_call(&after_call_proc)
|
213
|
+
raise ArgumentError, "after_call expects a block" unless after_call_proc
|
214
|
+
@after_call_proc = after_call_proc
|
215
|
+
self
|
216
|
+
end
|
217
|
+
|
218
|
+
# Double#verbose sets the Double to print out each method call it receives.
|
219
|
+
#
|
220
|
+
# Passing in a block sets the return value
|
221
|
+
def verbose(&after_call_proc)
|
222
|
+
@verbose = true
|
223
|
+
@after_call_proc = after_call_proc
|
224
|
+
self
|
225
|
+
end
|
226
|
+
|
227
|
+
# Double#returns accepts an argument value or a block.
|
228
|
+
# It will raise an ArgumentError if both are passed in.
|
229
|
+
#
|
230
|
+
# Passing in a block causes Double to return the return value of
|
231
|
+
# the passed in block.
|
232
|
+
#
|
233
|
+
# Passing in an argument causes Double to return the argument.
|
234
|
+
def returns(*args, &implementation)
|
235
|
+
if !args.empty? && implementation
|
236
|
+
raise ArgumentError, "returns cannot accept both an argument and a block"
|
237
|
+
end
|
238
|
+
if implementation
|
239
|
+
install_method_callback implementation
|
240
|
+
else
|
241
|
+
install_method_callback(lambda do |*lambda_args|
|
242
|
+
args.first
|
243
|
+
end)
|
244
|
+
end
|
245
|
+
self
|
246
|
+
end
|
247
|
+
|
248
|
+
def implemented_by_original_method
|
249
|
+
implemented_by ORIGINAL_METHOD
|
250
|
+
self
|
251
|
+
end
|
252
|
+
|
253
|
+
# Double#implemented_by sets the implementation of the Double.
|
254
|
+
# This method takes a Proc or a Method. Passing in a Method allows
|
255
|
+
# the Double to accept blocks.
|
256
|
+
#
|
257
|
+
# obj = Object.new
|
258
|
+
# def obj.foobar
|
259
|
+
# yield(1)
|
260
|
+
# end
|
261
|
+
# mock(obj).method_name.implemented_by(obj.method(:foobar))
|
262
|
+
def implemented_by(implementation)
|
263
|
+
@implementation = implementation
|
264
|
+
self
|
265
|
+
end
|
266
|
+
|
267
|
+
def verify_method_signature
|
268
|
+
@verify_method_signature = true
|
269
|
+
self
|
270
|
+
end
|
271
|
+
alias_method :strong, :verify_method_signature
|
272
|
+
|
273
|
+
protected
|
274
|
+
def install_method_callback(block)
|
275
|
+
if block
|
276
|
+
if implementation_is_original_method?
|
277
|
+
after_call(&block)
|
278
|
+
else
|
279
|
+
implemented_by block
|
280
|
+
end
|
281
|
+
end
|
282
|
+
end
|
283
|
+
end
|
284
|
+
include DefinitionConstructionMethods
|
285
|
+
|
286
|
+
module StateQueryMethods
|
287
|
+
# Double#ordered? returns true when the Double is ordered.
|
288
|
+
#
|
289
|
+
# mock(subject).method_name.ordered?
|
290
|
+
def ordered?
|
291
|
+
@ordered
|
292
|
+
end
|
293
|
+
|
294
|
+
# Double#verbose? returns true when verbose has been called on it. It returns
|
295
|
+
# true when the double is set to print each method call it receives.
|
296
|
+
def verbose?
|
297
|
+
@verbose ? true : false
|
298
|
+
end
|
299
|
+
|
300
|
+
def exact_match?(*arguments)
|
301
|
+
raise(Errors::DoubleDefinitionError, "#argument_expectation must be defined on #{inspect}") unless @argument_expectation
|
302
|
+
@argument_expectation.exact_match?(*arguments)
|
303
|
+
end
|
304
|
+
|
305
|
+
def wildcard_match?(*arguments)
|
306
|
+
raise(Errors::DoubleDefinitionError, "#argument_expectation must be defined on #{inspect}") unless @argument_expectation
|
307
|
+
@argument_expectation.wildcard_match?(*arguments)
|
308
|
+
end
|
309
|
+
|
310
|
+
def terminal?
|
311
|
+
raise(Errors::DoubleDefinitionError, "#argument_expectation must be defined on #{inspect}") unless @times_matcher
|
312
|
+
@times_matcher.terminal?
|
313
|
+
end
|
314
|
+
|
315
|
+
def expected_arguments
|
316
|
+
argument_expectation ? argument_expectation.expected_arguments : []
|
317
|
+
end
|
318
|
+
|
319
|
+
def implementation_is_original_method?
|
320
|
+
implementation_strategy.is_a?(Strategies::Implementation::Proxy)
|
321
|
+
end
|
322
|
+
|
323
|
+
def verify_method_signature?
|
324
|
+
!!@verify_method_signature
|
325
|
+
end
|
326
|
+
alias_method :strong?, :verify_method_signature?
|
327
|
+
|
328
|
+
protected
|
329
|
+
def implementation_strategy
|
330
|
+
double_definition_create.implementation_strategy
|
331
|
+
end
|
332
|
+
end
|
333
|
+
include StateQueryMethods
|
334
|
+
include ::RR::DoubleDefinitions::Strategies::StrategyMethods
|
335
|
+
|
336
|
+
def mock(subject=DoubleDefinitionCreate::NO_SUBJECT, method_name=nil, &definition_eval_block)
|
337
|
+
ChildDoubleDefinitionCreate.new(self).mock(subject, method_name, &definition_eval_block)
|
338
|
+
end
|
339
|
+
|
340
|
+
def stub(subject=DoubleDefinitionCreate::NO_SUBJECT, method_name=nil, &definition_eval_block)
|
341
|
+
ChildDoubleDefinitionCreate.new(self).stub(subject, method_name, &definition_eval_block)
|
342
|
+
end
|
343
|
+
|
344
|
+
def dont_allow(subject=DoubleDefinitionCreate::NO_SUBJECT, method_name=nil, &definition_eval_block)
|
345
|
+
ChildDoubleDefinitionCreate.new(self).dont_allow(subject, method_name, &definition_eval_block)
|
346
|
+
end
|
347
|
+
|
348
|
+
def proxy(subject=DoubleDefinitionCreate::NO_SUBJECT, method_name=nil, &definition_eval_block)
|
349
|
+
ChildDoubleDefinitionCreate.new(self).proxy(subject, method_name, &definition_eval_block)
|
350
|
+
end
|
351
|
+
|
352
|
+
def strong(subject=DoubleDefinitionCreate::NO_SUBJECT, method_name=nil, &definition_eval_block)
|
353
|
+
ChildDoubleDefinitionCreate.new(self).strong(subject, method_name, &definition_eval_block)
|
354
|
+
end
|
355
|
+
|
356
|
+
def any_instance_of(subject=DoubleDefinitionCreate::NO_SUBJECT, method_name=nil, &definition_eval_block)
|
357
|
+
ChildDoubleDefinitionCreate.new(self).any_instance_of(subject, method_name, &definition_eval_block)
|
358
|
+
end
|
359
|
+
|
360
|
+
def instance_of(subject=DoubleDefinitionCreate::NO_SUBJECT, method_name=nil, &definition_eval_block)
|
361
|
+
ChildDoubleDefinitionCreate.new(self).instance_of(subject, method_name, &definition_eval_block)
|
362
|
+
end
|
363
|
+
end
|
364
|
+
end
|
365
|
+
end
|