rspec-core 3.0.0.beta2 → 3.0.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -0
- data/.yardopts +1 -0
- data/Changelog.md +297 -57
- data/README.md +16 -13
- data/lib/rspec/core.rb +55 -84
- data/lib/rspec/core/backport_random.rb +35 -3
- data/lib/rspec/core/backtrace_formatter.rb +4 -13
- data/lib/rspec/core/configuration.rb +330 -114
- data/lib/rspec/core/configuration_options.rb +38 -22
- data/lib/rspec/core/drb.rb +111 -0
- data/lib/rspec/core/dsl.rb +8 -2
- data/lib/rspec/core/example.rb +203 -94
- data/lib/rspec/core/example_group.rb +344 -316
- data/lib/rspec/core/filter_manager.rb +135 -90
- data/lib/rspec/core/flat_map.rb +1 -0
- data/lib/rspec/core/formatters.rb +50 -14
- data/lib/rspec/core/formatters/base_formatter.rb +32 -138
- data/lib/rspec/core/formatters/base_text_formatter.rb +32 -253
- data/lib/rspec/core/formatters/console_codes.rb +65 -0
- data/lib/rspec/core/formatters/deprecation_formatter.rb +24 -15
- data/lib/rspec/core/formatters/documentation_formatter.rb +7 -10
- data/lib/rspec/core/formatters/helpers.rb +15 -9
- data/lib/rspec/core/formatters/html_formatter.rb +17 -16
- data/lib/rspec/core/formatters/html_printer.rb +1 -0
- data/lib/rspec/core/formatters/json_formatter.rb +18 -20
- data/lib/rspec/core/formatters/profile_formatter.rb +67 -0
- data/lib/rspec/core/formatters/progress_formatter.rb +6 -7
- data/lib/rspec/core/formatters/snippet_extractor.rb +8 -6
- data/lib/rspec/core/hooks.rb +131 -125
- data/lib/rspec/core/memoized_helpers.rb +31 -26
- data/lib/rspec/core/metadata.rb +277 -184
- data/lib/rspec/core/metadata_filter.rb +86 -0
- data/lib/rspec/core/minitest_assertions_adapter.rb +28 -0
- data/lib/rspec/core/mocking_adapters/flexmock.rb +1 -1
- data/lib/rspec/core/mocking_adapters/mocha.rb +1 -1
- data/lib/rspec/core/mocking_adapters/null.rb +1 -1
- data/lib/rspec/core/mocking_adapters/rr.rb +2 -1
- data/lib/rspec/core/mocking_adapters/rspec.rb +1 -1
- data/lib/rspec/core/notifications.rb +435 -24
- data/lib/rspec/core/option_parser.rb +16 -25
- data/lib/rspec/core/ordering.rb +3 -1
- data/lib/rspec/core/pending.rb +57 -33
- data/lib/rspec/core/project_initializer.rb +2 -0
- data/lib/rspec/core/project_initializer/spec_helper.rb +5 -4
- data/lib/rspec/core/rake_task.rb +45 -20
- data/lib/rspec/core/reporter.rb +50 -22
- data/lib/rspec/core/ruby_project.rb +1 -0
- data/lib/rspec/core/runner.rb +93 -39
- data/lib/rspec/core/shared_context.rb +7 -5
- data/lib/rspec/core/shared_example_group.rb +85 -77
- data/lib/rspec/core/test_unit_assertions_adapter.rb +30 -0
- data/lib/rspec/core/version.rb +3 -1
- data/lib/rspec/core/warnings.rb +35 -17
- data/lib/rspec/core/world.rb +57 -5
- metadata +56 -369
- metadata.gz.sig +3 -3
- data/features/README.md +0 -13
- data/features/Upgrade.md +0 -352
- data/features/command_line/README.md +0 -25
- data/features/command_line/dry_run.feature +0 -29
- data/features/command_line/example_name_option.feature +0 -97
- data/features/command_line/exit_status.feature +0 -82
- data/features/command_line/fail_fast.feature +0 -26
- data/features/command_line/format_option.feature +0 -75
- data/features/command_line/init.feature +0 -57
- data/features/command_line/line_number_appended_to_path.feature +0 -140
- data/features/command_line/line_number_option.feature +0 -58
- data/features/command_line/order.feature +0 -25
- data/features/command_line/pattern_option.feature +0 -49
- data/features/command_line/rake_task.feature +0 -122
- data/features/command_line/randomization.feature +0 -63
- data/features/command_line/require_option.feature +0 -43
- data/features/command_line/ruby.feature +0 -23
- data/features/command_line/tag.feature +0 -98
- data/features/command_line/warnings_option.feature +0 -29
- data/features/configuration/alias_example_to.feature +0 -39
- data/features/configuration/backtrace_exclusion_patterns.feature +0 -105
- data/features/configuration/custom_settings.feature +0 -84
- data/features/configuration/default_path.feature +0 -38
- data/features/configuration/deprecation_stream.feature +0 -58
- data/features/configuration/enable_global_dsl.feature +0 -54
- data/features/configuration/fail_fast.feature +0 -77
- data/features/configuration/failure_exit_code.feature +0 -36
- data/features/configuration/order_and_seed.feature +0 -3
- data/features/configuration/output_stream.feature +0 -24
- data/features/configuration/overriding_global_ordering.feature +0 -93
- data/features/configuration/pattern.feature +0 -38
- data/features/configuration/profile.feature +0 -220
- data/features/configuration/read_options_from_file.feature +0 -90
- data/features/configuration/run_all_when_everything_filtered.feature +0 -76
- data/features/example_groups/aliasing.feature +0 -48
- data/features/example_groups/basic_structure.feature +0 -55
- data/features/example_groups/shared_context.feature +0 -74
- data/features/example_groups/shared_examples.feature +0 -286
- data/features/expectation_framework_integration/configure_expectation_framework.feature +0 -102
- data/features/filtering/exclusion_filters.feature +0 -135
- data/features/filtering/if_and_unless.feature +0 -138
- data/features/filtering/inclusion_filters.feature +0 -101
- data/features/formatters/configurable_colors.feature +0 -31
- data/features/formatters/custom_formatter.feature +0 -68
- data/features/formatters/json_formatter.feature +0 -30
- data/features/formatters/regression_tests.feature +0 -95
- data/features/formatters/text_formatter.feature +0 -46
- data/features/helper_methods/arbitrary_methods.feature +0 -40
- data/features/helper_methods/let.feature +0 -50
- data/features/helper_methods/modules.feature +0 -146
- data/features/hooks/around_hooks.feature +0 -344
- data/features/hooks/before_and_after_hooks.feature +0 -427
- data/features/hooks/filtering.feature +0 -232
- data/features/metadata/current_example.feature +0 -56
- data/features/metadata/described_class.feature +0 -17
- data/features/metadata/user_defined.feature +0 -100
- data/features/mock_framework_integration/use_any_framework.feature +0 -106
- data/features/mock_framework_integration/use_flexmock.feature +0 -94
- data/features/mock_framework_integration/use_mocha.feature +0 -95
- data/features/mock_framework_integration/use_rr.feature +0 -96
- data/features/mock_framework_integration/use_rspec.feature +0 -95
- data/features/pending_and_skipped_examples/README.md +0 -3
- data/features/pending_and_skipped_examples/pending_examples.feature +0 -118
- data/features/pending_and_skipped_examples/skipped_examples.feature +0 -106
- data/features/spec_files/arbitrary_file_suffix.feature +0 -13
- data/features/step_definitions/additional_cli_steps.rb +0 -83
- data/features/subject/explicit_subject.feature +0 -101
- data/features/subject/implicit_subject.feature +0 -63
- data/features/subject/one_liner_syntax.feature +0 -71
- data/features/support/env.rb +0 -21
- data/features/support/require_expect_syntax_in_aruba_specs.rb +0 -16
- data/features/support/rubinius.rb +0 -6
- data/lib/rspec/core/command_line.rb +0 -35
- data/lib/rspec/core/drb_command_line.rb +0 -26
- data/lib/rspec/core/drb_options.rb +0 -87
- data/lib/rspec/core/formatters/legacy_formatter.rb +0 -227
- data/lib/rspec/core/shared_example_group/collection.rb +0 -27
- data/spec/command_line/order_spec.rb +0 -211
- data/spec/rspec/core/backtrace_formatter_spec.rb +0 -230
- data/spec/rspec/core/command_line_spec.rb +0 -112
- data/spec/rspec/core/command_line_spec_output.txt +0 -0
- data/spec/rspec/core/configuration_options_spec.rb +0 -409
- data/spec/rspec/core/configuration_spec.rb +0 -1479
- data/spec/rspec/core/drb_command_line_spec.rb +0 -102
- data/spec/rspec/core/drb_options_spec.rb +0 -193
- data/spec/rspec/core/dsl_spec.rb +0 -88
- data/spec/rspec/core/example_group_spec.rb +0 -1533
- data/spec/rspec/core/example_spec.rb +0 -642
- data/spec/rspec/core/filter_manager_spec.rb +0 -229
- data/spec/rspec/core/formatters/base_formatter_spec.rb +0 -64
- data/spec/rspec/core/formatters/base_text_formatter_spec.rb +0 -303
- data/spec/rspec/core/formatters/deprecation_formatter_spec.rb +0 -208
- data/spec/rspec/core/formatters/documentation_formatter_spec.rb +0 -75
- data/spec/rspec/core/formatters/helpers_spec.rb +0 -104
- data/spec/rspec/core/formatters/html_formatted-2.1.0.html +0 -392
- data/spec/rspec/core/formatters/html_formatted.html +0 -397
- data/spec/rspec/core/formatters/html_formatter_spec.rb +0 -122
- data/spec/rspec/core/formatters/json_formatter_spec.rb +0 -206
- data/spec/rspec/core/formatters/legacy_formatter_spec.rb +0 -137
- data/spec/rspec/core/formatters/progress_formatter_spec.rb +0 -43
- data/spec/rspec/core/formatters/snippet_extractor_spec.rb +0 -26
- data/spec/rspec/core/formatters_spec.rb +0 -120
- data/spec/rspec/core/hooks_filtering_spec.rb +0 -227
- data/spec/rspec/core/hooks_spec.rb +0 -294
- data/spec/rspec/core/memoized_helpers_spec.rb +0 -495
- data/spec/rspec/core/metadata_spec.rb +0 -491
- data/spec/rspec/core/option_parser_spec.rb +0 -262
- data/spec/rspec/core/ordering_spec.rb +0 -102
- data/spec/rspec/core/pending_example_spec.rb +0 -117
- data/spec/rspec/core/pending_spec.rb +0 -8
- data/spec/rspec/core/project_initializer_spec.rb +0 -73
- data/spec/rspec/core/rake_task_spec.rb +0 -146
- data/spec/rspec/core/random_spec.rb +0 -47
- data/spec/rspec/core/reporter_spec.rb +0 -155
- data/spec/rspec/core/resources/a_bar.rb +0 -0
- data/spec/rspec/core/resources/a_foo.rb +0 -0
- data/spec/rspec/core/resources/a_spec.rb +0 -1
- data/spec/rspec/core/resources/custom_example_group_runner.rb +0 -14
- data/spec/rspec/core/resources/formatter_specs.rb +0 -58
- data/spec/rspec/core/resources/utf8_encoded.rb +0 -8
- data/spec/rspec/core/rspec_matchers_spec.rb +0 -45
- data/spec/rspec/core/ruby_project_spec.rb +0 -26
- data/spec/rspec/core/runner_spec.rb +0 -151
- data/spec/rspec/core/shared_context_spec.rb +0 -102
- data/spec/rspec/core/shared_example_group/collection_spec.rb +0 -57
- data/spec/rspec/core/shared_example_group_spec.rb +0 -114
- data/spec/rspec/core/warnings_spec.rb +0 -29
- data/spec/rspec/core/world_spec.rb +0 -142
- data/spec/rspec/core_spec.rb +0 -91
- data/spec/spec_helper.rb +0 -160
- data/spec/support/config_options_helper.rb +0 -13
- data/spec/support/formatter_support.rb +0 -83
- data/spec/support/helper_methods.rb +0 -26
- data/spec/support/isolate_load_path_mutation.rb +0 -5
- data/spec/support/isolated_directory.rb +0 -10
- data/spec/support/isolated_home_directory.rb +0 -16
- data/spec/support/legacy_formatter_using_sub_classing_example.rb +0 -87
- data/spec/support/matchers.rb +0 -85
- data/spec/support/mathn_integration_support.rb +0 -12
- data/spec/support/old_style_formatter_example.rb +0 -69
- data/spec/support/shared_example_groups.rb +0 -13
- data/spec/support/spec_files.rb +0 -44
- data/spec/support/stderr_splitter.rb +0 -36
data/lib/rspec/core/hooks.rb
CHANGED
@@ -1,17 +1,28 @@
|
|
1
1
|
module RSpec
|
2
2
|
module Core
|
3
|
+
# Provides `before`, `after` and `around` hooks as a means of
|
4
|
+
# supporting common setup and teardown. This module is extended
|
5
|
+
# onto {ExampleGroup}, making the methods available from any `describe`
|
6
|
+
# or `context` block and included in {Configuration}, making them
|
7
|
+
# available off of the configuration object to define global setup
|
8
|
+
# or teardown logic.
|
3
9
|
module Hooks
|
4
10
|
# @api public
|
11
|
+
#
|
5
12
|
# @overload before(&block)
|
6
13
|
# @overload before(scope, &block)
|
14
|
+
# @param scope [Symbol] `:example`, `:context`, or `:suite` (defaults to `:example`)
|
7
15
|
# @overload before(scope, conditions, &block)
|
16
|
+
# @param scope [Symbol] `:example`, `:context`, or `:suite` (defaults to `:example`)
|
17
|
+
# @param conditions [Hash]
|
18
|
+
# constrains this hook to examples matching these conditions e.g.
|
19
|
+
# `before(:example, :ui => true) { ... }` will only run with examples or
|
20
|
+
# groups declared with `:ui => true`.
|
8
21
|
# @overload before(conditions, &block)
|
9
|
-
#
|
10
|
-
#
|
11
|
-
#
|
12
|
-
#
|
13
|
-
# `before(:each, :ui => true) { ... }` will only run with examples or
|
14
|
-
# groups declared with `:ui => true`.
|
22
|
+
# @param conditions [Hash]
|
23
|
+
# constrains this hook to examples matching these conditions e.g.
|
24
|
+
# `before(:example, :ui => true) { ... }` will only run with examples or
|
25
|
+
# groups declared with `:ui => true`.
|
15
26
|
#
|
16
27
|
# @see #after
|
17
28
|
# @see #around
|
@@ -20,43 +31,43 @@ module RSpec
|
|
20
31
|
# @see SharedExampleGroup
|
21
32
|
# @see Configuration
|
22
33
|
#
|
23
|
-
# Declare a block of code to be run before each example (using `:
|
24
|
-
# or once before any example (using `:
|
34
|
+
# Declare a block of code to be run before each example (using `:example`)
|
35
|
+
# or once before any example (using `:context`). These are usually declared
|
25
36
|
# directly in the {ExampleGroup} to which they apply, but they can also
|
26
37
|
# be shared across multiple groups.
|
27
38
|
#
|
28
39
|
# You can also use `before(:suite)` to run a block of code before any
|
29
40
|
# example groups are run. This should be declared in {RSpec.configure}
|
30
41
|
#
|
31
|
-
# Instance variables declared in `before(:
|
42
|
+
# Instance variables declared in `before(:example)` or `before(:context)` are
|
32
43
|
# accessible within each example.
|
33
44
|
#
|
34
45
|
# ### Order
|
35
46
|
#
|
36
47
|
# `before` hooks are stored in three scopes, which are run in order:
|
37
|
-
# `:suite`, `:
|
48
|
+
# `:suite`, `:context`, and `:example`. They can also be declared in several
|
38
49
|
# different places: `RSpec.configure`, a parent group, the current group.
|
39
50
|
# They are run in the following order:
|
40
51
|
#
|
41
|
-
# before(:suite)
|
42
|
-
# before(:
|
43
|
-
# before(:
|
44
|
-
# before(:
|
45
|
-
# before(:
|
46
|
-
# before(:
|
47
|
-
# before(:
|
52
|
+
# before(:suite) # declared in RSpec.configure
|
53
|
+
# before(:context) # declared in RSpec.configure
|
54
|
+
# before(:context) # declared in a parent group
|
55
|
+
# before(:context) # declared in the current group
|
56
|
+
# before(:example) # declared in RSpec.configure
|
57
|
+
# before(:example) # declared in a parent group
|
58
|
+
# before(:example) # declared in the current group
|
48
59
|
#
|
49
60
|
# If more than one `before` is declared within any one scope, they are run
|
50
61
|
# in the order in which they are declared.
|
51
62
|
#
|
52
63
|
# ### Conditions
|
53
64
|
#
|
54
|
-
# When you add a conditions hash to `before(:
|
65
|
+
# When you add a conditions hash to `before(:example)` or `before(:context)`,
|
55
66
|
# RSpec will only apply that hook to groups or examples that match the
|
56
67
|
# conditions. e.g.
|
57
68
|
#
|
58
69
|
# RSpec.configure do |config|
|
59
|
-
# config.before(:
|
70
|
+
# config.before(:example, :authorized => true) do
|
60
71
|
# log_in_as :authorized_user
|
61
72
|
# end
|
62
73
|
# end
|
@@ -85,7 +96,7 @@ module RSpec
|
|
85
96
|
#
|
86
97
|
# When an exception is raised in a `before` block, RSpec skips any
|
87
98
|
# subsequent `before` blocks and the example, but runs all of the
|
88
|
-
# `after(:
|
99
|
+
# `after(:example)` and `after(:context)` hooks.
|
89
100
|
#
|
90
101
|
# ### Warning: implicit before blocks
|
91
102
|
#
|
@@ -96,20 +107,20 @@ module RSpec
|
|
96
107
|
# before block depends on state that is prepared in another before block
|
97
108
|
# that gets run later.
|
98
109
|
#
|
99
|
-
# ### Warning: `before(:
|
110
|
+
# ### Warning: `before(:context)`
|
100
111
|
#
|
101
|
-
# It is very tempting to use `before(:
|
112
|
+
# It is very tempting to use `before(:context)` to speed things up, but we
|
102
113
|
# recommend that you avoid this as there are a number of gotchas, as well
|
103
114
|
# as things that simply don't work.
|
104
115
|
#
|
105
116
|
# #### context
|
106
117
|
#
|
107
|
-
# `before(:
|
118
|
+
# `before(:context)` is run in an example that is generated to provide group
|
108
119
|
# context for the block.
|
109
120
|
#
|
110
121
|
# #### instance variables
|
111
122
|
#
|
112
|
-
# Instance variables declared in `before(:
|
123
|
+
# Instance variables declared in `before(:context)` are shared across all the
|
113
124
|
# examples in the group. This means that each example can change the
|
114
125
|
# state of a shared object, resulting in an ordering dependency that can
|
115
126
|
# make it difficult to reason about failures.
|
@@ -117,7 +128,7 @@ module RSpec
|
|
117
128
|
# #### unsupported rspec constructs
|
118
129
|
#
|
119
130
|
# RSpec has several constructs that reset state between each example
|
120
|
-
# automatically. These are not intended for use from within `before(:
|
131
|
+
# automatically. These are not intended for use from within `before(:context)`:
|
121
132
|
#
|
122
133
|
# * `let` declarations
|
123
134
|
# * `subject` declarations
|
@@ -129,17 +140,17 @@ module RSpec
|
|
129
140
|
# ActiveRecord) are typically designed around the idea of setting up
|
130
141
|
# before an example, running that one example, and then tearing down.
|
131
142
|
# This means that mocks and stubs can (sometimes) be declared in
|
132
|
-
# `before(:
|
143
|
+
# `before(:context)`, but get torn down before the first real example is ever
|
133
144
|
# run.
|
134
145
|
#
|
135
|
-
# You _can_ create database-backed model objects in a `before(:
|
146
|
+
# You _can_ create database-backed model objects in a `before(:context)` in
|
136
147
|
# rspec-rails, but it will not be wrapped in a transaction for you, so
|
137
|
-
# you are on your own to clean up in an `after(:
|
148
|
+
# you are on your own to clean up in an `after(:context)` block.
|
138
149
|
#
|
139
|
-
# @example before(:
|
150
|
+
# @example before(:example) declared in an {ExampleGroup}
|
140
151
|
#
|
141
152
|
# describe Thing do
|
142
|
-
# before(:
|
153
|
+
# before(:example) do
|
143
154
|
# @thing = Thing.new
|
144
155
|
# end
|
145
156
|
#
|
@@ -148,10 +159,10 @@ module RSpec
|
|
148
159
|
# end
|
149
160
|
# end
|
150
161
|
#
|
151
|
-
# @example before(:
|
162
|
+
# @example before(:context) declared in an {ExampleGroup}
|
152
163
|
#
|
153
164
|
# describe Parser do
|
154
|
-
# before(:
|
165
|
+
# before(:context) do
|
155
166
|
# File.open(file_to_parse, 'w') do |f|
|
156
167
|
# f.write <<-CONTENT
|
157
168
|
# stuff in the file
|
@@ -163,10 +174,13 @@ module RSpec
|
|
163
174
|
# Parser.parse(file_to_parse)
|
164
175
|
# end
|
165
176
|
#
|
166
|
-
# after(:
|
177
|
+
# after(:context) do
|
167
178
|
# File.delete(file_to_parse)
|
168
179
|
# end
|
169
180
|
# end
|
181
|
+
#
|
182
|
+
# @note The `:example` and `:context` scopes are also available as
|
183
|
+
# `:each` and `:all`, respectively. Use whichever you prefer.
|
170
184
|
def before(*args, &block)
|
171
185
|
hooks.register :append, :before, *args, &block
|
172
186
|
end
|
@@ -174,9 +188,9 @@ module RSpec
|
|
174
188
|
alias_method :append_before, :before
|
175
189
|
|
176
190
|
# Adds `block` to the front of the list of `before` blocks in the same
|
177
|
-
# scope (`:
|
191
|
+
# scope (`:example`, `:context`, or `:suite`).
|
178
192
|
#
|
179
|
-
# See #before for scoping semantics.
|
193
|
+
# See {#before} for scoping semantics.
|
180
194
|
def prepend_before(*args, &block)
|
181
195
|
hooks.register :prepend, :before, *args, &block
|
182
196
|
end
|
@@ -184,14 +198,18 @@ module RSpec
|
|
184
198
|
# @api public
|
185
199
|
# @overload after(&block)
|
186
200
|
# @overload after(scope, &block)
|
201
|
+
# @param scope [Symbol] `:example`, `:context`, or `:suite` (defaults to `:example`)
|
187
202
|
# @overload after(scope, conditions, &block)
|
203
|
+
# @param scope [Symbol] `:example`, `:context`, or `:suite` (defaults to `:example`)
|
204
|
+
# @param conditions [Hash]
|
205
|
+
# constrains this hook to examples matching these conditions e.g.
|
206
|
+
# `after(:example, :ui => true) { ... }` will only run with examples or
|
207
|
+
# groups declared with `:ui => true`.
|
188
208
|
# @overload after(conditions, &block)
|
189
|
-
#
|
190
|
-
#
|
191
|
-
#
|
192
|
-
#
|
193
|
-
# `after(:each, :ui => true) { ... }` will only run with examples or
|
194
|
-
# groups declared with `:ui => true`.
|
209
|
+
# @param conditions [Hash]
|
210
|
+
# constrains this hook to examples matching these conditions e.g.
|
211
|
+
# `after(:example, :ui => true) { ... }` will only run with examples or
|
212
|
+
# groups declared with `:ui => true`.
|
195
213
|
#
|
196
214
|
# @see #before
|
197
215
|
# @see #around
|
@@ -200,10 +218,9 @@ module RSpec
|
|
200
218
|
# @see SharedExampleGroup
|
201
219
|
# @see Configuration
|
202
220
|
#
|
203
|
-
# Declare a block of code to be run after each example (using `:
|
204
|
-
# once after all examples (using `:
|
205
|
-
#
|
206
|
-
# ordering.
|
221
|
+
# Declare a block of code to be run after each example (using `:example`) or
|
222
|
+
# once after all examples n the context (using `:context`). See {#before} for
|
223
|
+
# more information about ordering.
|
207
224
|
#
|
208
225
|
# ### Exceptions
|
209
226
|
#
|
@@ -215,20 +232,24 @@ module RSpec
|
|
215
232
|
# ### Order
|
216
233
|
#
|
217
234
|
# `after` hooks are stored in three scopes, which are run in order:
|
218
|
-
# `:
|
235
|
+
# `:example`, `:context`, and `:suite`. They can also be declared in several
|
219
236
|
# different places: `RSpec.configure`, a parent group, the current group.
|
220
237
|
# They are run in the following order:
|
221
238
|
#
|
222
|
-
# after(:
|
223
|
-
# after(:
|
224
|
-
# after(:
|
225
|
-
# after(:
|
226
|
-
# after(:
|
227
|
-
# after(:
|
239
|
+
# after(:example) # declared in the current group
|
240
|
+
# after(:example) # declared in a parent group
|
241
|
+
# after(:example) # declared in RSpec.configure
|
242
|
+
# after(:context) # declared in the current group
|
243
|
+
# after(:context) # declared in a parent group
|
244
|
+
# after(:context) # declared in RSpec.configure
|
245
|
+
# after(:suite) # declared in RSpec.configure
|
228
246
|
#
|
229
247
|
# This is the reverse of the order in which `before` hooks are run.
|
230
248
|
# Similarly, if more than one `after` is declared within any one scope,
|
231
249
|
# they are run in reverse order of that in which they are declared.
|
250
|
+
#
|
251
|
+
# @note The `:example` and `:context` scopes are also available as
|
252
|
+
# `:each` and `:all`, respectively. Use whichever you prefer.
|
232
253
|
def after(*args, &block)
|
233
254
|
hooks.register :prepend, :after, *args, &block
|
234
255
|
end
|
@@ -236,9 +257,9 @@ module RSpec
|
|
236
257
|
alias_method :prepend_after, :after
|
237
258
|
|
238
259
|
# Adds `block` to the back of the list of `after` blocks in the same
|
239
|
-
# scope (`:
|
260
|
+
# scope (`:example`, `:context`, or `:suite`).
|
240
261
|
#
|
241
|
-
# See #after for scoping semantics.
|
262
|
+
# See {#after} for scoping semantics.
|
242
263
|
def append_after(*args, &block)
|
243
264
|
hooks.register :append, :after, *args, &block
|
244
265
|
end
|
@@ -246,17 +267,22 @@ module RSpec
|
|
246
267
|
# @api public
|
247
268
|
# @overload around(&block)
|
248
269
|
# @overload around(scope, &block)
|
270
|
+
# @param scope [Symbol] `:example` (defaults to `:example`)
|
271
|
+
# present for syntax parity with `before` and `after`, but
|
272
|
+
# `:example`/`:each` is the only supported value.
|
249
273
|
# @overload around(scope, conditions, &block)
|
274
|
+
# @param scope [Symbol] `:example` (defaults to `:example`)
|
275
|
+
# present for syntax parity with `before` and `after`, but
|
276
|
+
# `:example`/`:each` is the only supported value.
|
277
|
+
# @param conditions [Hash]
|
278
|
+
# constrains this hook to examples matching these conditions e.g.
|
279
|
+
# `around(:example, :ui => true) { ... }` will only run with examples or
|
280
|
+
# groups declared with `:ui => true`.
|
250
281
|
# @overload around(conditions, &block)
|
251
|
-
#
|
252
|
-
#
|
253
|
-
#
|
254
|
-
#
|
255
|
-
#
|
256
|
-
# @param [Hash] conditions
|
257
|
-
# constrains this hook to examples matching these conditions e.g.
|
258
|
-
# `around(:each, :ui => true) { ... }` will only run with examples or
|
259
|
-
# groups declared with `:ui => true`.
|
282
|
+
# @param conditions [Hash]
|
283
|
+
# constrains this hook to examples matching these conditions e.g.
|
284
|
+
# `around(:example, :ui => true) { ... }` will only run with examples or
|
285
|
+
# groups declared with `:ui => true`.
|
260
286
|
#
|
261
287
|
# @yield [Example] the example to run
|
262
288
|
#
|
@@ -268,12 +294,12 @@ module RSpec
|
|
268
294
|
# resources that are made available within the examples and their
|
269
295
|
# associated `before` and `after` hooks.
|
270
296
|
#
|
271
|
-
# @note `:each` is the only supported scope.
|
297
|
+
# @note `:example`/`:each` is the only supported scope.
|
272
298
|
#
|
273
299
|
# Declare a block of code, parts of which will be run before and parts
|
274
300
|
# after the example. It is your responsibility to run the example:
|
275
301
|
#
|
276
|
-
# around(:
|
302
|
+
# around(:example) do |ex|
|
277
303
|
# # do some stuff before
|
278
304
|
# ex.run
|
279
305
|
# # do some stuff after
|
@@ -284,20 +310,20 @@ module RSpec
|
|
284
310
|
# that manage their own setup and teardown using a block or proc syntax,
|
285
311
|
# e.g.
|
286
312
|
#
|
287
|
-
# around(:
|
288
|
-
# around(:
|
313
|
+
# around(:example) {|ex| Database.transaction(&ex)}
|
314
|
+
# around(:example) {|ex| FakeFS(&ex)}
|
289
315
|
#
|
290
316
|
def around(*args, &block)
|
291
317
|
hooks.register :prepend, :around, *args, &block
|
292
318
|
end
|
293
319
|
|
294
|
-
# @
|
320
|
+
# @private
|
295
321
|
# Holds the various registered hooks.
|
296
322
|
def hooks
|
297
323
|
@hooks ||= HookCollections.new(self,
|
298
|
-
:around => { :
|
299
|
-
:before => { :
|
300
|
-
:after
|
324
|
+
:around => { :example => AroundHookCollection.new },
|
325
|
+
:before => { :example => HookCollection.new, :context => HookCollection.new, :suite => HookCollection.new },
|
326
|
+
:after => { :example => HookCollection.new, :context => HookCollection.new, :suite => HookCollection.new }
|
301
327
|
)
|
302
328
|
end
|
303
329
|
|
@@ -322,10 +348,6 @@ module RSpec
|
|
322
348
|
def run(example)
|
323
349
|
example.instance_exec(example, &block)
|
324
350
|
end
|
325
|
-
|
326
|
-
def display_name
|
327
|
-
"before hook"
|
328
|
-
end
|
329
351
|
end
|
330
352
|
|
331
353
|
# @private
|
@@ -333,38 +355,26 @@ module RSpec
|
|
333
355
|
def run(example)
|
334
356
|
example.instance_exec_with_rescue("in an after hook", &block)
|
335
357
|
end
|
336
|
-
|
337
|
-
def display_name
|
338
|
-
"after hook"
|
339
|
-
end
|
340
358
|
end
|
341
359
|
|
342
360
|
# @private
|
343
|
-
class
|
361
|
+
class AfterContextHook < Hook
|
344
362
|
def run(example)
|
345
363
|
example.instance_exec(example, &block)
|
346
364
|
rescue Exception => e
|
347
365
|
# TODO: come up with a better solution for this.
|
348
366
|
RSpec.configuration.reporter.message <<-EOS
|
349
367
|
|
350
|
-
An error occurred in an after(:
|
368
|
+
An error occurred in an `after(:context)` hook.
|
351
369
|
#{e.class}: #{e.message}
|
352
370
|
occurred at #{e.backtrace.first}
|
353
371
|
|
354
372
|
EOS
|
355
373
|
end
|
356
|
-
|
357
|
-
def display_name
|
358
|
-
"after(:all) hook"
|
359
|
-
end
|
360
374
|
end
|
361
375
|
|
362
376
|
# @private
|
363
|
-
|
364
|
-
def display_name
|
365
|
-
"around hook"
|
366
|
-
end
|
367
|
-
end
|
377
|
+
AroundHook = Hook
|
368
378
|
|
369
379
|
# @private
|
370
380
|
class BaseHookCollection
|
@@ -447,17 +457,17 @@ EOS
|
|
447
457
|
end
|
448
458
|
|
449
459
|
def register_globals(host, globals)
|
450
|
-
process(host, globals, :before, :
|
451
|
-
process(host, globals, :after, :
|
452
|
-
process(host, globals, :around, :
|
460
|
+
process(host, globals, :before, :example)
|
461
|
+
process(host, globals, :after, :example)
|
462
|
+
process(host, globals, :around, :example)
|
453
463
|
|
454
|
-
process(host, globals, :before, :
|
455
|
-
process(host, globals, :after, :
|
464
|
+
process(host, globals, :before, :context)
|
465
|
+
process(host, globals, :after, :context)
|
456
466
|
end
|
457
467
|
|
458
|
-
def
|
468
|
+
def around_example_hooks_for(example, initial_procsy=nil)
|
459
469
|
AroundHookCollection.new(FlatMap.flat_map(@owner.parent_groups) do |a|
|
460
|
-
a.hooks[:around][:
|
470
|
+
a.hooks[:around][:example]
|
461
471
|
end).for(example, initial_procsy)
|
462
472
|
end
|
463
473
|
|
@@ -470,18 +480,14 @@ EOS
|
|
470
480
|
#
|
471
481
|
# Runs all of the blocks stored with the hook in the context of the
|
472
482
|
# example. If no example is provided, just calls the hook directly.
|
473
|
-
def run(hook, scope, example_or_group
|
483
|
+
def run(hook, scope, example_or_group, initial_procsy=nil)
|
474
484
|
return if RSpec.configuration.dry_run?
|
475
|
-
|
476
485
|
find_hook(hook, scope, example_or_group, initial_procsy).run
|
477
486
|
end
|
478
487
|
|
479
|
-
SCOPES = [:
|
488
|
+
SCOPES = [:example, :context, :suite]
|
480
489
|
|
481
|
-
SCOPE_ALIASES = {
|
482
|
-
:example => :each,
|
483
|
-
:context => :all,
|
484
|
-
}
|
490
|
+
SCOPE_ALIASES = { :each => :example, :all => :context }
|
485
491
|
|
486
492
|
HOOK_TYPES = {
|
487
493
|
:before => Hash.new { BeforeHook },
|
@@ -489,13 +495,13 @@ EOS
|
|
489
495
|
:around => Hash.new { AroundHook }
|
490
496
|
}
|
491
497
|
|
492
|
-
HOOK_TYPES[:after][:
|
498
|
+
HOOK_TYPES[:after][:context] = AfterContextHook
|
493
499
|
|
494
500
|
private
|
495
501
|
|
496
502
|
def process(host, globals, position, scope)
|
497
503
|
globals[position][scope].each do |hook|
|
498
|
-
next unless scope == :
|
504
|
+
next unless scope == :example || hook.options_apply?(host)
|
499
505
|
next if host.parent_groups.any? {|a| a.hooks[position][scope].include?(hook)}
|
500
506
|
self[position][scope] << hook
|
501
507
|
end
|
@@ -512,7 +518,7 @@ EOS
|
|
512
518
|
error_message = "You must explicitly give a scope (#{SCOPES.join(", ")}) or scope alias (#{SCOPE_ALIASES.keys.join(", ")}) when using symbols as metadata for a hook."
|
513
519
|
raise ArgumentError.new error_message
|
514
520
|
else
|
515
|
-
:
|
521
|
+
:example
|
516
522
|
end
|
517
523
|
end
|
518
524
|
|
@@ -528,38 +534,38 @@ EOS
|
|
528
534
|
|
529
535
|
def find_hook(hook, scope, example_or_group, initial_procsy)
|
530
536
|
case [hook, scope]
|
531
|
-
when [:before, :
|
532
|
-
|
533
|
-
when [:after, :
|
534
|
-
|
535
|
-
when [:around, :
|
536
|
-
|
537
|
-
when [:before, :
|
538
|
-
|
539
|
-
when [:after, :
|
540
|
-
|
537
|
+
when [:before, :context]
|
538
|
+
before_context_hooks_for(example_or_group)
|
539
|
+
when [:after, :context]
|
540
|
+
after_context_hooks_for(example_or_group)
|
541
|
+
when [:around, :example]
|
542
|
+
around_example_hooks_for(example_or_group, initial_procsy)
|
543
|
+
when [:before, :example]
|
544
|
+
before_example_hooks_for(example_or_group)
|
545
|
+
when [:after, :example]
|
546
|
+
after_example_hooks_for(example_or_group)
|
541
547
|
when [:before, :suite], [:after, :suite]
|
542
548
|
self[hook][:suite].with(example_or_group)
|
543
549
|
end
|
544
550
|
end
|
545
551
|
|
546
|
-
def
|
547
|
-
GroupHookCollection.new(self[:before][:
|
552
|
+
def before_context_hooks_for(group)
|
553
|
+
GroupHookCollection.new(self[:before][:context]).for(group)
|
548
554
|
end
|
549
555
|
|
550
|
-
def
|
551
|
-
GroupHookCollection.new(self[:after][:
|
556
|
+
def after_context_hooks_for(group)
|
557
|
+
GroupHookCollection.new(self[:after][:context]).for(group)
|
552
558
|
end
|
553
559
|
|
554
|
-
def
|
560
|
+
def before_example_hooks_for(example)
|
555
561
|
HookCollection.new(FlatMap.flat_map(@owner.parent_groups.reverse) do |a|
|
556
|
-
a.hooks[:before][:
|
562
|
+
a.hooks[:before][:example]
|
557
563
|
end).for(example)
|
558
564
|
end
|
559
565
|
|
560
|
-
def
|
566
|
+
def after_example_hooks_for(example)
|
561
567
|
HookCollection.new(FlatMap.flat_map(@owner.parent_groups) do |a|
|
562
|
-
a.hooks[:after][:
|
568
|
+
a.hooks[:after][:example]
|
563
569
|
end).for(example)
|
564
570
|
end
|
565
571
|
end
|