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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +29 -0
- data/README.md +70 -109
- data/lib/pbt/arbitrary/arbitrary.rb +23 -0
- data/lib/pbt/arbitrary/arbitrary_methods.rb +46 -16
- data/lib/pbt/arbitrary/bind_arbitrary.rb +54 -0
- data/lib/pbt/arbitrary/choose_arbitrary.rb +82 -5
- data/lib/pbt/arbitrary/constant.rb +42 -12
- data/lib/pbt/arbitrary/constant_from_arbitrary.rb +3 -3
- data/lib/pbt/arbitrary/float_arbitrary.rb +239 -17
- data/lib/pbt/arbitrary/frequency_arbitrary.rb +1 -1
- data/lib/pbt/arbitrary/integer_arbitrary.rb +31 -14
- data/lib/pbt/arbitrary/map_arbitrary.rb +4 -1
- data/lib/pbt/arbitrary/sample.rb +35 -0
- data/lib/pbt/check/configuration.rb +1 -1
- data/lib/pbt/check/property.rb +5 -0
- data/lib/pbt/check/runner_iterator.rb +38 -3
- data/lib/pbt/check/runner_methods.rb +70 -13
- data/lib/pbt/reporter/run_details_reporter.rb +39 -6
- data/lib/pbt/stateful/command.rb +220 -0
- data/lib/pbt/stateful/errors.rb +58 -0
- data/lib/pbt/stateful/property.rb +276 -280
- data/lib/pbt/stateful/shrinker.rb +318 -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 +34 -19
- data/sig/manifest.yaml +2 -0
- data/sig/pbt/arbitrary.rbs +150 -0
- data/sig/pbt/check.rbs +74 -0
- data/sig/pbt/reporter.rbs +44 -0
- data/sig/pbt/stateful.rbs +244 -0
- data/sig/pbt.rbs +40 -1
- metadata +18 -23
- 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: 6ef5bc626a0a2e8676646b9e289c9427acfe2c7ae519611aec3413314aa76693
|
|
4
|
+
data.tar.gz: 21e8013d277de6780f41cca76f78c0ccd23bc1d7f8dfcd4945c19d69349564ec
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 83b5153b1e953e71f97f9507a3facbc363a1c7c376278cf67e8db2adafe73280cefef204d6deea404b19e3e4e98d9c1995345501de40d4fa6e1e6b35a7c326e7
|
|
7
|
+
data.tar.gz: 25020fbc085c847bc9b685adbca777276ac569b1d4d619b2c1048b4e5158aefdb97d5c0816e497fb7e08b687db45c21226c7b018f0986ecc74c69a65bb5b40d9
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,34 @@
|
|
|
1
1
|
## [Unreleased]
|
|
2
2
|
|
|
3
|
+
## [1.0.0] - 2026-11-27
|
|
4
|
+
|
|
5
|
+
First stable `v1.0.0` release of `pbt` (zero runtime dependencies, Ruby 3.3+, bundled RBS signatures, stateful PBT, integrated shrinking, and edge-case bias).
|
|
6
|
+
|
|
7
|
+
### Features & Improvements
|
|
8
|
+
- Improve `IntegerArbitrary#shrink` to yield binary-search candidates toward `target`, enabling greedy shrinking to converge to exact failure thresholds ([#82](https://github.com/ohbarye/pbt/issues/82), [#87](https://github.com/ohbarye/pbt/pull/87)).
|
|
9
|
+
- Add `Arbitrary#bind` / `#flat_map` (`BindArbitrary`) for dependent value generation with integrated `Sample` shrinking, including state-change validation in `Pbt.stateful` shrinking ([#84](https://github.com/ohbarye/pbt/issues/84), [#88](https://github.com/ohbarye/pbt/pull/88)).
|
|
10
|
+
- Add `Pbt.string` (printable ASCII default), extend `Pbt.float` with `min:`, `max:`, `allow_nan:`, and `allow_infinity:` plus float-aware binary-search shrinking and inward bound rounding, and fix `ChooseArbitrary` generation/shrinking for `Float` and exclusive ranges ([#85](https://github.com/ohbarye/pbt/issues/85), [#89](https://github.com/ohbarye/pbt/pull/89)).
|
|
11
|
+
- Add seed-reproducible edge-case / boundary-value bias to built-in numeric and ranged arbitraries (`IntegerArbitrary`, `FloatArbitrary`, `ChooseArbitrary`, and large `ArrayArbitrary`/`Pbt.string` lengths) ([#83](https://github.com/ohbarye/pbt/issues/83), [#90](https://github.com/ohbarye/pbt/pull/90)).
|
|
12
|
+
- Fix character/Unicode generators: fix `DEL` (`"\x7F"`) in `ASCII_CHARS`, exclude UTF-16 surrogates from `Pbt.char` so generated strings are always valid UTF-8, and replace the broken `String#succ` `PRINTABLE_CHARS` expansion with codepoint-range generation in `Pbt.printable_char` / `Pbt.printable_string` covering printable ASCII, BMP (including CJK), and supplementary planes (including emoji) ([#91](https://github.com/ohbarye/pbt/pull/91)).
|
|
13
|
+
- Prevent non-terminating shrinking on non-injective `MapArbitrary#shrink` and pathological custom bare shrinkers via non-progress filtering, seen-value tracking, and a shrink-step safety cap in `RunnerIterator` ([#91](https://github.com/ohbarye/pbt/pull/91)).
|
|
14
|
+
- Preserve earlier property failures over later batch generation errors under `worker: :ractor` ([#91](https://github.com/ohbarye/pbt/pull/91)).
|
|
15
|
+
- Make `ChooseArbitrary[T]` generic in RBS (`sig/pbt/arbitrary.rbs`) and update `pbt.gemspec` metadata and documentation ([#91](https://github.com/ohbarye/pbt/pull/91)).
|
|
16
|
+
|
|
17
|
+
### Breaking Changes (since v0.9.0)
|
|
18
|
+
- [Breaking change] Rewrite `FloatArbitrary` as a direct `Arbitrary` subclass (no longer `< MapArbitrary`) with `Integer | Float | nil` bound validation, removing unused `FLOAT_MAPPER` / `FLOAT_UNMAPPER` ([#89](https://github.com/ohbarye/pbt/pull/89)).
|
|
19
|
+
- [Breaking change] Validate runner options (`num_runs`, `worker`, `seed`, `max_skips`, unknown keys) upfront with `Pbt::InvalidConfiguration`, and validate `Pbt.property` / `Pbt.check` block and arbitrary arguments eagerly with `ArgumentError` ([#91](https://github.com/ohbarye/pbt/pull/91)).
|
|
20
|
+
- [Breaking change] Format reported counterexamples, verbose failures, and execution summaries with `#inspect` (preserving multiline `Pbt::Stateful::Run` formatting) so `""`, `nil`, and `"1"` vs `1` are unambiguous ([#91](https://github.com/ohbarye/pbt/pull/91)).
|
|
21
|
+
- [Breaking change] Clean up public API surface: make `Pbt.toss` and internal mapper/range constants private, remove broken `PRINTABLE_CHARS`, and raise `EmptyDomainError` (`< ArgumentError`) from `Pbt.frequency` on empty domains ([#91](https://github.com/ohbarye/pbt/pull/91)).
|
|
22
|
+
- [Breaking change] PRNG draw sequences and shrink order changed due to binary-search shrinking and edge-case bias, so seeds from v0.9.0 will generate different values and counterexamples.
|
|
23
|
+
|
|
24
|
+
## [0.9.0] - 2026-11-23
|
|
25
|
+
|
|
26
|
+
- [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).
|
|
27
|
+
- 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.
|
|
28
|
+
- 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).
|
|
29
|
+
- Tighten `pbt.gemspec` packaged file allowlist to ship `lib/`, `sig/`, and root documentation only.
|
|
30
|
+
- Automatically create a GitHub Release in `.github/workflows/release.yml` when releasing a gem.
|
|
31
|
+
|
|
3
32
|
## [0.8.0] - 2026-11-22
|
|
4
33
|
|
|
5
34
|
- [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
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
[](https://github.com/ohbarye/pbt/actions/workflows/main.yml)
|
|
5
5
|
[](https://www.rubydoc.info/gems/pbt)
|
|
6
6
|
|
|
7
|
-
A property-based testing
|
|
7
|
+
A property-based testing library for Ruby with shrinking, stateful testing, RBS type signatures, and optional Ractor parallelism.
|
|
8
8
|
|
|
9
9
|
PBT stands for Property-Based Testing.
|
|
10
10
|
|
|
@@ -114,6 +114,8 @@ Discarded cases do not count toward `num_runs` and are skipped during shrinking.
|
|
|
114
114
|
|
|
115
115
|
Arbitrary generates random values. It is also responsible for shrinking those values if asked to shrink a failed value as input.
|
|
116
116
|
|
|
117
|
+
Built-in numeric and ranged arbitraries (`Pbt.integer`, `Pbt.nat`, `Pbt.float`, `Pbt.choose`, and large `Pbt.array`/`Pbt.string` lengths) apply seed-reproducible edge-case bias toward boundary values (such as `0`, `1`, `-1`, `min`, `max`, `0.0`, `-0.0`, and empty/max lengths) to increase the likelihood of catching common corner-case bugs within the default `num_runs: 100`.
|
|
118
|
+
|
|
117
119
|
Here, we used only one type of arbitrary, `Pbt.integer`. There are many other built-in arbitraries, and you can create a variety of inputs by combining existing ones.
|
|
118
120
|
|
|
119
121
|
#### Shrink
|
|
@@ -132,9 +134,13 @@ There are many built-in arbitraries in `Pbt`. You can use them to generate rando
|
|
|
132
134
|
```ruby
|
|
133
135
|
Pbt.integer.generate # => 42
|
|
134
136
|
Pbt.integer(min: -1, max: 8).generate # => Integer between -1 and 8
|
|
137
|
+
Pbt.float(min: 0.0, max: 1.0).generate # => Float between 0.0 and 1.0
|
|
138
|
+
Pbt.float(allow_nan: true, allow_infinity: true).generate # => Float (including NaN / ±Infinity)
|
|
135
139
|
|
|
136
140
|
Pbt.symbol.generate # => :atq
|
|
137
141
|
|
|
142
|
+
Pbt.string.generate # => "aagjZfao" (printable ASCII)
|
|
143
|
+
Pbt.printable_string.generate # => "a中😀" (printable Unicode)
|
|
138
144
|
Pbt.ascii_char.generate # => "a"
|
|
139
145
|
Pbt.ascii_string.generate # => "aagjZfao"
|
|
140
146
|
|
|
@@ -159,13 +165,17 @@ Pbt.tuple(Pbt.symbol, Pbt.integer).generate # => [:atq, 42]
|
|
|
159
165
|
|
|
160
166
|
Pbt.fixed_hash(x: Pbt.symbol, y: Pbt.integer).generate # => {x: :atq, y: 42}
|
|
161
167
|
Pbt.hash(Pbt.symbol, Pbt.integer).generate # => {atq: 121, ygab: -1142}
|
|
168
|
+
Pbt.set(Pbt.integer).generate # => #<Set: {1, 42}>
|
|
162
169
|
|
|
163
170
|
Pbt.constant_from(:a, 1, 0.1).generate # => :a or 1 or 0.1
|
|
164
171
|
Pbt.one_of(Pbt.integer, Pbt.constant(nil)).generate # => 42 or nil
|
|
165
172
|
|
|
166
173
|
Pbt.integer(min: 0, max: 10).map { |n| n * 2 }.generate # => 8
|
|
174
|
+
Pbt.integer(min: 0, max: 10).flat_map { |lo| Pbt.integer(min: lo, max: 10).map { |hi| [lo, hi] } }.generate # => [3, 7]
|
|
167
175
|
Pbt.integer(min: 0, max: 10).filter(&:even?).generate # => 4
|
|
168
|
-
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
Note: for `Pbt.set` and `Pbt.hash`, `min:` and `max:` bound the number of element/pair draws *before* deduplication (so duplicate elements/keys can result in fewer entries than `min:`).
|
|
169
179
|
|
|
170
180
|
See [ArbitraryMethods](https://github.com/ohbarye/pbt/blob/main/lib/pbt/arbitrary/arbitrary_methods.rb) module for more details.
|
|
171
181
|
|
|
@@ -188,10 +198,11 @@ branch. Generated values and reported counterexamples remain ordinary Ruby value
|
|
|
188
198
|
|
|
189
199
|
For manual shrinking, retain a sample: `sample = arb.sample(Random.new(42))`,
|
|
190
200
|
then inspect `sample.value` and `sample.shrink.map(&:value)`. Calling
|
|
191
|
-
`arb.shrink(value)` directly on `Pbt.frequency`, `Pbt.one_of`,
|
|
192
|
-
without an `unmapper` returns no candidates because a
|
|
193
|
-
source branch or preimage.
|
|
194
|
-
|
|
201
|
+
`arb.shrink(value)` directly on `Pbt.frequency`, `Pbt.one_of`, `Arbitrary#bind` /
|
|
202
|
+
`#flat_map`, or `Arbitrary#map` without an `unmapper` returns no candidates because a
|
|
203
|
+
bare value cannot identify its source branch or preimage. Both `Pbt.property` and
|
|
204
|
+
`Pbt.stateful` capture `Sample` provenance automatically, so contextual generators
|
|
205
|
+
shrink seamlessly in both.
|
|
195
206
|
|
|
196
207
|
### Generator statistics
|
|
197
208
|
|
|
@@ -216,98 +227,59 @@ Generator and classifier errors propagate to the caller.
|
|
|
216
227
|
|
|
217
228
|
## Stateful Testing
|
|
218
229
|
|
|
219
|
-
`Pbt`
|
|
220
|
-
It
|
|
221
|
-
|
|
222
|
-
> **Note:** The current `Pbt.stateful(model:, sut:)` protocol is planned to be replaced with a cleaner yield-builder DSL and improved shrinking engine in v0.9.0 before v1.0.0. See [#67](https://github.com/ohbarye/pbt/issues/67) and [docs/plans/stateful-pbt-redesign.md](docs/plans/stateful-pbt-redesign.md).
|
|
230
|
+
`Pbt.stateful` provides a yield-based model DSL for stateful / command-based property testing.
|
|
231
|
+
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).
|
|
223
232
|
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
### Minimal usage
|
|
233
|
+
### Usage
|
|
227
234
|
|
|
228
235
|
```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
|
|
247
|
-
|
|
248
|
-
def arguments(_state)
|
|
249
|
-
Pbt.nil
|
|
250
|
-
end
|
|
236
|
+
BankState = Data.define(:balance, :accounts)
|
|
251
237
|
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
def next_state(state, _args)
|
|
257
|
-
state + 1
|
|
258
|
-
end
|
|
238
|
+
property = Pbt.stateful(min_steps: 1, max_steps: 20) do |m|
|
|
239
|
+
m.initial_state BankState.new(balance: 0, accounts: [])
|
|
240
|
+
m.sut { Bank.new }
|
|
241
|
+
m.teardown { |sut| sut.close } # or use `m.around { |initial_state, run| ... }`
|
|
259
242
|
|
|
260
|
-
|
|
261
|
-
sut.
|
|
243
|
+
m.invariant(:non_negative) do |state, sut|
|
|
244
|
+
raise "negative balance" unless sut.balance == state.balance && state.balance >= 0
|
|
262
245
|
end
|
|
263
246
|
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
247
|
+
m.command(:deposit, weight: 3) do |c|
|
|
248
|
+
c.arguments Pbt.integer(min: 1, max: 100)
|
|
249
|
+
c.next_state { |s, amount| s.with(balance: s.balance + amount) }
|
|
250
|
+
c.run { |sut, amount| sut.deposit(amount) }
|
|
251
|
+
c.verify { |ctx| raise "mismatch" unless ctx.result == ctx.after.balance }
|
|
268
252
|
end
|
|
269
|
-
end
|
|
270
253
|
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
@value = 0
|
|
254
|
+
m.command(:create_sub_account) do |c|
|
|
255
|
+
# `ref` is an opaque symbolic token (`Pbt::Stateful::Ref`) referencing this step's SUT return value.
|
|
256
|
+
c.next_state { |s, _args, ref| s.with(accounts: [*s.accounts, ref]) }
|
|
257
|
+
c.run { |sut| sut.create_sub_account }
|
|
276
258
|
end
|
|
277
259
|
|
|
278
|
-
|
|
279
|
-
|
|
260
|
+
m.command(:close_sub_account) do |c|
|
|
261
|
+
c.guard { |s| !s.accounts.empty? }
|
|
262
|
+
c.arguments { |s| Pbt.constant_from(*s.accounts) }
|
|
263
|
+
c.next_state { |s, id| s.with(accounts: s.accounts - [id]) }
|
|
264
|
+
c.run { |sut, id| sut.close_sub_account(id) } # `id` is automatically resolved to the concrete SUT value
|
|
280
265
|
end
|
|
281
266
|
end
|
|
282
267
|
|
|
283
|
-
Pbt.assert
|
|
284
|
-
Pbt.stateful(
|
|
285
|
-
model: CounterModel.new,
|
|
286
|
-
sut: -> { Counter.new },
|
|
287
|
-
max_steps: 20
|
|
288
|
-
)
|
|
289
|
-
end
|
|
268
|
+
Pbt.assert(seed: 42) { property }
|
|
290
269
|
```
|
|
291
270
|
|
|
292
|
-
###
|
|
293
|
-
|
|
294
|
-
`Pbt.stateful(model:, sut:, max_steps:)` expects the following duck-typed interfaces:
|
|
271
|
+
### Key capabilities
|
|
295
272
|
|
|
296
|
-
- `
|
|
297
|
-
- `
|
|
298
|
-
- `
|
|
299
|
-
- `command.arguments(state)`
|
|
300
|
-
- `
|
|
301
|
-
- `
|
|
302
|
-
- `
|
|
303
|
-
- `
|
|
304
|
-
|
|
305
|
-
|
|
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)`).
|
|
273
|
+
- **Model hooks (`m`)**:
|
|
274
|
+
- `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.
|
|
275
|
+
- `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.
|
|
276
|
+
- `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`).
|
|
277
|
+
- `m.invariant(name = nil) { |state, sut| ... }`: checked after SUT setup (before step 1) and after every step's `verify`.
|
|
278
|
+
- **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`).
|
|
279
|
+
- **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.
|
|
280
|
+
- **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.
|
|
281
|
+
- **Step count bounds (`min_steps:` / `max_steps:`)**: `min_steps:` sets the target lower bound for generated run lengths; if a run reaches a terminal state where no command `guard` is truthy, generation stops early at that terminal state.
|
|
282
|
+
- **Worker behavior**: `Pbt.stateful` runs sequentially by default even when `Pbt.configuration.worker = :ractor`. Passing `worker: :ractor` explicitly raises `Pbt::InvalidConfiguration` before generation.
|
|
311
283
|
|
|
312
284
|
## What if property-based tests fail?
|
|
313
285
|
|
|
@@ -394,7 +366,7 @@ Pbt.configure do |config|
|
|
|
394
366
|
config.num_runs = 100
|
|
395
367
|
|
|
396
368
|
# The seed to use for random number generation.
|
|
397
|
-
#
|
|
369
|
+
# Initialized once per process by default; pass `seed:` to `Pbt.assert` / `Pbt.check` to vary or replay a failure.
|
|
398
370
|
config.seed = 42
|
|
399
371
|
|
|
400
372
|
# Whether to report exceptions in threads.
|
|
@@ -458,7 +430,7 @@ You cannot use any methods provided by test frameworks like `expect` or `assert`
|
|
|
458
430
|
it do
|
|
459
431
|
Pbt.assert(worker: :ractor) do
|
|
460
432
|
Pbt.property(Pbt.integer) do |n|
|
|
461
|
-
# This is not possible because `self`
|
|
433
|
+
# This is not possible because `self` is a Ractor here.
|
|
462
434
|
expect(n).to be_an(Integer) # => Ractor::RemoteError (cause by NoMethodError for `expect` or `be_an`)
|
|
463
435
|
end
|
|
464
436
|
end
|
|
@@ -477,31 +449,20 @@ Pbt.assert(worker: :none) do
|
|
|
477
449
|
end
|
|
478
450
|
```
|
|
479
451
|
|
|
480
|
-
##
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
-
|
|
493
|
-
|
|
494
|
-
- [x] Arbitrary usage
|
|
495
|
-
- [x] Configuration
|
|
496
|
-
- [x] Benchmark
|
|
497
|
-
- [x] Rich report by verbose mode
|
|
498
|
-
- [x] (Partially) Allow to use expectations and matchers provided by test framework in Ractor. (dropped)
|
|
499
|
-
- It'd be so hard to pass assertions like `expect`, `assert` to a Ractor.
|
|
500
|
-
- [x] Implement frequency arbitrary
|
|
501
|
-
- [x] Statistics feature to aggregate generated values
|
|
502
|
-
- [x] Decide DSL (explicit `Pbt.assert` / `Pbt.check` + `Pbt.property` / `Pbt.stateful` composition)
|
|
503
|
-
- [x] Try Fiber ([experiment and v1 decision](benchmark/FIBER.md))
|
|
504
|
-
- [x] Stateful property-based testing (via `Pbt.stateful`)
|
|
452
|
+
## Type Signatures (RBS)
|
|
453
|
+
|
|
454
|
+
`pbt` ships complete [RBS](https://github.com/ruby/rbs) type signatures under `sig/` (`sig/pbt.rbs`, `sig/pbt/*.rbs`, and `sig/manifest.yaml`), verified in CI with both `rbs validate` and `steep check`.
|
|
455
|
+
|
|
456
|
+
Steep and RBS-aware editors pick up the bundled signatures automatically. Note that on Ruby 3.3 with RBS 3.x (where `Set` is still a stdlib library rather than core), include `library "set"` in your `Steepfile` if you reference `Pbt.set` or `Pbt::Stateful`.
|
|
457
|
+
|
|
458
|
+
## Versioning and Stability
|
|
459
|
+
|
|
460
|
+
`pbt` follows [Semantic Versioning](https://semver.org/):
|
|
461
|
+
|
|
462
|
+
- **Stable public API**: top-level `Pbt.*` entry points (`assert`, `check`, `property`, `stateful`, `assume`, `statistics`, `configure`, `configuration`, and arbitrary constructors), public `Arbitrary` / `Sample` methods, the `Pbt.stateful` DSL and its value/error types (`Run`, `Step`, `Ref`, `Context`, `StepFailure`, `InvalidSpec`, `GenerationError`, `ModelError`), and `Pbt::Reporter::RunDetails`.
|
|
463
|
+
- **Experimental**: `worker: :ractor` remains experimental while Ruby's Ractor API continues to evolve.
|
|
464
|
+
- **Internal classes**: implementation helpers (such as `RunnerIterator`, `Tosser`, `Stateful::Shrinker`, `Stateful::ShrinkSession`, `Stateful::RunSample`, and `Stateful::Resolver`) are not covered by SemVer stability guarantees.
|
|
465
|
+
- **Seed reproducibility**: seeds reproduce identical generated values and shrink paths within the same `pbt` version. Minor and major releases may change PRNG consumption or shrinking order as generators and shrinkers improve.
|
|
505
466
|
|
|
506
467
|
## Development
|
|
507
468
|
|
|
@@ -509,7 +470,7 @@ Once this project finishes the following, we will release v1.0.0.
|
|
|
509
470
|
|
|
510
471
|
```shell
|
|
511
472
|
bin/setup
|
|
512
|
-
bundle exec rake # Run
|
|
473
|
+
bundle exec rake # Run specs, Standard Ruby lint, RBS validation, and Steep type-checking
|
|
513
474
|
```
|
|
514
475
|
|
|
515
476
|
### Test
|
|
@@ -92,6 +92,29 @@ module Pbt
|
|
|
92
92
|
MapArbitrary.new(self, resolved_mapper, unmapper)
|
|
93
93
|
end
|
|
94
94
|
|
|
95
|
+
# Create another arbitrary by feeding each generated value into `binder` (or a block)
|
|
96
|
+
# to produce a dependent arbitrary. Shrinking works in `Pbt.property`, `Pbt.stateful`,
|
|
97
|
+
# and `sample` by shrinking both the outer value and the dependent inner value.
|
|
98
|
+
#
|
|
99
|
+
# @example Block form
|
|
100
|
+
# Pbt.integer(min: 0, max: 100).flat_map { |min| Pbt.integer(min: min, max: 100).map { |max| [min, max] } }
|
|
101
|
+
#
|
|
102
|
+
# @param binder [#call, nil] Callable returning an arbitrary for each generated value.
|
|
103
|
+
# @return [BindArbitrary] New dependent arbitrary.
|
|
104
|
+
def bind(binder = nil, &block)
|
|
105
|
+
if binder && block
|
|
106
|
+
raise ArgumentError, "bind accepts either a binder argument or a block, not both"
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
resolved_binder = binder || block
|
|
110
|
+
unless resolved_binder.respond_to?(:call)
|
|
111
|
+
raise ArgumentError, "bind requires a callable binder or a block"
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
BindArbitrary.new(self, resolved_binder)
|
|
115
|
+
end
|
|
116
|
+
alias_method :flat_map, :bind
|
|
117
|
+
|
|
95
118
|
# Create another arbitrary by filtering values against `refinement`.
|
|
96
119
|
# All the values produced by the resulting arbitrary satisfy `!!refinement(value) == true`.
|
|
97
120
|
#
|
|
@@ -12,6 +12,7 @@ require "pbt/arbitrary/choose_arbitrary"
|
|
|
12
12
|
require "pbt/arbitrary/one_of_arbitrary"
|
|
13
13
|
require "pbt/arbitrary/frequency_arbitrary"
|
|
14
14
|
require "pbt/arbitrary/map_arbitrary"
|
|
15
|
+
require "pbt/arbitrary/bind_arbitrary"
|
|
15
16
|
require "pbt/arbitrary/float_arbitrary"
|
|
16
17
|
require "pbt/arbitrary/filter_arbitrary"
|
|
17
18
|
|
|
@@ -43,7 +44,7 @@ module Pbt
|
|
|
43
44
|
# @param empty [Boolean] Whether the array can be empty or not.
|
|
44
45
|
# @return [Arbitrary<T>]
|
|
45
46
|
def array(arb, min: 0, max: 10, empty: true)
|
|
46
|
-
raise ArgumentError if min < 0
|
|
47
|
+
raise ArgumentError, "min must be zero or positive number, got #{min.inspect}" if min < 0
|
|
47
48
|
min = 1 if min.zero? && !empty
|
|
48
49
|
|
|
49
50
|
ArrayArbitrary.new(arb, min, max)
|
|
@@ -128,11 +129,11 @@ module Pbt
|
|
|
128
129
|
MapArbitrary.new(array(hexa, **kwargs), STRING_MAPPER, STRING_UNMAPPER, bijective: true)
|
|
129
130
|
end
|
|
130
131
|
|
|
131
|
-
# For a single
|
|
132
|
+
# For a single valid Unicode scalar value (excluding UTF-16 surrogates U+D800..U+DFFF).
|
|
132
133
|
#
|
|
133
134
|
# @return [Arbitrary<String>]
|
|
134
135
|
def char
|
|
135
|
-
MapArbitrary.new(choose(
|
|
136
|
+
MapArbitrary.new(choose(UNICODE_SCALAR_INDEX_RANGE), CHAR_MAPPER, CHAR_UNMAPPER, bijective: true)
|
|
136
137
|
end
|
|
137
138
|
|
|
138
139
|
# For an alphanumeric character.
|
|
@@ -174,6 +175,16 @@ module Pbt
|
|
|
174
175
|
constant_from(*PRINTABLE_ASCII_CHARS)
|
|
175
176
|
end
|
|
176
177
|
|
|
178
|
+
# For printable ascii strings (default string generator).
|
|
179
|
+
# Use `ascii_string` to include ASCII control characters, or `printable_string` for full Unicode.
|
|
180
|
+
#
|
|
181
|
+
# @see Pbt.printable_ascii_string
|
|
182
|
+
# @param kwargs [Hash] Options for ArrayArbitrary (`min:`, `max:`, `empty:`).
|
|
183
|
+
# @return [Arbitrary<String>]
|
|
184
|
+
def string(**kwargs)
|
|
185
|
+
printable_ascii_string(**kwargs)
|
|
186
|
+
end
|
|
187
|
+
|
|
177
188
|
# For printable ascii strings.
|
|
178
189
|
#
|
|
179
190
|
# @see Pbt.array
|
|
@@ -183,11 +194,18 @@ module Pbt
|
|
|
183
194
|
MapArbitrary.new(array(printable_ascii_char, **kwargs), STRING_MAPPER, STRING_UNMAPPER, bijective: true)
|
|
184
195
|
end
|
|
185
196
|
|
|
186
|
-
# For a printable character.
|
|
197
|
+
# For a printable Unicode character across printable ASCII, BMP (including CJK), and supplementary planes (including emoji).
|
|
198
|
+
# Excludes C0/C1 control characters, DEL, UTF-16 surrogates, and U+FFFE/U+FFFF.
|
|
187
199
|
#
|
|
188
200
|
# @return [Arbitrary<String>]
|
|
189
201
|
def printable_char
|
|
190
|
-
|
|
202
|
+
codepoint_arb = frequency(
|
|
203
|
+
[3, choose(PRINTABLE_ASCII_CODEPOINT_RANGE)],
|
|
204
|
+
[4, choose(PRINTABLE_BMP_MAIN_CODEPOINT_RANGE)],
|
|
205
|
+
[1, choose(PRINTABLE_BMP_HIGH_CODEPOINT_RANGE)],
|
|
206
|
+
[2, choose(PRINTABLE_SUPPLEMENTARY_CODEPOINT_RANGE)]
|
|
207
|
+
)
|
|
208
|
+
MapArbitrary.new(codepoint_arb, CODEPOINT_MAPPER, CODEPOINT_UNMAPPER, bijective: true)
|
|
191
209
|
end
|
|
192
210
|
|
|
193
211
|
# For printable strings.
|
|
@@ -208,25 +226,35 @@ module Pbt
|
|
|
208
226
|
MapArbitrary.new(array(constant_from(*SYMBOL_SAFE_CHARS), empty: false, **kwargs), SYMBOL_MAPPER, SYMBOL_UNMAPPER, bijective: true)
|
|
209
227
|
end
|
|
210
228
|
|
|
211
|
-
# For floats.
|
|
229
|
+
# For floats within optional finite `Integer` / `Float` bounds, with opt-in NaN / Infinity support.
|
|
230
|
+
# Omitted bounds default generation to `-1_000_000.0..1_000_000.0` (or a scaled span when one-sided)
|
|
231
|
+
# while leaving `cover?` unbounded on omitted sides.
|
|
212
232
|
#
|
|
213
|
-
# @
|
|
214
|
-
|
|
215
|
-
|
|
233
|
+
# @param min [Integer, Float, nil] Inclusive finite lower bound.
|
|
234
|
+
# @param max [Integer, Float, nil] Inclusive finite upper bound.
|
|
235
|
+
# @param allow_nan [Boolean] Whether `Float::NAN` may be generated. Default is `false`.
|
|
236
|
+
# @param allow_infinity [Boolean] Whether `+/-Float::INFINITY` may be generated when the corresponding bound is unbounded. Default is `false`.
|
|
237
|
+
# @return [FloatArbitrary]
|
|
238
|
+
def float(min: nil, max: nil, allow_nan: false, allow_infinity: false)
|
|
239
|
+
FloatArbitrary.new(min: min, max: max, allow_nan: allow_nan, allow_infinity: allow_infinity)
|
|
216
240
|
end
|
|
217
241
|
|
|
218
|
-
# For
|
|
242
|
+
# For sets of values generated by `arb`.
|
|
243
|
+
# Note: `min` and `max` bound the number of element draws before Set deduplication,
|
|
244
|
+
# so the resulting Set may have fewer than `min` elements when `arb` produces duplicates.
|
|
219
245
|
#
|
|
220
|
-
# @param arb [Arbitrary<T>] Arbitrary used to generate the
|
|
221
|
-
# @param min [Integer] Lower limit of
|
|
222
|
-
# @param max [Integer] Upper limit of
|
|
223
|
-
# @param empty [Boolean] Whether the
|
|
224
|
-
# @return [Arbitrary<T
|
|
246
|
+
# @param arb [Arbitrary<T>] Arbitrary used to generate the elements of the set.
|
|
247
|
+
# @param min [Integer] Lower limit of generated draws before deduplication.
|
|
248
|
+
# @param max [Integer] Upper limit of generated draws before deduplication.
|
|
249
|
+
# @param empty [Boolean] Whether the set can be empty or not.
|
|
250
|
+
# @return [Arbitrary<Set<T>>]
|
|
225
251
|
def set(arb, min: 0, max: 10, empty: true)
|
|
226
252
|
array(arb, min: min, max: max, empty: empty).map(SET_MAPPER, SET_UNMAPPER)
|
|
227
253
|
end
|
|
228
254
|
|
|
229
255
|
# For hashes of any keys and values.
|
|
256
|
+
# Note: `min` and `max` bound the number of key-value pair draws before Hash key deduplication,
|
|
257
|
+
# so the resulting Hash may have fewer than `min` entries when `key_arbitrary` produces duplicate keys.
|
|
230
258
|
# If you want to call `Object#hash` for `Pbt`, call this method without arguments.
|
|
231
259
|
#
|
|
232
260
|
# @example
|
|
@@ -241,8 +269,10 @@ module Pbt
|
|
|
241
269
|
if args.size == 2
|
|
242
270
|
key_arbitrary, value_arbitrary = args
|
|
243
271
|
array(tuple(key_arbitrary, value_arbitrary), **kwargs).map(HASH_MAPPER, HASH_UNMAPPER)
|
|
244
|
-
|
|
272
|
+
elsif args.empty? && kwargs.empty?
|
|
245
273
|
super # call `Object#hash`
|
|
274
|
+
else
|
|
275
|
+
raise ArgumentError, "Pbt.hash expects (key_arbitrary, value_arbitrary) or no arguments (for Object#hash)"
|
|
246
276
|
end
|
|
247
277
|
end
|
|
248
278
|
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Pbt
|
|
4
|
+
module Arbitrary
|
|
5
|
+
# Generates dependent values by passing values from an outer arbitrary into a binder
|
|
6
|
+
# that returns an inner arbitrary. Shrinking uses `Sample` provenance to shrink both
|
|
7
|
+
# the outer choice and the dependent inner value together.
|
|
8
|
+
class BindArbitrary < Arbitrary
|
|
9
|
+
# @param arb [Arbitrary] Outer arbitrary.
|
|
10
|
+
# @param binder [#call] Callable returning an inner arbitrary for each outer value.
|
|
11
|
+
def initialize(arb, binder)
|
|
12
|
+
raise ArgumentError, "binder must be callable" unless binder.respond_to?(:call)
|
|
13
|
+
|
|
14
|
+
@arb = arb
|
|
15
|
+
@binder = binder
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# @see Arbitrary#generate
|
|
19
|
+
def generate(rng = Random.new)
|
|
20
|
+
outer_val = @arb.generate(rng)
|
|
21
|
+
inner_seed = rng.rand(1 << 64)
|
|
22
|
+
inner_arb = Sample.validate_bound_arbitrary!(@binder.call(outer_val))
|
|
23
|
+
inner_arb.generate(Random.new(inner_seed))
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# @private
|
|
27
|
+
def contextual?
|
|
28
|
+
true
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# @see Arbitrary#sample
|
|
32
|
+
def sample(rng = Random.new)
|
|
33
|
+
outer_sample = Sample.capture(@arb, rng)
|
|
34
|
+
inner_seed = rng.rand(1 << 64)
|
|
35
|
+
Sample.bind(outer_sample, inner_seed) { |v| @binder.call(v) }
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# Conservative 3-valued membership check (cannot determine dependent codomain in general).
|
|
39
|
+
#
|
|
40
|
+
# @see Arbitrary#cover?
|
|
41
|
+
def cover?(_value)
|
|
42
|
+
nil
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# A bare value cannot identify the outer choice that produced its inner arbitrary.
|
|
46
|
+
# Use `sample`, `Pbt.property`, or `Pbt.stateful` to shrink.
|
|
47
|
+
#
|
|
48
|
+
# @see Arbitrary#shrink
|
|
49
|
+
def shrink(_current)
|
|
50
|
+
[].each
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|