rspec-expectations 2.11.3 → 3.11.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (152) hide show
  1. checksums.yaml +7 -0
  2. checksums.yaml.gz.sig +0 -0
  3. data/.document +1 -1
  4. data/.yardopts +1 -1
  5. data/Changelog.md +1026 -21
  6. data/{License.txt → LICENSE.md} +5 -3
  7. data/README.md +174 -78
  8. data/lib/rspec/expectations/block_snippet_extractor.rb +253 -0
  9. data/lib/rspec/expectations/configuration.rb +230 -0
  10. data/lib/rspec/expectations/expectation_target.rb +130 -55
  11. data/lib/rspec/expectations/fail_with.rb +17 -33
  12. data/lib/rspec/expectations/failure_aggregator.rb +212 -0
  13. data/lib/rspec/expectations/handler.rb +163 -29
  14. data/lib/rspec/expectations/minitest_integration.rb +58 -0
  15. data/lib/rspec/expectations/syntax.rb +68 -54
  16. data/lib/rspec/expectations/version.rb +1 -1
  17. data/lib/rspec/expectations.rb +59 -24
  18. data/lib/rspec/matchers/aliased_matcher.rb +116 -0
  19. data/lib/rspec/matchers/built_in/all.rb +86 -0
  20. data/lib/rspec/matchers/built_in/base_matcher.rb +150 -20
  21. data/lib/rspec/matchers/built_in/be.rb +115 -109
  22. data/lib/rspec/matchers/built_in/be_between.rb +77 -0
  23. data/lib/rspec/matchers/built_in/be_instance_of.rb +16 -1
  24. data/lib/rspec/matchers/built_in/be_kind_of.rb +10 -1
  25. data/lib/rspec/matchers/built_in/be_within.rb +43 -17
  26. data/lib/rspec/matchers/built_in/change.rb +392 -75
  27. data/lib/rspec/matchers/built_in/compound.rb +290 -0
  28. data/lib/rspec/matchers/built_in/contain_exactly.rb +302 -0
  29. data/lib/rspec/matchers/built_in/count_expectation.rb +169 -0
  30. data/lib/rspec/matchers/built_in/cover.rb +3 -0
  31. data/lib/rspec/matchers/built_in/eq.rb +26 -8
  32. data/lib/rspec/matchers/built_in/eql.rb +19 -8
  33. data/lib/rspec/matchers/built_in/equal.rb +56 -19
  34. data/lib/rspec/matchers/built_in/exist.rb +74 -10
  35. data/lib/rspec/matchers/built_in/has.rb +141 -22
  36. data/lib/rspec/matchers/built_in/have_attributes.rb +114 -0
  37. data/lib/rspec/matchers/built_in/include.rb +175 -20
  38. data/lib/rspec/matchers/built_in/match.rb +95 -1
  39. data/lib/rspec/matchers/built_in/operators.rb +128 -0
  40. data/lib/rspec/matchers/built_in/output.rb +207 -0
  41. data/lib/rspec/matchers/built_in/raise_error.rb +212 -38
  42. data/lib/rspec/matchers/built_in/respond_to.rb +155 -29
  43. data/lib/rspec/matchers/built_in/satisfy.rb +39 -9
  44. data/lib/rspec/matchers/built_in/start_or_end_with.rb +94 -0
  45. data/lib/rspec/matchers/built_in/throw_symbol.rb +58 -14
  46. data/lib/rspec/matchers/built_in/yield.rb +252 -98
  47. data/lib/rspec/matchers/built_in.rb +47 -33
  48. data/lib/rspec/matchers/composable.rb +171 -0
  49. data/lib/rspec/matchers/dsl.rb +530 -10
  50. data/lib/rspec/matchers/english_phrasing.rb +58 -0
  51. data/lib/rspec/matchers/expecteds_for_multiple_diffs.rb +82 -0
  52. data/lib/rspec/matchers/fail_matchers.rb +42 -0
  53. data/lib/rspec/matchers/generated_descriptions.rb +15 -10
  54. data/lib/rspec/matchers/matcher_delegator.rb +35 -0
  55. data/lib/rspec/matchers/matcher_protocol.rb +105 -0
  56. data/lib/rspec/matchers.rb +604 -252
  57. data.tar.gz.sig +0 -0
  58. metadata +178 -278
  59. metadata.gz.sig +0 -0
  60. data/features/README.md +0 -49
  61. data/features/Upgrade.md +0 -53
  62. data/features/built_in_matchers/README.md +0 -90
  63. data/features/built_in_matchers/be.feature +0 -173
  64. data/features/built_in_matchers/be_within.feature +0 -46
  65. data/features/built_in_matchers/cover.feature +0 -45
  66. data/features/built_in_matchers/end_with.feature +0 -46
  67. data/features/built_in_matchers/equality.feature +0 -145
  68. data/features/built_in_matchers/exist.feature +0 -43
  69. data/features/built_in_matchers/expect_change.feature +0 -59
  70. data/features/built_in_matchers/expect_error.feature +0 -138
  71. data/features/built_in_matchers/have.feature +0 -103
  72. data/features/built_in_matchers/include.feature +0 -121
  73. data/features/built_in_matchers/match.feature +0 -50
  74. data/features/built_in_matchers/operators.feature +0 -221
  75. data/features/built_in_matchers/predicates.feature +0 -128
  76. data/features/built_in_matchers/respond_to.feature +0 -78
  77. data/features/built_in_matchers/satisfy.feature +0 -31
  78. data/features/built_in_matchers/start_with.feature +0 -46
  79. data/features/built_in_matchers/throw_symbol.feature +0 -85
  80. data/features/built_in_matchers/types.feature +0 -114
  81. data/features/built_in_matchers/yield.feature +0 -146
  82. data/features/custom_matchers/access_running_example.feature +0 -53
  83. data/features/custom_matchers/define_diffable_matcher.feature +0 -27
  84. data/features/custom_matchers/define_matcher.feature +0 -340
  85. data/features/custom_matchers/define_matcher_outside_rspec.feature +0 -38
  86. data/features/custom_matchers/define_matcher_with_fluent_interface.feature +0 -24
  87. data/features/customized_message.feature +0 -22
  88. data/features/diffing.feature +0 -85
  89. data/features/implicit_docstrings.feature +0 -52
  90. data/features/step_definitions/additional_cli_steps.rb +0 -22
  91. data/features/support/env.rb +0 -5
  92. data/features/syntax_configuration.feature +0 -68
  93. data/features/test_frameworks/test_unit.feature +0 -46
  94. data/lib/rspec/expectations/deprecation.rb +0 -38
  95. data/lib/rspec/expectations/differ.rb +0 -81
  96. data/lib/rspec/expectations/errors.rb +0 -9
  97. data/lib/rspec/expectations/extensions/array.rb +0 -9
  98. data/lib/rspec/expectations/extensions/object.rb +0 -39
  99. data/lib/rspec/expectations/extensions.rb +0 -2
  100. data/lib/rspec/matchers/be_close.rb +0 -9
  101. data/lib/rspec/matchers/built_in/have.rb +0 -108
  102. data/lib/rspec/matchers/built_in/match_array.rb +0 -45
  103. data/lib/rspec/matchers/built_in/start_and_end_with.rb +0 -48
  104. data/lib/rspec/matchers/compatibility.rb +0 -14
  105. data/lib/rspec/matchers/configuration.rb +0 -66
  106. data/lib/rspec/matchers/extensions/instance_eval_with_args.rb +0 -39
  107. data/lib/rspec/matchers/matcher.rb +0 -299
  108. data/lib/rspec/matchers/method_missing.rb +0 -12
  109. data/lib/rspec/matchers/operator_matcher.rb +0 -84
  110. data/lib/rspec/matchers/pretty.rb +0 -60
  111. data/lib/rspec-expectations.rb +0 -1
  112. data/spec/rspec/expectations/differ_spec.rb +0 -153
  113. data/spec/rspec/expectations/expectation_target_spec.rb +0 -65
  114. data/spec/rspec/expectations/extensions/kernel_spec.rb +0 -67
  115. data/spec/rspec/expectations/fail_with_spec.rb +0 -70
  116. data/spec/rspec/expectations/handler_spec.rb +0 -206
  117. data/spec/rspec/matchers/base_matcher_spec.rb +0 -60
  118. data/spec/rspec/matchers/be_close_spec.rb +0 -22
  119. data/spec/rspec/matchers/be_instance_of_spec.rb +0 -40
  120. data/spec/rspec/matchers/be_kind_of_spec.rb +0 -37
  121. data/spec/rspec/matchers/be_spec.rb +0 -452
  122. data/spec/rspec/matchers/be_within_spec.rb +0 -80
  123. data/spec/rspec/matchers/change_spec.rb +0 -528
  124. data/spec/rspec/matchers/configuration_spec.rb +0 -202
  125. data/spec/rspec/matchers/cover_spec.rb +0 -69
  126. data/spec/rspec/matchers/description_generation_spec.rb +0 -176
  127. data/spec/rspec/matchers/dsl_spec.rb +0 -57
  128. data/spec/rspec/matchers/eq_spec.rb +0 -54
  129. data/spec/rspec/matchers/eql_spec.rb +0 -41
  130. data/spec/rspec/matchers/equal_spec.rb +0 -60
  131. data/spec/rspec/matchers/exist_spec.rb +0 -110
  132. data/spec/rspec/matchers/has_spec.rb +0 -118
  133. data/spec/rspec/matchers/have_spec.rb +0 -461
  134. data/spec/rspec/matchers/include_spec.rb +0 -367
  135. data/spec/rspec/matchers/match_array_spec.rb +0 -124
  136. data/spec/rspec/matchers/match_spec.rb +0 -61
  137. data/spec/rspec/matchers/matcher_spec.rb +0 -434
  138. data/spec/rspec/matchers/matchers_spec.rb +0 -31
  139. data/spec/rspec/matchers/method_missing_spec.rb +0 -24
  140. data/spec/rspec/matchers/operator_matcher_spec.rb +0 -221
  141. data/spec/rspec/matchers/raise_error_spec.rb +0 -344
  142. data/spec/rspec/matchers/respond_to_spec.rb +0 -295
  143. data/spec/rspec/matchers/satisfy_spec.rb +0 -44
  144. data/spec/rspec/matchers/start_with_end_with_spec.rb +0 -182
  145. data/spec/rspec/matchers/throw_symbol_spec.rb +0 -116
  146. data/spec/rspec/matchers/yield_spec.rb +0 -402
  147. data/spec/spec_helper.rb +0 -27
  148. data/spec/support/classes.rb +0 -56
  149. data/spec/support/in_sub_process.rb +0 -31
  150. data/spec/support/matchers.rb +0 -22
  151. data/spec/support/ruby_version.rb +0 -10
  152. data/spec/support/shared_examples.rb +0 -13
@@ -1,24 +1,544 @@
1
+ RSpec::Support.require_rspec_support "with_keywords_when_needed"
2
+
1
3
  module RSpec
2
4
  module Matchers
5
+ # Defines the custom matcher DSL.
3
6
  module DSL
7
+ # Defines a matcher alias. The returned matcher's `description` will be overriden
8
+ # to reflect the phrasing of the new name, which will be used in failure messages
9
+ # when passed as an argument to another matcher in a composed matcher expression.
10
+ #
11
+ # @example
12
+ # RSpec::Matchers.alias_matcher :a_list_that_sums_to, :sum_to
13
+ # sum_to(3).description # => "sum to 3"
14
+ # a_list_that_sums_to(3).description # => "a list that sums to 3"
15
+ #
16
+ # @example
17
+ # RSpec::Matchers.alias_matcher :a_list_sorted_by, :be_sorted_by do |description|
18
+ # description.sub("be sorted by", "a list sorted by")
19
+ # end
20
+ #
21
+ # be_sorted_by(:age).description # => "be sorted by age"
22
+ # a_list_sorted_by(:age).description # => "a list sorted by age"
23
+ #
24
+ # @param new_name [Symbol] the new name for the matcher
25
+ # @param old_name [Symbol] the original name for the matcher
26
+ # @param options [Hash] options for the aliased matcher
27
+ # @option options [Class] :klass the ruby class to use as the decorator. (Not normally used).
28
+ # @yield [String] optional block that, when given, is used to define the overriden
29
+ # logic. The yielded arg is the original description or failure message. If no
30
+ # block is provided, a default override is used based on the old and new names.
31
+ # @see RSpec::Matchers
32
+ def alias_matcher(new_name, old_name, options={}, &description_override)
33
+ description_override ||= lambda do |old_desc|
34
+ old_desc.gsub(EnglishPhrasing.split_words(old_name), EnglishPhrasing.split_words(new_name))
35
+ end
36
+ klass = options.fetch(:klass) { AliasedMatcher }
37
+
38
+ define_method(new_name) do |*args, &block|
39
+ matcher = __send__(old_name, *args, &block)
40
+ matcher.matcher_name = new_name if matcher.respond_to?(:matcher_name=)
41
+ klass.new(matcher, description_override)
42
+ end
43
+ end
44
+
45
+ # Defines a negated matcher. The returned matcher's `description` and `failure_message`
46
+ # will be overriden to reflect the phrasing of the new name, and the match logic will
47
+ # be based on the original matcher but negated.
48
+ #
49
+ # @example
50
+ # RSpec::Matchers.define_negated_matcher :exclude, :include
51
+ # include(1, 2).description # => "include 1 and 2"
52
+ # exclude(1, 2).description # => "exclude 1 and 2"
53
+ #
54
+ # @param negated_name [Symbol] the name for the negated matcher
55
+ # @param base_name [Symbol] the name of the original matcher that will be negated
56
+ # @yield [String] optional block that, when given, is used to define the overriden
57
+ # logic. The yielded arg is the original description or failure message. If no
58
+ # block is provided, a default override is used based on the old and new names.
59
+ # @see RSpec::Matchers
60
+ def define_negated_matcher(negated_name, base_name, &description_override)
61
+ alias_matcher(negated_name, base_name, :klass => AliasedNegatedMatcher, &description_override)
62
+ end
63
+
4
64
  # Defines a custom matcher.
65
+ #
66
+ # @param name [Symbol] the name for the matcher
67
+ # @yield [Object] block that is used to define the matcher.
68
+ # The block is evaluated in the context of your custom matcher class.
69
+ # When args are passed to your matcher, they will be yielded here,
70
+ # usually representing the expected value(s).
5
71
  # @see RSpec::Matchers
6
72
  def define(name, &declarations)
7
- matcher_template = RSpec::Matchers::DSL::Matcher.new(name, &declarations)
8
- define_method name do |*expected|
9
- matcher = matcher_template.for_expected(*expected)
10
- matcher.matcher_execution_context = @matcher_execution_context ||= self
11
- matcher
73
+ warn_about_block_args(name, declarations)
74
+ define_method name do |*expected, &block_arg|
75
+ RSpec::Matchers::DSL::Matcher.new(name, declarations, self, *expected, &block_arg)
12
76
  end
13
77
  end
14
-
15
78
  alias_method :matcher, :define
16
79
 
17
- if RSpec.respond_to?(:configure)
18
- RSpec.configure {|c| c.extend self}
80
+ private
81
+
82
+ if Proc.method_defined?(:parameters)
83
+ def warn_about_block_args(name, declarations)
84
+ declarations.parameters.each do |type, arg_name|
85
+ next unless type == :block
86
+ RSpec.warning("Your `#{name}` custom matcher receives a block argument (`#{arg_name}`), " \
87
+ "but due to limitations in ruby, RSpec cannot provide the block. Instead, " \
88
+ "use the `block_arg` method to access the block")
89
+ end
90
+ end
91
+ else
92
+ # :nocov:
93
+ def warn_about_block_args(*)
94
+ # There's no way to detect block params on 1.8 since the method reflection APIs don't expose it
95
+ end
96
+ # :nocov:
97
+ end
98
+
99
+ RSpec.configure { |c| c.extend self } if RSpec.respond_to?(:configure)
100
+
101
+ # Contains the methods that are available from within the
102
+ # `RSpec::Matchers.define` DSL for creating custom matchers.
103
+ module Macros
104
+ # Stores the block that is used to determine whether this matcher passes
105
+ # or fails. The block should return a boolean value. When the matcher is
106
+ # passed to `expect(...).to` and the block returns `true`, then the expectation
107
+ # passes. Similarly, when the matcher is passed to `expect(...).not_to` and the
108
+ # block returns `false`, then the expectation passes.
109
+ #
110
+ # @example
111
+ #
112
+ # RSpec::Matchers.define :be_even do
113
+ # match do |actual|
114
+ # actual.even?
115
+ # end
116
+ # end
117
+ #
118
+ # expect(4).to be_even # passes
119
+ # expect(3).not_to be_even # passes
120
+ # expect(3).to be_even # fails
121
+ # expect(4).not_to be_even # fails
122
+ #
123
+ # By default the match block will swallow expectation errors (e.g.
124
+ # caused by using an expectation such as `expect(1).to eq 2`), if you
125
+ # with to allow these to bubble up, pass in the option
126
+ # `:notify_expectation_failures => true`.
127
+ #
128
+ # @param [Hash] options for defining the behavior of the match block.
129
+ # @yield [Object] actual the actual value (i.e. the value wrapped by `expect`)
130
+ def match(options={}, &match_block)
131
+ define_user_override(:matches?, match_block) do |actual|
132
+ @actual = actual
133
+ RSpec::Support.with_failure_notifier(RAISE_NOTIFIER) do
134
+ begin
135
+ super(*actual_arg_for(match_block))
136
+ rescue RSpec::Expectations::ExpectationNotMetError
137
+ raise if options[:notify_expectation_failures]
138
+ false
139
+ end
140
+ end
141
+ end
142
+ end
143
+
144
+ # @private
145
+ RAISE_NOTIFIER = Proc.new { |err, _opts| raise err }
146
+
147
+ # Use this to define the block for a negative expectation (`expect(...).not_to`)
148
+ # when the positive and negative forms require different handling. This
149
+ # is rarely necessary, but can be helpful, for example, when specifying
150
+ # asynchronous processes that require different timeouts.
151
+ #
152
+ # By default the match block will swallow expectation errors (e.g.
153
+ # caused by using an expectation such as `expect(1).to eq 2`), if you
154
+ # with to allow these to bubble up, pass in the option
155
+ # `:notify_expectation_failures => true`.
156
+ #
157
+ # @param [Hash] options for defining the behavior of the match block.
158
+ # @yield [Object] actual the actual value (i.e. the value wrapped by `expect`)
159
+ def match_when_negated(options={}, &match_block)
160
+ define_user_override(:does_not_match?, match_block) do |actual|
161
+ begin
162
+ @actual = actual
163
+ RSpec::Support.with_failure_notifier(RAISE_NOTIFIER) do
164
+ super(*actual_arg_for(match_block))
165
+ end
166
+ rescue RSpec::Expectations::ExpectationNotMetError
167
+ raise if options[:notify_expectation_failures]
168
+ false
169
+ end
170
+ end
171
+ end
172
+
173
+ # Use this instead of `match` when the block will raise an exception
174
+ # rather than returning false to indicate a failure.
175
+ #
176
+ # @example
177
+ #
178
+ # RSpec::Matchers.define :accept_as_valid do |candidate_address|
179
+ # match_unless_raises ValidationException do |validator|
180
+ # validator.validate(candidate_address)
181
+ # end
182
+ # end
183
+ #
184
+ # expect(email_validator).to accept_as_valid("person@company.com")
185
+ #
186
+ # @yield [Object] actual the actual object (i.e. the value wrapped by `expect`)
187
+ def match_unless_raises(expected_exception=Exception, &match_block)
188
+ define_user_override(:matches?, match_block) do |actual|
189
+ @actual = actual
190
+ begin
191
+ super(*actual_arg_for(match_block))
192
+ rescue expected_exception => @rescued_exception
193
+ false
194
+ else
195
+ true
196
+ end
197
+ end
198
+ end
199
+
200
+ # Customizes the failure messsage to use when this matcher is
201
+ # asked to positively match. Only use this when the message
202
+ # generated by default doesn't suit your needs.
203
+ #
204
+ # @example
205
+ #
206
+ # RSpec::Matchers.define :have_strength do |expected|
207
+ # match { your_match_logic }
208
+ #
209
+ # failure_message do |actual|
210
+ # "Expected strength of #{expected}, but had #{actual.strength}"
211
+ # end
212
+ # end
213
+ #
214
+ # @yield [Object] actual the actual object (i.e. the value wrapped by `expect`)
215
+ def failure_message(&definition)
216
+ define_user_override(__method__, definition)
217
+ end
218
+
219
+ # Customize the failure messsage to use when this matcher is asked
220
+ # to negatively match. Only use this when the message generated by
221
+ # default doesn't suit your needs.
222
+ #
223
+ # @example
224
+ #
225
+ # RSpec::Matchers.define :have_strength do |expected|
226
+ # match { your_match_logic }
227
+ #
228
+ # failure_message_when_negated do |actual|
229
+ # "Expected not to have strength of #{expected}, but did"
230
+ # end
231
+ # end
232
+ #
233
+ # @yield [Object] actual the actual object (i.e. the value wrapped by `expect`)
234
+ def failure_message_when_negated(&definition)
235
+ define_user_override(__method__, definition)
236
+ end
237
+
238
+ # Customize the description to use for one-liners. Only use this when
239
+ # the description generated by default doesn't suit your needs.
240
+ #
241
+ # @example
242
+ #
243
+ # RSpec::Matchers.define :qualify_for do |expected|
244
+ # match { your_match_logic }
245
+ #
246
+ # description do
247
+ # "qualify for #{expected}"
248
+ # end
249
+ # end
250
+ #
251
+ # @yield [Object] actual the actual object (i.e. the value wrapped by `expect`)
252
+ def description(&definition)
253
+ define_user_override(__method__, definition)
254
+ end
255
+
256
+ # Tells the matcher to diff the actual and expected values in the failure
257
+ # message.
258
+ def diffable
259
+ define_method(:diffable?) { true }
260
+ end
261
+
262
+ # Declares that the matcher can be used in a block expectation.
263
+ # Users will not be able to use your matcher in a block
264
+ # expectation without declaring this.
265
+ # (e.g. `expect { do_something }.to matcher`).
266
+ def supports_block_expectations
267
+ define_method(:supports_block_expectations?) { true }
268
+ end
269
+
270
+ # Convenience for defining methods on this matcher to create a fluent
271
+ # interface. The trick about fluent interfaces is that each method must
272
+ # return self in order to chain methods together. `chain` handles that
273
+ # for you. If the method is invoked and the
274
+ # `include_chain_clauses_in_custom_matcher_descriptions` config option
275
+ # hash been enabled, the chained method name and args will be added to the
276
+ # default description and failure message.
277
+ #
278
+ # In the common case where you just want the chained method to store some
279
+ # value(s) for later use (e.g. in `match`), you can provide one or more
280
+ # attribute names instead of a block; the chained method will store its
281
+ # arguments in instance variables with those names, and the values will
282
+ # be exposed via getters.
283
+ #
284
+ # @example
285
+ #
286
+ # RSpec::Matchers.define :have_errors_on do |key|
287
+ # chain :with do |message|
288
+ # @message = message
289
+ # end
290
+ #
291
+ # match do |actual|
292
+ # actual.errors[key] == @message
293
+ # end
294
+ # end
295
+ #
296
+ # expect(minor).to have_errors_on(:age).with("Not old enough to participate")
297
+ def chain(method_name, *attr_names, &definition)
298
+ unless block_given? ^ attr_names.any?
299
+ raise ArgumentError, "You must pass either a block or some attribute names (but not both) to `chain`."
300
+ end
301
+
302
+ definition = assign_attributes(attr_names) if attr_names.any?
303
+
304
+ define_user_override(method_name, definition) do |*args, &block|
305
+ super(*args, &block)
306
+ @chained_method_clauses.push([method_name, args])
307
+ self
308
+ end
309
+ end
310
+
311
+ def assign_attributes(attr_names)
312
+ attr_reader(*attr_names)
313
+ private(*attr_names)
314
+
315
+ lambda do |*attr_values|
316
+ attr_names.zip(attr_values) do |attr_name, attr_value|
317
+ instance_variable_set(:"@#{attr_name}", attr_value)
318
+ end
319
+ end
320
+ end
321
+
322
+ # assign_attributes isn't defined in the private section below because
323
+ # that makes MRI 1.9.2 emit a warning about private attributes.
324
+ private :assign_attributes
325
+
326
+ private
327
+
328
+ # Does the following:
329
+ #
330
+ # - Defines the named method using a user-provided block
331
+ # in @user_method_defs, which is included as an ancestor
332
+ # in the singleton class in which we eval the `define` block.
333
+ # - Defines an overriden definition for the same method
334
+ # usign the provided `our_def` block.
335
+ # - Provides a default `our_def` block for the common case
336
+ # of needing to call the user's definition with `@actual`
337
+ # as an arg, but only if their block's arity can handle it.
338
+ #
339
+ # This compiles the user block into an actual method, allowing
340
+ # them to use normal method constructs like `return`
341
+ # (e.g. for an early guard statement), while allowing us to define
342
+ # an override that can provide the wrapped handling
343
+ # (e.g. assigning `@actual`, rescueing errors, etc) and
344
+ # can `super` to the user's definition.
345
+ def define_user_override(method_name, user_def, &our_def)
346
+ @user_method_defs.__send__(:define_method, method_name, &user_def)
347
+ our_def ||= lambda { super(*actual_arg_for(user_def)) }
348
+ define_method(method_name, &our_def)
349
+ end
350
+
351
+ # Defines deprecated macro methods from RSpec 2 for backwards compatibility.
352
+ # @deprecated Use the methods from {Macros} instead.
353
+ module Deprecated
354
+ # @deprecated Use {Macros#match} instead.
355
+ def match_for_should(&definition)
356
+ RSpec.deprecate("`match_for_should`", :replacement => "`match`")
357
+ match(&definition)
358
+ end
359
+
360
+ # @deprecated Use {Macros#match_when_negated} instead.
361
+ def match_for_should_not(&definition)
362
+ RSpec.deprecate("`match_for_should_not`", :replacement => "`match_when_negated`")
363
+ match_when_negated(&definition)
364
+ end
365
+
366
+ # @deprecated Use {Macros#failure_message} instead.
367
+ def failure_message_for_should(&definition)
368
+ RSpec.deprecate("`failure_message_for_should`", :replacement => "`failure_message`")
369
+ failure_message(&definition)
370
+ end
371
+
372
+ # @deprecated Use {Macros#failure_message_when_negated} instead.
373
+ def failure_message_for_should_not(&definition)
374
+ RSpec.deprecate("`failure_message_for_should_not`", :replacement => "`failure_message_when_negated`")
375
+ failure_message_when_negated(&definition)
376
+ end
377
+ end
378
+ end
379
+
380
+ # Defines default implementations of the matcher
381
+ # protocol methods for custom matchers. You can
382
+ # override any of these using the {RSpec::Matchers::DSL::Macros Macros} methods
383
+ # from within an `RSpec::Matchers.define` block.
384
+ module DefaultImplementations
385
+ include BuiltIn::BaseMatcher::DefaultFailureMessages
386
+
387
+ # @api private
388
+ # Used internally by objects returns by `should` and `should_not`.
389
+ def diffable?
390
+ false
391
+ end
392
+
393
+ # The default description.
394
+ def description
395
+ english_name = EnglishPhrasing.split_words(name)
396
+ expected_list = EnglishPhrasing.list(expected)
397
+ "#{english_name}#{expected_list}#{chained_method_clause_sentences}"
398
+ end
399
+
400
+ # Matchers do not support block expectations by default. You
401
+ # must opt-in.
402
+ def supports_block_expectations?
403
+ false
404
+ end
405
+
406
+ def supports_value_expectations?
407
+ true
408
+ end
409
+
410
+ # Most matchers do not expect call stack jumps.
411
+ def expects_call_stack_jump?
412
+ false
413
+ end
414
+
415
+ private
416
+
417
+ def chained_method_clause_sentences
418
+ return '' unless Expectations.configuration.include_chain_clauses_in_custom_matcher_descriptions?
419
+
420
+ @chained_method_clauses.map do |(method_name, method_args)|
421
+ english_name = EnglishPhrasing.split_words(method_name)
422
+ arg_list = EnglishPhrasing.list(method_args)
423
+ " #{english_name}#{arg_list}"
424
+ end.join
425
+ end
426
+ end
427
+
428
+ # The class used for custom matchers. The block passed to
429
+ # `RSpec::Matchers.define` will be evaluated in the context
430
+ # of the singleton class of an instance, and will have the
431
+ # {RSpec::Matchers::DSL::Macros Macros} methods available.
432
+ class Matcher
433
+ # Provides default implementations for the matcher protocol methods.
434
+ include DefaultImplementations
435
+
436
+ # Allows expectation expressions to be used in the match block.
437
+ include RSpec::Matchers
438
+
439
+ # Supports the matcher composability features of RSpec 3+.
440
+ include Composable
441
+
442
+ # Makes the macro methods available to an `RSpec::Matchers.define` block.
443
+ extend Macros
444
+ extend Macros::Deprecated
445
+
446
+ # Exposes the value being matched against -- generally the object
447
+ # object wrapped by `expect`.
448
+ attr_reader :actual
449
+
450
+ # Exposes the exception raised during the matching by `match_unless_raises`.
451
+ # Could be useful to extract details for a failure message.
452
+ attr_reader :rescued_exception
453
+
454
+ # The block parameter used in the expectation
455
+ attr_reader :block_arg
456
+
457
+ # The name of the matcher.
458
+ attr_reader :name
459
+
460
+ # @api private
461
+ def initialize(name, declarations, matcher_execution_context, *expected, &block_arg)
462
+ @name = name
463
+ @actual = nil
464
+ @expected_as_array = expected
465
+ @matcher_execution_context = matcher_execution_context
466
+ @chained_method_clauses = []
467
+ @block_arg = block_arg
468
+
469
+ klass = class << self
470
+ # See `Macros#define_user_override` above, for an explanation.
471
+ include(@user_method_defs = Module.new)
472
+ self
473
+ end
474
+ RSpec::Support::WithKeywordsWhenNeeded.class_exec(klass, *expected, &declarations)
475
+ end
476
+
477
+ # Provides the expected value. This will return an array if
478
+ # multiple arguments were passed to the matcher; otherwise it
479
+ # will return a single value.
480
+ # @see #expected_as_array
481
+ def expected
482
+ if expected_as_array.size == 1
483
+ expected_as_array[0]
484
+ else
485
+ expected_as_array
486
+ end
487
+ end
488
+
489
+ # Returns the expected value as an an array. This exists primarily
490
+ # to aid in upgrading from RSpec 2.x, since in RSpec 2, `expected`
491
+ # always returned an array.
492
+ # @see #expected
493
+ attr_reader :expected_as_array
494
+
495
+ # Adds the name (rather than a cryptic hex number)
496
+ # so we can identify an instance of
497
+ # the matcher in error messages (e.g. for `NoMethodError`)
498
+ def inspect
499
+ "#<#{self.class.name} #{name}>"
500
+ end
501
+
502
+ if RUBY_VERSION.to_f >= 1.9
503
+ # Indicates that this matcher responds to messages
504
+ # from the `@matcher_execution_context` as well.
505
+ # Also, supports getting a method object for such methods.
506
+ def respond_to_missing?(method, include_private=false)
507
+ super || @matcher_execution_context.respond_to?(method, include_private)
508
+ end
509
+ else # for 1.8.7
510
+ # :nocov:
511
+ # Indicates that this matcher responds to messages
512
+ # from the `@matcher_execution_context` as well.
513
+ def respond_to?(method, include_private=false)
514
+ super || @matcher_execution_context.respond_to?(method, include_private)
515
+ end
516
+ # :nocov:
517
+ end
518
+
519
+ private
520
+
521
+ def actual_arg_for(block)
522
+ block.arity.zero? ? [] : [@actual]
523
+ end
524
+
525
+ # Takes care of forwarding unhandled messages to the
526
+ # `@matcher_execution_context` (typically the current
527
+ # running `RSpec::Core::Example`). This is needed by
528
+ # rspec-rails so that it can define matchers that wrap
529
+ # Rails' test helper methods, but it's also a useful
530
+ # feature in its own right.
531
+ def method_missing(method, *args, &block)
532
+ if @matcher_execution_context.respond_to?(method)
533
+ @matcher_execution_context.__send__ method, *args, &block
534
+ else
535
+ super(method, *args, &block)
536
+ end
537
+ end
538
+ # The method_missing method should be refactored to pass kw args in RSpec 4
539
+ # then this can be removed
540
+ ruby2_keywords :method_missing if respond_to?(:ruby2_keywords, true)
19
541
  end
20
542
  end
21
543
  end
22
544
  end
23
-
24
- RSpec::Matchers.extend RSpec::Matchers::DSL
@@ -0,0 +1,58 @@
1
+ module RSpec
2
+ module Matchers
3
+ # Facilitates converting ruby objects to English phrases.
4
+ module EnglishPhrasing
5
+ # Converts a symbol into an English expression.
6
+ #
7
+ # split_words(:banana_creme_pie) #=> "banana creme pie"
8
+ #
9
+ def self.split_words(sym)
10
+ sym.to_s.tr('_', ' ')
11
+ end
12
+
13
+ # @note The returned string has a leading space except
14
+ # when given an empty list.
15
+ #
16
+ # Converts an object (often a collection of objects)
17
+ # into an English list.
18
+ #
19
+ # list(['banana', 'kiwi', 'mango'])
20
+ # #=> " \"banana\", \"kiwi\", and \"mango\""
21
+ #
22
+ # Given an empty collection, returns the empty string.
23
+ #
24
+ # list([]) #=> ""
25
+ #
26
+ def self.list(obj)
27
+ return " #{RSpec::Support::ObjectFormatter.format(obj)}" if !obj || Struct === obj || Hash === obj
28
+ items = Array(obj).map { |w| RSpec::Support::ObjectFormatter.format(w) }
29
+ case items.length
30
+ when 0
31
+ ""
32
+ when 1
33
+ " #{items[0]}"
34
+ when 2
35
+ " #{items[0]} and #{items[1]}"
36
+ else
37
+ " #{items[0...-1].join(', ')}, and #{items[-1]}"
38
+ end
39
+ end
40
+
41
+ if RUBY_VERSION == '1.8.7'
42
+ # Not sure why, but on travis on 1.8.7 we have gotten these warnings:
43
+ # lib/rspec/matchers/english_phrasing.rb:28: warning: default `to_a' will be obsolete
44
+ # So it appears that `Array` can trigger that (e.g. by calling `to_a` on the passed object?)
45
+ # So here we replace `Kernel#Array` with our own warning-free implementation for 1.8.7.
46
+ # @private
47
+ # rubocop:disable Naming/MethodName
48
+ def self.Array(obj)
49
+ case obj
50
+ when Array then obj
51
+ else [obj]
52
+ end
53
+ end
54
+ # rubocop:enable Naming/MethodName
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,82 @@
1
+ module RSpec
2
+ module Matchers
3
+ # @api private
4
+ # Handles list of expected values when there is a need to render
5
+ # multiple diffs. Also can handle one value.
6
+ class ExpectedsForMultipleDiffs
7
+ # @private
8
+ # Default diff label when there is only one matcher in diff
9
+ # output
10
+ DEFAULT_DIFF_LABEL = "Diff:".freeze
11
+
12
+ # @private
13
+ # Maximum readable matcher description length
14
+ DESCRIPTION_MAX_LENGTH = 65
15
+
16
+ def initialize(expected_list)
17
+ @expected_list = expected_list
18
+ end
19
+
20
+ # @api private
21
+ # Wraps provided expected value in instance of
22
+ # ExpectedForMultipleDiffs. If provided value is already an
23
+ # ExpectedForMultipleDiffs then it just returns it.
24
+ # @param [Any] expected value to be wrapped
25
+ # @return [RSpec::Matchers::ExpectedsForMultipleDiffs]
26
+ def self.from(expected)
27
+ return expected if self === expected
28
+ new([[expected, DEFAULT_DIFF_LABEL]])
29
+ end
30
+
31
+ # @api private
32
+ # Wraps provided matcher list in instance of
33
+ # ExpectedForMultipleDiffs.
34
+ # @param [Array<Any>] matchers list of matchers to wrap
35
+ # @return [RSpec::Matchers::ExpectedsForMultipleDiffs]
36
+ def self.for_many_matchers(matchers)
37
+ new(matchers.map { |m| [m.expected, diff_label_for(m)] })
38
+ end
39
+
40
+ # @api private
41
+ # Returns message with diff(s) appended for provided differ
42
+ # factory and actual value if there are any
43
+ # @param [String] message original failure message
44
+ # @param [Proc] differ
45
+ # @param [Any] actual value
46
+ # @return [String]
47
+ def message_with_diff(message, differ, actual)
48
+ diff = diffs(differ, actual)
49
+ message = "#{message}\n#{diff}" unless diff.empty?
50
+ message
51
+ end
52
+
53
+ private
54
+
55
+ class << self
56
+ private
57
+
58
+ def diff_label_for(matcher)
59
+ "Diff for (#{truncated(RSpec::Support::ObjectFormatter.format(matcher))}):"
60
+ end
61
+
62
+ def truncated(description)
63
+ return description if description.length <= DESCRIPTION_MAX_LENGTH
64
+ description[0...DESCRIPTION_MAX_LENGTH - 3] << "..."
65
+ end
66
+ end
67
+
68
+ def diffs(differ, actual)
69
+ @expected_list.map do |(expected, diff_label)|
70
+ diff = differ.diff(actual, expected)
71
+ next if diff.strip.empty?
72
+ if diff == "\e[0m\n\e[0m"
73
+ "#{diff_label}\n" \
74
+ " <The diff is empty, are your objects producing identical `#inspect` output?>"
75
+ else
76
+ "#{diff_label}#{diff}"
77
+ end
78
+ end.compact.join("\n")
79
+ end
80
+ end
81
+ end
82
+ end