pbt 0.8.0 → 1.0.0

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 (52) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +29 -0
  3. data/README.md +70 -109
  4. data/lib/pbt/arbitrary/arbitrary.rb +23 -0
  5. data/lib/pbt/arbitrary/arbitrary_methods.rb +46 -16
  6. data/lib/pbt/arbitrary/bind_arbitrary.rb +54 -0
  7. data/lib/pbt/arbitrary/choose_arbitrary.rb +82 -5
  8. data/lib/pbt/arbitrary/constant.rb +42 -12
  9. data/lib/pbt/arbitrary/constant_from_arbitrary.rb +3 -3
  10. data/lib/pbt/arbitrary/float_arbitrary.rb +239 -17
  11. data/lib/pbt/arbitrary/frequency_arbitrary.rb +1 -1
  12. data/lib/pbt/arbitrary/integer_arbitrary.rb +31 -14
  13. data/lib/pbt/arbitrary/map_arbitrary.rb +4 -1
  14. data/lib/pbt/arbitrary/sample.rb +35 -0
  15. data/lib/pbt/check/configuration.rb +1 -1
  16. data/lib/pbt/check/property.rb +5 -0
  17. data/lib/pbt/check/runner_iterator.rb +38 -3
  18. data/lib/pbt/check/runner_methods.rb +70 -13
  19. data/lib/pbt/reporter/run_details_reporter.rb +39 -6
  20. data/lib/pbt/stateful/command.rb +220 -0
  21. data/lib/pbt/stateful/errors.rb +58 -0
  22. data/lib/pbt/stateful/property.rb +276 -280
  23. data/lib/pbt/stateful/shrinker.rb +318 -0
  24. data/lib/pbt/stateful/spec.rb +299 -0
  25. data/lib/pbt/stateful/types.rb +308 -0
  26. data/lib/pbt/version.rb +1 -1
  27. data/lib/pbt.rb +34 -19
  28. data/sig/manifest.yaml +2 -0
  29. data/sig/pbt/arbitrary.rbs +150 -0
  30. data/sig/pbt/check.rbs +74 -0
  31. data/sig/pbt/reporter.rbs +44 -0
  32. data/sig/pbt/stateful.rbs +244 -0
  33. data/sig/pbt.rbs +40 -1
  34. metadata +18 -23
  35. data/.rspec +0 -3
  36. data/.standard.yml +0 -3
  37. data/CLAUDE.md +0 -104
  38. data/Rakefile +0 -70
  39. data/benchmark/FIBER.md +0 -97
  40. data/benchmark/README.md +0 -103
  41. data/benchmark/example.txt +0 -299
  42. data/benchmark/failure_simple.rb +0 -29
  43. data/benchmark/fiber_experiment.rb +0 -39
  44. data/benchmark/fiber_scheduler/Gemfile +0 -7
  45. data/benchmark/fiber_scheduler/experiment.rb +0 -131
  46. data/benchmark/fiber_scheduler/spec/experiment_spec.rb +0 -38
  47. data/benchmark/success_cpu_bound.rb +0 -35
  48. data/benchmark/success_io_bound.rb +0 -28
  49. data/benchmark/success_simple.rb +0 -24
  50. data/docs/plans/fiber-experiment.md +0 -12
  51. data/docs/plans/generator-statistics.md +0 -16
  52. data/docs/plans/stateful-pbt-redesign.md +0 -415
@@ -0,0 +1,318 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "pbt/arbitrary/sample"
4
+ require "pbt/stateful/errors"
5
+ require "pbt/stateful/types"
6
+
7
+ module Pbt
8
+ module Stateful
9
+ # Per-failure shrink session sharing deduplication state and attempt budget across shrink passes.
10
+ class ShrinkSession
11
+ attr_reader :attempts, :max_attempts
12
+
13
+ def initialize(max_attempts:)
14
+ @max_attempts = max_attempts
15
+ @attempts = 0
16
+ @seen = {}
17
+ end
18
+
19
+ def exhausted?
20
+ @attempts >= @max_attempts
21
+ end
22
+
23
+ def seen?(run)
24
+ @seen.key?(run)
25
+ end
26
+
27
+ def mark_seen(run)
28
+ @seen[run] = true
29
+ end
30
+
31
+ def consume_attempt!
32
+ @attempts += 1
33
+ end
34
+ end
35
+
36
+ # Internal provenance wrapper carrying a pure `Run` alongside contextual `Sample` shrink trees.
37
+ class RunSample < Pbt::Arbitrary::Sample
38
+ attr_reader :initial_state_sample, :step_samples, :session
39
+
40
+ def initialize(run, initial_state_sample:, step_samples:, session:)
41
+ @initial_state_sample = initial_state_sample
42
+ @step_samples = step_samples
43
+ @session = session
44
+ super(run) { [].each }
45
+ end
46
+
47
+ def run
48
+ value
49
+ end
50
+ end
51
+
52
+ # Best-first stateful shrinker with offline 6-step model validation, deduplication, and attempt budget.
53
+ class Shrinker
54
+ def initialize(spec)
55
+ @spec = spec
56
+ end
57
+
58
+ # Build a lazy Enumerator of valid shrunk `RunSample` candidates for `run_sample`.
59
+ #
60
+ # @param run_sample [RunSample]
61
+ # @param failure [Exception, nil]
62
+ # @return [Enumerator<RunSample>]
63
+ def shrink(run_sample, failure:)
64
+ run = run_sample.run
65
+ session = run_sample.session
66
+ session.mark_seen(run)
67
+ original_states = before_states_by_step_id(run)
68
+
69
+ Enumerator.new do |y|
70
+ next if session.exhausted?
71
+
72
+ # Stage 1: Immediate post-failure truncation.
73
+ if failure.is_a?(StepFailure)
74
+ truncated_steps = post_failure_truncation(run.steps, failure)
75
+ if truncated_steps
76
+ candidate = Run.new(initial_state: run.initial_state, steps: truncated_steps.freeze)
77
+ status = emit_candidate(
78
+ y,
79
+ candidate,
80
+ session: session,
81
+ initial_state_sample: run_sample.initial_state_sample,
82
+ step_samples: run_sample.step_samples,
83
+ validate: false
84
+ )
85
+ next if status == :exhausted
86
+ end
87
+ end
88
+
89
+ # Stage 2: Chunk deletions (ddmin-style, back to front).
90
+ next if shrink_by_chunk_deletions(y, run_sample, session, original_states) == :exhausted
91
+
92
+ # Stage 3: Single-step deletions (back to front).
93
+ next if shrink_by_single_deletions(y, run_sample, session, original_states) == :exhausted
94
+
95
+ # Stage 4: Initial state shrinking (when initial_state is an Arbitrary).
96
+ next if shrink_initial_state(y, run_sample, session, original_states) == :exhausted
97
+
98
+ # Stage 5: Step argument shrinking (step 0..n-1).
99
+ shrink_step_arguments(y, run_sample, session, original_states)
100
+ end
101
+ end
102
+
103
+ # Validate a candidate run purely against the model without touching the SUT.
104
+ #
105
+ # @param run [Run]
106
+ # @param original_states [Hash<Integer, Object>, nil]
107
+ # @return [Boolean]
108
+ def valid_run?(run, original_states: nil)
109
+ state = Resolver.deep_freeze(run.initial_state)
110
+ available_ids = Set.new
111
+
112
+ run.steps.each do |step|
113
+ ref_ids = Resolver.collect_ref_ids(step.args)
114
+ return false unless ref_ids.all? { |id| available_ids.include?(id) }
115
+
116
+ command = @spec.command_by_name(step.command)
117
+ return false unless command.guard?(state)
118
+
119
+ arb =
120
+ begin
121
+ command.arguments_for(state)
122
+ rescue Pbt::Arbitrary::EmptyDomainError
123
+ return false
124
+ end
125
+
126
+ covered = Pbt::Arbitrary::Arbitrary.cover_result(arb, step.args)
127
+ return false if covered == false
128
+ if covered.nil? && command.state_dependent_arguments? &&
129
+ original_states&.key?(step.id) && original_states[step.id] != state
130
+ return false
131
+ end
132
+ return false unless command.precondition?(state, step.args)
133
+
134
+ state = command.next_state(state, step.args, Ref.new(step_id: step.id))
135
+ available_ids << step.id
136
+ end
137
+
138
+ true
139
+ end
140
+
141
+ private
142
+
143
+ def before_states_by_step_id(run)
144
+ states = {}
145
+ state = Resolver.deep_freeze(run.initial_state)
146
+ run.steps.each do |step|
147
+ states[step.id] = state
148
+ command = @spec.command_by_name(step.command)
149
+ state = command.next_state(state, step.args, Ref.new(step_id: step.id))
150
+ end
151
+ states
152
+ end
153
+
154
+ def post_failure_truncation(steps, failure)
155
+ if failure.step_index.is_a?(Integer) && failure.step_index < steps.length - 1
156
+ steps[0..failure.step_index]
157
+ elsif failure.step_index.nil? && (failure.stage == :invariant || failure.stage == :sut) && !steps.empty?
158
+ []
159
+ end
160
+ end
161
+
162
+ def shrink_by_chunk_deletions(y, run_sample, session, original_states)
163
+ run = run_sample.run
164
+ steps = run.steps
165
+ n = steps.length
166
+ chunk_size = n / 2
167
+
168
+ while chunk_size >= 2
169
+ chunk_starts(n, chunk_size).each do |start|
170
+ kept = steps[0...start] + steps[(start + chunk_size)..]
171
+ candidate = Run.new(initial_state: run.initial_state, steps: kept.freeze)
172
+ status = emit_candidate(
173
+ y,
174
+ candidate,
175
+ session: session,
176
+ initial_state_sample: run_sample.initial_state_sample,
177
+ step_samples: run_sample.step_samples,
178
+ original_states: original_states
179
+ )
180
+ return :exhausted if status == :exhausted
181
+ end
182
+ chunk_size /= 2
183
+ end
184
+
185
+ :ok
186
+ end
187
+
188
+ def chunk_starts(length, chunk_size)
189
+ starts = []
190
+ pos = length - chunk_size
191
+ while pos >= 0
192
+ starts << pos
193
+ pos -= chunk_size
194
+ end
195
+ starts << 0 unless starts.last == 0
196
+ starts
197
+ end
198
+
199
+ def shrink_by_single_deletions(y, run_sample, session, original_states)
200
+ run = run_sample.run
201
+ steps = run.steps
202
+ (steps.length - 1).downto(0) do |idx|
203
+ kept = steps[0...idx] + steps[(idx + 1)..]
204
+ candidate = Run.new(initial_state: run.initial_state, steps: kept.freeze)
205
+ status = emit_candidate(
206
+ y,
207
+ candidate,
208
+ session: session,
209
+ initial_state_sample: run_sample.initial_state_sample,
210
+ step_samples: run_sample.step_samples,
211
+ original_states: original_states
212
+ )
213
+ return :exhausted if status == :exhausted
214
+ end
215
+
216
+ :ok
217
+ end
218
+
219
+ def shrink_initial_state(y, run_sample, session, original_states)
220
+ initial_sample = run_sample.initial_state_sample
221
+ return :ok unless initial_sample
222
+
223
+ run = run_sample.run
224
+ initial_sample.shrink.each do |child_sample|
225
+ frozen_state = Resolver.deep_freeze(Resolver.deep_dup(child_sample.value))
226
+ candidate = Run.new(initial_state: frozen_state, steps: run.steps)
227
+ status = emit_candidate(
228
+ y,
229
+ candidate,
230
+ session: session,
231
+ initial_state_sample: child_sample,
232
+ step_samples: run_sample.step_samples,
233
+ original_states: original_states
234
+ )
235
+ return :exhausted if status == :exhausted
236
+ end
237
+
238
+ :ok
239
+ end
240
+
241
+ def shrink_step_arguments(y, run_sample, session, original_states)
242
+ run = run_sample.run
243
+ steps = run.steps
244
+ step_samples = run_sample.step_samples
245
+ state = run.initial_state
246
+
247
+ steps.each_with_index do |step, idx|
248
+ command = @spec.command_by_name(step.command)
249
+ step_sample = step_samples[step.id]
250
+
251
+ if step_sample
252
+ step_sample.shrink.each do |child_sample|
253
+ frozen_arg = Resolver.deep_freeze(Resolver.deep_dup(child_sample.value))
254
+ new_step = Step.new(id: step.id, command: step.command, args: frozen_arg)
255
+ new_steps = steps.dup
256
+ new_steps[idx] = new_step
257
+ candidate = Run.new(initial_state: run.initial_state, steps: new_steps.freeze)
258
+ status = emit_candidate(
259
+ y,
260
+ candidate,
261
+ session: session,
262
+ initial_state_sample: run_sample.initial_state_sample,
263
+ step_samples: step_samples.merge(step.id => child_sample),
264
+ original_states: original_states
265
+ )
266
+ return :exhausted if status == :exhausted
267
+ end
268
+ else
269
+ arb =
270
+ begin
271
+ command.arguments_for(state)
272
+ rescue Pbt::Arbitrary::EmptyDomainError
273
+ nil
274
+ end
275
+ arb&.shrink(step.args)&.each do |shrunk_arg|
276
+ frozen_arg = Resolver.deep_freeze(Resolver.deep_dup(shrunk_arg))
277
+ new_step = Step.new(id: step.id, command: step.command, args: frozen_arg)
278
+ new_steps = steps.dup
279
+ new_steps[idx] = new_step
280
+ candidate = Run.new(initial_state: run.initial_state, steps: new_steps.freeze)
281
+ status = emit_candidate(
282
+ y,
283
+ candidate,
284
+ session: session,
285
+ initial_state_sample: run_sample.initial_state_sample,
286
+ step_samples: step_samples,
287
+ original_states: original_states
288
+ )
289
+ return :exhausted if status == :exhausted
290
+ end
291
+ end
292
+
293
+ state = command.next_state(state, step.args, Ref.new(step_id: step.id))
294
+ end
295
+
296
+ :ok
297
+ end
298
+
299
+ def emit_candidate(y, candidate, session:, initial_state_sample:, step_samples:, validate: true, original_states: nil)
300
+ return :exhausted if session.exhausted?
301
+ return :duplicate if session.seen?(candidate)
302
+
303
+ session.mark_seen(candidate)
304
+ session.consume_attempt!
305
+
306
+ return :invalid if validate && !valid_run?(candidate, original_states: original_states)
307
+
308
+ y << RunSample.new(
309
+ candidate,
310
+ initial_state_sample: initial_state_sample,
311
+ step_samples: step_samples,
312
+ session: session
313
+ )
314
+ :emitted
315
+ end
316
+ end
317
+ end
318
+ end
@@ -0,0 +1,299 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "pbt/stateful/errors"
4
+ require "pbt/stateful/types"
5
+ require "pbt/stateful/command"
6
+
7
+ module Pbt
8
+ module Stateful
9
+ Invariant = Data.define(:name, :block)
10
+
11
+ # Immutable stateful model specification built by `SpecBuilder`.
12
+ class Spec
13
+ attr_reader :commands, :invariants
14
+
15
+ def initialize(initial_state_kind:, initial_state_source:, lifecycle:, commands:, invariants:)
16
+ @initial_state_kind = initial_state_kind
17
+ @initial_state_source = initial_state_source
18
+ @lifecycle = lifecycle
19
+ @commands = commands.freeze
20
+ @commands_by_name = commands.each_with_object({}) { |cmd, h| h[cmd.name] = cmd }.freeze
21
+ @invariants = invariants.freeze
22
+ end
23
+
24
+ def arbitrary_initial_state?
25
+ @initial_state_kind == :arbitrary
26
+ end
27
+
28
+ # Resolve initial state, returning `[frozen_value, sample_or_nil]`.
29
+ #
30
+ # @param rng [Random]
31
+ # @return [Array(Object, Pbt::Arbitrary::Sample, nil)]
32
+ def sample_initial_state(rng)
33
+ case @initial_state_kind
34
+ when :arbitrary
35
+ sample = Pbt::Arbitrary::Sample.capture(@initial_state_source, rng)
36
+ [Resolver.deep_freeze(Resolver.deep_dup(sample.value)), sample]
37
+ when :block
38
+ value =
39
+ begin
40
+ @initial_state_source.call
41
+ rescue *Check::FATAL_EXCEPTIONS, Pbt::Arbitrary::EmptyDomainError, Pbt::Arbitrary::FilterExhaustedError
42
+ raise
43
+ rescue Exception => e # standard:disable Lint/RescueException
44
+ err = ModelError.new("initial_state raised #{e.class}: #{e.message}")
45
+ err.set_backtrace(e.backtrace) if e.backtrace
46
+ raise err
47
+ end
48
+ [Resolver.deep_freeze(value), nil]
49
+ else
50
+ [Resolver.deep_freeze(Resolver.deep_dup(@initial_state_source)), nil]
51
+ end
52
+ end
53
+
54
+ # @param name [Symbol]
55
+ # @return [Command]
56
+ def command_by_name(name)
57
+ @commands_by_name.fetch(name)
58
+ rescue KeyError
59
+ raise InvalidSpec, "unknown stateful command: #{name.inspect}"
60
+ end
61
+
62
+ # Execute `body` with a freshly initialized SUT, guaranteeing teardown/around cleanup.
63
+ #
64
+ # @param initial_state [Object]
65
+ # @yieldparam sut [Object]
66
+ # @return [void]
67
+ def with_sut(initial_state, &body)
68
+ @lifecycle.call(initial_state, body)
69
+ end
70
+ end
71
+
72
+ # Yield-builder for defining a stateful property specification.
73
+ class SpecBuilder
74
+ UNSET = Object.new.freeze
75
+ private_constant :UNSET
76
+
77
+ def initialize
78
+ @initial_state_kind = :constant
79
+ @initial_state_source = nil
80
+ @initial_state_defined = false
81
+ @sut_block = nil
82
+ @teardown_block = nil
83
+ @around_block = nil
84
+ @commands = []
85
+ @command_names = {}
86
+ @invariants = []
87
+ end
88
+
89
+ def initial_state(value = UNSET, &block)
90
+ raise InvalidSpec, "initial_state already defined" if @initial_state_defined
91
+
92
+ if block && !value.equal?(UNSET)
93
+ raise InvalidSpec, "initial_state accepts either a value/arbitrary or a block, not both"
94
+ end
95
+
96
+ if block
97
+ @initial_state_kind = :block
98
+ @initial_state_source = block
99
+ elsif !value.equal?(UNSET)
100
+ if Resolver.arbitrary?(value)
101
+ @initial_state_kind = :arbitrary
102
+ @initial_state_source = value
103
+ else
104
+ @initial_state_kind = :constant
105
+ @initial_state_source = value
106
+ end
107
+ else
108
+ raise InvalidSpec, "initial_state requires a value, an arbitrary, or a block"
109
+ end
110
+
111
+ @initial_state_defined = true
112
+ end
113
+
114
+ def sut(&block)
115
+ raise InvalidSpec, "sut requires a block" unless block
116
+ raise InvalidSpec, "sut already defined" if @sut_block
117
+ raise InvalidSpec, "sut cannot be combined with around" if @around_block
118
+
119
+ @sut_block = block
120
+ end
121
+
122
+ def teardown(&block)
123
+ raise InvalidSpec, "teardown requires a block" unless block
124
+ raise InvalidSpec, "teardown already defined" if @teardown_block
125
+ raise InvalidSpec, "teardown cannot be combined with around" if @around_block
126
+
127
+ @teardown_block = block
128
+ end
129
+
130
+ def around(&block)
131
+ raise InvalidSpec, "around requires a block" unless block
132
+ raise InvalidSpec, "around already defined" if @around_block
133
+ raise InvalidSpec, "around cannot be combined with sut or teardown" if @sut_block || @teardown_block
134
+
135
+ @around_block = block
136
+ end
137
+
138
+ def command(name, weight: 1)
139
+ raise InvalidSpec, "command #{name.inspect} requires a block" unless block_given?
140
+
141
+ builder = CommandBuilder.new(name: name, weight: weight)
142
+ yield(builder)
143
+ cmd = builder.build
144
+
145
+ raise InvalidSpec, "duplicate command name: #{cmd.name.inspect}" if @command_names.key?(cmd.name)
146
+
147
+ @command_names[cmd.name] = true
148
+ @commands << cmd
149
+ end
150
+
151
+ def invariant(name = nil, &block)
152
+ raise InvalidSpec, "invariant requires a block" unless block
153
+
154
+ @invariants << Invariant.new(name: name, block: block)
155
+ end
156
+
157
+ def build
158
+ raise InvalidSpec, "stateful spec must define at least one command" if @commands.empty?
159
+ if @teardown_block && !@sut_block
160
+ raise InvalidSpec, "teardown requires sut"
161
+ end
162
+ unless @sut_block || @around_block
163
+ raise InvalidSpec, "stateful spec must define sut or around"
164
+ end
165
+
166
+ Spec.new(
167
+ initial_state_kind: @initial_state_kind,
168
+ initial_state_source: @initial_state_source,
169
+ lifecycle: build_lifecycle,
170
+ commands: @commands,
171
+ invariants: @invariants
172
+ )
173
+ end
174
+
175
+ private
176
+
177
+ def build_lifecycle
178
+ if @around_block
179
+ build_around_lifecycle(@around_block)
180
+ else
181
+ build_sut_teardown_lifecycle(@sut_block, @teardown_block)
182
+ end
183
+ end
184
+
185
+ def build_sut_teardown_lifecycle(sut_block, teardown_block)
186
+ lambda do |initial_state, body|
187
+ sut =
188
+ begin
189
+ Resolver.invoke_with_arity(sut_block, initial_state)
190
+ rescue *Check::FATAL_EXCEPTIONS
191
+ raise
192
+ rescue Exception => e # standard:disable Lint/RescueException
193
+ err = StepFailure.new(
194
+ "sut setup failed: #{e.class}: #{e.message}",
195
+ step_index: nil,
196
+ step: nil,
197
+ stage: :sut,
198
+ target: nil,
199
+ exception_class: e.class
200
+ )
201
+ err.set_backtrace(e.backtrace) if e.backtrace
202
+ raise err
203
+ end
204
+
205
+ propagating_error = nil
206
+ begin
207
+ body.call(sut)
208
+ rescue Exception => e # standard:disable Lint/RescueException
209
+ propagating_error = e
210
+ raise
211
+ ensure
212
+ if teardown_block
213
+ begin
214
+ Resolver.invoke_with_arity(teardown_block, sut)
215
+ rescue *Check::FATAL_EXCEPTIONS
216
+ raise
217
+ rescue Exception => teardown_err # standard:disable Lint/RescueException
218
+ if propagating_error.is_a?(StepFailure)
219
+ propagating_error.attach_teardown_error(teardown_err)
220
+ elsif propagating_error.nil? || propagating_error.is_a?(PreconditionNotMet)
221
+ err = StepFailure.new(
222
+ "teardown failed: #{teardown_err.class}: #{teardown_err.message}",
223
+ step_index: nil,
224
+ step: nil,
225
+ stage: :teardown,
226
+ target: nil,
227
+ exception_class: teardown_err.class,
228
+ teardown_error: teardown_err
229
+ )
230
+ err.set_backtrace(teardown_err.backtrace) if teardown_err.backtrace
231
+ raise err
232
+ end
233
+ end
234
+ end
235
+ end
236
+ end
237
+ end
238
+
239
+ def build_around_lifecycle(around_block)
240
+ lambda do |initial_state, body|
241
+ call_count = 0
242
+ completed = false
243
+ propagating_error = nil
244
+
245
+ run = lambda do |sut|
246
+ call_count += 1
247
+ if call_count > 1
248
+ raise InvalidSpec, "around must call run exactly once (called #{call_count} times)"
249
+ end
250
+
251
+ begin
252
+ body.call(sut)
253
+ completed = true
254
+ rescue Exception => e # standard:disable Lint/RescueException
255
+ propagating_error = e
256
+ raise
257
+ end
258
+ end
259
+
260
+ begin
261
+ Resolver.invoke_with_arity(around_block, initial_state, run)
262
+ rescue StepFailure, InvalidSpec, ModelError, PreconditionNotMet, *Check::FATAL_EXCEPTIONS
263
+ raise
264
+ rescue Exception => e # standard:disable Lint/RescueException
265
+ if propagating_error.is_a?(StepFailure)
266
+ propagating_error.attach_teardown_error(e)
267
+ raise propagating_error
268
+ elsif propagating_error && !propagating_error.is_a?(PreconditionNotMet)
269
+ raise propagating_error
270
+ end
271
+
272
+ stage = (call_count == 0) ? :sut : :teardown
273
+ err = StepFailure.new(
274
+ "#{stage} failed: #{e.class}: #{e.message}",
275
+ step_index: nil,
276
+ step: nil,
277
+ stage: stage,
278
+ target: nil,
279
+ exception_class: e.class,
280
+ teardown_error: (stage == :teardown) ? e : nil
281
+ )
282
+ err.set_backtrace(e.backtrace) if e.backtrace
283
+ raise err
284
+ end
285
+
286
+ if call_count != 1
287
+ raise InvalidSpec, "around must call run exactly once (called #{call_count} times)"
288
+ end
289
+
290
+ raise propagating_error if propagating_error
291
+
292
+ unless completed
293
+ raise InvalidSpec, "around must let run complete before returning"
294
+ end
295
+ end
296
+ end
297
+ end
298
+ end
299
+ end