pbt 0.8.0 → 0.9.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +8 -0
- data/README.md +38 -78
- data/lib/pbt/check/runner_methods.rb +6 -2
- data/lib/pbt/reporter/run_details_reporter.rb +33 -4
- data/lib/pbt/stateful/command.rb +215 -0
- data/lib/pbt/stateful/errors.rb +58 -0
- data/lib/pbt/stateful/property.rb +276 -280
- data/lib/pbt/stateful/shrinker.rb +295 -0
- data/lib/pbt/stateful/spec.rb +299 -0
- data/lib/pbt/stateful/types.rb +308 -0
- data/lib/pbt/version.rb +1 -1
- data/lib/pbt.rb +19 -19
- data/sig/manifest.yaml +2 -0
- data/sig/pbt/arbitrary.rbs +139 -0
- data/sig/pbt/check.rbs +74 -0
- data/sig/pbt/reporter.rbs +44 -0
- data/sig/pbt/stateful.rbs +241 -0
- data/sig/pbt.rbs +40 -1
- metadata +11 -19
- data/.rspec +0 -3
- data/.standard.yml +0 -3
- data/CLAUDE.md +0 -104
- data/Rakefile +0 -70
- data/benchmark/FIBER.md +0 -97
- data/benchmark/README.md +0 -103
- data/benchmark/example.txt +0 -299
- data/benchmark/failure_simple.rb +0 -29
- data/benchmark/fiber_experiment.rb +0 -39
- data/benchmark/fiber_scheduler/Gemfile +0 -7
- data/benchmark/fiber_scheduler/experiment.rb +0 -131
- data/benchmark/fiber_scheduler/spec/experiment_spec.rb +0 -38
- data/benchmark/success_cpu_bound.rb +0 -35
- data/benchmark/success_io_bound.rb +0 -28
- data/benchmark/success_simple.rb +0 -24
- data/docs/plans/fiber-experiment.md +0 -12
- data/docs/plans/generator-statistics.md +0 -16
- data/docs/plans/stateful-pbt-redesign.md +0 -415
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 4b46223742e56120329f6e7d8bc534c26440b87309f001d715656c3c472b18e4
|
|
4
|
+
data.tar.gz: 307990ded90589295c40d806579e2e586fd52c38d8a20ad0359a1dcc0de57718
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: fc5a37505bd13299292e7304b43f7c020e96531aa8bd6bbd7e397474775854200d91c647387906b3dd389753bc39db4e3514f962f0034013956a0ca1254a253f
|
|
7
|
+
data.tar.gz: 3ea6eed118b195d4e9676f604d564dafd87d2bb3b1ada8d44661d588c169f3fc46d71a4138674a1dbc253a6a22a2a6d674ca170e34f1eaf98d51380e479151cb
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
## [Unreleased]
|
|
2
2
|
|
|
3
|
+
## [0.9.0] - 2026-11-23
|
|
4
|
+
|
|
5
|
+
- [Breaking change] Replace `Pbt.stateful(model:, sut:)` with a redesigned yield-based DSL (`Pbt.stateful(max_steps:, min_steps:, max_shrink_attempts:) { |m| ... }`) featuring `initial_state` (value, block, or shrinkable `Arbitrary`), `sut`+`teardown` / `around` lifecycle, weighted commands (`guard`, `arguments`, `precondition`, `next_state`, `run`, `verify`), `invariant`s, symbolic `Ref`s, best-first shrinking with offline `cover?` validation and `FailureSignature` equivalence, `property.replay`, `Pbt.statistics` support, and multiline counterexample reporting [#67](https://github.com/ohbarye/pbt/issues/67) [#79](https://github.com/ohbarye/pbt/pull/79) [#80](https://github.com/ohbarye/pbt/pull/80).
|
|
6
|
+
- Migration note: replace `Pbt.stateful(model:, sut:)` and per-command classes (`applicable?`, `run!`, `verify!`) with `Pbt.stateful { |m| m.initial_state ...; m.sut { ... }; m.command(:name) { |c| c.guard ...; c.arguments ...; c.next_state ...; c.run ...; c.verify ... } }`. See `README.md` for full examples.
|
|
7
|
+
- Add complete RBS type signatures under `sig/` (`sig/pbt.rbs`, `sig/pbt/*.rbs`, `sig/manifest.yaml`) with `rbs validate` and `steep check` in CI [#81](https://github.com/ohbarye/pbt/pull/81).
|
|
8
|
+
- Tighten `pbt.gemspec` packaged file allowlist to ship `lib/`, `sig/`, and root documentation only.
|
|
9
|
+
- Automatically create a GitHub Release in `.github/workflows/release.yml` when releasing a gem.
|
|
10
|
+
|
|
3
11
|
## [0.8.0] - 2026-11-22
|
|
4
12
|
|
|
5
13
|
- [Breaking change] Drop support for Ruby 3.2 and earlier (EOL); Ruby 3.3 or later is now required, and Ruby 4.1.0-preview1 is added to CI [#66](https://github.com/ohbarye/pbt/pull/66)
|
data/README.md
CHANGED
|
@@ -190,8 +190,8 @@ For manual shrinking, retain a sample: `sample = arb.sample(Random.new(42))`,
|
|
|
190
190
|
then inspect `sample.value` and `sample.shrink.map(&:value)`. Calling
|
|
191
191
|
`arb.shrink(value)` directly on `Pbt.frequency`, `Pbt.one_of`, or `Arbitrary#map`
|
|
192
192
|
without an `unmapper` returns no candidates because a bare value cannot identify its
|
|
193
|
-
source branch or preimage.
|
|
194
|
-
|
|
193
|
+
source branch or preimage. Both `Pbt.property` and `Pbt.stateful` capture `Sample`
|
|
194
|
+
provenance automatically, so contextual generators shrink seamlessly in both.
|
|
195
195
|
|
|
196
196
|
### Generator statistics
|
|
197
197
|
|
|
@@ -216,98 +216,58 @@ Generator and classifier errors propagate to the caller.
|
|
|
216
216
|
|
|
217
217
|
## Stateful Testing
|
|
218
218
|
|
|
219
|
-
`Pbt`
|
|
220
|
-
It
|
|
219
|
+
`Pbt.stateful` provides a yield-based model DSL for stateful / command-based property testing.
|
|
220
|
+
It works directly with `Pbt.assert`, `Pbt.check`, `Pbt.statistics`, and `property.replay(run)` while preserving lexical `self` (so RSpec `expect(...)`/`let` and Minitest assertions work naturally inside callbacks).
|
|
221
221
|
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
Commands currently implement `arguments(state)` and `applicable?(state, args)`; the earlier zero-argument / state-only forms are no longer supported.
|
|
225
|
-
|
|
226
|
-
### Minimal usage
|
|
222
|
+
### Usage
|
|
227
223
|
|
|
228
224
|
```ruby
|
|
229
|
-
|
|
230
|
-
def initialize
|
|
231
|
-
@inc = IncrementCommand.new
|
|
232
|
-
end
|
|
233
|
-
|
|
234
|
-
def initial_state
|
|
235
|
-
0
|
|
236
|
-
end
|
|
237
|
-
|
|
238
|
-
def commands(_state)
|
|
239
|
-
[@inc]
|
|
240
|
-
end
|
|
241
|
-
end
|
|
242
|
-
|
|
243
|
-
class IncrementCommand
|
|
244
|
-
def name
|
|
245
|
-
:increment
|
|
246
|
-
end
|
|
225
|
+
BankState = Data.define(:balance, :accounts)
|
|
247
226
|
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
227
|
+
property = Pbt.stateful(min_steps: 1, max_steps: 20) do |m|
|
|
228
|
+
m.initial_state BankState.new(balance: 0, accounts: [])
|
|
229
|
+
m.sut { Bank.new }
|
|
230
|
+
m.teardown { |sut| sut.close } # or use `m.around { |initial_state, run| ... }`
|
|
251
231
|
|
|
252
|
-
|
|
253
|
-
|
|
232
|
+
m.invariant(:non_negative) do |state, sut|
|
|
233
|
+
raise "negative balance" unless sut.balance == state.balance && state.balance >= 0
|
|
254
234
|
end
|
|
255
235
|
|
|
256
|
-
|
|
257
|
-
|
|
236
|
+
m.command(:deposit, weight: 3) do |c|
|
|
237
|
+
c.arguments Pbt.integer(min: 1, max: 100)
|
|
238
|
+
c.next_state { |s, amount| s.with(balance: s.balance + amount) }
|
|
239
|
+
c.run { |sut, amount| sut.deposit(amount) }
|
|
240
|
+
c.verify { |ctx| raise "mismatch" unless ctx.result == ctx.after.balance }
|
|
258
241
|
end
|
|
259
242
|
|
|
260
|
-
|
|
261
|
-
|
|
243
|
+
m.command(:create_sub_account) do |c|
|
|
244
|
+
# `ref` is an opaque symbolic token (`Pbt::Stateful::Ref`) referencing this step's SUT return value.
|
|
245
|
+
c.next_state { |s, _args, ref| s.with(accounts: [*s.accounts, ref]) }
|
|
246
|
+
c.run { |sut| sut.create_sub_account }
|
|
262
247
|
end
|
|
263
248
|
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
249
|
+
m.command(:close_sub_account) do |c|
|
|
250
|
+
c.guard { |s| !s.accounts.empty? }
|
|
251
|
+
c.arguments { |s| Pbt.constant_from(*s.accounts) }
|
|
252
|
+
c.next_state { |s, id| s.with(accounts: s.accounts - [id]) }
|
|
253
|
+
c.run { |sut, id| sut.close_sub_account(id) } # `id` is automatically resolved to the concrete SUT value
|
|
268
254
|
end
|
|
269
255
|
end
|
|
270
256
|
|
|
271
|
-
|
|
272
|
-
attr_reader :value
|
|
273
|
-
|
|
274
|
-
def initialize
|
|
275
|
-
@value = 0
|
|
276
|
-
end
|
|
277
|
-
|
|
278
|
-
def increment
|
|
279
|
-
@value += 1
|
|
280
|
-
end
|
|
281
|
-
end
|
|
282
|
-
|
|
283
|
-
Pbt.assert do
|
|
284
|
-
Pbt.stateful(
|
|
285
|
-
model: CounterModel.new,
|
|
286
|
-
sut: -> { Counter.new },
|
|
287
|
-
max_steps: 20
|
|
288
|
-
)
|
|
289
|
-
end
|
|
257
|
+
Pbt.assert(seed: 42) { property }
|
|
290
258
|
```
|
|
291
259
|
|
|
292
|
-
###
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
- `
|
|
297
|
-
- `
|
|
298
|
-
- `
|
|
299
|
-
- `
|
|
300
|
-
- `
|
|
301
|
-
- `
|
|
302
|
-
- `
|
|
303
|
-
- `command.verify!(before_state:, after_state:, args:, result:, sut:)`
|
|
304
|
-
|
|
305
|
-
### Current limitations
|
|
306
|
-
|
|
307
|
-
- `Pbt.stateful` runs sequentially by default, even if the global worker is `:ractor`.
|
|
308
|
-
- You can still pass `worker: :none` explicitly if you want to make that choice obvious in a test.
|
|
309
|
-
- `worker: :ractor` is currently unsupported and raises `Pbt::InvalidConfiguration`.
|
|
310
|
-
- Shrinking supports shorter prefixes and command-argument shrinking (using `command.arguments(state).shrink(args)`).
|
|
260
|
+
### Key capabilities
|
|
261
|
+
|
|
262
|
+
- **Model hooks (`m`)**:
|
|
263
|
+
- `m.initial_state`: accepts a static value, an `Arbitrary` (shrunk automatically on failure), or a zero-arg factory block. Model states are deep-frozen to catch accidental mutation immediately.
|
|
264
|
+
- `m.sut { |initial_state| ... }` + optional `m.teardown { |sut| ... }`, or `m.around { |initial_state, run| ... }` (mutually exclusive with `sut`/`teardown`; must call `run.call(sut)` once). Cleanup runs in `ensure` on every trial and shrink candidate.
|
|
265
|
+
- `m.command(name, weight: 1) { |c| ... }`: defines weighted commands with `c.guard`, `c.arguments` (arbitrary, keyword shorthand `c.arguments(k: arb)`, or state-dependent block `{ |state| arb }`), `c.precondition`, `c.next_state`, `c.run` (required), and `c.verify` (`Context` with `:step`, `:before`, `:after`, `:args`, `:result`, `:sut`).
|
|
266
|
+
- `m.invariant(name = nil) { |state, sut| ... }`: checked after SUT setup (before step 1) and after every step's `verify`.
|
|
267
|
+
- **Symbolic references (`Pbt::Stateful::Ref`)**: `c.next_state { |s, args, ref| ... }` can store `ref` in the pure model state and select it in later commands' `c.arguments`. During execution, `Ref`s inside `Array`, `Hash`, `Set`, `Struct`, and `Data` are resolved to concrete SUT return values (including `nil`).
|
|
268
|
+
- **Best-first shrinking**: performs post-failure truncation, ddmin chunk deletion, single-step deletion, `initial_state` shrinking, and contextual step argument shrinking (preserving `Pbt.frequency`, `Pbt.one_of`, and block `#map` shrink trees), with offline model validation (`Arbitrary#cover?`, `guard`, `precondition`, `Ref` dependencies), `FailureSignature` equivalence, deduplication, and `max_shrink_attempts` budget.
|
|
269
|
+
- **Replay & statistics**: `details = Pbt.check { property }; property.replay(details.counterexample)` replays the minimized `Pbt::Stateful::Run`; `Pbt.statistics(property) { |run| run.command_names }` inspects generated command distributions.
|
|
270
|
+
- **Worker behavior**: `Pbt.stateful` runs sequentially by default even when `Pbt.configuration.worker = :ractor`. Passing `worker: :ractor` explicitly raises `Pbt::InvalidConfiguration` before generation.
|
|
311
271
|
|
|
312
272
|
## What if property-based tests fail?
|
|
313
273
|
|
|
@@ -39,8 +39,12 @@ module Pbt
|
|
|
39
39
|
property = property.call
|
|
40
40
|
config = Pbt.configuration.to_h.merge(options.to_h)
|
|
41
41
|
|
|
42
|
-
if property.respond_to?(:stateful?) && property.stateful?
|
|
43
|
-
config[:worker]
|
|
42
|
+
if property.respond_to?(:stateful?) && property.stateful?
|
|
43
|
+
if options.key?(:worker) && config[:worker] == :ractor
|
|
44
|
+
raise Pbt::InvalidConfiguration, "Pbt.stateful does not support worker: :ractor. Use worker: :none instead."
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
config[:worker] = :none unless options.key?(:worker)
|
|
44
48
|
end
|
|
45
49
|
|
|
46
50
|
max_skips = config[:max_skips]
|
|
@@ -43,20 +43,49 @@ module Pbt
|
|
|
43
43
|
|
|
44
44
|
# @return [String]
|
|
45
45
|
def format_error_message
|
|
46
|
+
counterexample_str = format_counterexample
|
|
47
|
+
error_line = "Got #{@run_details.error_instance.class}: #{@run_details.error_message}"
|
|
48
|
+
if @run_details.error_instance.is_a?(Pbt::Stateful::StepFailure) && @run_details.error_instance.cause
|
|
49
|
+
error_line = "#{error_line}\n (cause: #{@run_details.error_instance.cause.class})"
|
|
50
|
+
end
|
|
51
|
+
|
|
46
52
|
<<~MSG.chomp
|
|
47
53
|
Property failed after #{@run_details.num_runs} test(s)
|
|
48
54
|
seed: #{@run_details.seed}
|
|
49
|
-
counterexample
|
|
55
|
+
counterexample:#{counterexample_str}
|
|
50
56
|
Shrunk #{@run_details.num_shrinks} time(s)
|
|
51
|
-
|
|
57
|
+
#{error_line}
|
|
52
58
|
MSG
|
|
53
59
|
end
|
|
54
60
|
|
|
61
|
+
def format_counterexample
|
|
62
|
+
ce = @run_details.counterexample
|
|
63
|
+
if ce.is_a?(Pbt::Stateful::Run)
|
|
64
|
+
err = @run_details.error_instance
|
|
65
|
+
formatted =
|
|
66
|
+
if err.is_a?(Pbt::Stateful::StepFailure)
|
|
67
|
+
ce.format(failed_step_index: err.step_index, failed_stage: err.stage)
|
|
68
|
+
else
|
|
69
|
+
ce.format
|
|
70
|
+
end
|
|
71
|
+
indented = formatted.lines.map { |line| " #{line.chomp}" }.join("\n")
|
|
72
|
+
"\n#{indented}"
|
|
73
|
+
else
|
|
74
|
+
" #{ce}"
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
|
|
55
78
|
def error_backtrace
|
|
56
|
-
|
|
79
|
+
locations =
|
|
80
|
+
if @run_details.error_instance.is_a?(Pbt::Stateful::StepFailure)
|
|
81
|
+
@run_details.error_instance.backtrace || @run_details.error_instance.backtrace_locations
|
|
82
|
+
else
|
|
83
|
+
@run_details.error_instance.backtrace_locations
|
|
84
|
+
end
|
|
85
|
+
return "" if locations.nil? # It can be nil.
|
|
57
86
|
|
|
58
87
|
i = @run_details.verbose ? -1 : 3
|
|
59
|
-
" #{
|
|
88
|
+
" #{locations[..i].join("\n ")}"
|
|
60
89
|
end
|
|
61
90
|
|
|
62
91
|
# @return [String]
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "pbt/stateful/errors"
|
|
4
|
+
require "pbt/stateful/types"
|
|
5
|
+
|
|
6
|
+
module Pbt
|
|
7
|
+
module Stateful
|
|
8
|
+
# Immutable command specification built by `CommandBuilder`.
|
|
9
|
+
class Command
|
|
10
|
+
attr_reader :name, :weight
|
|
11
|
+
|
|
12
|
+
def initialize(name:, weight:, guard_block:, arguments_source:, precondition_block:, next_state_block:, run_block:, verify_block:)
|
|
13
|
+
@name = name
|
|
14
|
+
@weight = weight
|
|
15
|
+
@guard_block = guard_block
|
|
16
|
+
@arguments_source = arguments_source
|
|
17
|
+
@precondition_block = precondition_block
|
|
18
|
+
@next_state_block = next_state_block
|
|
19
|
+
@run_block = run_block
|
|
20
|
+
@verify_block = verify_block
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# @param state [Object]
|
|
24
|
+
# @return [Boolean]
|
|
25
|
+
def guard?(state)
|
|
26
|
+
return true unless @guard_block
|
|
27
|
+
|
|
28
|
+
!!Resolver.invoke_with_arity(@guard_block, state)
|
|
29
|
+
rescue *Check::FATAL_EXCEPTIONS
|
|
30
|
+
raise
|
|
31
|
+
rescue Exception => e # standard:disable Lint/RescueException
|
|
32
|
+
raise_model_error("guard", e)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# @param state [Object]
|
|
36
|
+
# @return [Object] arbitrary responding to generate/shrink (or sample)
|
|
37
|
+
def arguments_for(state)
|
|
38
|
+
arb =
|
|
39
|
+
if @arguments_source.nil?
|
|
40
|
+
Pbt.nil
|
|
41
|
+
elsif @arguments_source.is_a?(Proc)
|
|
42
|
+
Resolver.invoke_with_arity(@arguments_source, state)
|
|
43
|
+
else
|
|
44
|
+
@arguments_source
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
validate_arbitrary!(arb)
|
|
48
|
+
arb
|
|
49
|
+
rescue Pbt::Arbitrary::EmptyDomainError, InvalidSpec, *Check::FATAL_EXCEPTIONS
|
|
50
|
+
raise
|
|
51
|
+
rescue Exception => e # standard:disable Lint/RescueException
|
|
52
|
+
raise_model_error("arguments", e)
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# @param state [Object]
|
|
56
|
+
# @param args [Object]
|
|
57
|
+
# @return [Boolean]
|
|
58
|
+
def precondition?(state, args)
|
|
59
|
+
return true unless @precondition_block
|
|
60
|
+
|
|
61
|
+
!!Resolver.invoke_with_arity(@precondition_block, state, args)
|
|
62
|
+
rescue *Check::FATAL_EXCEPTIONS
|
|
63
|
+
raise
|
|
64
|
+
rescue Exception => e # standard:disable Lint/RescueException
|
|
65
|
+
raise_model_error("precondition", e)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# @param state [Object]
|
|
69
|
+
# @param args [Object]
|
|
70
|
+
# @param ref [Ref]
|
|
71
|
+
# @return [Object] deep-frozen next model state
|
|
72
|
+
def next_state(state, args, ref)
|
|
73
|
+
return state unless @next_state_block
|
|
74
|
+
|
|
75
|
+
next_s = Resolver.invoke_with_arity(@next_state_block, state, args, ref)
|
|
76
|
+
Resolver.deep_freeze(next_s)
|
|
77
|
+
rescue FrozenError => e
|
|
78
|
+
raise ModelError,
|
|
79
|
+
"command #{name.inspect} next_state attempted to mutate frozen model state: #{e.message}. " \
|
|
80
|
+
"Return a new state value instead (e.g. `state.with(...)`, `[*arr, x]`, `hash.merge(...)`)."
|
|
81
|
+
rescue *Check::FATAL_EXCEPTIONS
|
|
82
|
+
raise
|
|
83
|
+
rescue Exception => e # standard:disable Lint/RescueException
|
|
84
|
+
raise_model_error("next_state", e)
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
# @param sut [Object]
|
|
88
|
+
# @param concrete_args [Object]
|
|
89
|
+
# @return [Object]
|
|
90
|
+
def run!(sut, concrete_args)
|
|
91
|
+
Resolver.invoke_with_arity(@run_block, sut, concrete_args)
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# @param context [Context]
|
|
95
|
+
# @return [void]
|
|
96
|
+
def verify!(context)
|
|
97
|
+
return unless @verify_block
|
|
98
|
+
|
|
99
|
+
Resolver.invoke_with_arity(@verify_block, context)
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
private
|
|
103
|
+
|
|
104
|
+
def validate_arbitrary!(arb)
|
|
105
|
+
return if Resolver.arbitrary?(arb)
|
|
106
|
+
|
|
107
|
+
raise InvalidSpec,
|
|
108
|
+
"command #{name.inspect} arguments must return an arbitrary responding to generate and shrink, got #{arb.class}"
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def raise_model_error(callback_name, error)
|
|
112
|
+
err = ModelError.new("command #{name.inspect} #{callback_name} raised #{error.class}: #{error.message}")
|
|
113
|
+
err.set_backtrace(error.backtrace) if error.backtrace
|
|
114
|
+
raise err
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
# Yield-builder for defining a single stateful command.
|
|
119
|
+
class CommandBuilder
|
|
120
|
+
def initialize(name:, weight:)
|
|
121
|
+
raise InvalidSpec, "command name must be a Symbol, got #{name.inspect}" unless name.is_a?(Symbol)
|
|
122
|
+
unless weight.is_a?(Integer) && weight.positive?
|
|
123
|
+
raise InvalidSpec, "command #{name.inspect} weight must be a positive Integer, got #{weight.inspect}"
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
@name = name
|
|
127
|
+
@weight = weight
|
|
128
|
+
@guard_block = nil
|
|
129
|
+
@arguments_source = nil
|
|
130
|
+
@arguments_defined = false
|
|
131
|
+
@precondition_block = nil
|
|
132
|
+
@next_state_block = nil
|
|
133
|
+
@run_block = nil
|
|
134
|
+
@verify_block = nil
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
def guard(&block)
|
|
138
|
+
raise InvalidSpec, "command #{@name.inspect} guard requires a block" unless block
|
|
139
|
+
raise InvalidSpec, "command #{@name.inspect} guard already defined" if @guard_block
|
|
140
|
+
|
|
141
|
+
@guard_block = block
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
def arguments(*args, **kwargs, &block)
|
|
145
|
+
raise InvalidSpec, "command #{@name.inspect} arguments already defined" if @arguments_defined
|
|
146
|
+
|
|
147
|
+
@arguments_source =
|
|
148
|
+
if block
|
|
149
|
+
unless args.empty? && kwargs.empty?
|
|
150
|
+
raise InvalidSpec, "command #{@name.inspect} arguments accepts either a value, keyword arbitraries, or a block"
|
|
151
|
+
end
|
|
152
|
+
block
|
|
153
|
+
elsif !kwargs.empty?
|
|
154
|
+
unless args.empty?
|
|
155
|
+
raise InvalidSpec, "command #{@name.inspect} arguments cannot mix positional and keyword arguments"
|
|
156
|
+
end
|
|
157
|
+
Pbt.fixed_hash(kwargs)
|
|
158
|
+
elsif args.size == 1
|
|
159
|
+
arb = args.first
|
|
160
|
+
if !Resolver.arbitrary?(arb)
|
|
161
|
+
raise InvalidSpec, "command #{@name.inspect} arguments must be an arbitrary, got #{arb.class}"
|
|
162
|
+
end
|
|
163
|
+
arb
|
|
164
|
+
else
|
|
165
|
+
raise InvalidSpec, "command #{@name.inspect} arguments expects a single arbitrary, keyword arbitraries, or a block"
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
@arguments_defined = true
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
def precondition(&block)
|
|
172
|
+
raise InvalidSpec, "command #{@name.inspect} precondition requires a block" unless block
|
|
173
|
+
raise InvalidSpec, "command #{@name.inspect} precondition already defined" if @precondition_block
|
|
174
|
+
|
|
175
|
+
@precondition_block = block
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
def next_state(&block)
|
|
179
|
+
raise InvalidSpec, "command #{@name.inspect} next_state requires a block" unless block
|
|
180
|
+
raise InvalidSpec, "command #{@name.inspect} next_state already defined" if @next_state_block
|
|
181
|
+
|
|
182
|
+
@next_state_block = block
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
def run(&block)
|
|
186
|
+
raise InvalidSpec, "command #{@name.inspect} run requires a block" unless block
|
|
187
|
+
raise InvalidSpec, "command #{@name.inspect} run already defined" if @run_block
|
|
188
|
+
|
|
189
|
+
@run_block = block
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
def verify(&block)
|
|
193
|
+
raise InvalidSpec, "command #{@name.inspect} verify requires a block" unless block
|
|
194
|
+
raise InvalidSpec, "command #{@name.inspect} verify already defined" if @verify_block
|
|
195
|
+
|
|
196
|
+
@verify_block = block
|
|
197
|
+
end
|
|
198
|
+
|
|
199
|
+
def build
|
|
200
|
+
raise InvalidSpec, "command #{@name.inspect} must define run" unless @run_block
|
|
201
|
+
|
|
202
|
+
Command.new(
|
|
203
|
+
name: @name,
|
|
204
|
+
weight: @weight,
|
|
205
|
+
guard_block: @guard_block,
|
|
206
|
+
arguments_source: @arguments_source,
|
|
207
|
+
precondition_block: @precondition_block,
|
|
208
|
+
next_state_block: @next_state_block,
|
|
209
|
+
run_block: @run_block,
|
|
210
|
+
verify_block: @verify_block
|
|
211
|
+
)
|
|
212
|
+
end
|
|
213
|
+
end
|
|
214
|
+
end
|
|
215
|
+
end
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Pbt
|
|
4
|
+
class InvalidConfiguration < StandardError; end
|
|
5
|
+
|
|
6
|
+
module Stateful
|
|
7
|
+
# Raised when a stateful specification is invalid (missing sut/commands, invalid DSL options, etc.).
|
|
8
|
+
class InvalidSpec < Pbt::InvalidConfiguration; end
|
|
9
|
+
|
|
10
|
+
# Raised when the generator cannot produce a valid command sequence or step arguments within budget.
|
|
11
|
+
class GenerationError < StandardError; end
|
|
12
|
+
|
|
13
|
+
# Raised when a user model callback violates the pure-model contract (e.g. mutating frozen state).
|
|
14
|
+
class ModelError < StandardError; end
|
|
15
|
+
|
|
16
|
+
# Normalized failure identity used during shrinking to prevent slipping into an unrelated bug.
|
|
17
|
+
FailureSignature = Data.define(:stage, :target, :exception_class)
|
|
18
|
+
|
|
19
|
+
# Wraps a failure raised during SUT lifecycle, command execution, verification, or invariant checking.
|
|
20
|
+
class StepFailure < StandardError
|
|
21
|
+
attr_reader :step_index, :step, :stage, :target, :signature, :teardown_error
|
|
22
|
+
|
|
23
|
+
# @param message [String]
|
|
24
|
+
# @param step_index [Integer, nil] 0-based index of the failing step, or nil for sut/initial invariant/teardown
|
|
25
|
+
# @param step [Step, nil]
|
|
26
|
+
# @param stage [Symbol] :sut, :run, :verify, :invariant, or :teardown
|
|
27
|
+
# @param target [Symbol, String, nil] command name or invariant name
|
|
28
|
+
# @param exception_class [Class] original exception class
|
|
29
|
+
# @param teardown_error [Exception, nil] secondary error raised during teardown cleanup
|
|
30
|
+
def initialize(message, step_index:, step:, stage:, target:, exception_class:, teardown_error: nil)
|
|
31
|
+
frozen_target = target.is_a?(String) ? target.dup.freeze : target
|
|
32
|
+
@step_index = step_index
|
|
33
|
+
@step = step
|
|
34
|
+
@stage = stage
|
|
35
|
+
@target = frozen_target
|
|
36
|
+
@signature = FailureSignature.new(stage: stage, target: frozen_target, exception_class: exception_class)
|
|
37
|
+
@teardown_error = teardown_error
|
|
38
|
+
super(message)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# Attach a teardown error raised while this failure was already propagating.
|
|
42
|
+
#
|
|
43
|
+
# @param error [Exception]
|
|
44
|
+
# @return [self]
|
|
45
|
+
def attach_teardown_error(error)
|
|
46
|
+
@teardown_error = error
|
|
47
|
+
self
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def message
|
|
51
|
+
base = super
|
|
52
|
+
return base unless @teardown_error
|
|
53
|
+
|
|
54
|
+
"#{base}\n (teardown error: #{@teardown_error.class}: #{@teardown_error.message})"
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|