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.
- checksums.yaml +4 -4
- data/.gitignore +0 -1
- data/.gitmodules +15 -0
- data/.travis.yml +12 -0
- data/.yardopts +5 -0
- data/CHANGELOG.md +3 -1
- data/Gemfile +6 -7
- data/README.md +2 -0
- data/Rakefile +12 -50
- data/lib/opal/rspec/version.rb +1 -1
- data/lib/opal/rspec.rb +14 -0
- data/opal/opal/rspec/async.rb +146 -11
- data/opal/opal/rspec/fixes.rb +18 -8
- data/opal/opal/rspec/requires.rb +45 -0
- data/opal/opal/rspec.rb +1 -24
- data/opal-rspec.gemspec +1 -1
- data/spec/async_spec.rb +4 -5
- data/spec/matchers_spec.rb +20 -0
- data/spec/named_subject_spec.rb +11 -0
- data/spec/should_syntax_spec.rb +17 -0
- data/vendor_lib/rspec/autorun.rb +2 -0
- data/vendor_lib/rspec/core/backport_random.rb +302 -0
- data/vendor_lib/rspec/core/backtrace_formatter.rb +65 -0
- data/vendor_lib/rspec/core/command_line.rb +36 -0
- data/vendor_lib/rspec/core/configuration.rb +1129 -0
- data/vendor_lib/rspec/core/configuration_options.rb +143 -0
- data/vendor_lib/rspec/core/drb_command_line.rb +26 -0
- data/vendor_lib/rspec/core/drb_options.rb +87 -0
- data/vendor_lib/rspec/core/dsl.rb +26 -0
- data/vendor_lib/rspec/core/example.rb +312 -0
- data/vendor_lib/rspec/core/example_group.rb +540 -0
- data/vendor_lib/rspec/core/filter_manager.rb +224 -0
- data/vendor_lib/rspec/core/flat_map.rb +17 -0
- data/vendor_lib/rspec/core/formatters/base_formatter.rb +291 -0
- data/vendor_lib/rspec/core/formatters/base_text_formatter.rb +307 -0
- data/vendor_lib/rspec/core/formatters/deprecation_formatter.rb +193 -0
- data/vendor_lib/rspec/core/formatters/documentation_formatter.rb +67 -0
- data/vendor_lib/rspec/core/formatters/helpers.rb +82 -0
- data/vendor_lib/rspec/core/formatters/html_formatter.rb +155 -0
- data/vendor_lib/rspec/core/formatters/html_printer.rb +408 -0
- data/vendor_lib/rspec/core/formatters/json_formatter.rb +99 -0
- data/vendor_lib/rspec/core/formatters/progress_formatter.rb +32 -0
- data/vendor_lib/rspec/core/formatters/snippet_extractor.rb +101 -0
- data/vendor_lib/rspec/core/formatters.rb +54 -0
- data/vendor_lib/rspec/core/hooks.rb +535 -0
- data/vendor_lib/rspec/core/memoized_helpers.rb +431 -0
- data/vendor_lib/rspec/core/metadata.rb +313 -0
- data/vendor_lib/rspec/core/mocking/with_absolutely_nothing.rb +11 -0
- data/vendor_lib/rspec/core/mocking/with_flexmock.rb +27 -0
- data/vendor_lib/rspec/core/mocking/with_mocha.rb +52 -0
- data/vendor_lib/rspec/core/mocking/with_rr.rb +27 -0
- data/vendor_lib/rspec/core/mocking/with_rspec.rb +27 -0
- data/vendor_lib/rspec/core/option_parser.rb +234 -0
- data/vendor_lib/rspec/core/ordering.rb +154 -0
- data/vendor_lib/rspec/core/pending.rb +110 -0
- data/vendor_lib/rspec/core/project_initializer.rb +88 -0
- data/vendor_lib/rspec/core/rake_task.rb +128 -0
- data/vendor_lib/rspec/core/reporter.rb +132 -0
- data/vendor_lib/rspec/core/ruby_project.rb +44 -0
- data/vendor_lib/rspec/core/runner.rb +97 -0
- data/vendor_lib/rspec/core/shared_context.rb +53 -0
- data/vendor_lib/rspec/core/shared_example_group/collection.rb +27 -0
- data/vendor_lib/rspec/core/shared_example_group.rb +146 -0
- data/vendor_lib/rspec/core/version.rb +7 -0
- data/vendor_lib/rspec/core/warnings.rb +22 -0
- data/vendor_lib/rspec/core/world.rb +131 -0
- data/vendor_lib/rspec/core.rb +203 -0
- data/vendor_lib/rspec/expectations/differ.rb +154 -0
- data/vendor_lib/rspec/expectations/errors.rb +9 -0
- data/vendor_lib/rspec/expectations/expectation_target.rb +87 -0
- data/vendor_lib/rspec/expectations/extensions/object.rb +29 -0
- data/vendor_lib/rspec/expectations/extensions.rb +1 -0
- data/vendor_lib/rspec/expectations/fail_with.rb +79 -0
- data/vendor_lib/rspec/expectations/handler.rb +68 -0
- data/vendor_lib/rspec/expectations/syntax.rb +182 -0
- data/vendor_lib/rspec/expectations/version.rb +8 -0
- data/vendor_lib/rspec/expectations.rb +75 -0
- data/vendor_lib/rspec/matchers/built_in/base_matcher.rb +68 -0
- data/vendor_lib/rspec/matchers/built_in/be.rb +213 -0
- data/vendor_lib/rspec/matchers/built_in/be_instance_of.rb +15 -0
- data/vendor_lib/rspec/matchers/built_in/be_kind_of.rb +11 -0
- data/vendor_lib/rspec/matchers/built_in/be_within.rb +55 -0
- data/vendor_lib/rspec/matchers/built_in/change.rb +141 -0
- data/vendor_lib/rspec/matchers/built_in/cover.rb +21 -0
- data/vendor_lib/rspec/matchers/built_in/eq.rb +22 -0
- data/vendor_lib/rspec/matchers/built_in/eql.rb +23 -0
- data/vendor_lib/rspec/matchers/built_in/equal.rb +48 -0
- data/vendor_lib/rspec/matchers/built_in/exist.rb +26 -0
- data/vendor_lib/rspec/matchers/built_in/has.rb +48 -0
- data/vendor_lib/rspec/matchers/built_in/include.rb +61 -0
- data/vendor_lib/rspec/matchers/built_in/match.rb +17 -0
- data/vendor_lib/rspec/matchers/built_in/match_array.rb +51 -0
- data/vendor_lib/rspec/matchers/built_in/raise_error.rb +154 -0
- data/vendor_lib/rspec/matchers/built_in/respond_to.rb +74 -0
- data/vendor_lib/rspec/matchers/built_in/satisfy.rb +30 -0
- data/vendor_lib/rspec/matchers/built_in/start_and_end_with.rb +48 -0
- data/vendor_lib/rspec/matchers/built_in/throw_symbol.rb +94 -0
- data/vendor_lib/rspec/matchers/built_in/yield.rb +297 -0
- data/vendor_lib/rspec/matchers/built_in.rb +39 -0
- data/vendor_lib/rspec/matchers/compatibility.rb +14 -0
- data/vendor_lib/rspec/matchers/configuration.rb +113 -0
- data/vendor_lib/rspec/matchers/dsl.rb +23 -0
- data/vendor_lib/rspec/matchers/generated_descriptions.rb +35 -0
- data/vendor_lib/rspec/matchers/matcher.rb +301 -0
- data/vendor_lib/rspec/matchers/method_missing.rb +12 -0
- data/vendor_lib/rspec/matchers/operator_matcher.rb +99 -0
- data/vendor_lib/rspec/matchers/pretty.rb +70 -0
- data/vendor_lib/rspec/matchers/test_unit_integration.rb +11 -0
- data/vendor_lib/rspec/matchers.rb +633 -0
- data/vendor_lib/rspec/mocks/any_instance/chain.rb +92 -0
- data/vendor_lib/rspec/mocks/any_instance/expectation_chain.rb +47 -0
- data/vendor_lib/rspec/mocks/any_instance/message_chains.rb +75 -0
- data/vendor_lib/rspec/mocks/any_instance/recorder.rb +200 -0
- data/vendor_lib/rspec/mocks/any_instance/stub_chain.rb +45 -0
- data/vendor_lib/rspec/mocks/any_instance/stub_chain_chain.rb +23 -0
- data/vendor_lib/rspec/mocks/argument_list_matcher.rb +104 -0
- data/vendor_lib/rspec/mocks/argument_matchers.rb +264 -0
- data/vendor_lib/rspec/mocks/arity_calculator.rb +66 -0
- data/vendor_lib/rspec/mocks/configuration.rb +111 -0
- data/vendor_lib/rspec/mocks/error_generator.rb +203 -0
- data/vendor_lib/rspec/mocks/errors.rb +12 -0
- data/vendor_lib/rspec/mocks/example_methods.rb +201 -0
- data/vendor_lib/rspec/mocks/extensions/marshal.rb +17 -0
- data/vendor_lib/rspec/mocks/framework.rb +36 -0
- data/vendor_lib/rspec/mocks/instance_method_stasher.rb +112 -0
- data/vendor_lib/rspec/mocks/matchers/have_received.rb +99 -0
- data/vendor_lib/rspec/mocks/matchers/receive.rb +112 -0
- data/vendor_lib/rspec/mocks/matchers/receive_messages.rb +72 -0
- data/vendor_lib/rspec/mocks/message_expectation.rb +643 -0
- data/vendor_lib/rspec/mocks/method_double.rb +209 -0
- data/vendor_lib/rspec/mocks/method_reference.rb +95 -0
- data/vendor_lib/rspec/mocks/mock.rb +7 -0
- data/vendor_lib/rspec/mocks/mutate_const.rb +406 -0
- data/vendor_lib/rspec/mocks/object_reference.rb +90 -0
- data/vendor_lib/rspec/mocks/order_group.rb +82 -0
- data/vendor_lib/rspec/mocks/proxy.rb +269 -0
- data/vendor_lib/rspec/mocks/proxy_for_nil.rb +37 -0
- data/vendor_lib/rspec/mocks/space.rb +95 -0
- data/vendor_lib/rspec/mocks/standalone.rb +3 -0
- data/vendor_lib/rspec/mocks/stub_chain.rb +51 -0
- data/vendor_lib/rspec/mocks/syntax.rb +374 -0
- data/vendor_lib/rspec/mocks/targets.rb +90 -0
- data/vendor_lib/rspec/mocks/test_double.rb +109 -0
- data/vendor_lib/rspec/mocks/verifying_double.rb +77 -0
- data/vendor_lib/rspec/mocks/verifying_message_expecation.rb +60 -0
- data/vendor_lib/rspec/mocks/verifying_proxy.rb +151 -0
- data/vendor_lib/rspec/mocks/version.rb +7 -0
- data/vendor_lib/rspec/mocks.rb +100 -0
- data/vendor_lib/rspec/support/caller_filter.rb +56 -0
- data/vendor_lib/rspec/support/spec/deprecation_helpers.rb +29 -0
- data/vendor_lib/rspec/support/spec/in_sub_process.rb +40 -0
- data/vendor_lib/rspec/support/spec/stderr_splitter.rb +50 -0
- data/vendor_lib/rspec/support/spec.rb +14 -0
- data/vendor_lib/rspec/support/version.rb +7 -0
- data/vendor_lib/rspec/support/warnings.rb +41 -0
- data/vendor_lib/rspec/support.rb +6 -0
- data/vendor_lib/rspec/version.rb +5 -0
- data/vendor_lib/rspec-expectations.rb +1 -0
- data/vendor_lib/rspec.rb +3 -0
- metadata +163 -4
- data/opal/opal/rspec/rspec.js +0 -20384
@@ -0,0 +1,301 @@
|
|
1
|
+
require 'set'
|
2
|
+
|
3
|
+
module RSpec
|
4
|
+
module Matchers
|
5
|
+
module DSL
|
6
|
+
# Contains the methods that are available from within the
|
7
|
+
# `RSpec::Matchers.define` DSL for creating custom matchers.
|
8
|
+
module Macros
|
9
|
+
# Stores the block that is used to determine whether this matcher passes
|
10
|
+
# or fails. The block should return a boolean value. When the matcher is
|
11
|
+
# passed to `expect(...).to` and the block returns `true`, then the expectation
|
12
|
+
# passes. Similarly, when the matcher is passed to `expect(...).not_to` and the
|
13
|
+
# block returns `false`, then the expectation passes.
|
14
|
+
#
|
15
|
+
# Use `match_for_should` when used in conjunction with
|
16
|
+
# `match_for_should_not`.
|
17
|
+
#
|
18
|
+
# @example
|
19
|
+
#
|
20
|
+
# RSpec::Matchers.define :be_even do
|
21
|
+
# match do |actual|
|
22
|
+
# actual.even?
|
23
|
+
# end
|
24
|
+
# end
|
25
|
+
#
|
26
|
+
# expect(4).to be_even # passes
|
27
|
+
# expect(3).not_to be_even # passes
|
28
|
+
# expect(3).to be_even # fails
|
29
|
+
# expect(4).not_to be_even # fails
|
30
|
+
#
|
31
|
+
# @yield [Object] actual the actual value (i.e. the value wrapped by `expect`)
|
32
|
+
def match(&match_block)
|
33
|
+
define_user_override(:matches?, match_block) do |actual|
|
34
|
+
begin
|
35
|
+
@actual = actual
|
36
|
+
super(*actual_arg_for(match_block))
|
37
|
+
rescue RSpec::Expectations::ExpectationNotMetError
|
38
|
+
false
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
alias match_for_should match
|
43
|
+
|
44
|
+
# Use this to define the block for a negative expectation (`expect(...).not_to`)
|
45
|
+
# when the positive and negative forms require different handling. This
|
46
|
+
# is rarely necessary, but can be helpful, for example, when specifying
|
47
|
+
# asynchronous processes that require different timeouts.
|
48
|
+
#
|
49
|
+
# @yield [Object] actual the actual value (i.e. the value wrapped by `expect`)
|
50
|
+
def match_for_should_not(&match_block)
|
51
|
+
define_user_override(:does_not_match?, match_block) do |actual|
|
52
|
+
@actual = actual
|
53
|
+
super(*actual_arg_for(match_block))
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
# Use this instead of `match` when the block will raise an exception
|
58
|
+
# rather than returning false to indicate a failure.
|
59
|
+
#
|
60
|
+
# @example
|
61
|
+
#
|
62
|
+
# RSpec::Matchers.define :accept_as_valid do |candidate_address|
|
63
|
+
# match_unless_raises ValidationException do |validator|
|
64
|
+
# validator.validate(candidate_address)
|
65
|
+
# end
|
66
|
+
# end
|
67
|
+
#
|
68
|
+
# expect(email_validator).to accept_as_valid("person@company.com")
|
69
|
+
#
|
70
|
+
# @yield [Object] actual the actual object (i.e. the value wrapped by `expect`)
|
71
|
+
def match_unless_raises(expected_exception=Exception, &match_block)
|
72
|
+
define_user_override(:matches?, match_block) do |actual|
|
73
|
+
@actual = actual
|
74
|
+
begin
|
75
|
+
super(*actual_arg_for(match_block))
|
76
|
+
rescue expected_exception => @rescued_exception
|
77
|
+
false
|
78
|
+
else
|
79
|
+
true
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
# Customizes the failure messsage to use when this matcher is
|
85
|
+
# asked to positively match. Only use this when the message
|
86
|
+
# generated by default doesn't suit your needs.
|
87
|
+
#
|
88
|
+
# @example
|
89
|
+
#
|
90
|
+
# RSpec::Matchers.define :have_strength do |expected|
|
91
|
+
# match { your_match_logic }
|
92
|
+
#
|
93
|
+
# failure_message_for_should do |actual|
|
94
|
+
# "Expected strength of #{expected}, but had #{actual.strength}"
|
95
|
+
# end
|
96
|
+
# end
|
97
|
+
#
|
98
|
+
# @yield [Object] actual the actual object (i.e. the value wrapped by `expect`)
|
99
|
+
def failure_message_for_should(&definition)
|
100
|
+
define_user_override(__method__, definition)
|
101
|
+
end
|
102
|
+
|
103
|
+
# Customize the failure messsage to use when this matcher is asked
|
104
|
+
# to negatively match. Only use this when the message generated by
|
105
|
+
# default doesn't suit your needs.
|
106
|
+
#
|
107
|
+
# @example
|
108
|
+
#
|
109
|
+
# RSpec::Matchers.define :have_strength do |expected|
|
110
|
+
# match { your_match_logic }
|
111
|
+
#
|
112
|
+
# failure_message_for_should_not do |actual|
|
113
|
+
# "Expected not to have strength of #{expected}, but did"
|
114
|
+
# end
|
115
|
+
# end
|
116
|
+
#
|
117
|
+
# @yield [Object] actual the actual object (i.e. the value wrapped by `expect`)
|
118
|
+
def failure_message_for_should_not(&definition)
|
119
|
+
define_user_override(__method__, definition)
|
120
|
+
end
|
121
|
+
|
122
|
+
# Customize the description to use for one-liners. Only use this when
|
123
|
+
# the description generated by default doesn't suit your needs.
|
124
|
+
#
|
125
|
+
# @example
|
126
|
+
#
|
127
|
+
# RSpec::Matchers.define :qualify_for do |expected|
|
128
|
+
# match { your_match_logic }
|
129
|
+
#
|
130
|
+
# description do
|
131
|
+
# "qualify for #{expected}"
|
132
|
+
# end
|
133
|
+
# end
|
134
|
+
#
|
135
|
+
# @yield [Object] actual the actual object (i.e. the value wrapped by `expect`)
|
136
|
+
def description(&definition)
|
137
|
+
define_user_override(__method__, definition)
|
138
|
+
end
|
139
|
+
|
140
|
+
# Tells the matcher to diff the actual and expected values in the failure
|
141
|
+
# message.
|
142
|
+
def diffable
|
143
|
+
define_method(:diffable?) { true }
|
144
|
+
end
|
145
|
+
|
146
|
+
# Convenience for defining methods on this matcher to create a fluent
|
147
|
+
# interface. The trick about fluent interfaces is that each method must
|
148
|
+
# return self in order to chain methods together. `chain` handles that
|
149
|
+
# for you.
|
150
|
+
#
|
151
|
+
# @example
|
152
|
+
#
|
153
|
+
# RSpec::Matchers.define :have_errors_on do |key|
|
154
|
+
# chain :with do |message|
|
155
|
+
# @message = message
|
156
|
+
# end
|
157
|
+
#
|
158
|
+
# match do |actual|
|
159
|
+
# actual.errors[key] == @message
|
160
|
+
# end
|
161
|
+
# end
|
162
|
+
#
|
163
|
+
# expect(minor).to have_errors_on(:age).with("Not old enough to participate")
|
164
|
+
def chain(name, &definition)
|
165
|
+
define_user_override(name, definition) do |*args, &block|
|
166
|
+
super(*args, &block)
|
167
|
+
self
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
private
|
172
|
+
|
173
|
+
# Does the following:
|
174
|
+
#
|
175
|
+
# - Defines the named method usign a user-provided block
|
176
|
+
# in @user_method_defs, which is included as an ancestor
|
177
|
+
# in the singleton class in which we eval the `define` block.
|
178
|
+
# - Defines an overriden definition for the same method
|
179
|
+
# usign the provided `our_def` block.
|
180
|
+
# - Provides a default `our_def` block for the common case
|
181
|
+
# of needing to call the user's definition with `@actual`
|
182
|
+
# as an arg, but only if their block's arity can handle it.
|
183
|
+
#
|
184
|
+
# This compiles the user block into an actual method, allowing
|
185
|
+
# them to use normal method constructs like `return`
|
186
|
+
# (e.g. for a early guard statement), while allowing us to define
|
187
|
+
# an override that can provide the wrapped handling
|
188
|
+
# (e.g. assigning `@actual`, rescueing errors, etc) and
|
189
|
+
# can `super` to the user's definition.
|
190
|
+
def define_user_override(method_name, user_def, &our_def)
|
191
|
+
@user_method_defs.__send__(:define_method, method_name, &user_def)
|
192
|
+
our_def ||= lambda { super(*actual_arg_for(user_def)) }
|
193
|
+
define_method(method_name, &our_def)
|
194
|
+
end
|
195
|
+
end
|
196
|
+
|
197
|
+
# Defines default implementations of the matcher
|
198
|
+
# protocol methods for custom matchers. You can
|
199
|
+
# override any of these using the {RSpec::Matchers::DSL::Macros Macros} methods
|
200
|
+
# from within an `RSpec::Matchers.define` block.
|
201
|
+
module DefaultImplementations
|
202
|
+
# @api private
|
203
|
+
# Used internally by objects returns by `should` and `should_not`.
|
204
|
+
def diffable?
|
205
|
+
false
|
206
|
+
end
|
207
|
+
|
208
|
+
# The default description.
|
209
|
+
def description
|
210
|
+
"#{name_to_sentence}#{expected_to_sentence}"
|
211
|
+
end
|
212
|
+
|
213
|
+
# The default failure message for positive expectations.
|
214
|
+
def failure_message_for_should
|
215
|
+
"expected #{actual.inspect} to #{name_to_sentence}#{expected_to_sentence}"
|
216
|
+
end
|
217
|
+
|
218
|
+
# The default failure message for negative expectations.
|
219
|
+
def failure_message_for_should_not
|
220
|
+
"expected #{actual.inspect} not to #{name_to_sentence}#{expected_to_sentence}"
|
221
|
+
end
|
222
|
+
end
|
223
|
+
|
224
|
+
# The class used for custom matchers. The block passed to
|
225
|
+
# `RSpec::Matchers.define` will be evaluated in the context
|
226
|
+
# of the singleton class of an instance, and will have the
|
227
|
+
# {RSpec::Matchers::DSL::Macros Macros} methods available.
|
228
|
+
class Matcher
|
229
|
+
# Provides default implementations for the matcher protocol methods.
|
230
|
+
include DefaultImplementations
|
231
|
+
|
232
|
+
# Allows expectation expressions to be used in the match block.
|
233
|
+
include RSpec::Matchers
|
234
|
+
|
235
|
+
# Converts matcher name and expected args to an English expresion.
|
236
|
+
include RSpec::Matchers::Pretty
|
237
|
+
|
238
|
+
# Makes the macro methods available to an `RSpec::Matchers.define` block.
|
239
|
+
extend Macros
|
240
|
+
|
241
|
+
attr_reader :expected, :actual, :rescued_exception
|
242
|
+
attr_accessor :matcher_execution_context
|
243
|
+
|
244
|
+
# @api private
|
245
|
+
def initialize(name, declarations, *expected)
|
246
|
+
@name = name
|
247
|
+
@actual = nil
|
248
|
+
@expected = expected
|
249
|
+
|
250
|
+
class << self
|
251
|
+
# See `Macros#define_user_override` above, for an explanation.
|
252
|
+
include(@user_method_defs = Module.new)
|
253
|
+
self
|
254
|
+
end.class_exec(*expected, &declarations)
|
255
|
+
end
|
256
|
+
|
257
|
+
# Adds the name (rather than a cryptic hex number)
|
258
|
+
# so we can identify an instance of
|
259
|
+
# the matcher in error messages (e.g. for `NoMethodError`)
|
260
|
+
def inspect
|
261
|
+
"#<#{self.class.name} #{name}>"
|
262
|
+
end
|
263
|
+
|
264
|
+
if RUBY_VERSION.to_f >= 1.9
|
265
|
+
# Indicates that this matcher responds to messages
|
266
|
+
# from the `matcher_execution_context` as well.
|
267
|
+
# Also, supports getting a method object for such methods.
|
268
|
+
def respond_to_missing?(method, include_private=false)
|
269
|
+
super || matcher_execution_context.respond_to?(method, include_private)
|
270
|
+
end
|
271
|
+
else # for 1.8.7
|
272
|
+
# Indicates that this matcher responds to messages
|
273
|
+
# from the `matcher_execution_context` as well.
|
274
|
+
def respond_to?(method, include_private=false)
|
275
|
+
super || matcher_execution_context.respond_to?(method, include_private)
|
276
|
+
end
|
277
|
+
end
|
278
|
+
|
279
|
+
private
|
280
|
+
|
281
|
+
def actual_arg_for(block)
|
282
|
+
block.arity.zero? ? [] : [@actual]
|
283
|
+
end
|
284
|
+
|
285
|
+
# Takes care of forwarding unhandled messages to the
|
286
|
+
# `matcher_execution_context` (typically the current
|
287
|
+
# running `RSpec::Core::Example`). This is needed by
|
288
|
+
# rspec-rails so that it can define matchers that wrap
|
289
|
+
# Rails' test helper methods, but it's also a useful
|
290
|
+
# feature in its own right.
|
291
|
+
def method_missing(method, *args, &block)
|
292
|
+
if matcher_execution_context.respond_to?(method)
|
293
|
+
matcher_execution_context.__send__ method, *args, &block
|
294
|
+
else
|
295
|
+
super(method, *args, &block)
|
296
|
+
end
|
297
|
+
end
|
298
|
+
end
|
299
|
+
end
|
300
|
+
end
|
301
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module RSpec
|
2
|
+
module Matchers
|
3
|
+
|
4
|
+
private
|
5
|
+
|
6
|
+
def method_missing(method, *args, &block)
|
7
|
+
return Matchers::BuiltIn::BePredicate.new(method, *args, &block) if method.to_s =~ /^be_/
|
8
|
+
return Matchers::BuiltIn::Has.new(method, *args, &block) if method.to_s =~ /^have_/
|
9
|
+
super
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,99 @@
|
|
1
|
+
module RSpec
|
2
|
+
module Matchers
|
3
|
+
class OperatorMatcher
|
4
|
+
class << self
|
5
|
+
def registry
|
6
|
+
@registry ||= {}
|
7
|
+
end
|
8
|
+
|
9
|
+
def register(klass, operator, matcher)
|
10
|
+
registry[klass] ||= {}
|
11
|
+
registry[klass][operator] = matcher
|
12
|
+
end
|
13
|
+
|
14
|
+
def unregister(klass, operator)
|
15
|
+
registry[klass] && registry[klass].delete(operator)
|
16
|
+
end
|
17
|
+
|
18
|
+
def get(klass, operator)
|
19
|
+
klass.ancestors.each { |ancestor|
|
20
|
+
matcher = registry[ancestor] && registry[ancestor][operator]
|
21
|
+
return matcher if matcher
|
22
|
+
}
|
23
|
+
|
24
|
+
nil
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def initialize(actual)
|
29
|
+
@actual = actual
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.use_custom_matcher_or_delegate(operator)
|
33
|
+
define_method(operator) do |expected|
|
34
|
+
if uses_generic_implementation_of?(operator) && matcher = OperatorMatcher.get(@actual.class, operator)
|
35
|
+
@actual.__send__(::RSpec::Matchers.last_should, matcher.new(expected))
|
36
|
+
else
|
37
|
+
eval_match(@actual, operator, expected)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
negative_operator = operator.sub(/^=/, '!')
|
42
|
+
if negative_operator != operator && respond_to?(negative_operator)
|
43
|
+
define_method(negative_operator) do |expected|
|
44
|
+
opposite_should = ::RSpec::Matchers.last_should == :should ? :should_not : :should
|
45
|
+
raise "RSpec does not support `#{::RSpec::Matchers.last_should} #{negative_operator} expected`. " +
|
46
|
+
"Use `#{opposite_should} #{operator} expected` instead."
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
['==', '===', '=~', '>', '>=', '<', '<='].each do |operator|
|
52
|
+
use_custom_matcher_or_delegate operator
|
53
|
+
end
|
54
|
+
|
55
|
+
def fail_with_message(message)
|
56
|
+
RSpec::Expectations.fail_with(message, @expected, @actual)
|
57
|
+
end
|
58
|
+
|
59
|
+
def description
|
60
|
+
"#{@operator} #{@expected.inspect}"
|
61
|
+
end
|
62
|
+
|
63
|
+
private
|
64
|
+
|
65
|
+
def uses_generic_implementation_of?(op)
|
66
|
+
Expectations.method_handle_for(@actual, op).owner == ::Kernel
|
67
|
+
rescue NameError
|
68
|
+
false
|
69
|
+
end
|
70
|
+
|
71
|
+
def eval_match(actual, operator, expected)
|
72
|
+
::RSpec::Matchers.last_matcher = self
|
73
|
+
@operator, @expected = operator, expected
|
74
|
+
__delegate_operator(actual, operator, expected)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
module BuiltIn
|
79
|
+
class PositiveOperatorMatcher < OperatorMatcher
|
80
|
+
def __delegate_operator(actual, operator, expected)
|
81
|
+
if actual.__send__(operator, expected)
|
82
|
+
true
|
83
|
+
elsif ['==','===', '=~'].include?(operator)
|
84
|
+
fail_with_message("expected: #{expected.inspect}\n got: #{actual.inspect} (using #{operator})")
|
85
|
+
else
|
86
|
+
fail_with_message("expected: #{operator} #{expected.inspect}\n got: #{operator.gsub(/./, ' ')} #{actual.inspect}")
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
class NegativeOperatorMatcher < OperatorMatcher
|
92
|
+
def __delegate_operator(actual, operator, expected)
|
93
|
+
return false unless actual.__send__(operator, expected)
|
94
|
+
return fail_with_message("expected not: #{operator} #{expected.inspect}\n got: #{operator.gsub(/./, ' ')} #{actual.inspect}")
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
module RSpec
|
2
|
+
module Matchers
|
3
|
+
module Pretty
|
4
|
+
def split_words(sym)
|
5
|
+
sym.to_s.gsub(/_/,' ')
|
6
|
+
end
|
7
|
+
|
8
|
+
def to_sentence(words)
|
9
|
+
return "" unless words
|
10
|
+
words = Array(words).map { |w| to_word(w) }
|
11
|
+
case words.length
|
12
|
+
when 0
|
13
|
+
""
|
14
|
+
when 1
|
15
|
+
" #{words[0]}"
|
16
|
+
when 2
|
17
|
+
" #{words[0]} and #{words[1]}"
|
18
|
+
else
|
19
|
+
" #{words[0...-1].join(', ')}, and #{words[-1]}"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def _pretty_print(array)
|
24
|
+
result = ""
|
25
|
+
array.each_with_index do |item, index|
|
26
|
+
if index < (array.length - 2)
|
27
|
+
result << "#{item.inspect}, "
|
28
|
+
elsif index < (array.length - 1)
|
29
|
+
result << "#{item.inspect} and "
|
30
|
+
else
|
31
|
+
result << "#{item.inspect}"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
result
|
35
|
+
end
|
36
|
+
|
37
|
+
def to_word(item)
|
38
|
+
is_matcher_with_description?(item) ? item.description : item.inspect
|
39
|
+
end
|
40
|
+
|
41
|
+
def name_to_sentence
|
42
|
+
split_words(name)
|
43
|
+
end
|
44
|
+
|
45
|
+
def expected_to_sentence
|
46
|
+
to_sentence(@expected) if defined?(@expected)
|
47
|
+
end
|
48
|
+
|
49
|
+
def name
|
50
|
+
defined?(@name) ? @name : underscore(self.class.name.split("::").last)
|
51
|
+
end
|
52
|
+
|
53
|
+
# Borrowed from ActiveSupport
|
54
|
+
def underscore(camel_cased_word)
|
55
|
+
word = camel_cased_word.to_s.dup
|
56
|
+
word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
|
57
|
+
word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
|
58
|
+
word.tr!("-", "_")
|
59
|
+
word.downcase!
|
60
|
+
word
|
61
|
+
end
|
62
|
+
|
63
|
+
private
|
64
|
+
|
65
|
+
def is_matcher_with_description?(object)
|
66
|
+
RSpec::Matchers.is_a_matcher?(object) && object.respond_to?(:description)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# Include Matchers for other test frameworks. Note that MiniTest _must_
|
2
|
+
# come before TU because on ruby 1.9, T::U::TC is a subclass of MT::U::TC
|
3
|
+
# and a 1.9 bug can lead to infinite recursion from the `super` call in our
|
4
|
+
# method_missing hook. See this gist for more info:
|
5
|
+
# https://gist.github.com/845896
|
6
|
+
if defined?(MiniTest::Unit::TestCase)
|
7
|
+
MiniTest::Unit::TestCase.send(:include, RSpec::Matchers)
|
8
|
+
end
|
9
|
+
if defined?(Test::Unit::TestCase)
|
10
|
+
Test::Unit::TestCase.send(:include, RSpec::Matchers)
|
11
|
+
end
|