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,23 @@
|
|
1
|
+
module RR
|
2
|
+
module TimesCalledMatchers #:nodoc:
|
3
|
+
class AtMostMatcher < TimesCalledMatcher
|
4
|
+
include Terminal
|
5
|
+
|
6
|
+
def possible_match?(times_called)
|
7
|
+
times_called <= @times
|
8
|
+
end
|
9
|
+
|
10
|
+
def matches?(times_called)
|
11
|
+
times_called <= @times
|
12
|
+
end
|
13
|
+
|
14
|
+
def attempt?(times_called)
|
15
|
+
times_called < @times
|
16
|
+
end
|
17
|
+
|
18
|
+
def expected_times_message
|
19
|
+
"at most #{@times.inspect} times"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module RR
|
2
|
+
module TimesCalledMatchers #:nodoc:
|
3
|
+
class IntegerMatcher < TimesCalledMatcher
|
4
|
+
include Terminal
|
5
|
+
|
6
|
+
def possible_match?(times_called)
|
7
|
+
times_called <= @times
|
8
|
+
end
|
9
|
+
|
10
|
+
def matches?(times_called)
|
11
|
+
times_called == @times
|
12
|
+
end
|
13
|
+
|
14
|
+
def attempt?(times_called)
|
15
|
+
times_called < @times
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module RR
|
2
|
+
module TimesCalledMatchers #:nodoc:
|
3
|
+
class NeverMatcher < TimesCalledMatcher
|
4
|
+
include Terminal
|
5
|
+
|
6
|
+
def initialize
|
7
|
+
super 0
|
8
|
+
end
|
9
|
+
|
10
|
+
def possible_match?(times_called)
|
11
|
+
true
|
12
|
+
end
|
13
|
+
|
14
|
+
def matches?(times_called)
|
15
|
+
true
|
16
|
+
end
|
17
|
+
|
18
|
+
def attempt?(times_called)
|
19
|
+
raise RR::Errors::TimesCalledError, error_message(1)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module RR
|
2
|
+
module TimesCalledMatchers
|
3
|
+
# Including this module marks the TimesCalledMatcher as NonTerminal.
|
4
|
+
# Being NonTerminal means the Double will not "terminate" even when
|
5
|
+
# called infinite times.
|
6
|
+
#
|
7
|
+
# The Double that uses a NonTerminal TimesCalledMatcher will
|
8
|
+
# continue using the Double when passed the matching arguments.
|
9
|
+
# This is done by the attempt? always returning true.
|
10
|
+
#
|
11
|
+
# This is in opposition to Terminal TimesCalledMatchers, where
|
12
|
+
# attempt? will eventually return false.
|
13
|
+
module NonTerminal #:nodoc:
|
14
|
+
def terminal?
|
15
|
+
false
|
16
|
+
end
|
17
|
+
|
18
|
+
def possible_match?(times_called)
|
19
|
+
true
|
20
|
+
end
|
21
|
+
|
22
|
+
def attempt?(times_called)
|
23
|
+
true
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module RR
|
2
|
+
module TimesCalledMatchers
|
3
|
+
class RangeMatcher < TimesCalledMatcher #:nodoc:
|
4
|
+
include Terminal
|
5
|
+
|
6
|
+
def possible_match?(times_called)
|
7
|
+
return true if times_called < @times.begin
|
8
|
+
return true if @times.include?(times_called)
|
9
|
+
return false
|
10
|
+
end
|
11
|
+
|
12
|
+
def matches?(times_called)
|
13
|
+
@times.include?(times_called)
|
14
|
+
end
|
15
|
+
|
16
|
+
def attempt?(times_called)
|
17
|
+
possible_match?(times_called)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module RR
|
2
|
+
module TimesCalledMatchers
|
3
|
+
# Including this module marks the TimesCalledMatcher as Terminal.
|
4
|
+
# Being Terminal the Double will "terminate" when times called is
|
5
|
+
# finite.
|
6
|
+
#
|
7
|
+
# The Double that uses a Terminal TimesCalledMatcher will
|
8
|
+
# eventually be passed over to the next Double when passed
|
9
|
+
# the matching arguments enough times. This is done by the attempt?
|
10
|
+
# method returning false when executed a finite number of times.
|
11
|
+
#
|
12
|
+
# This is in opposition to NonTerminal TimesCalledMatchers, where
|
13
|
+
# attempt? will always return true.
|
14
|
+
module Terminal #:nodoc:
|
15
|
+
def terminal?
|
16
|
+
true
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module RR
|
2
|
+
module TimesCalledMatchers
|
3
|
+
class TimesCalledMatcher #:nodoc:
|
4
|
+
extend(Module.new do
|
5
|
+
def create(value)
|
6
|
+
return value if value.is_a?(TimesCalledMatcher)
|
7
|
+
return IntegerMatcher.new(value) if value.is_a?(Integer)
|
8
|
+
return RangeMatcher.new(value) if value.is_a?(Range )
|
9
|
+
return ProcMatcher.new(value) if value.is_a?(Proc)
|
10
|
+
raise ArgumentError, "There is no TimesCalledMatcher for #{value.inspect}."
|
11
|
+
end
|
12
|
+
end)
|
13
|
+
|
14
|
+
attr_reader :times
|
15
|
+
|
16
|
+
def initialize(times)
|
17
|
+
@times = times
|
18
|
+
end
|
19
|
+
|
20
|
+
def matches?(times_called)
|
21
|
+
end
|
22
|
+
|
23
|
+
def attempt?(times_called)
|
24
|
+
end
|
25
|
+
|
26
|
+
def error_message(times_called)
|
27
|
+
"Called #{times_called.inspect} #{pluralized_time(times_called)}.\nExpected #{expected_times_message}."
|
28
|
+
end
|
29
|
+
|
30
|
+
def ==(other)
|
31
|
+
self.class == other.class && self.times == other.times
|
32
|
+
end
|
33
|
+
|
34
|
+
def expected_times_message
|
35
|
+
"#{@times.inspect} times"
|
36
|
+
end
|
37
|
+
|
38
|
+
protected
|
39
|
+
def pluralized_time(times_called)
|
40
|
+
(times_called == 1) ? "time" : "times"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,158 @@
|
|
1
|
+
=begin rdoc
|
2
|
+
|
3
|
+
= Writing your own custom wildcard matchers.
|
4
|
+
Writing new wildcard matchers is not too difficult. If you've ever written
|
5
|
+
a custom expectation in RSpec, the implementation is very similar.
|
6
|
+
|
7
|
+
As an example, let's say that you want a matcher that will match any number
|
8
|
+
divisible by a certain integer. In use, it might look like this:
|
9
|
+
|
10
|
+
# Will pass if BananaGrabber#bunch_bananas is called with an integer
|
11
|
+
# divisible by 5.
|
12
|
+
|
13
|
+
mock(BananaGrabber).bunch_bananas(divisible_by(5))
|
14
|
+
|
15
|
+
To implement this, we need a class RR::WildcardMatchers::DivisibleBy with
|
16
|
+
these instance methods:
|
17
|
+
|
18
|
+
* ==(other)
|
19
|
+
* eql?(other) (usually aliased to #==)
|
20
|
+
* inspect
|
21
|
+
* wildcard_match?(other)
|
22
|
+
|
23
|
+
and optionally, a sensible initialize method. Let's look at each of these.
|
24
|
+
|
25
|
+
=== .initialize
|
26
|
+
|
27
|
+
Most custom wildcard matchers will want to define initialize to store
|
28
|
+
some information about just what should be matched. DivisibleBy#initialize
|
29
|
+
might look like this:
|
30
|
+
|
31
|
+
class RR::WildcardMatchers::DivisibleBy
|
32
|
+
def initialize(divisor)
|
33
|
+
@expected_divisor = divisor
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
=== #==(other)
|
38
|
+
DivisibleBy#==(other) should return true if other is a wildcard matcher that
|
39
|
+
matches the same things as self, so a natural way to write DivisibleBy#== is:
|
40
|
+
|
41
|
+
|
42
|
+
class RR::WildcardMatchers::DivisibleBy
|
43
|
+
def ==(other)
|
44
|
+
# Ensure that other is actually a DivisibleBy
|
45
|
+
return false unless other.is_a?(self.class)
|
46
|
+
|
47
|
+
# Does other expect to match the same divisor we do?
|
48
|
+
self.expected_divisor = other.expected_divisor
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
Note that this implementation of #== assumes that we've also declared
|
53
|
+
attr_reader :expected_divisor
|
54
|
+
|
55
|
+
=== #inspect
|
56
|
+
|
57
|
+
Technically we don't have to declare DivisibleBy#inspect, since inspect is
|
58
|
+
defined for every object already. But putting a helpful message in inspect
|
59
|
+
will make test failures much clearer, and it only takes about two seconds to
|
60
|
+
write it, so let's be nice and do so:
|
61
|
+
|
62
|
+
class RR::WildcardMatchers::DivisibleBy
|
63
|
+
def inspect
|
64
|
+
"integer divisible by #{expected.divisor}"
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
Now if we run the example from above:
|
69
|
+
|
70
|
+
mock(BananaGrabber).bunch_bananas(divisible_by(5))
|
71
|
+
|
72
|
+
and it fails, we get a helpful message saying
|
73
|
+
|
74
|
+
bunch_bananas(integer divisible by 5)
|
75
|
+
Called 0 times.
|
76
|
+
Expected 1 times.
|
77
|
+
|
78
|
+
=== #wildcard_matches?(other)
|
79
|
+
|
80
|
+
wildcard_matches? is the method that actually checks the argument against the
|
81
|
+
expectation. It should return true if other is considered to match,
|
82
|
+
false otherwise. In the case of DivisibleBy, wildcard_matches? reads:
|
83
|
+
|
84
|
+
class RR::WildcardMatchers::DivisibleBy
|
85
|
+
def wildcard_matches?(other)
|
86
|
+
# If other isn't a number, how can it be divisible by anything?
|
87
|
+
return false unless other.is_a?(Numeric)
|
88
|
+
|
89
|
+
# If other is in fact divisible by expected_divisor, then
|
90
|
+
# other modulo expected_divisor should be 0.
|
91
|
+
|
92
|
+
other % expected_divisor == 0
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
=== A finishing touch: wrapping it neatly
|
97
|
+
|
98
|
+
We could stop here if we were willing to resign ourselves to using
|
99
|
+
DivisibleBy this way:
|
100
|
+
|
101
|
+
mock(BananaGrabber).bunch_bananas(DivisibleBy.new(5))
|
102
|
+
|
103
|
+
But that's less expressive than the original:
|
104
|
+
|
105
|
+
mock(BananaGrabber).bunch_bananas(divisible_by(5))
|
106
|
+
|
107
|
+
To be able to use the convenient divisible_by matcher rather than the uglier
|
108
|
+
DivisibleBy.new version, re-open the module RR::Adapters::RRMethods and
|
109
|
+
define divisible_by there as a simple wrapper around DivisibleBy.new:
|
110
|
+
|
111
|
+
module RR::Adapters::RRMethods
|
112
|
+
def divisible_by(expected_divisor)
|
113
|
+
RR::WildcardMatchers::DivisibleBy.new(expected_divisor)
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
== Recap
|
118
|
+
|
119
|
+
Here's all the code for DivisibleBy in one place for easy reference:
|
120
|
+
|
121
|
+
class RR::WildcardMatchers::DivisibleBy
|
122
|
+
def initialize(divisor)
|
123
|
+
@expected_divisor = divisor
|
124
|
+
end
|
125
|
+
|
126
|
+
def ==(other)
|
127
|
+
# Ensure that other is actually a DivisibleBy
|
128
|
+
return false unless other.is_a?(self.class)
|
129
|
+
|
130
|
+
# Does other expect to match the same divisor we do?
|
131
|
+
self.expected_divisor = other.expected_divisor
|
132
|
+
end
|
133
|
+
|
134
|
+
def inspect
|
135
|
+
"integer divisible by #{expected.divisor}"
|
136
|
+
end
|
137
|
+
|
138
|
+
def wildcard_matches?(other)
|
139
|
+
# If other isn't a number, how can it be divisible by anything?
|
140
|
+
return false unless other.is_a?(Numeric)
|
141
|
+
|
142
|
+
# If other is in fact divisible by expected_divisor, then
|
143
|
+
# other modulo expected_divisor should be 0.
|
144
|
+
|
145
|
+
other % expected_divisor == 0
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
module RR::Adapters::RRMethods
|
150
|
+
def divisible_by(expected_divisor)
|
151
|
+
RR::WildcardMatchers::DivisibleBy.new(expected_divisor)
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
=end
|
156
|
+
|
157
|
+
module RR::WildcardMatchers
|
158
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module RR
|
2
|
+
module WildcardMatchers
|
3
|
+
class Boolean
|
4
|
+
def wildcard_match?(other)
|
5
|
+
self == other || is_a_boolean?(other)
|
6
|
+
end
|
7
|
+
|
8
|
+
def ==(other)
|
9
|
+
other.is_a?(self.class)
|
10
|
+
end
|
11
|
+
alias_method :eql?, :==
|
12
|
+
|
13
|
+
def inspect
|
14
|
+
'boolean'
|
15
|
+
end
|
16
|
+
|
17
|
+
protected
|
18
|
+
def is_a_boolean?(subject)
|
19
|
+
subject.is_a?(TrueClass) || subject.is_a?(FalseClass)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module RR
|
2
|
+
module WildcardMatchers
|
3
|
+
class DuckType
|
4
|
+
attr_accessor :required_methods
|
5
|
+
|
6
|
+
def initialize(*required_methods)
|
7
|
+
@required_methods = required_methods
|
8
|
+
end
|
9
|
+
|
10
|
+
def wildcard_match?(other)
|
11
|
+
return true if self == other
|
12
|
+
required_methods.each do |m|
|
13
|
+
return false unless other.respond_to?(m)
|
14
|
+
end
|
15
|
+
return true
|
16
|
+
end
|
17
|
+
|
18
|
+
def inspect
|
19
|
+
formatted_required_methods = required_methods.collect do |method_name|
|
20
|
+
method_name.inspect
|
21
|
+
end.join(', ')
|
22
|
+
"duck_type(#{formatted_required_methods})"
|
23
|
+
end
|
24
|
+
|
25
|
+
def ==(other)
|
26
|
+
return false unless other.is_a?(self.class)
|
27
|
+
self.required_methods == other.required_methods
|
28
|
+
end
|
29
|
+
alias_method :eql?, :==
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module RR
|
2
|
+
module WildcardMatchers
|
3
|
+
class HashIncluding
|
4
|
+
attr_reader :expected_hash
|
5
|
+
|
6
|
+
def initialize(expected_hash)
|
7
|
+
@expected_hash = expected_hash.clone
|
8
|
+
end
|
9
|
+
|
10
|
+
def wildcard_match?(other)
|
11
|
+
return true if self == other
|
12
|
+
expected_hash.each_pair do |key, value|
|
13
|
+
return false unless other.has_key?(key) && other[key] == expected_hash[key]
|
14
|
+
end
|
15
|
+
return true
|
16
|
+
end
|
17
|
+
|
18
|
+
def inspect
|
19
|
+
"hash_including(#{expected_hash.inspect})"
|
20
|
+
end
|
21
|
+
|
22
|
+
def ==(other)
|
23
|
+
return false unless other.is_a?(self.class)
|
24
|
+
self.expected_hash == other.expected_hash
|
25
|
+
end
|
26
|
+
alias_method :eql?, :==
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|