rspec-core 2.11.1 → 3.11.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (222) hide show
  1. checksums.yaml +7 -0
  2. checksums.yaml.gz.sig +0 -0
  3. data/.document +1 -1
  4. data/.yardopts +3 -1
  5. data/Changelog.md +1814 -29
  6. data/{License.txt → LICENSE.md} +6 -4
  7. data/README.md +197 -48
  8. data/exe/rspec +2 -23
  9. data/lib/rspec/autorun.rb +1 -0
  10. data/lib/rspec/core/backtrace_formatter.rb +65 -0
  11. data/lib/rspec/core/bisect/coordinator.rb +62 -0
  12. data/lib/rspec/core/bisect/example_minimizer.rb +173 -0
  13. data/lib/rspec/core/bisect/fork_runner.rb +138 -0
  14. data/lib/rspec/core/bisect/server.rb +61 -0
  15. data/lib/rspec/core/bisect/shell_command.rb +126 -0
  16. data/lib/rspec/core/bisect/shell_runner.rb +73 -0
  17. data/lib/rspec/core/bisect/utilities.rb +69 -0
  18. data/lib/rspec/core/configuration.rb +1846 -407
  19. data/lib/rspec/core/configuration_options.rb +154 -50
  20. data/lib/rspec/core/did_you_mean.rb +46 -0
  21. data/lib/rspec/core/drb.rb +120 -0
  22. data/lib/rspec/core/dsl.rb +90 -18
  23. data/lib/rspec/core/example.rb +488 -152
  24. data/lib/rspec/core/example_group.rb +733 -294
  25. data/lib/rspec/core/example_status_persister.rb +235 -0
  26. data/lib/rspec/core/filter_manager.rb +175 -147
  27. data/lib/rspec/core/flat_map.rb +20 -0
  28. data/lib/rspec/core/formatters/base_bisect_formatter.rb +45 -0
  29. data/lib/rspec/core/formatters/base_formatter.rb +32 -130
  30. data/lib/rspec/core/formatters/base_text_formatter.rb +62 -190
  31. data/lib/rspec/core/formatters/bisect_drb_formatter.rb +29 -0
  32. data/lib/rspec/core/formatters/bisect_progress_formatter.rb +157 -0
  33. data/lib/rspec/core/formatters/console_codes.rb +76 -0
  34. data/lib/rspec/core/formatters/deprecation_formatter.rb +223 -0
  35. data/lib/rspec/core/formatters/documentation_formatter.rb +62 -27
  36. data/lib/rspec/core/formatters/exception_presenter.rb +521 -0
  37. data/lib/rspec/core/formatters/failure_list_formatter.rb +23 -0
  38. data/lib/rspec/core/formatters/fallback_message_formatter.rb +28 -0
  39. data/lib/rspec/core/formatters/helpers.rb +93 -14
  40. data/lib/rspec/core/formatters/html_formatter.rb +104 -415
  41. data/lib/rspec/core/formatters/html_printer.rb +414 -0
  42. data/lib/rspec/core/formatters/html_snippet_extractor.rb +120 -0
  43. data/lib/rspec/core/formatters/json_formatter.rb +102 -0
  44. data/lib/rspec/core/formatters/profile_formatter.rb +68 -0
  45. data/lib/rspec/core/formatters/progress_formatter.rb +12 -15
  46. data/lib/rspec/core/formatters/protocol.rb +182 -0
  47. data/lib/rspec/core/formatters/snippet_extractor.rb +115 -39
  48. data/lib/rspec/core/formatters/syntax_highlighter.rb +91 -0
  49. data/lib/rspec/core/formatters.rb +279 -0
  50. data/lib/rspec/core/hooks.rb +451 -300
  51. data/lib/rspec/core/invocations.rb +87 -0
  52. data/lib/rspec/core/memoized_helpers.rb +580 -0
  53. data/lib/rspec/core/metadata.rb +395 -173
  54. data/lib/rspec/core/metadata_filter.rb +255 -0
  55. data/lib/rspec/core/minitest_assertions_adapter.rb +31 -0
  56. data/lib/rspec/core/mocking_adapters/flexmock.rb +31 -0
  57. data/lib/rspec/core/mocking_adapters/mocha.rb +57 -0
  58. data/lib/rspec/core/mocking_adapters/null.rb +14 -0
  59. data/lib/rspec/core/mocking_adapters/rr.rb +31 -0
  60. data/lib/rspec/core/mocking_adapters/rspec.rb +32 -0
  61. data/lib/rspec/core/notifications.rb +521 -0
  62. data/lib/rspec/core/option_parser.rb +208 -64
  63. data/lib/rspec/core/ordering.rb +169 -0
  64. data/lib/rspec/core/output_wrapper.rb +29 -0
  65. data/lib/rspec/core/pending.rb +115 -59
  66. data/lib/rspec/core/profiler.rb +34 -0
  67. data/lib/rspec/core/project_initializer/.rspec +1 -0
  68. data/lib/rspec/core/project_initializer/spec/spec_helper.rb +98 -0
  69. data/lib/rspec/core/project_initializer.rb +26 -65
  70. data/lib/rspec/core/rake_task.rb +140 -131
  71. data/lib/rspec/core/reporter.rb +207 -44
  72. data/lib/rspec/core/ruby_project.rb +15 -6
  73. data/lib/rspec/core/runner.rb +180 -44
  74. data/lib/rspec/core/sandbox.rb +37 -0
  75. data/lib/rspec/core/set.rb +54 -0
  76. data/lib/rspec/core/shared_context.rb +25 -19
  77. data/lib/rspec/core/shared_example_group.rb +229 -54
  78. data/lib/rspec/core/shell_escape.rb +49 -0
  79. data/lib/rspec/core/test_unit_assertions_adapter.rb +30 -0
  80. data/lib/rspec/core/version.rb +3 -1
  81. data/lib/rspec/core/warnings.rb +40 -0
  82. data/lib/rspec/core/world.rb +208 -49
  83. data/lib/rspec/core.rb +166 -80
  84. data.tar.gz.sig +0 -0
  85. metadata +230 -445
  86. metadata.gz.sig +0 -0
  87. data/exe/autospec +0 -13
  88. data/features/Autotest.md +0 -38
  89. data/features/README.md +0 -17
  90. data/features/Upgrade.md +0 -364
  91. data/features/command_line/README.md +0 -28
  92. data/features/command_line/example_name_option.feature +0 -101
  93. data/features/command_line/exit_status.feature +0 -83
  94. data/features/command_line/format_option.feature +0 -81
  95. data/features/command_line/init.feature +0 -18
  96. data/features/command_line/line_number_appended_to_path.feature +0 -140
  97. data/features/command_line/line_number_option.feature +0 -58
  98. data/features/command_line/order.feature +0 -29
  99. data/features/command_line/pattern_option.feature +0 -31
  100. data/features/command_line/rake_task.feature +0 -68
  101. data/features/command_line/ruby.feature +0 -22
  102. data/features/command_line/tag.feature +0 -91
  103. data/features/configuration/alias_example_to.feature +0 -48
  104. data/features/configuration/custom_settings.feature +0 -84
  105. data/features/configuration/default_path.feature +0 -38
  106. data/features/configuration/fail_fast.feature +0 -77
  107. data/features/configuration/read_options_from_file.feature +0 -87
  108. data/features/example_groups/basic_structure.feature +0 -55
  109. data/features/example_groups/shared_context.feature +0 -74
  110. data/features/example_groups/shared_examples.feature +0 -204
  111. data/features/expectation_framework_integration/configure_expectation_framework.feature +0 -102
  112. data/features/filtering/exclusion_filters.feature +0 -139
  113. data/features/filtering/if_and_unless.feature +0 -168
  114. data/features/filtering/inclusion_filters.feature +0 -105
  115. data/features/filtering/run_all_when_everything_filtered.feature +0 -46
  116. data/features/formatters/custom_formatter.feature +0 -36
  117. data/features/formatters/text_formatter.feature +0 -46
  118. data/features/helper_methods/arbitrary_methods.feature +0 -40
  119. data/features/helper_methods/let.feature +0 -50
  120. data/features/helper_methods/modules.feature +0 -149
  121. data/features/hooks/around_hooks.feature +0 -343
  122. data/features/hooks/before_and_after_hooks.feature +0 -423
  123. data/features/hooks/filtering.feature +0 -234
  124. data/features/metadata/current_example.feature +0 -17
  125. data/features/metadata/described_class.feature +0 -17
  126. data/features/metadata/user_defined.feature +0 -113
  127. data/features/mock_framework_integration/use_any_framework.feature +0 -106
  128. data/features/mock_framework_integration/use_flexmock.feature +0 -96
  129. data/features/mock_framework_integration/use_mocha.feature +0 -97
  130. data/features/mock_framework_integration/use_rr.feature +0 -98
  131. data/features/mock_framework_integration/use_rspec.feature +0 -97
  132. data/features/pending/pending_examples.feature +0 -229
  133. data/features/spec_files/arbitrary_file_suffix.feature +0 -13
  134. data/features/step_definitions/additional_cli_steps.rb +0 -30
  135. data/features/subject/attribute_of_subject.feature +0 -124
  136. data/features/subject/explicit_subject.feature +0 -82
  137. data/features/subject/implicit_receiver.feature +0 -29
  138. data/features/subject/implicit_subject.feature +0 -63
  139. data/features/support/env.rb +0 -12
  140. data/lib/autotest/discover.rb +0 -1
  141. data/lib/autotest/rspec2.rb +0 -73
  142. data/lib/rspec/core/backward_compatibility.rb +0 -65
  143. data/lib/rspec/core/command_line.rb +0 -36
  144. data/lib/rspec/core/deprecation.rb +0 -36
  145. data/lib/rspec/core/drb_command_line.rb +0 -26
  146. data/lib/rspec/core/drb_options.rb +0 -87
  147. data/lib/rspec/core/extensions/instance_eval_with_args.rb +0 -44
  148. data/lib/rspec/core/extensions/kernel.rb +0 -9
  149. data/lib/rspec/core/extensions/module_eval_with_args.rb +0 -38
  150. data/lib/rspec/core/extensions/ordered.rb +0 -21
  151. data/lib/rspec/core/extensions.rb +0 -4
  152. data/lib/rspec/core/formatters/text_mate_formatter.rb +0 -34
  153. data/lib/rspec/core/let.rb +0 -110
  154. data/lib/rspec/core/load_path.rb +0 -3
  155. data/lib/rspec/core/metadata_hash_builder.rb +0 -97
  156. data/lib/rspec/core/mocking/with_absolutely_nothing.rb +0 -11
  157. data/lib/rspec/core/mocking/with_flexmock.rb +0 -27
  158. data/lib/rspec/core/mocking/with_mocha.rb +0 -29
  159. data/lib/rspec/core/mocking/with_rr.rb +0 -27
  160. data/lib/rspec/core/mocking/with_rspec.rb +0 -23
  161. data/lib/rspec/core/subject.rb +0 -219
  162. data/spec/autotest/discover_spec.rb +0 -19
  163. data/spec/autotest/failed_results_re_spec.rb +0 -45
  164. data/spec/autotest/rspec_spec.rb +0 -123
  165. data/spec/command_line/order_spec.rb +0 -137
  166. data/spec/rspec/core/command_line_spec.rb +0 -108
  167. data/spec/rspec/core/command_line_spec_output.txt +0 -0
  168. data/spec/rspec/core/configuration_options_spec.rb +0 -377
  169. data/spec/rspec/core/configuration_spec.rb +0 -1196
  170. data/spec/rspec/core/deprecations_spec.rb +0 -66
  171. data/spec/rspec/core/drb_command_line_spec.rb +0 -108
  172. data/spec/rspec/core/drb_options_spec.rb +0 -180
  173. data/spec/rspec/core/dsl_spec.rb +0 -17
  174. data/spec/rspec/core/example_group_spec.rb +0 -1098
  175. data/spec/rspec/core/example_spec.rb +0 -370
  176. data/spec/rspec/core/filter_manager_spec.rb +0 -256
  177. data/spec/rspec/core/formatters/base_formatter_spec.rb +0 -80
  178. data/spec/rspec/core/formatters/base_text_formatter_spec.rb +0 -363
  179. data/spec/rspec/core/formatters/documentation_formatter_spec.rb +0 -88
  180. data/spec/rspec/core/formatters/helpers_spec.rb +0 -66
  181. data/spec/rspec/core/formatters/html_formatted-1.8.7-jruby.html +0 -410
  182. data/spec/rspec/core/formatters/html_formatted-1.8.7.html +0 -409
  183. data/spec/rspec/core/formatters/html_formatted-1.9.2.html +0 -416
  184. data/spec/rspec/core/formatters/html_formatted-1.9.3.html +0 -416
  185. data/spec/rspec/core/formatters/html_formatter_spec.rb +0 -82
  186. data/spec/rspec/core/formatters/progress_formatter_spec.rb +0 -30
  187. data/spec/rspec/core/formatters/snippet_extractor_spec.rb +0 -18
  188. data/spec/rspec/core/formatters/text_mate_formatted-1.8.7-jruby.html +0 -410
  189. data/spec/rspec/core/formatters/text_mate_formatted-1.8.7.html +0 -409
  190. data/spec/rspec/core/formatters/text_mate_formatted-1.9.2.html +0 -416
  191. data/spec/rspec/core/formatters/text_mate_formatted-1.9.3.html +0 -416
  192. data/spec/rspec/core/formatters/text_mate_formatter_spec.rb +0 -83
  193. data/spec/rspec/core/hooks_filtering_spec.rb +0 -227
  194. data/spec/rspec/core/hooks_spec.rb +0 -250
  195. data/spec/rspec/core/kernel_extensions_spec.rb +0 -9
  196. data/spec/rspec/core/let_spec.rb +0 -55
  197. data/spec/rspec/core/metadata_spec.rb +0 -447
  198. data/spec/rspec/core/option_parser_spec.rb +0 -166
  199. data/spec/rspec/core/pending_example_spec.rb +0 -220
  200. data/spec/rspec/core/project_initializer_spec.rb +0 -130
  201. data/spec/rspec/core/rake_task_spec.rb +0 -138
  202. data/spec/rspec/core/reporter_spec.rb +0 -103
  203. data/spec/rspec/core/resources/a_bar.rb +0 -0
  204. data/spec/rspec/core/resources/a_foo.rb +0 -0
  205. data/spec/rspec/core/resources/a_spec.rb +0 -1
  206. data/spec/rspec/core/resources/custom_example_group_runner.rb +0 -14
  207. data/spec/rspec/core/resources/formatter_specs.rb +0 -60
  208. data/spec/rspec/core/resources/utf8_encoded.rb +0 -8
  209. data/spec/rspec/core/rspec_matchers_spec.rb +0 -45
  210. data/spec/rspec/core/ruby_project_spec.rb +0 -24
  211. data/spec/rspec/core/runner_spec.rb +0 -81
  212. data/spec/rspec/core/shared_context_spec.rb +0 -67
  213. data/spec/rspec/core/shared_example_group_spec.rb +0 -84
  214. data/spec/rspec/core/subject_spec.rb +0 -244
  215. data/spec/rspec/core/world_spec.rb +0 -144
  216. data/spec/rspec/core_spec.rb +0 -35
  217. data/spec/spec_helper.rb +0 -98
  218. data/spec/support/config_options_helper.rb +0 -24
  219. data/spec/support/helper_methods.rb +0 -5
  220. data/spec/support/matchers.rb +0 -65
  221. data/spec/support/shared_example_groups.rb +0 -41
  222. data/spec/support/spec_files.rb +0 -44
@@ -1,75 +1,211 @@
1
1
  module RSpec
2
2
  module Core
3
+ # Provides the main entry point to run a suite of RSpec examples.
3
4
  class Runner
5
+ # @attr_reader
6
+ # @private
7
+ attr_reader :options, :configuration, :world
4
8
 
5
- # Register an at_exit hook that runs the suite.
9
+ # Register an `at_exit` hook that runs the suite when the process exits.
10
+ #
11
+ # @note This is not generally needed. The `rspec` command takes care
12
+ # of running examples for you without involving an `at_exit`
13
+ # hook. This is only needed if you are running specs using
14
+ # the `ruby` command, and even then, the normal way to invoke
15
+ # this is by requiring `rspec/autorun`.
6
16
  def self.autorun
7
- return if autorun_disabled? || installed_at_exit? || running_in_drb?
8
- at_exit { exit run(ARGV, $stderr, $stdout).to_i unless $! }
17
+ if autorun_disabled?
18
+ RSpec.deprecate("Requiring `rspec/autorun` when running RSpec via the `rspec` command")
19
+ return
20
+ elsif installed_at_exit? || running_in_drb?
21
+ return
22
+ end
23
+
24
+ at_exit { perform_at_exit }
9
25
  @installed_at_exit = true
10
26
  end
11
- AT_EXIT_HOOK_BACKTRACE_LINE = "#{__FILE__}:#{__LINE__ - 2}:in `autorun'"
12
27
 
28
+ # @private
29
+ def self.perform_at_exit
30
+ # Don't bother running any specs and just let the program terminate
31
+ # if we got here due to an unrescued exception (anything other than
32
+ # SystemExit, which is raised when somebody calls Kernel#exit).
33
+ return unless $!.nil? || $!.is_a?(SystemExit)
34
+
35
+ # We got here because either the end of the program was reached or
36
+ # somebody called Kernel#exit. Run the specs and then override any
37
+ # existing exit status with RSpec's exit status if any specs failed.
38
+ invoke
39
+ end
40
+
41
+ # Runs the suite of specs and exits the process with an appropriate exit
42
+ # code.
43
+ def self.invoke
44
+ disable_autorun!
45
+ status = run(ARGV, $stderr, $stdout).to_i
46
+ exit(status) if status != 0
47
+ end
48
+
49
+ # Run a suite of RSpec examples. Does not exit.
50
+ #
51
+ # This is used internally by RSpec to run a suite, but is available
52
+ # for use by any other automation tool.
53
+ #
54
+ # If you want to run this multiple times in the same process, and you
55
+ # want files like `spec_helper.rb` to be reloaded, be sure to load `load`
56
+ # instead of `require`.
57
+ #
58
+ # @param args [Array] command-line-supported arguments
59
+ # @param err [IO] error stream
60
+ # @param out [IO] output stream
61
+ # @return [Fixnum] exit status code. 0 if all specs passed,
62
+ # or the configured failure exit code (1 by default) if specs
63
+ # failed.
64
+ def self.run(args, err=$stderr, out=$stdout)
65
+ trap_interrupt
66
+ options = ConfigurationOptions.new(args)
67
+
68
+ if options.options[:runner]
69
+ options.options[:runner].call(options, err, out)
70
+ else
71
+ new(options).run(err, out)
72
+ end
73
+ end
74
+
75
+ def initialize(options, configuration=RSpec.configuration, world=RSpec.world)
76
+ @options = options
77
+ @configuration = configuration
78
+ @world = world
79
+ end
80
+
81
+ # Configures and runs a spec suite.
82
+ #
83
+ # @param err [IO] error stream
84
+ # @param out [IO] output stream
85
+ def run(err, out)
86
+ setup(err, out)
87
+ return @configuration.reporter.exit_early(exit_code) if RSpec.world.wants_to_quit
88
+
89
+ run_specs(@world.ordered_example_groups).tap do
90
+ persist_example_statuses
91
+ end
92
+ end
93
+
94
+ # Wires together the various configuration objects and state holders.
95
+ #
96
+ # @param err [IO] error stream
97
+ # @param out [IO] output stream
98
+ def setup(err, out)
99
+ configure(err, out)
100
+ return if RSpec.world.wants_to_quit
101
+
102
+ @configuration.load_spec_files
103
+ ensure
104
+ @world.announce_filters
105
+ end
106
+
107
+ # Runs the provided example groups.
108
+ #
109
+ # @param example_groups [Array<RSpec::Core::ExampleGroup>] groups to run
110
+ # @return [Fixnum] exit status code. 0 if all specs passed,
111
+ # or the configured failure exit code (1 by default) if specs
112
+ # failed.
113
+ def run_specs(example_groups)
114
+ examples_count = @world.example_count(example_groups)
115
+ examples_passed = @configuration.reporter.report(examples_count) do |reporter|
116
+ @configuration.with_suite_hooks do
117
+ if examples_count == 0 && @configuration.fail_if_no_examples
118
+ return @configuration.failure_exit_code
119
+ end
120
+
121
+ example_groups.map { |g| g.run(reporter) }.all?
122
+ end
123
+ end
124
+
125
+ exit_code(examples_passed)
126
+ end
127
+
128
+ # @private
129
+ def configure(err, out)
130
+ @configuration.error_stream = err
131
+ @configuration.output_stream = out if @configuration.output_stream == $stdout
132
+ @options.configure(@configuration)
133
+ end
134
+
135
+ # @private
13
136
  def self.disable_autorun!
14
137
  @autorun_disabled = true
15
138
  end
16
139
 
140
+ # @private
17
141
  def self.autorun_disabled?
18
142
  @autorun_disabled ||= false
19
143
  end
20
144
 
145
+ # @private
21
146
  def self.installed_at_exit?
22
147
  @installed_at_exit ||= false
23
148
  end
24
149
 
150
+ # @private
25
151
  def self.running_in_drb?
26
- defined?(DRb) &&
27
- (DRb.current_server rescue false) &&
28
- DRb.current_server.uri =~ /druby\:\/\/127.0.0.1\:/
152
+ return false unless defined?(DRb)
153
+
154
+ server = begin
155
+ DRb.current_server
156
+ rescue DRb::DRbServerNotFound
157
+ return false
158
+ end
159
+
160
+ return false unless server && server.alive?
161
+
162
+ require 'socket'
163
+ require 'uri'
164
+
165
+ local_ipv4 = begin
166
+ IPSocket.getaddress(Socket.gethostname)
167
+ rescue SocketError
168
+ return false
169
+ end
170
+
171
+ ["127.0.0.1", "localhost", local_ipv4].any? { |addr| addr == URI(DRb.current_server.uri).host }
29
172
  end
30
173
 
174
+ # @private
31
175
  def self.trap_interrupt
32
- trap('INT') do
33
- exit!(1) if RSpec.wants_to_quit
34
- RSpec.wants_to_quit = true
35
- STDERR.puts "\nExiting... Interrupt again to exit immediately."
36
- end
176
+ trap('INT') { handle_interrupt }
37
177
  end
38
178
 
39
- # Run a suite of RSpec examples.
40
- #
41
- # This is used internally by RSpec to run a suite, but is available
42
- # for use by any other automation tool.
43
- #
44
- # If you want to run this multiple times in the same process, and you
45
- # want files like spec_helper.rb to be reloaded, be sure to load `load`
46
- # instead of `require`.
47
- #
48
- # #### Parameters
49
- # * +args+ - an array of command-line-supported arguments
50
- # * +err+ - error stream (Default: $stderr)
51
- # * +out+ - output stream (Default: $stdout)
52
- #
53
- # #### Returns
54
- # * +Fixnum+ - exit status code (0/1)
55
- def self.run(args, err=$stderr, out=$stdout)
56
- trap_interrupt
57
- options = ConfigurationOptions.new(args)
58
- options.parse_options
59
-
60
- if options.options[:drb]
61
- require 'rspec/core/drb_command_line'
62
- begin
63
- DRbCommandLine.new(options).run(err, out)
64
- rescue DRb::DRbConnError
65
- err.puts "No DRb server is running. Running in local process instead ..."
66
- CommandLine.new(options).run(err, out)
67
- end
179
+ # @private
180
+ def self.handle_interrupt
181
+ if RSpec.world.wants_to_quit
182
+ exit!(1)
68
183
  else
69
- CommandLine.new(options).run(err, out)
184
+ RSpec.world.wants_to_quit = true
185
+ $stderr.puts "\nRSpec is shutting down and will print the summary report... Interrupt again to force quit."
70
186
  end
71
- ensure
72
- RSpec.reset
187
+ end
188
+
189
+ # @private
190
+ def exit_code(examples_passed=false)
191
+ return @configuration.error_exit_code || @configuration.failure_exit_code if @world.non_example_failure
192
+ return @configuration.failure_exit_code unless examples_passed
193
+
194
+ 0
195
+ end
196
+
197
+ private
198
+
199
+ def persist_example_statuses
200
+ return if @configuration.dry_run
201
+ return unless (path = @configuration.example_status_persistence_file_path)
202
+
203
+ ExampleStatusPersister.persist(@world.all_examples, path)
204
+ rescue SystemCallError => e
205
+ RSpec.warning "Could not write example statuses to #{path} (configured as " \
206
+ "`config.example_status_persistence_file_path`) due to a " \
207
+ "system error: #{e.inspect}. Please check that the config " \
208
+ "option is set to an accessible, valid file path", :call_site => nil
73
209
  end
74
210
  end
75
211
  end
@@ -0,0 +1,37 @@
1
+ module RSpec
2
+ module Core
3
+ # A sandbox isolates the enclosed code into an environment that looks 'new'
4
+ # meaning globally accessed objects are reset for the duration of the
5
+ # sandbox.
6
+ #
7
+ # @note This module is not normally available. You must require
8
+ # `rspec/core/sandbox` to load it.
9
+ module Sandbox
10
+ # Execute a provided block with RSpec global objects (configuration,
11
+ # world) reset. This is used to test RSpec with RSpec.
12
+ #
13
+ # When calling this the configuration is passed into the provided block.
14
+ # Use this to set custom configs for your sandboxed examples.
15
+ #
16
+ # ```
17
+ # Sandbox.sandboxed do |config|
18
+ # config.before(:context) { RSpec.current_example = nil }
19
+ # end
20
+ # ```
21
+ def self.sandboxed
22
+ orig_config = RSpec.configuration
23
+ orig_world = RSpec.world
24
+ orig_example = RSpec.current_example
25
+
26
+ RSpec.configuration = RSpec::Core::Configuration.new
27
+ RSpec.world = RSpec::Core::World.new(RSpec.configuration)
28
+
29
+ yield RSpec.configuration
30
+ ensure
31
+ RSpec.configuration = orig_config
32
+ RSpec.world = orig_world
33
+ RSpec.current_example = orig_example
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,54 @@
1
+ module RSpec
2
+ module Core
3
+ # @private
4
+ #
5
+ # We use this to replace `::Set` so we can have the advantage of
6
+ # constant time key lookups for unique arrays but without the
7
+ # potential to pollute a developers environment with an extra
8
+ # piece of the stdlib. This helps to prevent false positive
9
+ # builds.
10
+ #
11
+ class Set
12
+ include Enumerable
13
+
14
+ def initialize(array=[])
15
+ @values = {}
16
+ merge(array)
17
+ end
18
+
19
+ def empty?
20
+ @values.empty?
21
+ end
22
+
23
+ def <<(key)
24
+ @values[key] = true
25
+ self
26
+ end
27
+
28
+ def delete(key)
29
+ @values.delete(key)
30
+ end
31
+
32
+ def each(&block)
33
+ @values.keys.each(&block)
34
+ self
35
+ end
36
+
37
+ def include?(key)
38
+ @values.key?(key)
39
+ end
40
+
41
+ def merge(values)
42
+ values.each do |key|
43
+ @values[key] = true
44
+ end
45
+ self
46
+ end
47
+
48
+ def clear
49
+ @values.clear
50
+ self
51
+ end
52
+ end
53
+ end
54
+ end
@@ -7,7 +7,7 @@ module RSpec
7
7
  #
8
8
  # module LoggedInAsAdmin
9
9
  # extend RSpec::Core::SharedContext
10
- # before(:each) do
10
+ # before(:example) do
11
11
  # log_in_as :admin
12
12
  # end
13
13
  # end
@@ -17,33 +17,39 @@ module RSpec
17
17
  # # ...
18
18
  # end
19
19
  module SharedContext
20
- include Hooks
21
- include Let::ExampleGroupMethods
22
-
20
+ # @private
23
21
  def included(group)
24
- [:before, :after].each do |type|
25
- [:all, :each].each do |scope|
26
- group.hooks[type][scope].concat hooks[type][scope]
27
- end
28
- end
29
- _nested_group_declarations.each do |name, block, *args|
30
- group.describe name, *args, &block
22
+ __shared_context_recordings.each do |recording|
23
+ recording.playback_onto(group)
31
24
  end
32
25
  end
33
26
 
34
- def describe(name, *args, &block)
35
- _nested_group_declarations << [name, block, *args]
27
+ # @private
28
+ def __shared_context_recordings
29
+ @__shared_context_recordings ||= []
36
30
  end
37
31
 
38
- alias_method :context, :describe
39
-
40
- private
32
+ # @private
33
+ Recording = Struct.new(:method_name, :args, :block) do
34
+ def playback_onto(group)
35
+ group.__send__(method_name, *args, &block)
36
+ end
37
+ end
41
38
 
42
- def _nested_group_declarations
43
- @_nested_group_declarations ||= []
39
+ # @private
40
+ def self.record(methods)
41
+ methods.each do |meth|
42
+ define_method(meth) do |*args, &block|
43
+ __shared_context_recordings << Recording.new(meth, args, block)
44
+ end
45
+ end
44
46
  end
47
+
48
+ # @private
49
+ record [:describe, :context] + Hooks.instance_methods(false) +
50
+ MemoizedHelpers::ClassMethods.instance_methods(false)
45
51
  end
46
52
  end
47
-
53
+ # @private
48
54
  SharedContext = Core::SharedContext
49
55
  end