pbt 0.7.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 +23 -0
- data/README.md +117 -80
- data/lib/pbt/arbitrary/arbitrary.rb +63 -10
- data/lib/pbt/arbitrary/arbitrary_methods.rb +51 -24
- data/lib/pbt/arbitrary/array_arbitrary.rb +45 -11
- data/lib/pbt/arbitrary/choose_arbitrary.rb +27 -0
- data/lib/pbt/arbitrary/constant.rb +0 -1
- data/lib/pbt/arbitrary/constant_arbitrary.rb +5 -0
- data/lib/pbt/arbitrary/constant_from_arbitrary.rb +39 -0
- data/lib/pbt/arbitrary/filter_arbitrary.rb +40 -2
- data/lib/pbt/arbitrary/fixed_hash_arbitrary.rb +16 -0
- data/lib/pbt/arbitrary/float_arbitrary.rb +31 -0
- data/lib/pbt/arbitrary/frequency_arbitrary.rb +61 -0
- data/lib/pbt/arbitrary/integer_arbitrary.rb +11 -0
- data/lib/pbt/arbitrary/map_arbitrary.rb +40 -2
- data/lib/pbt/arbitrary/one_of_arbitrary.rb +46 -13
- data/lib/pbt/arbitrary/sample.rb +97 -0
- data/lib/pbt/arbitrary/tuple_arbitrary.rb +22 -0
- data/lib/pbt/check/case.rb +1 -1
- data/lib/pbt/check/configuration.rb +5 -3
- data/lib/pbt/check/property.rb +23 -4
- data/lib/pbt/check/runner_iterator.rb +84 -10
- data/lib/pbt/check/runner_methods.rb +53 -19
- data/lib/pbt/check/tosser.rb +1 -1
- data/lib/pbt/reporter/run_details.rb +2 -2
- data/lib/pbt/reporter/run_details_reporter.rb +46 -4
- data/lib/pbt/reporter/run_execution.rb +47 -4
- data/lib/pbt/stateful/command.rb +215 -0
- data/lib/pbt/stateful/errors.rb +58 -0
- data/lib/pbt/stateful/property.rb +276 -276
- 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/statistics.rb +28 -0
- data/lib/pbt/version.rb +1 -1
- data/lib/pbt.rb +62 -26
- 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 +18 -13
- data/.rspec +0 -3
- data/.standard.yml +0 -3
- data/CLAUDE.md +0 -104
- data/Rakefile +0 -70
- data/benchmark/README.md +0 -103
- data/benchmark/example.txt +0 -299
- data/benchmark/failure_simple.rb +0 -29
- data/benchmark/success_cpu_bound.rb +0 -35
- data/benchmark/success_io_bound.rb +0 -28
- data/benchmark/success_simple.rb +0 -24
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Pbt
|
|
4
|
+
module Stateful
|
|
5
|
+
# Opaque symbolic token referencing the return value of an earlier step with `id == step_id`.
|
|
6
|
+
Ref = Data.define(:step_id) do
|
|
7
|
+
def to_s
|
|
8
|
+
"v#{step_id}"
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
alias_method :inspect, :to_s
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
# Immutable record of a single generated command step.
|
|
15
|
+
Step = Data.define(:id, :command, :args) do
|
|
16
|
+
def format(assign: false)
|
|
17
|
+
call = args.nil? ? command.to_s : "#{command}(#{Resolver.format_value(args)})"
|
|
18
|
+
assign ? "v#{id} = #{call}" : call
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def to_s
|
|
22
|
+
format
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# Pure, immutable representation of a generated stateful test run.
|
|
27
|
+
Run = Data.define(:initial_state, :steps) do
|
|
28
|
+
def initialize(initial_state:, steps:)
|
|
29
|
+
super(initial_state: initial_state, steps: steps.dup.freeze)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def size
|
|
33
|
+
steps.size
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def empty?
|
|
37
|
+
steps.empty?
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def command_names
|
|
41
|
+
steps.map(&:command)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def format(failed_step_index: nil, failed_stage: nil)
|
|
45
|
+
referenced_ids = Resolver.referenced_step_ids(steps)
|
|
46
|
+
lines = ["initial_state: #{Resolver.format_value(initial_state)}"]
|
|
47
|
+
if steps.empty?
|
|
48
|
+
lines << "(no steps)"
|
|
49
|
+
else
|
|
50
|
+
steps.each_with_index do |step, idx|
|
|
51
|
+
line = "#{idx + 1}. #{step.format(assign: referenced_ids.include?(step.id))}"
|
|
52
|
+
if failed_step_index == idx && failed_stage
|
|
53
|
+
line = "#{line} # <- failed (#{failed_stage})"
|
|
54
|
+
end
|
|
55
|
+
lines << line
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
lines.join("\n")
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def to_s
|
|
62
|
+
format
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
alias_method :inspect, :to_s
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# Concrete execution context passed to `c.verify`.
|
|
69
|
+
Context = Data.define(:step, :before, :after, :args, :result, :sut)
|
|
70
|
+
|
|
71
|
+
# Helpers for resolving symbolic Refs, traversing containers, and deep-freezing model states.
|
|
72
|
+
module Resolver
|
|
73
|
+
module_function
|
|
74
|
+
|
|
75
|
+
# Return whether `obj` is a Pbt arbitrary (explicitly avoiding false positives like `Array#sample`).
|
|
76
|
+
#
|
|
77
|
+
# @param obj [Object]
|
|
78
|
+
# @return [Boolean]
|
|
79
|
+
def arbitrary?(obj)
|
|
80
|
+
obj.is_a?(Pbt::Arbitrary::Arbitrary) || (obj.respond_to?(:generate) && obj.respond_to?(:shrink))
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# Deep-duplicate standard value containers and mutable Strings while preserving cycles and opaque objects.
|
|
84
|
+
#
|
|
85
|
+
# @param obj [Object]
|
|
86
|
+
# @param cache [Hash]
|
|
87
|
+
# @return [Object]
|
|
88
|
+
def deep_dup(obj, cache = {}.compare_by_identity)
|
|
89
|
+
return cache[obj] if cache.key?(obj)
|
|
90
|
+
|
|
91
|
+
case obj
|
|
92
|
+
when String
|
|
93
|
+
obj.dup
|
|
94
|
+
when Array
|
|
95
|
+
res = []
|
|
96
|
+
cache[obj] = res
|
|
97
|
+
obj.each { |elem| res << deep_dup(elem, cache) }
|
|
98
|
+
res
|
|
99
|
+
when Hash
|
|
100
|
+
res = {}
|
|
101
|
+
cache[obj] = res
|
|
102
|
+
obj.each { |k, v| res[deep_dup(k, cache)] = deep_dup(v, cache) }
|
|
103
|
+
res
|
|
104
|
+
when Set
|
|
105
|
+
res = Set.new
|
|
106
|
+
cache[obj] = res
|
|
107
|
+
obj.each { |elem| res << deep_dup(elem, cache) }
|
|
108
|
+
res
|
|
109
|
+
when Struct
|
|
110
|
+
res = obj.dup
|
|
111
|
+
cache[obj] = res
|
|
112
|
+
obj.members.each { |member| res[member] = deep_dup(obj[member], cache) }
|
|
113
|
+
res
|
|
114
|
+
when Data
|
|
115
|
+
members = obj.to_h
|
|
116
|
+
members.empty? ? obj : obj.with(**members.transform_values { |v| deep_dup(v, cache) })
|
|
117
|
+
else
|
|
118
|
+
obj
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
# Resolve symbolic `Ref` tokens into concrete SUT return values inside standard value containers.
|
|
123
|
+
# When `detach: true`, mutable Strings and frozen Structs in symbolic values are duplicated so SUT
|
|
124
|
+
# mutations never corrupt immutable `Run`/`Step` values or trigger `FrozenError`, while concrete
|
|
125
|
+
# `Ref` targets from `env` retain their exact object identity.
|
|
126
|
+
#
|
|
127
|
+
# @param obj [Object]
|
|
128
|
+
# @param env [Hash{Integer => Object}]
|
|
129
|
+
# @param detach [Boolean]
|
|
130
|
+
# @param cache [Hash]
|
|
131
|
+
# @return [Object]
|
|
132
|
+
def resolve(obj, env, detach: false, cache: {}.compare_by_identity)
|
|
133
|
+
return cache[obj] if cache.key?(obj)
|
|
134
|
+
|
|
135
|
+
case obj
|
|
136
|
+
when Ref
|
|
137
|
+
env.fetch(obj.step_id)
|
|
138
|
+
when String
|
|
139
|
+
detach ? obj.dup : obj
|
|
140
|
+
when Array
|
|
141
|
+
res = []
|
|
142
|
+
cache[obj] = res
|
|
143
|
+
obj.each { |elem| res << resolve(elem, env, detach: detach, cache: cache) }
|
|
144
|
+
res
|
|
145
|
+
when Hash
|
|
146
|
+
res = {}
|
|
147
|
+
cache[obj] = res
|
|
148
|
+
obj.each do |k, v|
|
|
149
|
+
res[resolve(k, env, detach: detach, cache: cache)] = resolve(v, env, detach: detach, cache: cache)
|
|
150
|
+
end
|
|
151
|
+
res
|
|
152
|
+
when Set
|
|
153
|
+
res = Set.new
|
|
154
|
+
cache[obj] = res
|
|
155
|
+
obj.each { |elem| res << resolve(elem, env, detach: detach, cache: cache) }
|
|
156
|
+
res
|
|
157
|
+
when Struct
|
|
158
|
+
copy = obj.dup
|
|
159
|
+
cache[obj] = copy
|
|
160
|
+
obj.members.each { |member| copy[member] = resolve(obj[member], env, detach: detach, cache: cache) }
|
|
161
|
+
(!detach && obj.frozen?) ? copy.freeze : copy
|
|
162
|
+
when Data
|
|
163
|
+
members = obj.to_h
|
|
164
|
+
members.empty? ? obj : obj.with(**members.transform_values { |v| resolve(v, env, detach: detach, cache: cache) })
|
|
165
|
+
else
|
|
166
|
+
obj
|
|
167
|
+
end
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
# Collect all `Ref` step IDs reachable inside standard value containers.
|
|
171
|
+
#
|
|
172
|
+
# @param obj [Object]
|
|
173
|
+
# @param ids [Set<Integer>]
|
|
174
|
+
# @param visited [Hash]
|
|
175
|
+
# @return [Set<Integer>]
|
|
176
|
+
def collect_ref_ids(obj, ids = Set.new, visited = {}.compare_by_identity)
|
|
177
|
+
return ids if visited.key?(obj)
|
|
178
|
+
|
|
179
|
+
case obj
|
|
180
|
+
when Ref
|
|
181
|
+
ids << obj.step_id
|
|
182
|
+
when Array
|
|
183
|
+
visited[obj] = true
|
|
184
|
+
obj.each { |elem| collect_ref_ids(elem, ids, visited) }
|
|
185
|
+
when Hash
|
|
186
|
+
visited[obj] = true
|
|
187
|
+
obj.each do |k, v|
|
|
188
|
+
collect_ref_ids(k, ids, visited)
|
|
189
|
+
collect_ref_ids(v, ids, visited)
|
|
190
|
+
end
|
|
191
|
+
when Set
|
|
192
|
+
visited[obj] = true
|
|
193
|
+
obj.each { |elem| collect_ref_ids(elem, ids, visited) }
|
|
194
|
+
when Struct
|
|
195
|
+
visited[obj] = true
|
|
196
|
+
obj.members.each { |member| collect_ref_ids(obj[member], ids, visited) }
|
|
197
|
+
when Data
|
|
198
|
+
visited[obj] = true
|
|
199
|
+
obj.to_h.each_value { |v| collect_ref_ids(v, ids, visited) }
|
|
200
|
+
end
|
|
201
|
+
ids
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
# Collect all `Ref` step IDs referenced across a step array's arguments.
|
|
205
|
+
#
|
|
206
|
+
# @param steps [Array<Step>]
|
|
207
|
+
# @return [Set<Integer>]
|
|
208
|
+
def referenced_step_ids(steps)
|
|
209
|
+
ids = Set.new
|
|
210
|
+
steps.each { |step| collect_ref_ids(step.args, ids) }
|
|
211
|
+
ids
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
# Format a value for counterexample traces, rendering `Ref` as `v<id>`.
|
|
215
|
+
#
|
|
216
|
+
# @param obj [Object]
|
|
217
|
+
# @param visited [Hash]
|
|
218
|
+
# @return [String]
|
|
219
|
+
def format_value(obj, visited = {}.compare_by_identity)
|
|
220
|
+
case obj
|
|
221
|
+
when Ref
|
|
222
|
+
obj.to_s
|
|
223
|
+
when Array
|
|
224
|
+
return "[...]" if visited.key?(obj)
|
|
225
|
+
|
|
226
|
+
visited[obj] = true
|
|
227
|
+
begin
|
|
228
|
+
"[#{obj.map { |e| format_value(e, visited) }.join(", ")}]"
|
|
229
|
+
ensure
|
|
230
|
+
visited.delete(obj)
|
|
231
|
+
end
|
|
232
|
+
when Hash
|
|
233
|
+
return "{...}" if visited.key?(obj)
|
|
234
|
+
|
|
235
|
+
visited[obj] = true
|
|
236
|
+
begin
|
|
237
|
+
pairs = obj.map do |k, v|
|
|
238
|
+
key_str = k.is_a?(Symbol) ? "#{k}:" : "#{format_value(k, visited)} =>"
|
|
239
|
+
"#{key_str} #{format_value(v, visited)}"
|
|
240
|
+
end
|
|
241
|
+
"{#{pairs.join(", ")}}"
|
|
242
|
+
ensure
|
|
243
|
+
visited.delete(obj)
|
|
244
|
+
end
|
|
245
|
+
when Set
|
|
246
|
+
return "Set[...]" if visited.key?(obj)
|
|
247
|
+
|
|
248
|
+
visited[obj] = true
|
|
249
|
+
begin
|
|
250
|
+
"Set[#{obj.map { |e| format_value(e, visited) }.join(", ")}]"
|
|
251
|
+
ensure
|
|
252
|
+
visited.delete(obj)
|
|
253
|
+
end
|
|
254
|
+
else
|
|
255
|
+
obj.inspect
|
|
256
|
+
end
|
|
257
|
+
end
|
|
258
|
+
|
|
259
|
+
# Deep-freeze model state to detect accidental mutation immediately.
|
|
260
|
+
#
|
|
261
|
+
# @param obj [Object]
|
|
262
|
+
# @return [Object]
|
|
263
|
+
def deep_freeze(obj)
|
|
264
|
+
Ractor.make_shareable(obj)
|
|
265
|
+
rescue Ractor::Error
|
|
266
|
+
fallback_freeze(obj, {}.compare_by_identity)
|
|
267
|
+
end
|
|
268
|
+
|
|
269
|
+
# Invoke a Proc/lambda with only as many positional arguments as it accepts.
|
|
270
|
+
#
|
|
271
|
+
# @param callable [#call, #parameters]
|
|
272
|
+
# @param args [Array<Object>]
|
|
273
|
+
# @return [Object]
|
|
274
|
+
def invoke_with_arity(callable, *args)
|
|
275
|
+
params = callable.respond_to?(:parameters) ? callable.parameters : nil
|
|
276
|
+
return callable.call(*args) unless params
|
|
277
|
+
|
|
278
|
+
return callable.call(*args) if params.any? { |type, _| type == :rest }
|
|
279
|
+
|
|
280
|
+
max_positional = params.count { |type, _| type == :req || type == :opt }
|
|
281
|
+
callable.call(*args.first(max_positional))
|
|
282
|
+
end
|
|
283
|
+
|
|
284
|
+
def fallback_freeze(obj, visited)
|
|
285
|
+
return obj if visited.key?(obj)
|
|
286
|
+
|
|
287
|
+
visited[obj] = true
|
|
288
|
+
case obj
|
|
289
|
+
when Array
|
|
290
|
+
obj.each { |elem| fallback_freeze(elem, visited) }
|
|
291
|
+
when Hash
|
|
292
|
+
obj.each do |k, v|
|
|
293
|
+
fallback_freeze(k, visited)
|
|
294
|
+
fallback_freeze(v, visited)
|
|
295
|
+
end
|
|
296
|
+
when Set
|
|
297
|
+
obj.each { |elem| fallback_freeze(elem, visited) }
|
|
298
|
+
when Struct
|
|
299
|
+
obj.members.each { |member| fallback_freeze(obj[member], visited) }
|
|
300
|
+
when Data
|
|
301
|
+
obj.to_h.each_value { |v| fallback_freeze(v, visited) }
|
|
302
|
+
end
|
|
303
|
+
obj.freeze
|
|
304
|
+
end
|
|
305
|
+
private_class_method :fallback_freeze
|
|
306
|
+
end
|
|
307
|
+
end
|
|
308
|
+
end
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Pbt
|
|
4
|
+
# Distribution of an independent, seeded sample of generated values.
|
|
5
|
+
class Statistics
|
|
6
|
+
attr_reader :seed, :num_runs, :counts
|
|
7
|
+
|
|
8
|
+
def initialize(arbitrary, num_runs:, seed:, &classifier)
|
|
9
|
+
unless num_runs.is_a?(Integer) && num_runs >= 0
|
|
10
|
+
raise ArgumentError, "num_runs must be a nonnegative Integer"
|
|
11
|
+
end
|
|
12
|
+
@seed = seed
|
|
13
|
+
@num_runs = num_runs
|
|
14
|
+
@counts = Hash.new(0)
|
|
15
|
+
rng = Random.new(seed)
|
|
16
|
+
num_runs.times do
|
|
17
|
+
value = arbitrary.generate(rng)
|
|
18
|
+
label = classifier ? classifier.call(value) : value
|
|
19
|
+
@counts[label] += 1
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# Percentages are not rounded. Empty samples return an empty Hash.
|
|
24
|
+
def percentages
|
|
25
|
+
counts.transform_values { |count| count * 100.0 / num_runs }
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
data/lib/pbt/version.rb
CHANGED
data/lib/pbt.rb
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require_relative "pbt/version"
|
|
4
|
+
require_relative "pbt/statistics"
|
|
4
5
|
require_relative "pbt/arbitrary/arbitrary_methods"
|
|
5
6
|
require_relative "pbt/check/runner_methods"
|
|
6
7
|
require_relative "pbt/check/property"
|
|
@@ -11,6 +12,12 @@ module Pbt
|
|
|
11
12
|
# Represents a property-based test failure.
|
|
12
13
|
class PropertyFailure < StandardError; end
|
|
13
14
|
|
|
15
|
+
# Represents a property-based test failure caused by exceeding the skip budget via `Pbt.assume`.
|
|
16
|
+
class TooManySkipsError < PropertyFailure; end
|
|
17
|
+
|
|
18
|
+
# Internal control-flow exception raised by `Pbt.assume` when a precondition is not met.
|
|
19
|
+
class PreconditionNotMet < StandardError; end
|
|
20
|
+
|
|
14
21
|
# Represents an invalid configuration.
|
|
15
22
|
class InvalidConfiguration < StandardError; end
|
|
16
23
|
|
|
@@ -18,7 +25,19 @@ module Pbt
|
|
|
18
25
|
extend Check::RunnerMethods
|
|
19
26
|
extend Check::ConfigurationMethods
|
|
20
27
|
|
|
21
|
-
#
|
|
28
|
+
# Discard the current generated test case when `condition` is falsy.
|
|
29
|
+
# Discarded cases do not count toward `num_runs` and are skipped during shrinking.
|
|
30
|
+
# Exceeding `max_skips` fails the property with `Pbt::TooManySkipsError`.
|
|
31
|
+
#
|
|
32
|
+
# @param condition [Object]
|
|
33
|
+
# @return [void]
|
|
34
|
+
# @raise [Pbt::PreconditionNotMet]
|
|
35
|
+
def self.assume(condition)
|
|
36
|
+
raise PreconditionNotMet unless condition
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# Create a property-based test with arbitraries. To run the test, pass the returned value to `Pbt.assert` or `Pbt.check`.
|
|
40
|
+
# Predicates fail by raising an exception (including RSpec/Minitest assertions); returning `false` alone does not fail.
|
|
22
41
|
# Be aware that using both positional and keyword arguments is not supported.
|
|
23
42
|
#
|
|
24
43
|
# @example Basic usage
|
|
@@ -26,46 +45,63 @@ module Pbt
|
|
|
26
45
|
# # your test code here
|
|
27
46
|
# end
|
|
28
47
|
#
|
|
29
|
-
# @example Use multiple arbitraries
|
|
30
|
-
# Pbt.property(Pbt.
|
|
48
|
+
# @example Use multiple positional arbitraries
|
|
49
|
+
# Pbt.property(Pbt.ascii_string, Pbt.symbol) do |str, sym|
|
|
31
50
|
# # your test code here
|
|
32
51
|
# end
|
|
33
52
|
#
|
|
34
|
-
# @example Use
|
|
35
|
-
# Pbt.property(x: Pbt.integer, y: Pbt.integer) do |x
|
|
53
|
+
# @example Use keyword arbitraries (under default `worker: :none`)
|
|
54
|
+
# Pbt.property(x: Pbt.integer, y: Pbt.integer) do |x:, y:|
|
|
36
55
|
# # your test code here
|
|
37
56
|
# end
|
|
38
57
|
#
|
|
58
|
+
# @example Use keyword arbitraries with `worker: :ractor` (Ractor passes the Hash as a single argument)
|
|
59
|
+
# Pbt.property(x: Pbt.integer, y: Pbt.integer) do |h|
|
|
60
|
+
# # h[:x], h[:y]
|
|
61
|
+
# end
|
|
62
|
+
#
|
|
39
63
|
# @param args [Array<Arbitrary>] Arbitraries to generate values. You can pass one or more arbitraries.
|
|
40
64
|
# @param kwargs [Hash<Symbol,Arbitrary>] Arbitraries to generate values. You can pass arbitraries with keyword arguments.
|
|
41
65
|
# @param predicate [Proc] Test code that receives generated values and runs the test.
|
|
42
|
-
# @return [Property]
|
|
66
|
+
# @return [Check::Property]
|
|
43
67
|
def self.property(*args, **kwargs, &predicate)
|
|
44
68
|
arb = to_arbitrary(args, kwargs)
|
|
45
|
-
Check::Property.new(
|
|
69
|
+
Check::Property.new(
|
|
70
|
+
arb,
|
|
71
|
+
keyword_args: args.empty? && !kwargs.empty?,
|
|
72
|
+
splat_args: args.size > 1,
|
|
73
|
+
&predicate
|
|
74
|
+
)
|
|
46
75
|
end
|
|
47
76
|
|
|
48
|
-
# Create a stateful property-based test
|
|
49
|
-
# The returned
|
|
50
|
-
#
|
|
51
|
-
# The model object is expected to provide:
|
|
52
|
-
# - `initial_state`
|
|
53
|
-
# - `commands(state)` -> Array of command objects
|
|
54
|
-
#
|
|
55
|
-
# Each command object is expected to provide:
|
|
56
|
-
# - `name`
|
|
57
|
-
# - `arguments` (an arbitrary) or `arguments(state)`
|
|
58
|
-
# - `applicable?(state)` -> bool or `applicable?(state, args)` -> bool
|
|
59
|
-
# - `next_state(state, args)`
|
|
60
|
-
# - `run!(sut, args)` -> result
|
|
61
|
-
# - `verify!(before_state:, after_state:, args:, result:, sut:)`
|
|
77
|
+
# Create a stateful property-based test using a yield-based model builder DSL.
|
|
78
|
+
# The returned `Pbt::Stateful::Property` is compatible with `Pbt.assert`, `Pbt.check`,
|
|
79
|
+
# `Pbt.statistics`, and `property.replay(run)`.
|
|
62
80
|
#
|
|
63
|
-
# @param
|
|
64
|
-
# @param
|
|
65
|
-
# @param
|
|
81
|
+
# @param max_steps [Integer] Maximum number of commands per generated run (default: 20).
|
|
82
|
+
# @param min_steps [Integer] Minimum number of commands per generated run (default: 1).
|
|
83
|
+
# @param max_shrink_attempts [Integer] Total shrink attempt budget per failure (default: 1000).
|
|
84
|
+
# @yieldparam m [Pbt::Stateful::SpecBuilder] Model builder for `initial_state`, `sut`/`teardown`/`around`, `command`, and `invariant`.
|
|
66
85
|
# @return [Pbt::Stateful::Property]
|
|
67
|
-
def self.stateful(
|
|
68
|
-
Stateful::
|
|
86
|
+
def self.stateful(max_steps: 20, min_steps: 1, max_shrink_attempts: 1_000)
|
|
87
|
+
raise Stateful::InvalidSpec, "Pbt.stateful requires a block" unless block_given?
|
|
88
|
+
|
|
89
|
+
builder = Stateful::SpecBuilder.new
|
|
90
|
+
yield(builder)
|
|
91
|
+
spec = builder.build
|
|
92
|
+
Stateful::Property.new(
|
|
93
|
+
spec: spec,
|
|
94
|
+
max_steps: max_steps,
|
|
95
|
+
min_steps: min_steps,
|
|
96
|
+
max_shrink_attempts: max_shrink_attempts
|
|
97
|
+
)
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
# Inspect an independent sample of generated values, optionally grouped by a classifier.
|
|
101
|
+
# Values/labels used as Hash keys must not be mutated during or after sampling.
|
|
102
|
+
# @return [Statistics] Counts, percentages and the seed used for replay.
|
|
103
|
+
def self.statistics(arbitrary, num_runs: 100, seed: Random.new_seed, &classifier)
|
|
104
|
+
Statistics.new(arbitrary, num_runs: num_runs, seed: seed, &classifier)
|
|
69
105
|
end
|
|
70
106
|
|
|
71
107
|
class << self
|
data/sig/manifest.yaml
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
module Pbt
|
|
2
|
+
module Arbitrary
|
|
3
|
+
class EmptyDomainError < ArgumentError
|
|
4
|
+
end
|
|
5
|
+
|
|
6
|
+
class FilterExhaustedError < StandardError
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
interface _Callable
|
|
10
|
+
def call: (*untyped, **untyped) -> untyped
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
interface _Generatable[out T]
|
|
14
|
+
def generate: (?Random rng) -> T
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
interface _Arbitrary[out T]
|
|
18
|
+
def generate: (?Random rng) -> T
|
|
19
|
+
def shrink: (untyped current) -> Enumerator[T, untyped]
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
class Sample[out T]
|
|
23
|
+
attr_reader value: T
|
|
24
|
+
|
|
25
|
+
def initialize: (T value) { () -> Enumerable[Sample[T]] } -> void
|
|
26
|
+
def shrink: () -> Enumerator[Sample[T], untyped]
|
|
27
|
+
def map: [U] () { (T) -> U } -> Sample[U]
|
|
28
|
+
def filter: () { (T) -> boolish } -> Sample[T]
|
|
29
|
+
|
|
30
|
+
def self.capture: [T] (_Arbitrary[T] arbitrary, Random rng) -> Sample[T]
|
|
31
|
+
def self.from_value: [T] (_Arbitrary[T] arbitrary, T value) -> Sample[T]
|
|
32
|
+
def self.contextual?: (untyped arbitrary) -> bool
|
|
33
|
+
def self.tuple: (Array[Sample[untyped]] samples) -> Sample[Array[untyped]]
|
|
34
|
+
def self.array: [T] (Array[Sample[T]] samples, _Arbitrary[Integer] length_arbitrary, Integer min_length) -> Sample[Array[T]]
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
class Arbitrary[out T = untyped]
|
|
38
|
+
def self.cover_result: (untyped arbitrary, untyped value) -> bool?
|
|
39
|
+
|
|
40
|
+
def generate: (?Random rng) -> T
|
|
41
|
+
def shrink: (untyped current) -> Enumerator[T, untyped]
|
|
42
|
+
def cover?: (untyped value) -> bool?
|
|
43
|
+
def sample: (?Random rng) -> Sample[T]
|
|
44
|
+
def contextual?: () -> bool
|
|
45
|
+
def map: [U] () { (T) -> U } -> MapArbitrary[U]
|
|
46
|
+
| [U] (^(T) -> U mapper, ?(^(untyped) -> untyped | _Callable)? unmapper) -> MapArbitrary[U]
|
|
47
|
+
| (_Callable mapper, ?(^(untyped) -> untyped | _Callable)? unmapper) -> MapArbitrary[untyped]
|
|
48
|
+
def filter: (?max_attempts: Integer) { (T) -> boolish } -> FilterArbitrary[T]
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
class IntegerArbitrary < Arbitrary[Integer]
|
|
52
|
+
def initialize: (Integer min, Integer max) -> void
|
|
53
|
+
def shrink: (Integer current, ?target: Integer?) -> Enumerator[Integer, untyped]
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
class ChooseArbitrary < Arbitrary[Integer | Float]
|
|
57
|
+
def initialize: (Range[Integer] | Range[Float] range) -> void
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
class ConstantArbitrary[out T = untyped] < Arbitrary[T]
|
|
61
|
+
def initialize: (T val) -> void
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
class ConstantFromArbitrary[out T = untyped] < Arbitrary[T]
|
|
65
|
+
def initialize: (Array[T] choices) -> void
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
class ArrayArbitrary[out T = untyped] < Arbitrary[Array[T]]
|
|
69
|
+
def initialize: (_Arbitrary[T] value_arb, ?Integer min_length, ?Integer max_length) -> void
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
class TupleArbitrary < Arbitrary[Array[untyped]]
|
|
73
|
+
def initialize: (*_Arbitrary[untyped] arbs) -> void
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
class FixedHashArbitrary < Arbitrary[Hash[untyped, untyped]]
|
|
77
|
+
def initialize: (Hash[untyped, _Arbitrary[untyped]] hash) -> void
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
class OneOfArbitrary[out T = untyped] < Arbitrary[T]
|
|
81
|
+
def initialize: (Array[_Arbitrary[T]] arbitraries) -> void
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
class FrequencyArbitrary[out T = untyped] < Arbitrary[T]
|
|
85
|
+
def initialize: (Array[[Integer, _Arbitrary[T]]] choices) -> void
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
class MapArbitrary[out T = untyped] < Arbitrary[T]
|
|
89
|
+
def initialize: (_Arbitrary[untyped] arb, ^(untyped) -> untyped | _Callable mapper, ?(^(untyped) -> untyped | _Callable)? unmapper, ?bijective: bool) -> void
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
class FloatArbitrary < MapArbitrary[Float]
|
|
93
|
+
def initialize: () -> void
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
class FilterArbitrary[out T = untyped] < Arbitrary[T]
|
|
97
|
+
DEFAULT_MAX_ATTEMPTS: Integer
|
|
98
|
+
|
|
99
|
+
def initialize: (_Arbitrary[T] arb, ?max_attempts: Integer) { (T) -> boolish } -> void
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
module ArbitraryMethods
|
|
103
|
+
def integer: (?min: Integer, ?max: Integer) -> IntegerArbitrary
|
|
104
|
+
def nat: (?max: Integer) -> IntegerArbitrary
|
|
105
|
+
def array: [T] (_Arbitrary[T] arb, ?min: Integer, ?max: Integer, ?empty: bool) -> ArrayArbitrary[T]
|
|
106
|
+
def tuple: (*_Arbitrary[untyped] arbs) -> TupleArbitrary
|
|
107
|
+
def fixed_hash: (Hash[untyped, _Arbitrary[untyped]] hash) -> FixedHashArbitrary
|
|
108
|
+
def choose: (Range[Integer] | Range[Float] range) -> ChooseArbitrary
|
|
109
|
+
def constant_from: [T] (*T choices) -> ConstantFromArbitrary[T]
|
|
110
|
+
def one_of: [T] (*_Arbitrary[T] arbitraries) -> OneOfArbitrary[T]
|
|
111
|
+
def frequency: [T] (*[Integer, _Arbitrary[T]] choices) -> FrequencyArbitrary[T]
|
|
112
|
+
def hexa: () -> ConstantFromArbitrary[String]
|
|
113
|
+
def hexa_string: (?min: Integer, ?max: Integer, ?empty: bool) -> MapArbitrary[String]
|
|
114
|
+
def char: () -> MapArbitrary[String]
|
|
115
|
+
def alphanumeric_char: () -> ConstantFromArbitrary[String]
|
|
116
|
+
def alphanumeric_string: (?min: Integer, ?max: Integer, ?empty: bool) -> MapArbitrary[String]
|
|
117
|
+
def ascii_char: () -> ConstantFromArbitrary[String]
|
|
118
|
+
def ascii_string: (?min: Integer, ?max: Integer, ?empty: bool) -> MapArbitrary[String]
|
|
119
|
+
def printable_ascii_char: () -> ConstantFromArbitrary[String]
|
|
120
|
+
def printable_ascii_string: (?min: Integer, ?max: Integer, ?empty: bool) -> MapArbitrary[String]
|
|
121
|
+
def printable_char: () -> ConstantFromArbitrary[String]
|
|
122
|
+
def printable_string: (?min: Integer, ?max: Integer, ?empty: bool) -> MapArbitrary[String]
|
|
123
|
+
def symbol: (?min: Integer, ?max: Integer, ?empty: bool) -> MapArbitrary[Symbol]
|
|
124
|
+
def float: () -> FloatArbitrary
|
|
125
|
+
def set: [T] (_Arbitrary[T] arb, ?min: Integer, ?max: Integer, ?empty: bool) -> MapArbitrary[Set[T]]
|
|
126
|
+
def hash: () -> Integer
|
|
127
|
+
| [K, V] (_Arbitrary[K] key_arbitrary, _Arbitrary[V] value_arbitrary, ?min: Integer, ?max: Integer, ?empty: bool) -> MapArbitrary[Hash[K, V]]
|
|
128
|
+
def boolean: () -> ConstantFromArbitrary[bool]
|
|
129
|
+
def constant: [T] (T val) -> ConstantArbitrary[T]
|
|
130
|
+
def nil: () -> ConstantArbitrary[nil]
|
|
131
|
+
def date: (?base_date: Date, ?past_offset_days: Integer, ?future_offset_days: Integer) -> MapArbitrary[Date]
|
|
132
|
+
def past_date: (?base_date: Date, ?past_offset_days: Integer) -> MapArbitrary[Date]
|
|
133
|
+
def future_date: (?base_date: Date, ?future_offset_days: Integer) -> MapArbitrary[Date]
|
|
134
|
+
def time: (?base_time: Time, ?past_offset_seconds: Integer, ?future_offset_seconds: Integer) -> MapArbitrary[Time]
|
|
135
|
+
def past_time: (?base_time: Time, ?past_offset_seconds: Integer) -> MapArbitrary[Time]
|
|
136
|
+
def future_time: (?base_time: Time, ?future_offset_seconds: Integer) -> MapArbitrary[Time]
|
|
137
|
+
end
|
|
138
|
+
end
|
|
139
|
+
end
|
data/sig/pbt/check.rbs
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
module Pbt
|
|
2
|
+
module Check
|
|
3
|
+
FATAL_EXCEPTIONS: Array[singleton(Exception)]
|
|
4
|
+
|
|
5
|
+
type worker_mode = :none | :ractor
|
|
6
|
+
|
|
7
|
+
interface _Property
|
|
8
|
+
def generate: (?Random rng) -> untyped
|
|
9
|
+
def shrink: (untyped val) -> Enumerator[untyped, untyped]
|
|
10
|
+
def run: (untyped val) -> untyped
|
|
11
|
+
def run_in_ractor: (untyped val) -> Ractor
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
class Configuration < Struct[untyped]
|
|
15
|
+
attr_accessor verbose: bool
|
|
16
|
+
attr_accessor worker: worker_mode
|
|
17
|
+
attr_accessor num_runs: Integer
|
|
18
|
+
attr_accessor seed: Integer
|
|
19
|
+
attr_accessor thread_report_on_exception: bool
|
|
20
|
+
attr_accessor max_skips: Integer?
|
|
21
|
+
|
|
22
|
+
def initialize: (
|
|
23
|
+
?verbose: bool,
|
|
24
|
+
?worker: worker_mode,
|
|
25
|
+
?num_runs: Integer,
|
|
26
|
+
?seed: Integer,
|
|
27
|
+
?thread_report_on_exception: bool,
|
|
28
|
+
?max_skips: Integer?
|
|
29
|
+
) -> void
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
module ConfigurationMethods
|
|
33
|
+
def configuration: () -> Configuration
|
|
34
|
+
def configure: () { (Configuration) -> untyped } -> untyped
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
class Case < Struct[untyped]
|
|
38
|
+
attr_accessor val: untyped
|
|
39
|
+
attr_accessor ractor: Ractor?
|
|
40
|
+
attr_accessor exception: Exception?
|
|
41
|
+
attr_accessor index: Integer?
|
|
42
|
+
attr_accessor sample: Arbitrary::Sample[untyped]?
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
class Property
|
|
46
|
+
def initialize: (Arbitrary::_Arbitrary[untyped] arb, ?keyword_args: bool, ?splat_args: bool) { (*untyped, **untyped) -> untyped } -> void
|
|
47
|
+
def generate: (?Random rng) -> untyped
|
|
48
|
+
def generate_for_check: (Random rng) -> untyped
|
|
49
|
+
def shrink: (untyped val) -> Enumerator[untyped, untyped]
|
|
50
|
+
def run: (untyped val) -> void
|
|
51
|
+
def run_in_ractor: (untyped val) -> Ractor
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
module RunnerMethods
|
|
55
|
+
def assert: (
|
|
56
|
+
?verbose: bool,
|
|
57
|
+
?worker: worker_mode,
|
|
58
|
+
?num_runs: Integer,
|
|
59
|
+
?seed: Integer,
|
|
60
|
+
?thread_report_on_exception: bool,
|
|
61
|
+
?max_skips: Integer?
|
|
62
|
+
) { () -> _Property } -> void
|
|
63
|
+
|
|
64
|
+
def check: (
|
|
65
|
+
?verbose: bool,
|
|
66
|
+
?worker: worker_mode,
|
|
67
|
+
?num_runs: Integer,
|
|
68
|
+
?seed: Integer,
|
|
69
|
+
?thread_report_on_exception: bool,
|
|
70
|
+
?max_skips: Integer?
|
|
71
|
+
) { () -> _Property } -> Reporter::RunDetails
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|