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,431 @@
|
|
1
|
+
module RSpec
|
2
|
+
module Core
|
3
|
+
module MemoizedHelpers
|
4
|
+
# @note `subject` was contributed by Joe Ferris to support the one-liner
|
5
|
+
# syntax embraced by shoulda matchers:
|
6
|
+
#
|
7
|
+
# describe Widget do
|
8
|
+
# it { should validate_presence_of(:name) }
|
9
|
+
# end
|
10
|
+
#
|
11
|
+
# While the examples below demonstrate how to use `subject`
|
12
|
+
# explicitly in examples, we recommend that you define a method with
|
13
|
+
# an intention revealing name instead.
|
14
|
+
#
|
15
|
+
# @example
|
16
|
+
#
|
17
|
+
# # explicit declaration of subject
|
18
|
+
# describe Person do
|
19
|
+
# subject { Person.new(:birthdate => 19.years.ago) }
|
20
|
+
# it "should be eligible to vote" do
|
21
|
+
# subject.should be_eligible_to_vote
|
22
|
+
# # ^ ^ explicit reference to subject not recommended
|
23
|
+
# end
|
24
|
+
# end
|
25
|
+
#
|
26
|
+
# # implicit subject => { Person.new }
|
27
|
+
# describe Person do
|
28
|
+
# it "should be eligible to vote" do
|
29
|
+
# subject.should be_eligible_to_vote
|
30
|
+
# # ^ ^ explicit reference to subject not recommended
|
31
|
+
# end
|
32
|
+
# end
|
33
|
+
#
|
34
|
+
# # one-liner syntax - should is invoked on subject
|
35
|
+
# describe Person do
|
36
|
+
# it { should be_eligible_to_vote }
|
37
|
+
# end
|
38
|
+
#
|
39
|
+
# @note Because `subject` is designed to create state that is reset between
|
40
|
+
# each example, and `before(:all)` is designed to setup state that is
|
41
|
+
# shared across _all_ examples in an example group, `subject` is _not_
|
42
|
+
# intended to be used in a `before(:all)` hook. RSpec 2.13.1 prints
|
43
|
+
# a warning when you reference a `subject` from `before(:all)` and we plan
|
44
|
+
# to have it raise an error in RSpec 3.
|
45
|
+
#
|
46
|
+
# @see #should
|
47
|
+
def subject
|
48
|
+
__memoized.fetch(:subject) do
|
49
|
+
__memoized[:subject] = begin
|
50
|
+
described = described_class || self.class.description
|
51
|
+
Class === described ? described.new : described
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
# When `should` is called with no explicit receiver, the call is
|
57
|
+
# delegated to the object returned by `subject`. Combined with an
|
58
|
+
# implicit subject this supports very concise expressions.
|
59
|
+
#
|
60
|
+
# @example
|
61
|
+
#
|
62
|
+
# describe Person do
|
63
|
+
# it { should be_eligible_to_vote }
|
64
|
+
# end
|
65
|
+
#
|
66
|
+
# @see #subject
|
67
|
+
def should(matcher=nil, message=nil)
|
68
|
+
RSpec::Expectations::PositiveExpectationHandler.handle_matcher(subject, matcher, message)
|
69
|
+
end
|
70
|
+
|
71
|
+
# Just like `should`, `should_not` delegates to the subject (implicit or
|
72
|
+
# explicit) of the example group.
|
73
|
+
#
|
74
|
+
# @example
|
75
|
+
#
|
76
|
+
# describe Person do
|
77
|
+
# it { should_not be_eligible_to_vote }
|
78
|
+
# end
|
79
|
+
#
|
80
|
+
# @see #subject
|
81
|
+
def should_not(matcher=nil, message=nil)
|
82
|
+
RSpec::Expectations::NegativeExpectationHandler.handle_matcher(subject, matcher, message)
|
83
|
+
end
|
84
|
+
|
85
|
+
private
|
86
|
+
|
87
|
+
# @private
|
88
|
+
def __memoized
|
89
|
+
@__memoized ||= {}
|
90
|
+
end
|
91
|
+
|
92
|
+
# Used internally to customize the behavior of the
|
93
|
+
# memoized hash when used in a `before(:all)` hook.
|
94
|
+
#
|
95
|
+
# @private
|
96
|
+
class AllHookMemoizedHash
|
97
|
+
def self.isolate_for_all_hook(example_group_instance)
|
98
|
+
hash = self
|
99
|
+
|
100
|
+
example_group_instance.instance_eval do
|
101
|
+
@__memoized = hash
|
102
|
+
|
103
|
+
begin
|
104
|
+
yield
|
105
|
+
ensure
|
106
|
+
@__memoized = nil
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
def self.fetch(key, &block)
|
112
|
+
description = if key == :subject
|
113
|
+
"subject"
|
114
|
+
else
|
115
|
+
"let declaration `#{key}`"
|
116
|
+
end
|
117
|
+
|
118
|
+
raise <<-EOS
|
119
|
+
#{description} accessed in #{article} #{hook_expression} hook at:
|
120
|
+
#{CallerFilter.first_non_rspec_line}
|
121
|
+
|
122
|
+
`let` and `subject` declarations are not intended to be called
|
123
|
+
in #{article} #{hook_expression} hook, as they exist to define state that
|
124
|
+
is reset between each example, while #{hook_expression} exists to
|
125
|
+
#{hook_intention}.
|
126
|
+
EOS
|
127
|
+
end
|
128
|
+
|
129
|
+
class Before < self
|
130
|
+
def self.hook_expression
|
131
|
+
"`before(:all)`"
|
132
|
+
end
|
133
|
+
|
134
|
+
def self.article
|
135
|
+
"a"
|
136
|
+
end
|
137
|
+
|
138
|
+
def self.hook_intention
|
139
|
+
"define state that is shared across examples in an example group"
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
class After < self
|
144
|
+
def self.hook_expression
|
145
|
+
"`after(:all)`"
|
146
|
+
end
|
147
|
+
|
148
|
+
def self.article
|
149
|
+
"an"
|
150
|
+
end
|
151
|
+
|
152
|
+
def self.hook_intention
|
153
|
+
"cleanup state that is shared across examples in an example group"
|
154
|
+
end
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
def self.included(mod)
|
159
|
+
mod.extend(ClassMethods)
|
160
|
+
end
|
161
|
+
|
162
|
+
module ClassMethods
|
163
|
+
# Generates a method whose return value is memoized after the first
|
164
|
+
# call. Useful for reducing duplication between examples that assign
|
165
|
+
# values to the same local variable.
|
166
|
+
#
|
167
|
+
# @note `let` _can_ enhance readability when used sparingly (1,2, or
|
168
|
+
# maybe 3 declarations) in any given example group, but that can
|
169
|
+
# quickly degrade with overuse. YMMV.
|
170
|
+
#
|
171
|
+
# @note `let` uses an `||=` conditional that has the potential to
|
172
|
+
# behave in surprising ways in examples that spawn separate threads,
|
173
|
+
# though we have yet to see this in practice. You've been warned.
|
174
|
+
#
|
175
|
+
# @note Because `let` is designed to create state that is reset between
|
176
|
+
# each example, and `before(:all)` is designed to setup state that is
|
177
|
+
# shared across _all_ examples in an example group, `let` is _not_
|
178
|
+
# intended to be used in a `before(:all)` hook. RSpec 2.13.1 prints
|
179
|
+
# a warning when you reference a `let` from `before(:all)` and we plan
|
180
|
+
# to have it raise an error in RSpec 3.
|
181
|
+
#
|
182
|
+
# @example
|
183
|
+
#
|
184
|
+
# describe Thing do
|
185
|
+
# let(:thing) { Thing.new }
|
186
|
+
#
|
187
|
+
# it "does something" do
|
188
|
+
# # first invocation, executes block, memoizes and returns result
|
189
|
+
# thing.do_something
|
190
|
+
#
|
191
|
+
# # second invocation, returns the memoized value
|
192
|
+
# thing.should be_something
|
193
|
+
# end
|
194
|
+
# end
|
195
|
+
def let(name, &block)
|
196
|
+
# We have to pass the block directly to `define_method` to
|
197
|
+
# allow it to use method constructs like `super` and `return`.
|
198
|
+
raise "#let or #subject called without a block" if block.nil?
|
199
|
+
MemoizedHelpers.module_for(self).send(:define_method, name, &block)
|
200
|
+
|
201
|
+
# Apply the memoization. The method has been defined in an ancestor
|
202
|
+
# module so we can use `super` here to get the value.
|
203
|
+
if block.arity == 1
|
204
|
+
define_method(name) { __memoized.fetch(name) { |k| __memoized[k] = super(RSpec.current_example, &nil) } }
|
205
|
+
else
|
206
|
+
define_method(name) { __memoized.fetch(name) { |k| __memoized[k] = super(&nil) } }
|
207
|
+
end
|
208
|
+
end
|
209
|
+
|
210
|
+
# Just like `let`, except the block is invoked by an implicit `before`
|
211
|
+
# hook. This serves a dual purpose of setting up state and providing a
|
212
|
+
# memoized reference to that state.
|
213
|
+
#
|
214
|
+
# @example
|
215
|
+
#
|
216
|
+
# class Thing
|
217
|
+
# def self.count
|
218
|
+
# @count ||= 0
|
219
|
+
# end
|
220
|
+
#
|
221
|
+
# def self.count=(val)
|
222
|
+
# @count += val
|
223
|
+
# end
|
224
|
+
#
|
225
|
+
# def self.reset_count
|
226
|
+
# @count = 0
|
227
|
+
# end
|
228
|
+
#
|
229
|
+
# def initialize
|
230
|
+
# self.class.count += 1
|
231
|
+
# end
|
232
|
+
# end
|
233
|
+
#
|
234
|
+
# describe Thing do
|
235
|
+
# after(:each) { Thing.reset_count }
|
236
|
+
#
|
237
|
+
# context "using let" do
|
238
|
+
# let(:thing) { Thing.new }
|
239
|
+
#
|
240
|
+
# it "is not invoked implicitly" do
|
241
|
+
# Thing.count.should eq(0)
|
242
|
+
# end
|
243
|
+
#
|
244
|
+
# it "can be invoked explicitly" do
|
245
|
+
# thing
|
246
|
+
# Thing.count.should eq(1)
|
247
|
+
# end
|
248
|
+
# end
|
249
|
+
#
|
250
|
+
# context "using let!" do
|
251
|
+
# let!(:thing) { Thing.new }
|
252
|
+
#
|
253
|
+
# it "is invoked implicitly" do
|
254
|
+
# Thing.count.should eq(1)
|
255
|
+
# end
|
256
|
+
#
|
257
|
+
# it "returns memoized version on first invocation" do
|
258
|
+
# thing
|
259
|
+
# Thing.count.should eq(1)
|
260
|
+
# end
|
261
|
+
# end
|
262
|
+
# end
|
263
|
+
def let!(name, &block)
|
264
|
+
let(name, &block)
|
265
|
+
before { __send__(name) }
|
266
|
+
end
|
267
|
+
|
268
|
+
# Declares a `subject` for an example group which can then be the
|
269
|
+
# implicit receiver (through delegation) of calls to `should`.
|
270
|
+
#
|
271
|
+
# Given a `name`, defines a method with that name which returns the
|
272
|
+
# `subject`. This lets you declare the subject once and access it
|
273
|
+
# implicitly in one-liners and explicitly using an intention revealing
|
274
|
+
# name.
|
275
|
+
#
|
276
|
+
# @param [String,Symbol] name used to define an accessor with an
|
277
|
+
# intention revealing name
|
278
|
+
# @param block defines the value to be returned by `subject` in examples
|
279
|
+
#
|
280
|
+
# @example
|
281
|
+
#
|
282
|
+
# describe CheckingAccount, "with $50" do
|
283
|
+
# subject { CheckingAccount.new(Money.new(50, :USD)) }
|
284
|
+
# it { should have_a_balance_of(Money.new(50, :USD)) }
|
285
|
+
# it { should_not be_overdrawn }
|
286
|
+
# end
|
287
|
+
#
|
288
|
+
# describe CheckingAccount, "with a non-zero starting balance" do
|
289
|
+
# subject(:account) { CheckingAccount.new(Money.new(50, :USD)) }
|
290
|
+
# it { should_not be_overdrawn }
|
291
|
+
# it "has a balance equal to the starting balance" do
|
292
|
+
# account.balance.should eq(Money.new(50, :USD))
|
293
|
+
# end
|
294
|
+
# end
|
295
|
+
#
|
296
|
+
# @see MemoizedHelpers#should
|
297
|
+
def subject(name=nil, &block)
|
298
|
+
if name
|
299
|
+
let(name, &block)
|
300
|
+
alias_method :subject, name
|
301
|
+
|
302
|
+
self::NamedSubjectPreventSuper.send(:define_method, name) do
|
303
|
+
raise NotImplementedError, "`super` in named subjects is not supported"
|
304
|
+
end
|
305
|
+
else
|
306
|
+
let(:subject, &block)
|
307
|
+
end
|
308
|
+
end
|
309
|
+
|
310
|
+
# Just like `subject`, except the block is invoked by an implicit `before`
|
311
|
+
# hook. This serves a dual purpose of setting up state and providing a
|
312
|
+
# memoized reference to that state.
|
313
|
+
#
|
314
|
+
# @example
|
315
|
+
#
|
316
|
+
# class Thing
|
317
|
+
# def self.count
|
318
|
+
# @count ||= 0
|
319
|
+
# end
|
320
|
+
#
|
321
|
+
# def self.count=(val)
|
322
|
+
# @count += val
|
323
|
+
# end
|
324
|
+
#
|
325
|
+
# def self.reset_count
|
326
|
+
# @count = 0
|
327
|
+
# end
|
328
|
+
#
|
329
|
+
# def initialize
|
330
|
+
# self.class.count += 1
|
331
|
+
# end
|
332
|
+
# end
|
333
|
+
#
|
334
|
+
# describe Thing do
|
335
|
+
# after(:each) { Thing.reset_count }
|
336
|
+
#
|
337
|
+
# context "using subject" do
|
338
|
+
# subject { Thing.new }
|
339
|
+
#
|
340
|
+
# it "is not invoked implicitly" do
|
341
|
+
# Thing.count.should eq(0)
|
342
|
+
# end
|
343
|
+
#
|
344
|
+
# it "can be invoked explicitly" do
|
345
|
+
# subject
|
346
|
+
# Thing.count.should eq(1)
|
347
|
+
# end
|
348
|
+
# end
|
349
|
+
#
|
350
|
+
# context "using subject!" do
|
351
|
+
# subject!(:thing) { Thing.new }
|
352
|
+
#
|
353
|
+
# it "is invoked implicitly" do
|
354
|
+
# Thing.count.should eq(1)
|
355
|
+
# end
|
356
|
+
#
|
357
|
+
# it "returns memoized version on first invocation" do
|
358
|
+
# subject
|
359
|
+
# Thing.count.should eq(1)
|
360
|
+
# end
|
361
|
+
# end
|
362
|
+
# end
|
363
|
+
def subject!(name=nil, &block)
|
364
|
+
subject(name, &block)
|
365
|
+
before { subject }
|
366
|
+
end
|
367
|
+
end
|
368
|
+
|
369
|
+
# @api private
|
370
|
+
#
|
371
|
+
# Gets the LetDefinitions module. The module is mixed into
|
372
|
+
# the example group and is used to hold all let definitions.
|
373
|
+
# This is done so that the block passed to `let` can be
|
374
|
+
# forwarded directly on to `define_method`, so that all method
|
375
|
+
# constructs (including `super` and `return`) can be used in
|
376
|
+
# a `let` block.
|
377
|
+
#
|
378
|
+
# The memoization is provided by a method definition on the
|
379
|
+
# example group that supers to the LetDefinitions definition
|
380
|
+
# in order to get the value to memoize.
|
381
|
+
def self.module_for(example_group)
|
382
|
+
get_constant_or_yield(example_group, :LetDefinitions) do
|
383
|
+
mod = Module.new do
|
384
|
+
include Module.new {
|
385
|
+
example_group.const_set(:NamedSubjectPreventSuper, self)
|
386
|
+
}
|
387
|
+
end
|
388
|
+
|
389
|
+
example_group.const_set(:LetDefinitions, mod)
|
390
|
+
mod
|
391
|
+
end
|
392
|
+
end
|
393
|
+
|
394
|
+
# @api private
|
395
|
+
def self.define_helpers_on(example_group)
|
396
|
+
example_group.send(:include, module_for(example_group))
|
397
|
+
end
|
398
|
+
|
399
|
+
if Module.method(:const_defined?).arity == 1 # for 1.8
|
400
|
+
# @api private
|
401
|
+
#
|
402
|
+
# Gets the named constant or yields.
|
403
|
+
# On 1.8, const_defined? / const_get do not take into
|
404
|
+
# account the inheritance hierarchy.
|
405
|
+
def self.get_constant_or_yield(example_group, name)
|
406
|
+
if example_group.const_defined?(name)
|
407
|
+
example_group.const_get(name)
|
408
|
+
else
|
409
|
+
yield
|
410
|
+
end
|
411
|
+
end
|
412
|
+
else
|
413
|
+
# @api private
|
414
|
+
#
|
415
|
+
# Gets the named constant or yields.
|
416
|
+
# On 1.9, const_defined? / const_get take into account the
|
417
|
+
# the inheritance by default, and accept an argument to
|
418
|
+
# disable this behavior. It's important that we don't
|
419
|
+
# consider inheritance here; each example group level that
|
420
|
+
# uses a `let` should get its own `LetDefinitions` module.
|
421
|
+
def self.get_constant_or_yield(example_group, name)
|
422
|
+
if example_group.const_defined?(name, (check_ancestors = false))
|
423
|
+
example_group.const_get(name, check_ancestors)
|
424
|
+
else
|
425
|
+
yield
|
426
|
+
end
|
427
|
+
end
|
428
|
+
end
|
429
|
+
end
|
430
|
+
end
|
431
|
+
end
|