opal-rspec 0.4.0.beta3 → 0.4.0.beta4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (161) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +0 -1
  3. data/.gitmodules +15 -0
  4. data/.travis.yml +12 -0
  5. data/.yardopts +5 -0
  6. data/CHANGELOG.md +3 -1
  7. data/Gemfile +6 -7
  8. data/README.md +2 -0
  9. data/Rakefile +12 -50
  10. data/lib/opal/rspec/version.rb +1 -1
  11. data/lib/opal/rspec.rb +14 -0
  12. data/opal/opal/rspec/async.rb +146 -11
  13. data/opal/opal/rspec/fixes.rb +18 -8
  14. data/opal/opal/rspec/requires.rb +45 -0
  15. data/opal/opal/rspec.rb +1 -24
  16. data/opal-rspec.gemspec +1 -1
  17. data/spec/async_spec.rb +4 -5
  18. data/spec/matchers_spec.rb +20 -0
  19. data/spec/named_subject_spec.rb +11 -0
  20. data/spec/should_syntax_spec.rb +17 -0
  21. data/vendor_lib/rspec/autorun.rb +2 -0
  22. data/vendor_lib/rspec/core/backport_random.rb +302 -0
  23. data/vendor_lib/rspec/core/backtrace_formatter.rb +65 -0
  24. data/vendor_lib/rspec/core/command_line.rb +36 -0
  25. data/vendor_lib/rspec/core/configuration.rb +1129 -0
  26. data/vendor_lib/rspec/core/configuration_options.rb +143 -0
  27. data/vendor_lib/rspec/core/drb_command_line.rb +26 -0
  28. data/vendor_lib/rspec/core/drb_options.rb +87 -0
  29. data/vendor_lib/rspec/core/dsl.rb +26 -0
  30. data/vendor_lib/rspec/core/example.rb +312 -0
  31. data/vendor_lib/rspec/core/example_group.rb +540 -0
  32. data/vendor_lib/rspec/core/filter_manager.rb +224 -0
  33. data/vendor_lib/rspec/core/flat_map.rb +17 -0
  34. data/vendor_lib/rspec/core/formatters/base_formatter.rb +291 -0
  35. data/vendor_lib/rspec/core/formatters/base_text_formatter.rb +307 -0
  36. data/vendor_lib/rspec/core/formatters/deprecation_formatter.rb +193 -0
  37. data/vendor_lib/rspec/core/formatters/documentation_formatter.rb +67 -0
  38. data/vendor_lib/rspec/core/formatters/helpers.rb +82 -0
  39. data/vendor_lib/rspec/core/formatters/html_formatter.rb +155 -0
  40. data/vendor_lib/rspec/core/formatters/html_printer.rb +408 -0
  41. data/vendor_lib/rspec/core/formatters/json_formatter.rb +99 -0
  42. data/vendor_lib/rspec/core/formatters/progress_formatter.rb +32 -0
  43. data/vendor_lib/rspec/core/formatters/snippet_extractor.rb +101 -0
  44. data/vendor_lib/rspec/core/formatters.rb +54 -0
  45. data/vendor_lib/rspec/core/hooks.rb +535 -0
  46. data/vendor_lib/rspec/core/memoized_helpers.rb +431 -0
  47. data/vendor_lib/rspec/core/metadata.rb +313 -0
  48. data/vendor_lib/rspec/core/mocking/with_absolutely_nothing.rb +11 -0
  49. data/vendor_lib/rspec/core/mocking/with_flexmock.rb +27 -0
  50. data/vendor_lib/rspec/core/mocking/with_mocha.rb +52 -0
  51. data/vendor_lib/rspec/core/mocking/with_rr.rb +27 -0
  52. data/vendor_lib/rspec/core/mocking/with_rspec.rb +27 -0
  53. data/vendor_lib/rspec/core/option_parser.rb +234 -0
  54. data/vendor_lib/rspec/core/ordering.rb +154 -0
  55. data/vendor_lib/rspec/core/pending.rb +110 -0
  56. data/vendor_lib/rspec/core/project_initializer.rb +88 -0
  57. data/vendor_lib/rspec/core/rake_task.rb +128 -0
  58. data/vendor_lib/rspec/core/reporter.rb +132 -0
  59. data/vendor_lib/rspec/core/ruby_project.rb +44 -0
  60. data/vendor_lib/rspec/core/runner.rb +97 -0
  61. data/vendor_lib/rspec/core/shared_context.rb +53 -0
  62. data/vendor_lib/rspec/core/shared_example_group/collection.rb +27 -0
  63. data/vendor_lib/rspec/core/shared_example_group.rb +146 -0
  64. data/vendor_lib/rspec/core/version.rb +7 -0
  65. data/vendor_lib/rspec/core/warnings.rb +22 -0
  66. data/vendor_lib/rspec/core/world.rb +131 -0
  67. data/vendor_lib/rspec/core.rb +203 -0
  68. data/vendor_lib/rspec/expectations/differ.rb +154 -0
  69. data/vendor_lib/rspec/expectations/errors.rb +9 -0
  70. data/vendor_lib/rspec/expectations/expectation_target.rb +87 -0
  71. data/vendor_lib/rspec/expectations/extensions/object.rb +29 -0
  72. data/vendor_lib/rspec/expectations/extensions.rb +1 -0
  73. data/vendor_lib/rspec/expectations/fail_with.rb +79 -0
  74. data/vendor_lib/rspec/expectations/handler.rb +68 -0
  75. data/vendor_lib/rspec/expectations/syntax.rb +182 -0
  76. data/vendor_lib/rspec/expectations/version.rb +8 -0
  77. data/vendor_lib/rspec/expectations.rb +75 -0
  78. data/vendor_lib/rspec/matchers/built_in/base_matcher.rb +68 -0
  79. data/vendor_lib/rspec/matchers/built_in/be.rb +213 -0
  80. data/vendor_lib/rspec/matchers/built_in/be_instance_of.rb +15 -0
  81. data/vendor_lib/rspec/matchers/built_in/be_kind_of.rb +11 -0
  82. data/vendor_lib/rspec/matchers/built_in/be_within.rb +55 -0
  83. data/vendor_lib/rspec/matchers/built_in/change.rb +141 -0
  84. data/vendor_lib/rspec/matchers/built_in/cover.rb +21 -0
  85. data/vendor_lib/rspec/matchers/built_in/eq.rb +22 -0
  86. data/vendor_lib/rspec/matchers/built_in/eql.rb +23 -0
  87. data/vendor_lib/rspec/matchers/built_in/equal.rb +48 -0
  88. data/vendor_lib/rspec/matchers/built_in/exist.rb +26 -0
  89. data/vendor_lib/rspec/matchers/built_in/has.rb +48 -0
  90. data/vendor_lib/rspec/matchers/built_in/include.rb +61 -0
  91. data/vendor_lib/rspec/matchers/built_in/match.rb +17 -0
  92. data/vendor_lib/rspec/matchers/built_in/match_array.rb +51 -0
  93. data/vendor_lib/rspec/matchers/built_in/raise_error.rb +154 -0
  94. data/vendor_lib/rspec/matchers/built_in/respond_to.rb +74 -0
  95. data/vendor_lib/rspec/matchers/built_in/satisfy.rb +30 -0
  96. data/vendor_lib/rspec/matchers/built_in/start_and_end_with.rb +48 -0
  97. data/vendor_lib/rspec/matchers/built_in/throw_symbol.rb +94 -0
  98. data/vendor_lib/rspec/matchers/built_in/yield.rb +297 -0
  99. data/vendor_lib/rspec/matchers/built_in.rb +39 -0
  100. data/vendor_lib/rspec/matchers/compatibility.rb +14 -0
  101. data/vendor_lib/rspec/matchers/configuration.rb +113 -0
  102. data/vendor_lib/rspec/matchers/dsl.rb +23 -0
  103. data/vendor_lib/rspec/matchers/generated_descriptions.rb +35 -0
  104. data/vendor_lib/rspec/matchers/matcher.rb +301 -0
  105. data/vendor_lib/rspec/matchers/method_missing.rb +12 -0
  106. data/vendor_lib/rspec/matchers/operator_matcher.rb +99 -0
  107. data/vendor_lib/rspec/matchers/pretty.rb +70 -0
  108. data/vendor_lib/rspec/matchers/test_unit_integration.rb +11 -0
  109. data/vendor_lib/rspec/matchers.rb +633 -0
  110. data/vendor_lib/rspec/mocks/any_instance/chain.rb +92 -0
  111. data/vendor_lib/rspec/mocks/any_instance/expectation_chain.rb +47 -0
  112. data/vendor_lib/rspec/mocks/any_instance/message_chains.rb +75 -0
  113. data/vendor_lib/rspec/mocks/any_instance/recorder.rb +200 -0
  114. data/vendor_lib/rspec/mocks/any_instance/stub_chain.rb +45 -0
  115. data/vendor_lib/rspec/mocks/any_instance/stub_chain_chain.rb +23 -0
  116. data/vendor_lib/rspec/mocks/argument_list_matcher.rb +104 -0
  117. data/vendor_lib/rspec/mocks/argument_matchers.rb +264 -0
  118. data/vendor_lib/rspec/mocks/arity_calculator.rb +66 -0
  119. data/vendor_lib/rspec/mocks/configuration.rb +111 -0
  120. data/vendor_lib/rspec/mocks/error_generator.rb +203 -0
  121. data/vendor_lib/rspec/mocks/errors.rb +12 -0
  122. data/vendor_lib/rspec/mocks/example_methods.rb +201 -0
  123. data/vendor_lib/rspec/mocks/extensions/marshal.rb +17 -0
  124. data/vendor_lib/rspec/mocks/framework.rb +36 -0
  125. data/vendor_lib/rspec/mocks/instance_method_stasher.rb +112 -0
  126. data/vendor_lib/rspec/mocks/matchers/have_received.rb +99 -0
  127. data/vendor_lib/rspec/mocks/matchers/receive.rb +112 -0
  128. data/vendor_lib/rspec/mocks/matchers/receive_messages.rb +72 -0
  129. data/vendor_lib/rspec/mocks/message_expectation.rb +643 -0
  130. data/vendor_lib/rspec/mocks/method_double.rb +209 -0
  131. data/vendor_lib/rspec/mocks/method_reference.rb +95 -0
  132. data/vendor_lib/rspec/mocks/mock.rb +7 -0
  133. data/vendor_lib/rspec/mocks/mutate_const.rb +406 -0
  134. data/vendor_lib/rspec/mocks/object_reference.rb +90 -0
  135. data/vendor_lib/rspec/mocks/order_group.rb +82 -0
  136. data/vendor_lib/rspec/mocks/proxy.rb +269 -0
  137. data/vendor_lib/rspec/mocks/proxy_for_nil.rb +37 -0
  138. data/vendor_lib/rspec/mocks/space.rb +95 -0
  139. data/vendor_lib/rspec/mocks/standalone.rb +3 -0
  140. data/vendor_lib/rspec/mocks/stub_chain.rb +51 -0
  141. data/vendor_lib/rspec/mocks/syntax.rb +374 -0
  142. data/vendor_lib/rspec/mocks/targets.rb +90 -0
  143. data/vendor_lib/rspec/mocks/test_double.rb +109 -0
  144. data/vendor_lib/rspec/mocks/verifying_double.rb +77 -0
  145. data/vendor_lib/rspec/mocks/verifying_message_expecation.rb +60 -0
  146. data/vendor_lib/rspec/mocks/verifying_proxy.rb +151 -0
  147. data/vendor_lib/rspec/mocks/version.rb +7 -0
  148. data/vendor_lib/rspec/mocks.rb +100 -0
  149. data/vendor_lib/rspec/support/caller_filter.rb +56 -0
  150. data/vendor_lib/rspec/support/spec/deprecation_helpers.rb +29 -0
  151. data/vendor_lib/rspec/support/spec/in_sub_process.rb +40 -0
  152. data/vendor_lib/rspec/support/spec/stderr_splitter.rb +50 -0
  153. data/vendor_lib/rspec/support/spec.rb +14 -0
  154. data/vendor_lib/rspec/support/version.rb +7 -0
  155. data/vendor_lib/rspec/support/warnings.rb +41 -0
  156. data/vendor_lib/rspec/support.rb +6 -0
  157. data/vendor_lib/rspec/version.rb +5 -0
  158. data/vendor_lib/rspec-expectations.rb +1 -0
  159. data/vendor_lib/rspec.rb +3 -0
  160. metadata +163 -4
  161. data/opal/opal/rspec/rspec.js +0 -20384
@@ -0,0 +1,633 @@
1
+ require 'rspec/matchers/pretty'
2
+
3
+ require 'rspec/matchers/built_in'
4
+ require 'rspec/matchers/matcher'
5
+ require 'rspec/matchers/operator_matcher'
6
+
7
+ require 'rspec/matchers/generated_descriptions'
8
+ require 'rspec/matchers/method_missing'
9
+ require 'rspec/matchers/compatibility'
10
+ require 'rspec/matchers/dsl'
11
+ require 'rspec/matchers/test_unit_integration'
12
+
13
+ module RSpec
14
+ # RSpec::Matchers provides a number of useful matchers we use to define
15
+ # expectations. A matcher is any object that responds to the following:
16
+ #
17
+ # matches?(actual)
18
+ # failure_message_for_should
19
+ #
20
+ # These methods are also part of the matcher protocol, but are optional:
21
+ #
22
+ # does_not_match?(actual)
23
+ # failure_message_for_should_not
24
+ # description
25
+ #
26
+ # ## Predicates
27
+ #
28
+ # In addition to matchers that are defined explicitly, RSpec will create
29
+ # custom matchers on the fly for any arbitrary predicate, giving your specs a
30
+ # much more natural language feel.
31
+ #
32
+ # A Ruby predicate is a method that ends with a "?" and returns true or false.
33
+ # Common examples are `empty?`, `nil?`, and `instance_of?`.
34
+ #
35
+ # All you need to do is write `expect(..).to be_` followed by the predicate
36
+ # without the question mark, and RSpec will figure it out from there.
37
+ # For example:
38
+ #
39
+ # expect([]).to be_empty # => [].empty?() | passes
40
+ # expect([]).not_to be_empty # => [].empty?() | fails
41
+ #
42
+ # In addtion to prefixing the predicate matchers with "be_", you can also use "be_a_"
43
+ # and "be_an_", making your specs read much more naturally:
44
+ #
45
+ # expect("a string").to be_an_instance_of(String) # =>"a string".instance_of?(String) # passes
46
+ #
47
+ # expect(3).to be_a_kind_of(Fixnum) # => 3.kind_of?(Numeric) | passes
48
+ # expect(3).to be_a_kind_of(Numeric) # => 3.kind_of?(Numeric) | passes
49
+ # expect(3).to be_an_instance_of(Fixnum) # => 3.instance_of?(Fixnum) | passes
50
+ # expect(3).not_to be_an_instance_of(Numeric) # => 3.instance_of?(Numeric) | fails
51
+ #
52
+ # RSpec will also create custom matchers for predicates like `has_key?`. To
53
+ # use this feature, just state that the object should have_key(:key) and RSpec will
54
+ # call has_key?(:key) on the target. For example:
55
+ #
56
+ # expect(:a => "A").to have_key(:a)
57
+ # expect(:a => "A").to have_key(:b) # fails
58
+ #
59
+ # You can use this feature to invoke any predicate that begins with "has_", whether it is
60
+ # part of the Ruby libraries (like `Hash#has_key?`) or a method you wrote on your own class.
61
+ #
62
+ # ## Custom Matchers
63
+ #
64
+ # When you find that none of the stock matchers provide a natural feeling
65
+ # expectation, you can very easily write your own using RSpec's matcher DSL
66
+ # or writing one from scratch.
67
+ #
68
+ # ### Matcher DSL
69
+ #
70
+ # Imagine that you are writing a game in which players can be in various
71
+ # zones on a virtual board. To specify that bob should be in zone 4, you
72
+ # could say:
73
+ #
74
+ # expect(bob.current_zone).to eql(Zone.new("4"))
75
+ #
76
+ # But you might find it more expressive to say:
77
+ #
78
+ # expect(bob).to be_in_zone("4")
79
+ #
80
+ # and/or
81
+ #
82
+ # expect(bob).not_to be_in_zone("3")
83
+ #
84
+ # You can create such a matcher like so:
85
+ #
86
+ # RSpec::Matchers.define :be_in_zone do |zone|
87
+ # match do |player|
88
+ # player.in_zone?(zone)
89
+ # end
90
+ # end
91
+ #
92
+ # This will generate a <tt>be_in_zone</tt> method that returns a matcher
93
+ # with logical default messages for failures. You can override the failure
94
+ # messages and the generated description as follows:
95
+ #
96
+ # RSpec::Matchers.define :be_in_zone do |zone|
97
+ # match do |player|
98
+ # player.in_zone?(zone)
99
+ # end
100
+ #
101
+ # failure_message_for_should do |player|
102
+ # # generate and return the appropriate string.
103
+ # end
104
+ #
105
+ # failure_message_for_should_not do |player|
106
+ # # generate and return the appropriate string.
107
+ # end
108
+ #
109
+ # description do
110
+ # # generate and return the appropriate string.
111
+ # end
112
+ # end
113
+ #
114
+ # Each of the message-generation methods has access to the block arguments
115
+ # passed to the <tt>create</tt> method (in this case, <tt>zone</tt>). The
116
+ # failure message methods (<tt>failure_message_for_should</tt> and
117
+ # <tt>failure_message_for_should_not</tt>) are passed the actual value (the
118
+ # receiver of <tt>expect(..)</tt> or <tt>expect(..).not_to</tt>).
119
+ #
120
+ # ### Custom Matcher from scratch
121
+ #
122
+ # You could also write a custom matcher from scratch, as follows:
123
+ #
124
+ # class BeInZone
125
+ # def initialize(expected)
126
+ # @expected = expected
127
+ # end
128
+ #
129
+ # def matches?(target)
130
+ # @target = target
131
+ # @target.current_zone.eql?(Zone.new(@expected))
132
+ # end
133
+ #
134
+ # def failure_message_for_should
135
+ # "expected #{@target.inspect} to be in Zone #{@expected}"
136
+ # end
137
+ #
138
+ # def failure_message_for_should_not
139
+ # "expected #{@target.inspect} not to be in Zone #{@expected}"
140
+ # end
141
+ # end
142
+ #
143
+ # ... and a method like this:
144
+ #
145
+ # def be_in_zone(expected)
146
+ # BeInZone.new(expected)
147
+ # end
148
+ #
149
+ # And then expose the method to your specs. This is normally done
150
+ # by including the method and the class in a module, which is then
151
+ # included in your spec:
152
+ #
153
+ # module CustomGameMatchers
154
+ # class BeInZone
155
+ # # ...
156
+ # end
157
+ #
158
+ # def be_in_zone(expected)
159
+ # # ...
160
+ # end
161
+ # end
162
+ #
163
+ # describe "Player behaviour" do
164
+ # include CustomGameMatchers
165
+ # # ...
166
+ # end
167
+ #
168
+ # or you can include in globally in a spec_helper.rb file <tt>require</tt>d
169
+ # from your spec file(s):
170
+ #
171
+ # RSpec::configure do |config|
172
+ # config.include(CustomGameMatchers)
173
+ # end
174
+ module Matchers
175
+ # @api private
176
+ def self.is_a_matcher?(obj)
177
+ return true if ::RSpec::Matchers::BuiltIn::BaseMatcher === obj
178
+ return false if obj.respond_to?(:i_respond_to_everything_so_im_not_really_a_matcher)
179
+ return false unless obj.respond_to?(:matches?)
180
+
181
+ obj.respond_to?(:failure_message_for_should) || obj.respond_to?(:failure_message)
182
+ end
183
+
184
+ # Passes if actual is truthy (anything but false or nil)
185
+ def be_truthy
186
+ BuiltIn::BeTruthy.new
187
+ end
188
+
189
+ # Passes if actual is falsy (false or nil)
190
+ def be_falsey
191
+ BuiltIn::BeFalsey.new
192
+ end
193
+
194
+ alias_method :be_falsy, :be_falsey
195
+
196
+ # Passes if actual is nil
197
+ def be_nil
198
+ BuiltIn::BeNil.new
199
+ end
200
+
201
+ # @example
202
+ # expect(actual).to be_truthy
203
+ # expect(actual).to be_falsey
204
+ # expect(actual).to be_nil
205
+ # expect(actual).to be_[arbitrary_predicate](*args)
206
+ # expect(actual).not_to be_nil
207
+ # expect(actual).not_to be_[arbitrary_predicate](*args)
208
+ #
209
+ # Given true, false, or nil, will pass if actual value is true, false or
210
+ # nil (respectively). Given no args means the caller should satisfy an if
211
+ # condition (to be or not to be).
212
+ #
213
+ # Predicates are any Ruby method that ends in a "?" and returns true or
214
+ # false. Given be_ followed by arbitrary_predicate (without the "?"),
215
+ # RSpec will match convert that into a query against the target object.
216
+ #
217
+ # The arbitrary_predicate feature will handle any predicate prefixed with
218
+ # "be_an_" (e.g. be_an_instance_of), "be_a_" (e.g. be_a_kind_of) or "be_"
219
+ # (e.g. be_empty), letting you choose the prefix that best suits the
220
+ # predicate.
221
+ def be(*args)
222
+ args.empty? ?
223
+ Matchers::BuiltIn::Be.new : equal(*args)
224
+ end
225
+
226
+ # passes if target.kind_of?(klass)
227
+ def be_a(klass)
228
+ be_a_kind_of(klass)
229
+ end
230
+
231
+ alias_method :be_an, :be_a
232
+
233
+ # Passes if actual.instance_of?(expected)
234
+ #
235
+ # @example
236
+ #
237
+ # expect(5).to be_an_instance_of(Fixnum)
238
+ # expect(5).not_to be_an_instance_of(Numeric)
239
+ # expect(5).not_to be_an_instance_of(Float)
240
+ def be_an_instance_of(expected)
241
+ BuiltIn::BeAnInstanceOf.new(expected)
242
+ end
243
+
244
+ alias_method :be_instance_of, :be_an_instance_of
245
+
246
+ # Passes if actual.kind_of?(expected)
247
+ #
248
+ # @example
249
+ #
250
+ # expect(5).to be_a_kind_of(Fixnum)
251
+ # expect(5).to be_a_kind_of(Numeric)
252
+ # expect(5).not_to be_a_kind_of(Float)
253
+ def be_a_kind_of(expected)
254
+ BuiltIn::BeAKindOf.new(expected)
255
+ end
256
+
257
+ alias_method :be_kind_of, :be_a_kind_of
258
+
259
+ # Passes if actual == expected +/- delta
260
+ #
261
+ # @example
262
+ #
263
+ # expect(result).to be_within(0.5).of(3.0)
264
+ # expect(result).not_to be_within(0.5).of(3.0)
265
+ def be_within(delta)
266
+ BuiltIn::BeWithin.new(delta)
267
+ end
268
+
269
+ # Applied to a proc, specifies that its execution will cause some value to
270
+ # change.
271
+ #
272
+ # @param [Object] receiver
273
+ # @param [Symbol] message the message to send the receiver
274
+ #
275
+ # You can either pass <tt>receiver</tt> and <tt>message</tt>, or a block,
276
+ # but not both.
277
+ #
278
+ # When passing a block, it must use the <tt>{ ... }</tt> format, not
279
+ # do/end, as <tt>{ ... }</tt> binds to the +change+ method, whereas do/end
280
+ # would errantly bind to the +expect(..)+ or +expect(..).not_to+ method.
281
+ #
282
+ # @example
283
+ #
284
+ # expect {
285
+ # team.add_player(player)
286
+ # }.to change(roster, :count)
287
+ #
288
+ # expect {
289
+ # team.add_player(player)
290
+ # }.to change(roster, :count).by(1)
291
+ #
292
+ # expect {
293
+ # team.add_player(player)
294
+ # }.to change(roster, :count).by_at_least(1)
295
+ #
296
+ # expect {
297
+ # team.add_player(player)
298
+ # }.to change(roster, :count).by_at_most(1)
299
+ #
300
+ # string = "string"
301
+ # expect {
302
+ # string.reverse!
303
+ # }.to change { string }.from("string").to("gnirts")
304
+ #
305
+ # string = "string"
306
+ # expect {
307
+ # string
308
+ # }.not_to change { string }
309
+ #
310
+ # expect {
311
+ # person.happy_birthday
312
+ # }.to change(person, :birthday).from(32).to(33)
313
+ #
314
+ # expect {
315
+ # employee.develop_great_new_social_networking_app
316
+ # }.to change(employee, :title).from("Mail Clerk").to("CEO")
317
+ #
318
+ # expect {
319
+ # doctor.leave_office
320
+ # }.to change(doctor, :sign).from(/is in/).to(/is out/)
321
+ #
322
+ # user = User.new(:type => "admin")
323
+ # expect {
324
+ # user.symbolize_type
325
+ # }.to change(user, :type).from(String).to(Symbol)
326
+ #
327
+ # == Notes
328
+ #
329
+ # Evaluates <tt>receiver.message</tt> or <tt>block</tt> before and after it
330
+ # evaluates the block passed to <tt>expect</tt>.
331
+ #
332
+ # <tt>expect( ... ).not_to change</tt> only supports the form with no subsequent
333
+ # calls to <tt>by</tt>, <tt>by_at_least</tt>, <tt>by_at_most</tt>,
334
+ # <tt>to</tt> or <tt>from</tt>.
335
+ def change(receiver=nil, message=nil, &block)
336
+ BuiltIn::Change.new(receiver, message, &block)
337
+ end
338
+
339
+ # Passes if actual covers expected. This works for
340
+ # Ranges. You can also pass in multiple args
341
+ # and it will only pass if all args are found in Range.
342
+ #
343
+ # @example
344
+ # expect(1..10).to cover(5)
345
+ # expect(1..10).to cover(4, 6)
346
+ # expect(1..10).to cover(4, 6, 11) # fails
347
+ # expect(1..10).not_to cover(11)
348
+ # expect(1..10).not_to cover(5) # fails
349
+ #
350
+ # ### Warning:: Ruby >= 1.9 only
351
+ def cover(*values)
352
+ BuiltIn::Cover.new(*values)
353
+ end if (1..2).respond_to?(:cover?)
354
+
355
+ # Matches if the actual value ends with the expected value(s). In the case
356
+ # of a string, matches against the last `expected.length` characters of the
357
+ # actual string. In the case of an array, matches against the last
358
+ # `expected.length` elements of the actual array.
359
+ #
360
+ # @example
361
+ #
362
+ # expect("this string").to end_with "string"
363
+ # expect([0, 1, 2, 3, 4]).to end_with 4
364
+ # expect([0, 2, 3, 4, 4]).to end_with 3, 4
365
+ def end_with(*expected)
366
+ BuiltIn::EndWith.new(*expected)
367
+ end
368
+
369
+ # Passes if <tt>actual == expected</tt>.
370
+ #
371
+ # See http://www.ruby-doc.org/core/classes/Object.html#M001057 for more
372
+ # information about equality in Ruby.
373
+ #
374
+ # @example
375
+ #
376
+ # expect(5).to eq(5)
377
+ # expect(5).not_to eq(3)
378
+ def eq(expected)
379
+ BuiltIn::Eq.new(expected)
380
+ end
381
+
382
+ # Passes if +actual.eql?(expected)+
383
+ #
384
+ # See http://www.ruby-doc.org/core/classes/Object.html#M001057 for more
385
+ # information about equality in Ruby.
386
+ #
387
+ # @example
388
+ #
389
+ # expect(5).to eql(5)
390
+ # expect(5).not_to eql(3)
391
+ def eql(expected)
392
+ BuiltIn::Eql.new(expected)
393
+ end
394
+
395
+ # Passes if <tt>actual.equal?(expected)</tt> (object identity).
396
+ #
397
+ # See http://www.ruby-doc.org/core/classes/Object.html#M001057 for more
398
+ # information about equality in Ruby.
399
+ #
400
+ # @example
401
+ #
402
+ # expect(5).to equal(5) # Fixnums are equal
403
+ # expect("5").not_to equal("5") # Strings that look the same are not the same object
404
+ def equal(expected)
405
+ BuiltIn::Equal.new(expected)
406
+ end
407
+
408
+ # Passes if `actual.exist?` or `actual.exists?`
409
+ #
410
+ # @example
411
+ # expect(File).to exist("path/to/file")
412
+ def exist(*args)
413
+ BuiltIn::Exist.new(*args)
414
+ end
415
+
416
+ # Passes if actual includes expected. This works for
417
+ # collections and Strings. You can also pass in multiple args
418
+ # and it will only pass if all args are found in collection.
419
+ #
420
+ # @example
421
+ #
422
+ # expect([1,2,3]).to include(3)
423
+ # expect([1,2,3]).to include(2,3)
424
+ # expect([1,2,3]).to include(2,3,4) # fails
425
+ # expect([1,2,3]).not_to include(4)
426
+ # expect("spread").to include("read")
427
+ # expect("spread").not_to include("red")
428
+ def include(*expected)
429
+ BuiltIn::Include.new(*expected)
430
+ end
431
+
432
+ # Given a Regexp or String, passes if actual.match(pattern)
433
+ #
434
+ # @example
435
+ #
436
+ # expect(email).to match(/^([^\s]+)((?:[-a-z0-9]+\.)+[a-z]{2,})$/i)
437
+ # expect(email).to match("@example.com")
438
+ #
439
+ # @note The `match_regex` alias is deprecated and is not recommended for use.
440
+ # It was added in 2.12.1 to facilitate its use from within custom
441
+ # matchers (due to how the custom matcher DSL was evaluated in 2.x,
442
+ # `match` could not be used there), but is no longer needed in 3.x.
443
+ def match(expected)
444
+ BuiltIn::Match.new(expected)
445
+ end
446
+ alias_method :match_regex, :match
447
+
448
+ # With no args, matches if any error is raised.
449
+ # With a named error, matches only if that specific error is raised.
450
+ # With a named error and messsage specified as a String, matches only if both match.
451
+ # With a named error and messsage specified as a Regexp, matches only if both match.
452
+ # Pass an optional block to perform extra verifications on the exception matched
453
+ #
454
+ # @example
455
+ #
456
+ # expect { do_something_risky }.to raise_error
457
+ # expect { do_something_risky }.to raise_error(PoorRiskDecisionError)
458
+ # expect { do_something_risky }.to raise_error(PoorRiskDecisionError) { |error| expect(error.data).to eq 42 }
459
+ # expect { do_something_risky }.to raise_error(PoorRiskDecisionError, "that was too risky")
460
+ # expect { do_something_risky }.to raise_error(PoorRiskDecisionError, /oo ri/)
461
+ #
462
+ # expect { do_something_risky }.not_to raise_error
463
+ def raise_error(error=Exception, message=nil, &block)
464
+ BuiltIn::RaiseError.new(error, message, &block)
465
+ end
466
+
467
+ alias_method :raise_exception, :raise_error
468
+
469
+ # Matches if the target object responds to all of the names
470
+ # provided. Names can be Strings or Symbols.
471
+ #
472
+ # @example
473
+ #
474
+ # expect("string").to respond_to(:length)
475
+ #
476
+ def respond_to(*names)
477
+ BuiltIn::RespondTo.new(*names)
478
+ end
479
+
480
+ # Passes if the submitted block returns true. Yields target to the
481
+ # block.
482
+ #
483
+ # Generally speaking, this should be thought of as a last resort when
484
+ # you can't find any other way to specify the behaviour you wish to
485
+ # specify.
486
+ #
487
+ # If you do find yourself in such a situation, you could always write
488
+ # a custom matcher, which would likely make your specs more expressive.
489
+ #
490
+ # @example
491
+ #
492
+ # expect(5).to satisfy { |n| n > 3 }
493
+ def satisfy(&block)
494
+ BuiltIn::Satisfy.new(&block)
495
+ end
496
+
497
+ # Matches if the actual value starts with the expected value(s). In the
498
+ # case of a string, matches against the first `expected.length` characters
499
+ # of the actual string. In the case of an array, matches against the first
500
+ # `expected.length` elements of the actual array.
501
+ #
502
+ # @example
503
+ #
504
+ # expect("this string").to start_with "this s"
505
+ # expect([0, 1, 2, 3, 4]).to start_with 0
506
+ # expect([0, 2, 3, 4, 4]).to start_with 0, 1
507
+ def start_with(*expected)
508
+ BuiltIn::StartWith.new(*expected)
509
+ end
510
+
511
+ # Given no argument, matches if a proc throws any Symbol.
512
+ #
513
+ # Given a Symbol, matches if the given proc throws the specified Symbol.
514
+ #
515
+ # Given a Symbol and an arg, matches if the given proc throws the
516
+ # specified Symbol with the specified arg.
517
+ #
518
+ # @example
519
+ #
520
+ # expect { do_something_risky }.to throw_symbol
521
+ # expect { do_something_risky }.to throw_symbol(:that_was_risky)
522
+ # expect { do_something_risky }.to throw_symbol(:that_was_risky, 'culprit')
523
+ #
524
+ # expect { do_something_risky }.not_to throw_symbol
525
+ # expect { do_something_risky }.not_to throw_symbol(:that_was_risky)
526
+ # expect { do_something_risky }.not_to throw_symbol(:that_was_risky, 'culprit')
527
+ def throw_symbol(expected_symbol=nil, expected_arg=nil)
528
+ BuiltIn::ThrowSymbol.new(expected_symbol, expected_arg)
529
+ end
530
+
531
+ # Passes if the method called in the expect block yields, regardless
532
+ # of whether or not arguments are yielded.
533
+ #
534
+ # @example
535
+ #
536
+ # expect { |b| 5.tap(&b) }.to yield_control
537
+ # expect { |b| "a".to_sym(&b) }.not_to yield_control
538
+ #
539
+ # @note Your expect block must accept a parameter and pass it on to
540
+ # the method-under-test as a block.
541
+ # @note This matcher is not designed for use with methods that yield
542
+ # multiple times.
543
+ def yield_control
544
+ BuiltIn::YieldControl.new
545
+ end
546
+
547
+ # Passes if the method called in the expect block yields with
548
+ # no arguments. Fails if it does not yield, or yields with arguments.
549
+ #
550
+ # @example
551
+ #
552
+ # expect { |b| User.transaction(&b) }.to yield_with_no_args
553
+ # expect { |b| 5.tap(&b) }.not_to yield_with_no_args # because it yields with `5`
554
+ # expect { |b| "a".to_sym(&b) }.not_to yield_with_no_args # because it does not yield
555
+ #
556
+ # @note Your expect block must accept a parameter and pass it on to
557
+ # the method-under-test as a block.
558
+ # @note This matcher is not designed for use with methods that yield
559
+ # multiple times.
560
+ def yield_with_no_args
561
+ BuiltIn::YieldWithNoArgs.new
562
+ end
563
+
564
+ # Given no arguments, matches if the method called in the expect
565
+ # block yields with arguments (regardless of what they are or how
566
+ # many there are).
567
+ #
568
+ # Given arguments, matches if the method called in the expect block
569
+ # yields with arguments that match the given arguments.
570
+ #
571
+ # Argument matching is done using `===` (the case match operator)
572
+ # and `==`. If the expected and actual arguments match with either
573
+ # operator, the matcher will pass.
574
+ #
575
+ # @example
576
+ #
577
+ # expect { |b| 5.tap(&b) }.to yield_with_args # because #tap yields an arg
578
+ # expect { |b| 5.tap(&b) }.to yield_with_args(5) # because 5 == 5
579
+ # expect { |b| 5.tap(&b) }.to yield_with_args(Fixnum) # because Fixnum === 5
580
+ # expect { |b| File.open("f.txt", &b) }.to yield_with_args(/txt/) # because /txt/ === "f.txt"
581
+ #
582
+ # expect { |b| User.transaction(&b) }.not_to yield_with_args # because it yields no args
583
+ # expect { |b| 5.tap(&b) }.not_to yield_with_args(1, 2, 3)
584
+ #
585
+ # @note Your expect block must accept a parameter and pass it on to
586
+ # the method-under-test as a block.
587
+ # @note This matcher is not designed for use with methods that yield
588
+ # multiple times.
589
+ def yield_with_args(*args)
590
+ BuiltIn::YieldWithArgs.new(*args)
591
+ end
592
+
593
+ # Designed for use with methods that repeatedly yield (such as
594
+ # iterators). Passes if the method called in the expect block yields
595
+ # multiple times with arguments matching those given.
596
+ #
597
+ # Argument matching is done using `===` (the case match operator)
598
+ # and `==`. If the expected and actual arguments match with either
599
+ # operator, the matcher will pass.
600
+ #
601
+ # @example
602
+ #
603
+ # expect { |b| [1, 2, 3].each(&b) }.to yield_successive_args(1, 2, 3)
604
+ # expect { |b| { :a => 1, :b => 2 }.each(&b) }.to yield_successive_args([:a, 1], [:b, 2])
605
+ # expect { |b| [1, 2, 3].each(&b) }.not_to yield_successive_args(1, 2)
606
+ #
607
+ # @note Your expect block must accept a parameter and pass it on to
608
+ # the method-under-test as a block.
609
+ def yield_successive_args(*args)
610
+ BuiltIn::YieldSuccessiveArgs.new(*args)
611
+ end
612
+
613
+ # Passes if actual contains all of the expected regardless of order.
614
+ # This works for collections. Pass in multiple args and it will only
615
+ # pass if all args are found in collection.
616
+ #
617
+ # @note This is also available using the `=~` operator with `should`,
618
+ # but `=~` is not supported with `expect`.
619
+ #
620
+ # @note This matcher only supports positive expectations.
621
+ # expect(..).not_to match_array(other_array) is not supported.
622
+ #
623
+ # @example
624
+ #
625
+ # expect([1,2,3]).to match_array([1,2,3])
626
+ # expect([1,2,3]).to match_array([1,3,2])
627
+ def match_array(array)
628
+ BuiltIn::MatchArray.new(array)
629
+ end
630
+
631
+ OperatorMatcher.register(Enumerable, '=~', BuiltIn::MatchArray)
632
+ end
633
+ end