opal-rspec-cj 0.4.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (176) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +3 -0
  3. data/.gitmodules +15 -0
  4. data/.travis.yml +13 -0
  5. data/.yardopts +5 -0
  6. data/CHANGELOG.md +25 -0
  7. data/Gemfile +8 -0
  8. data/README.md +147 -0
  9. data/Rakefile +26 -0
  10. data/config.ru +10 -0
  11. data/example/Gemfile +4 -0
  12. data/example/README.md +13 -0
  13. data/example/Rakefile +8 -0
  14. data/example/opal/user.rb +11 -0
  15. data/example/spec/user_spec.rb +15 -0
  16. data/lib/opal-rspec.rb +2 -0
  17. data/lib/opal/rspec.rb +20 -0
  18. data/lib/opal/rspec/rake_task.rb +63 -0
  19. data/lib/opal/rspec/version.rb +5 -0
  20. data/opal-rspec.gemspec +21 -0
  21. data/opal/opal-rspec.rb +1 -0
  22. data/opal/opal/rspec.rb +25 -0
  23. data/opal/opal/rspec/async.rb +289 -0
  24. data/opal/opal/rspec/browser_formatter.rb +188 -0
  25. data/opal/opal/rspec/fixes.rb +116 -0
  26. data/opal/opal/rspec/requires.rb +45 -0
  27. data/opal/opal/rspec/runner.rb +69 -0
  28. data/opal/opal/rspec/sprockets_runner.rb.erb +11 -0
  29. data/opal/opal/rspec/text_formatter.rb +74 -0
  30. data/spec/async_spec.rb +38 -0
  31. data/spec/example_spec.rb +163 -0
  32. data/spec/matchers_spec.rb +201 -0
  33. data/spec/mock_spec.rb +63 -0
  34. data/spec/named_subject_spec.rb +11 -0
  35. data/spec/should_syntax_spec.rb +17 -0
  36. data/vendor/spec_runner.js +50 -0
  37. data/vendor_lib/rspec-expectations.rb +1 -0
  38. data/vendor_lib/rspec.rb +3 -0
  39. data/vendor_lib/rspec/autorun.rb +2 -0
  40. data/vendor_lib/rspec/core.rb +203 -0
  41. data/vendor_lib/rspec/core/backport_random.rb +302 -0
  42. data/vendor_lib/rspec/core/backtrace_formatter.rb +65 -0
  43. data/vendor_lib/rspec/core/command_line.rb +36 -0
  44. data/vendor_lib/rspec/core/configuration.rb +1129 -0
  45. data/vendor_lib/rspec/core/configuration_options.rb +143 -0
  46. data/vendor_lib/rspec/core/drb_command_line.rb +26 -0
  47. data/vendor_lib/rspec/core/drb_options.rb +87 -0
  48. data/vendor_lib/rspec/core/dsl.rb +26 -0
  49. data/vendor_lib/rspec/core/example.rb +312 -0
  50. data/vendor_lib/rspec/core/example_group.rb +540 -0
  51. data/vendor_lib/rspec/core/filter_manager.rb +224 -0
  52. data/vendor_lib/rspec/core/flat_map.rb +17 -0
  53. data/vendor_lib/rspec/core/formatters.rb +54 -0
  54. data/vendor_lib/rspec/core/formatters/base_formatter.rb +291 -0
  55. data/vendor_lib/rspec/core/formatters/base_text_formatter.rb +307 -0
  56. data/vendor_lib/rspec/core/formatters/deprecation_formatter.rb +193 -0
  57. data/vendor_lib/rspec/core/formatters/documentation_formatter.rb +67 -0
  58. data/vendor_lib/rspec/core/formatters/helpers.rb +82 -0
  59. data/vendor_lib/rspec/core/formatters/html_formatter.rb +155 -0
  60. data/vendor_lib/rspec/core/formatters/html_printer.rb +408 -0
  61. data/vendor_lib/rspec/core/formatters/json_formatter.rb +99 -0
  62. data/vendor_lib/rspec/core/formatters/progress_formatter.rb +32 -0
  63. data/vendor_lib/rspec/core/formatters/snippet_extractor.rb +101 -0
  64. data/vendor_lib/rspec/core/hooks.rb +535 -0
  65. data/vendor_lib/rspec/core/memoized_helpers.rb +431 -0
  66. data/vendor_lib/rspec/core/metadata.rb +313 -0
  67. data/vendor_lib/rspec/core/mocking/with_absolutely_nothing.rb +11 -0
  68. data/vendor_lib/rspec/core/mocking/with_flexmock.rb +27 -0
  69. data/vendor_lib/rspec/core/mocking/with_mocha.rb +52 -0
  70. data/vendor_lib/rspec/core/mocking/with_rr.rb +27 -0
  71. data/vendor_lib/rspec/core/mocking/with_rspec.rb +27 -0
  72. data/vendor_lib/rspec/core/option_parser.rb +234 -0
  73. data/vendor_lib/rspec/core/ordering.rb +154 -0
  74. data/vendor_lib/rspec/core/pending.rb +110 -0
  75. data/vendor_lib/rspec/core/project_initializer.rb +88 -0
  76. data/vendor_lib/rspec/core/rake_task.rb +128 -0
  77. data/vendor_lib/rspec/core/reporter.rb +132 -0
  78. data/vendor_lib/rspec/core/ruby_project.rb +44 -0
  79. data/vendor_lib/rspec/core/runner.rb +97 -0
  80. data/vendor_lib/rspec/core/shared_context.rb +53 -0
  81. data/vendor_lib/rspec/core/shared_example_group.rb +146 -0
  82. data/vendor_lib/rspec/core/shared_example_group/collection.rb +27 -0
  83. data/vendor_lib/rspec/core/version.rb +7 -0
  84. data/vendor_lib/rspec/core/warnings.rb +22 -0
  85. data/vendor_lib/rspec/core/world.rb +131 -0
  86. data/vendor_lib/rspec/expectations.rb +75 -0
  87. data/vendor_lib/rspec/expectations/differ.rb +154 -0
  88. data/vendor_lib/rspec/expectations/errors.rb +9 -0
  89. data/vendor_lib/rspec/expectations/expectation_target.rb +87 -0
  90. data/vendor_lib/rspec/expectations/extensions.rb +1 -0
  91. data/vendor_lib/rspec/expectations/extensions/object.rb +29 -0
  92. data/vendor_lib/rspec/expectations/fail_with.rb +79 -0
  93. data/vendor_lib/rspec/expectations/handler.rb +68 -0
  94. data/vendor_lib/rspec/expectations/syntax.rb +182 -0
  95. data/vendor_lib/rspec/expectations/version.rb +8 -0
  96. data/vendor_lib/rspec/matchers.rb +633 -0
  97. data/vendor_lib/rspec/matchers/built_in.rb +39 -0
  98. data/vendor_lib/rspec/matchers/built_in/base_matcher.rb +68 -0
  99. data/vendor_lib/rspec/matchers/built_in/be.rb +213 -0
  100. data/vendor_lib/rspec/matchers/built_in/be_instance_of.rb +15 -0
  101. data/vendor_lib/rspec/matchers/built_in/be_kind_of.rb +11 -0
  102. data/vendor_lib/rspec/matchers/built_in/be_within.rb +55 -0
  103. data/vendor_lib/rspec/matchers/built_in/change.rb +141 -0
  104. data/vendor_lib/rspec/matchers/built_in/cover.rb +21 -0
  105. data/vendor_lib/rspec/matchers/built_in/eq.rb +22 -0
  106. data/vendor_lib/rspec/matchers/built_in/eql.rb +23 -0
  107. data/vendor_lib/rspec/matchers/built_in/equal.rb +48 -0
  108. data/vendor_lib/rspec/matchers/built_in/exist.rb +26 -0
  109. data/vendor_lib/rspec/matchers/built_in/has.rb +48 -0
  110. data/vendor_lib/rspec/matchers/built_in/include.rb +61 -0
  111. data/vendor_lib/rspec/matchers/built_in/match.rb +17 -0
  112. data/vendor_lib/rspec/matchers/built_in/match_array.rb +51 -0
  113. data/vendor_lib/rspec/matchers/built_in/raise_error.rb +154 -0
  114. data/vendor_lib/rspec/matchers/built_in/respond_to.rb +74 -0
  115. data/vendor_lib/rspec/matchers/built_in/satisfy.rb +30 -0
  116. data/vendor_lib/rspec/matchers/built_in/start_and_end_with.rb +48 -0
  117. data/vendor_lib/rspec/matchers/built_in/throw_symbol.rb +94 -0
  118. data/vendor_lib/rspec/matchers/built_in/yield.rb +297 -0
  119. data/vendor_lib/rspec/matchers/compatibility.rb +14 -0
  120. data/vendor_lib/rspec/matchers/configuration.rb +113 -0
  121. data/vendor_lib/rspec/matchers/dsl.rb +23 -0
  122. data/vendor_lib/rspec/matchers/generated_descriptions.rb +35 -0
  123. data/vendor_lib/rspec/matchers/matcher.rb +301 -0
  124. data/vendor_lib/rspec/matchers/method_missing.rb +12 -0
  125. data/vendor_lib/rspec/matchers/operator_matcher.rb +99 -0
  126. data/vendor_lib/rspec/matchers/pretty.rb +70 -0
  127. data/vendor_lib/rspec/matchers/test_unit_integration.rb +11 -0
  128. data/vendor_lib/rspec/mocks.rb +100 -0
  129. data/vendor_lib/rspec/mocks/any_instance/chain.rb +92 -0
  130. data/vendor_lib/rspec/mocks/any_instance/expectation_chain.rb +47 -0
  131. data/vendor_lib/rspec/mocks/any_instance/message_chains.rb +75 -0
  132. data/vendor_lib/rspec/mocks/any_instance/recorder.rb +200 -0
  133. data/vendor_lib/rspec/mocks/any_instance/stub_chain.rb +45 -0
  134. data/vendor_lib/rspec/mocks/any_instance/stub_chain_chain.rb +23 -0
  135. data/vendor_lib/rspec/mocks/argument_list_matcher.rb +104 -0
  136. data/vendor_lib/rspec/mocks/argument_matchers.rb +264 -0
  137. data/vendor_lib/rspec/mocks/arity_calculator.rb +66 -0
  138. data/vendor_lib/rspec/mocks/configuration.rb +111 -0
  139. data/vendor_lib/rspec/mocks/error_generator.rb +203 -0
  140. data/vendor_lib/rspec/mocks/errors.rb +12 -0
  141. data/vendor_lib/rspec/mocks/example_methods.rb +201 -0
  142. data/vendor_lib/rspec/mocks/extensions/marshal.rb +17 -0
  143. data/vendor_lib/rspec/mocks/framework.rb +36 -0
  144. data/vendor_lib/rspec/mocks/instance_method_stasher.rb +112 -0
  145. data/vendor_lib/rspec/mocks/matchers/have_received.rb +99 -0
  146. data/vendor_lib/rspec/mocks/matchers/receive.rb +112 -0
  147. data/vendor_lib/rspec/mocks/matchers/receive_messages.rb +72 -0
  148. data/vendor_lib/rspec/mocks/message_expectation.rb +643 -0
  149. data/vendor_lib/rspec/mocks/method_double.rb +209 -0
  150. data/vendor_lib/rspec/mocks/method_reference.rb +95 -0
  151. data/vendor_lib/rspec/mocks/mock.rb +7 -0
  152. data/vendor_lib/rspec/mocks/mutate_const.rb +406 -0
  153. data/vendor_lib/rspec/mocks/object_reference.rb +90 -0
  154. data/vendor_lib/rspec/mocks/order_group.rb +82 -0
  155. data/vendor_lib/rspec/mocks/proxy.rb +269 -0
  156. data/vendor_lib/rspec/mocks/proxy_for_nil.rb +37 -0
  157. data/vendor_lib/rspec/mocks/space.rb +95 -0
  158. data/vendor_lib/rspec/mocks/standalone.rb +3 -0
  159. data/vendor_lib/rspec/mocks/stub_chain.rb +51 -0
  160. data/vendor_lib/rspec/mocks/syntax.rb +374 -0
  161. data/vendor_lib/rspec/mocks/targets.rb +90 -0
  162. data/vendor_lib/rspec/mocks/test_double.rb +109 -0
  163. data/vendor_lib/rspec/mocks/verifying_double.rb +77 -0
  164. data/vendor_lib/rspec/mocks/verifying_message_expecation.rb +60 -0
  165. data/vendor_lib/rspec/mocks/verifying_proxy.rb +151 -0
  166. data/vendor_lib/rspec/mocks/version.rb +7 -0
  167. data/vendor_lib/rspec/support.rb +6 -0
  168. data/vendor_lib/rspec/support/caller_filter.rb +56 -0
  169. data/vendor_lib/rspec/support/spec.rb +14 -0
  170. data/vendor_lib/rspec/support/spec/deprecation_helpers.rb +29 -0
  171. data/vendor_lib/rspec/support/spec/in_sub_process.rb +40 -0
  172. data/vendor_lib/rspec/support/spec/stderr_splitter.rb +50 -0
  173. data/vendor_lib/rspec/support/version.rb +7 -0
  174. data/vendor_lib/rspec/support/warnings.rb +41 -0
  175. data/vendor_lib/rspec/version.rb +5 -0
  176. metadata +268 -0
@@ -0,0 +1,540 @@
1
+ module RSpec
2
+ module Core
3
+ # ExampleGroup and {Example} are the main structural elements of
4
+ # rspec-core. Consider this example:
5
+ #
6
+ # describe Thing do
7
+ # it "does something" do
8
+ # end
9
+ # end
10
+ #
11
+ # The object returned by `describe Thing` is a subclass of ExampleGroup.
12
+ # The object returned by `it "does something"` is an instance of Example,
13
+ # which serves as a wrapper for an instance of the ExampleGroup in which it
14
+ # is declared.
15
+ class ExampleGroup
16
+ extend Hooks
17
+
18
+ include MemoizedHelpers
19
+ include Pending
20
+ extend SharedExampleGroup
21
+
22
+ # @private
23
+ def self.world
24
+ RSpec.world
25
+ end
26
+
27
+ # @private
28
+ def self.register
29
+ world.register(self)
30
+ end
31
+
32
+ class << self
33
+ # @private
34
+ def self.delegate_to_metadata(*names)
35
+ names.each do |name|
36
+ define_method name do
37
+ metadata[:example_group][name]
38
+ end
39
+ end
40
+ end
41
+
42
+ def description
43
+ description = metadata[:example_group][:description]
44
+ RSpec.configuration.format_docstrings_block.call(description)
45
+ end
46
+
47
+ delegate_to_metadata :described_class, :file_path
48
+ alias_method :display_name, :description
49
+ # @private
50
+ alias_method :describes, :described_class
51
+
52
+ # @private
53
+ # @macro [attach] define_example_method
54
+ # @param [String] name
55
+ # @param [Hash] extra_options
56
+ # @param [Block] implementation
57
+ # @yield [Example] the example object
58
+ def self.define_example_method(name, extra_options={})
59
+ define_method(name) do |*all_args, &block|
60
+ desc, *args = *all_args
61
+ options = Metadata.build_hash_from(args)
62
+ options.update(:pending => RSpec::Core::Pending::NOT_YET_IMPLEMENTED) unless block
63
+ options.update(extra_options)
64
+ examples << RSpec::Core::Example.new(self, desc, options, block)
65
+ examples.last
66
+ end
67
+ end
68
+
69
+ # Defines an example within a group.
70
+ # @example
71
+ # example do
72
+ # end
73
+ #
74
+ # example "does something" do
75
+ # end
76
+ #
77
+ # example "does something", :with => 'additional metadata' do
78
+ # end
79
+ #
80
+ # example "does something" do |ex|
81
+ # # ex is the Example object that evals this block
82
+ # end
83
+ define_example_method :example
84
+ # Defines an example within a group.
85
+ # @example
86
+ define_example_method :it
87
+ # Defines an example within a group.
88
+ # This is here primarily for backward compatibility with early versions
89
+ # of RSpec which used `context` and `specify` instead of `describe` and
90
+ # `it`.
91
+ define_example_method :specify
92
+
93
+ # Shortcut to define an example with `:focus` => true
94
+ # @see example
95
+ define_example_method :focus, :focused => true, :focus => true
96
+ # Shortcut to define an example with `:focus` => true
97
+ # @see example
98
+ define_example_method :focused, :focused => true, :focus => true
99
+ # Shortcut to define an example with `:focus` => true
100
+ # @see example
101
+ define_example_method :fit, :focused => true, :focus => true
102
+
103
+ # Shortcut to define an example with :pending => true
104
+ # @see example
105
+ define_example_method :pending, :pending => true
106
+ # Shortcut to define an example with :pending => 'Temporarily disabled with xexample'
107
+ # @see example
108
+ define_example_method :xexample, :pending => 'Temporarily disabled with xexample'
109
+ # Shortcut to define an example with :pending => 'Temporarily disabled with xit'
110
+ # @see example
111
+ define_example_method :xit, :pending => 'Temporarily disabled with xit'
112
+ # Shortcut to define an example with :pending => 'Temporarily disabled with xspecify'
113
+ # @see example
114
+ define_example_method :xspecify, :pending => 'Temporarily disabled with xspecify'
115
+
116
+ # Works like `alias_method :name, :example` with the added benefit of
117
+ # assigning default metadata to the generated example.
118
+ #
119
+ # @note Use with caution. This extends the language used in your
120
+ # specs, but does not add any additional documentation. We use this
121
+ # in rspec to define methods like `focus` and `xit`, but we also add
122
+ # docs for those methods.
123
+ def alias_example_to name, extra={}
124
+ (class << self; self; end).define_example_method name, extra
125
+ end
126
+
127
+ # @private
128
+ # @macro [attach] define_nested_shared_group_method
129
+ #
130
+ # @see SharedExampleGroup
131
+ def self.define_nested_shared_group_method(new_name, report_label="it should behave like")
132
+ define_method(new_name) do |name, *args, &customization_block|
133
+ group = describe("#{report_label} #{name}") do
134
+ find_and_eval_shared("examples", name, *args, &customization_block)
135
+ end
136
+ group.metadata[:shared_group_name] = name
137
+ group
138
+ end
139
+ end
140
+
141
+ # Generates a nested example group and includes the shared content
142
+ # mapped to `name` in the nested group.
143
+ define_nested_shared_group_method :it_behaves_like, "behaves like"
144
+ # Generates a nested example group and includes the shared content
145
+ # mapped to `name` in the nested group.
146
+ define_nested_shared_group_method :it_should_behave_like
147
+
148
+ # Works like `alias_method :name, :it_behaves_like` with the added
149
+ # benefit of assigning default metadata to the generated example.
150
+ #
151
+ # @note Use with caution. This extends the language used in your
152
+ # specs, but does not add any additional documentation. We use this
153
+ # in rspec to define `it_should_behave_like` (for backward
154
+ # compatibility), but we also add docs for that method.
155
+ def alias_it_behaves_like_to name, *args, &block
156
+ (class << self; self; end).define_nested_shared_group_method name, *args, &block
157
+ end
158
+ end
159
+
160
+ # Includes shared content mapped to `name` directly in the group in which
161
+ # it is declared, as opposed to `it_behaves_like`, which creates a nested
162
+ # group. If given a block, that block is also eval'd in the current context.
163
+ #
164
+ # @see SharedExampleGroup
165
+ def self.include_context(name, *args, &block)
166
+ find_and_eval_shared("context", name, *args, &block)
167
+ end
168
+
169
+ # Includes shared content mapped to `name` directly in the group in which
170
+ # it is declared, as opposed to `it_behaves_like`, which creates a nested
171
+ # group. If given a block, that block is also eval'd in the current context.
172
+ #
173
+ # @see SharedExampleGroup
174
+ def self.include_examples(name, *args, &block)
175
+ find_and_eval_shared("examples", name, *args, &block)
176
+ end
177
+
178
+ # @private
179
+ def self.find_and_eval_shared(label, name, *args, &customization_block)
180
+ raise ArgumentError, "Could not find shared #{label} #{name.inspect}" unless
181
+ shared_block = shared_example_groups[name]
182
+
183
+ module_exec(*args, &shared_block)
184
+ module_eval(&customization_block) if customization_block
185
+ end
186
+
187
+ # @private
188
+ def self.examples
189
+ @examples ||= []
190
+ end
191
+
192
+ # @private
193
+ def self.filtered_examples
194
+ world.filtered_examples[self]
195
+ end
196
+
197
+ # @private
198
+ def self.descendant_filtered_examples
199
+ @descendant_filtered_examples ||= filtered_examples + children.inject([]){|l,c| l + c.descendant_filtered_examples}
200
+ end
201
+
202
+ # The [Metadata](Metadata) object associated with this group.
203
+ # @see Metadata
204
+ def self.metadata
205
+ @metadata if defined?(@metadata)
206
+ end
207
+
208
+ # @private
209
+ # @return [Metadata] belonging to the parent of a nested {ExampleGroup}
210
+ def self.superclass_metadata
211
+ @superclass_metadata ||= self.superclass.respond_to?(:metadata) ? self.superclass.metadata : nil
212
+ end
213
+
214
+ # Generates a subclass of this example group which inherits
215
+ # everything except the examples themselves.
216
+ #
217
+ # ## Examples
218
+ #
219
+ # describe "something" do # << This describe method is defined in
220
+ # # << RSpec::Core::DSL, included in the
221
+ # # << global namespace
222
+ # before do
223
+ # do_something_before
224
+ # end
225
+ #
226
+ # let(:thing) { Thing.new }
227
+ #
228
+ # describe "attribute (of something)" do
229
+ # # examples in the group get the before hook
230
+ # # declared above, and can access `thing`
231
+ # end
232
+ # end
233
+ #
234
+ # @see DSL#describe
235
+ def self.describe(*args, &example_group_block)
236
+ args << {} unless args.last.is_a?(Hash)
237
+ args.last.update(:example_group_block => example_group_block)
238
+
239
+ child = subclass(self, args, &example_group_block)
240
+ children << child
241
+ child
242
+ end
243
+
244
+ class << self
245
+ alias_method :context, :describe
246
+ end
247
+
248
+ # @private
249
+ def self.subclass(parent, args, &example_group_block)
250
+ subclass = Class.new(parent)
251
+ subclass.set_it_up(*args)
252
+ ExampleGroups.assign_const(subclass)
253
+ subclass.module_eval(&example_group_block) if example_group_block
254
+
255
+ # The LetDefinitions module must be included _after_ other modules
256
+ # to ensure that it takes precendence when there are name collisions.
257
+ # Thus, we delay including it until after the example group block
258
+ # has been eval'd.
259
+ MemoizedHelpers.define_helpers_on(subclass)
260
+
261
+ subclass
262
+ end
263
+
264
+ # @private
265
+ def self.children
266
+ @children ||= []
267
+ end
268
+
269
+ # @private
270
+ def self.descendants
271
+ @_descendants ||= [self] + children.inject([]) {|list, c| list + c.descendants}
272
+ end
273
+
274
+ ## @private
275
+ def self.parent_groups
276
+ @parent_groups ||= ancestors.select {|a| a < RSpec::Core::ExampleGroup}
277
+ end
278
+
279
+ # @private
280
+ def self.top_level?
281
+ @top_level ||= superclass == ExampleGroup
282
+ end
283
+
284
+ # @private
285
+ def self.ensure_example_groups_are_configured
286
+ unless defined?(@@example_groups_configured)
287
+ RSpec.configuration.configure_mock_framework
288
+ RSpec.configuration.configure_expectation_framework
289
+ @@example_groups_configured = true
290
+ end
291
+ end
292
+
293
+ # @private
294
+ def self.set_it_up(*args)
295
+ # Ruby 1.9 has a bug that can lead to infinite recursion and a
296
+ # SystemStackError if you include a module in a superclass after
297
+ # including it in a subclass: https://gist.github.com/845896
298
+ # To prevent this, we must include any modules in RSpec::Core::ExampleGroup
299
+ # before users create example groups and have a chance to include
300
+ # the same module in a subclass of RSpec::Core::ExampleGroup.
301
+ # So we need to configure example groups here.
302
+ ensure_example_groups_are_configured
303
+
304
+ symbol_description = args.shift if args.first.is_a?(Symbol)
305
+ args << Metadata.build_hash_from(args)
306
+ args.unshift(symbol_description) if symbol_description
307
+ @metadata = RSpec::Core::Metadata.new(superclass_metadata).process(*args)
308
+ @order = nil
309
+ hooks.register_globals(self, RSpec.configuration.hooks)
310
+ world.configure_group(self)
311
+ end
312
+
313
+ # @private
314
+ def self.before_all_ivars
315
+ @before_all_ivars ||= {}
316
+ end
317
+
318
+ # @private
319
+ def self.store_before_all_ivars(example_group_instance)
320
+ return if example_group_instance.instance_variables.empty?
321
+
322
+ example_group_instance.instance_variables.each { |ivar|
323
+ before_all_ivars[ivar] = example_group_instance.instance_variable_get(ivar)
324
+ }
325
+ end
326
+
327
+ # @private
328
+ def self.assign_before_all_ivars(ivars, example_group_instance)
329
+ ivars.each { |ivar, val| example_group_instance.instance_variable_set(ivar, val) }
330
+ end
331
+
332
+ # @private
333
+ def self.run_before_all_hooks(example_group_instance)
334
+ return if descendant_filtered_examples.empty?
335
+ begin
336
+ assign_before_all_ivars(superclass.before_all_ivars, example_group_instance)
337
+
338
+ AllHookMemoizedHash::Before.isolate_for_all_hook(example_group_instance) do
339
+ run_hook(:before, :all, example_group_instance)
340
+ end
341
+ ensure
342
+ store_before_all_ivars(example_group_instance)
343
+ end
344
+ end
345
+
346
+ # @private
347
+ def self.run_around_each_hooks(example, initial_procsy)
348
+ run_hook(:around, :each, example, initial_procsy)
349
+ end
350
+
351
+ # @private
352
+ def self.run_before_each_hooks(example)
353
+ run_hook(:before, :each, example)
354
+ end
355
+
356
+ # @private
357
+ def self.run_after_each_hooks(example)
358
+ run_hook(:after, :each, example)
359
+ end
360
+
361
+ # @private
362
+ def self.run_after_all_hooks(example_group_instance)
363
+ return if descendant_filtered_examples.empty?
364
+ assign_before_all_ivars(before_all_ivars, example_group_instance)
365
+
366
+ AllHookMemoizedHash::After.isolate_for_all_hook(example_group_instance) do
367
+ run_hook(:after, :all, example_group_instance)
368
+ end
369
+ end
370
+
371
+ # Runs all the examples in this group
372
+ def self.run(reporter)
373
+ if RSpec.wants_to_quit
374
+ RSpec.clear_remaining_example_groups if top_level?
375
+ return
376
+ end
377
+ reporter.example_group_started(self)
378
+
379
+ begin
380
+ run_before_all_hooks(new)
381
+ result_for_this_group = run_examples(reporter)
382
+ results_for_descendants = ordering_strategy.order(children).map { |child| child.run(reporter) }.all?
383
+ result_for_this_group && results_for_descendants
384
+ rescue Exception => ex
385
+ RSpec.wants_to_quit = true if fail_fast?
386
+ fail_filtered_examples(ex, reporter)
387
+ ensure
388
+ run_after_all_hooks(new)
389
+ before_all_ivars.clear
390
+ reporter.example_group_finished(self)
391
+ end
392
+ end
393
+
394
+ # @private
395
+ def self.ordering_strategy
396
+ order = metadata.fetch(:order, :global)
397
+ registry = RSpec.configuration.ordering_registry
398
+
399
+ registry.fetch(order) do
400
+ warn <<-WARNING.gsub(/^ +\|/, '')
401
+ |WARNING: Ignoring unknown ordering specified using `:order => #{order.inspect}` metadata.
402
+ | Falling back to configured global ordering.
403
+ | Unrecognized ordering specified at: #{metadata[:example_group][:location]}
404
+ WARNING
405
+
406
+ registry.fetch(:global)
407
+ end
408
+ end
409
+
410
+ # @private
411
+ def self.run_examples(reporter)
412
+ ordering_strategy.order(filtered_examples).map do |example|
413
+ next if RSpec.wants_to_quit
414
+ instance = new
415
+ set_ivars(instance, before_all_ivars)
416
+ succeeded = example.run(instance, reporter)
417
+ RSpec.wants_to_quit = true if fail_fast? && !succeeded
418
+ succeeded
419
+ end.all?
420
+ end
421
+
422
+ # @private
423
+ def self.fail_filtered_examples(exception, reporter)
424
+ filtered_examples.each { |example| example.fail_with_exception(reporter, exception) }
425
+
426
+ children.each do |child|
427
+ reporter.example_group_started(child)
428
+ child.fail_filtered_examples(exception, reporter)
429
+ reporter.example_group_finished(child)
430
+ end
431
+ false
432
+ end
433
+
434
+ # @private
435
+ def self.fail_fast?
436
+ RSpec.configuration.fail_fast?
437
+ end
438
+
439
+ # @private
440
+ def self.any_apply?(filters)
441
+ metadata.any_apply?(filters)
442
+ end
443
+
444
+ # @private
445
+ def self.all_apply?(filters)
446
+ metadata.all_apply?(filters)
447
+ end
448
+
449
+ # @private
450
+ def self.declaration_line_numbers
451
+ @declaration_line_numbers ||= [metadata[:example_group][:line_number]] +
452
+ examples.collect {|e| e.metadata[:line_number]} +
453
+ children.inject([]) {|l,c| l + c.declaration_line_numbers}
454
+ end
455
+
456
+ # @private
457
+ def self.top_level_description
458
+ parent_groups.last.description
459
+ end
460
+
461
+ # @private
462
+ def self.set_ivars(instance, ivars)
463
+ ivars.each {|name, value| instance.instance_variable_set(name, value)}
464
+ end
465
+
466
+ # Returns the class or module passed to the `describe` method (or alias).
467
+ # Returns nil if the subject is not a class or module.
468
+ # @example
469
+ # describe Thing do
470
+ # it "does something" do
471
+ # described_class == Thing
472
+ # end
473
+ # end
474
+ #
475
+ #
476
+ def described_class
477
+ self.class.described_class
478
+ end
479
+
480
+ # @private
481
+ # instance_evals the block, capturing and reporting an exception if
482
+ # raised
483
+ def instance_exec_with_rescue(example, context = nil, &hook)
484
+ begin
485
+ instance_exec(example, &hook)
486
+ rescue Exception => e
487
+ if RSpec.current_example
488
+ RSpec.current_example.set_exception(e, context)
489
+ else
490
+ raise
491
+ end
492
+ end
493
+ end
494
+ end
495
+ end
496
+
497
+ # Namespace for the example group subclasses generated by top-level `describe`.
498
+ module ExampleGroups
499
+ def self.assign_const(group)
500
+ base_name = base_name_for(group)
501
+ const_scope = constant_scope_for(group)
502
+ name = disambiguate(base_name, const_scope)
503
+
504
+ const_scope.const_set(name, group)
505
+ end
506
+
507
+ def self.constant_scope_for(group)
508
+ const_scope = group.superclass
509
+ const_scope = self if const_scope == Core::ExampleGroup
510
+ const_scope
511
+ end
512
+
513
+ def self.base_name_for(group)
514
+ return "Anonymous" if group.description.empty?
515
+
516
+ # convert to CamelCase
517
+ name = ' ' + group.description
518
+ name.gsub!(/[^0-9a-zA-Z]+([0-9a-zA-Z])/) { $1.upcase }
519
+
520
+ name.lstrip! # Remove leading whitespace
521
+ name.gsub!(/\W/, '') # JRuby, RBX and others don't like non-ascii in const names
522
+
523
+ # Ruby requires first const letter to be A-Z. Use `Nested`
524
+ # as necessary to enforce that.
525
+ name.gsub!(/\A([^A-Z]|\z)/, 'Nested\1')
526
+
527
+ name
528
+ end
529
+
530
+ def self.disambiguate(name, const_scope)
531
+ return name unless const_scope.const_defined?(name)
532
+
533
+ # Add a trailing number if needed to disambiguate from an existing constant.
534
+ name << "_2"
535
+ name.next! while const_scope.const_defined?(name)
536
+ name
537
+ end
538
+ end
539
+ end
540
+