surrogate 0.4.3 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,229 +0,0 @@
1
- require 'erb'
2
-
3
-
4
- class Surrogate
5
- module RSpec
6
- module MessagesFor
7
-
8
- MESSAGES = {
9
- verb: {
10
- should: {
11
- default: "was never told to <%= subject %>",
12
- with: "should have been told to <%= subject %> with <%= inspect_arguments expected_arguments %>, but <%= actual_invocation %>",
13
- times: "should have been told to <%= subject %> <%= times_msg expected_times_invoked %> but was told to <%= subject %> <%= times_msg times_invoked %>",
14
- with_times: "should have been told to <%= subject %> <%= times_msg expected_times_invoked %> with <%= inspect_arguments expected_arguments %>, but <%= actual_invocation %>",
15
- },
16
- should_not: {
17
- default: "shouldn't have been told to <%= subject %>, but was told to <%= subject %> <%= times_msg times_invoked %>",
18
- with: "should not have been told to <%= subject %> with <%= inspect_arguments expected_arguments %>, but <%= actual_invocation %>",
19
- times: "shouldn't have been told to <%= subject %> <%= times_msg expected_times_invoked %>, but was",
20
- with_times: "should not have been told to <%= subject %> <%= times_msg expected_times_invoked %> with <%= inspect_arguments expected_arguments %>, but <%= actual_invocation %>",
21
- },
22
- other: {
23
- not_invoked: "was never told to",
24
- invoked_description: "got it",
25
- },
26
- },
27
- noun: {
28
- should: {
29
- default: "was never asked for its <%= subject %>",
30
- with: "should have been asked for its <%= subject %> with <%= inspect_arguments expected_arguments %>, but <%= actual_invocation %>",
31
- times: "should have been asked for its <%= subject %> <%= times_msg expected_times_invoked %>, but was asked <%= times_msg times_invoked %>",
32
- with_times: "should have been asked for its <%= subject %> <%= times_msg expected_times_invoked %> with <%= inspect_arguments expected_arguments %>, but <%= actual_invocation %>",
33
- },
34
- should_not: {
35
- default: "shouldn't have been asked for its <%= subject %>, but was asked <%= times_msg times_invoked %>",
36
- with: "should not have been asked for its <%= subject %> with <%= inspect_arguments expected_arguments %>, but <%= actual_invocation %>",
37
- times: "shouldn't have been asked for its <%= subject %> <%= times_msg expected_times_invoked %>, but was",
38
- with_times: "should not have been asked for its <%= subject %> <%= times_msg expected_times_invoked %> with <%= inspect_arguments expected_arguments %>, but <%= actual_invocation %>",
39
- },
40
- other: {
41
- not_invoked: "was never asked",
42
- invoked_description: "was asked",
43
- },
44
- },
45
- }
46
-
47
- def message_for(language_type, message_category, message_type, binding)
48
- message = MESSAGES[language_type][message_category].fetch(message_type)
49
- ERB.new(message).result(binding)
50
- end
51
-
52
- def inspect_arguments(arguments)
53
- inspected_arguments = arguments.map { |argument| inspect_argument argument }
54
- inspected_arguments << 'no args' if inspected_arguments.empty?
55
- "`" << inspected_arguments.join(", ") << "'"
56
- end
57
-
58
- def inspect_argument(to_inspect)
59
- if RSpec.rspec_mocks_loaded? && to_inspect.respond_to?(:description)
60
- to_inspect.description
61
- else
62
- to_inspect.inspect
63
- end
64
- end
65
-
66
- extend self
67
- end
68
-
69
-
70
- # sigh, surely there is a better name!
71
- class Handler < Struct.new(:subject, :language_type)
72
- attr_accessor :instance
73
-
74
- def message_for(message_category, message_type)
75
- MessagesFor.message_for(language_type, message_category, message_type, binding)
76
- end
77
-
78
- def inspect_arguments(args)
79
- MessagesFor.inspect_arguments args
80
- end
81
-
82
- def message_type
83
- :default
84
- end
85
-
86
- def invocations
87
- instance.invocations(subject)
88
- end
89
-
90
- def times_invoked
91
- invocations.size
92
- end
93
-
94
- def match?
95
- times_invoked > 0
96
- end
97
-
98
- def times_msg(n)
99
- "#{n} time#{'s' unless n == 1}"
100
- end
101
-
102
- def failure_message_for_should
103
- message_for :should, message_type
104
- end
105
-
106
- def failure_message_for_should_not
107
- message_for :should_not, message_type
108
- end
109
-
110
- def times(times_invoked)
111
- # is there a good way to remove these conditionals?
112
- extend (kind_of?(MatchWithArguments) ? MatchNumTimesWith : MatchNumTimes)
113
- self.expected_times_invoked = times_invoked
114
- self
115
- end
116
-
117
- def with(*arguments)
118
- extend (kind_of?(MatchNumTimes) ? MatchNumTimesWith : MatchWithArguments)
119
- self.expected_arguments = arguments
120
- self
121
- end
122
- end
123
-
124
-
125
- module ArgumentComparer
126
- def args_match?(actual_arguments)
127
- if RSpec.rspec_mocks_loaded?
128
- rspec_arg_expectation = ::RSpec::Mocks::ArgumentExpectation.new *expected_arguments
129
- rspec_arg_expectation.args_match? *actual_arguments
130
- else
131
- expected_arguments == actual_arguments
132
- end
133
- end
134
- end
135
-
136
- module MatchWithArguments
137
- include ArgumentComparer
138
-
139
- attr_accessor :expected_arguments
140
-
141
- def message_type
142
- :with
143
- end
144
-
145
- def match?
146
- invocations.any? { |invocation| args_match? invocation }
147
- end
148
-
149
- def actual_invocation
150
- return message_for :other, :not_invoked if times_invoked.zero?
151
- inspected_invocations = invocations.map { |invocation| inspect_arguments invocation }
152
- "got #{inspected_invocations.join ', '}"
153
- end
154
- end
155
-
156
-
157
- module MatchNumTimes
158
- def message_type
159
- :times
160
- end
161
-
162
- attr_accessor :expected_times_invoked
163
-
164
- def match?
165
- expected_times_invoked == times_invoked
166
- end
167
- end
168
-
169
-
170
- module MatchNumTimesWith
171
- include ArgumentComparer
172
-
173
- def message_type
174
- :with_times
175
- end
176
-
177
- attr_accessor :expected_times_invoked, :expected_arguments
178
-
179
- def times_invoked_with_expected_args
180
- invocations.select { |invocation| args_match? invocation }.size
181
- end
182
-
183
- def match?
184
- times_invoked_with_expected_args == expected_times_invoked
185
- end
186
-
187
- def actual_invocation
188
- return message_for :other, :not_invoked if times_invoked.zero?
189
- "#{message_for :other, :invoked_description} #{times_msg times_invoked_with_expected_args}"
190
- end
191
- end
192
-
193
-
194
- surrogate_matcher = lambda do |use_case, matcher, morphable=false|
195
- if morphable
196
- matcher.chain(:times) { |number| use_case.times number }
197
- matcher.chain(:with) { |*arguments| use_case.with *arguments }
198
- end
199
-
200
- matcher.match do |mocked_instance|
201
- use_case.instance = mocked_instance
202
- use_case.match?
203
- end
204
-
205
- matcher.failure_message_for_should { use_case.failure_message_for_should }
206
- matcher.failure_message_for_should_not { use_case.failure_message_for_should_not }
207
- end
208
-
209
-
210
- # have_been_told_to
211
- ::RSpec::Matchers.define :have_been_told_to do |verb|
212
- surrogate_matcher[Handler.new(verb, :verb), self, true]
213
- end
214
-
215
-
216
- # have_been_asked_for_its
217
- ::RSpec::Matchers.define :have_been_asked_for_its do |noun|
218
- surrogate_matcher[Handler.new(noun, :noun), self, true]
219
- end
220
-
221
-
222
- # have_been_initialized_with
223
- ::RSpec::Matchers.define :have_been_initialized_with do |*init_args|
224
- surrogate_matcher[Handler.new(:initialize, :verb).with(*init_args), self]
225
- end
226
- end
227
- end
228
-
229
-