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
|
@@ -1,361 +1,357 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require "pbt/stateful/errors"
|
|
4
|
+
require "pbt/stateful/types"
|
|
5
|
+
require "pbt/stateful/command"
|
|
6
|
+
require "pbt/stateful/spec"
|
|
7
|
+
require "pbt/stateful/shrinker"
|
|
8
|
+
|
|
3
9
|
module Pbt
|
|
4
10
|
module Stateful
|
|
5
|
-
# Property-compatible
|
|
6
|
-
# It provides `generate`, `shrink` and `run`, so existing runners can execute it.
|
|
11
|
+
# Property-compatible stateful test driver backing `Pbt.stateful { |m| ... }`.
|
|
7
12
|
class Property
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
REQUIRED_COMMAND_METHODS = %i[
|
|
11
|
-
name
|
|
12
|
-
arguments
|
|
13
|
-
applicable?
|
|
14
|
-
next_state
|
|
15
|
-
run!
|
|
16
|
-
verify!
|
|
17
|
-
].freeze
|
|
18
|
-
|
|
19
|
-
Step = Struct.new(:command, :args) do
|
|
20
|
-
def inspect
|
|
21
|
-
"#<Pbt::Stateful::Step command=#{command_label}, args=#{args.inspect}>"
|
|
22
|
-
end
|
|
13
|
+
PER_STEP_MAX_ATTEMPTS = 32
|
|
14
|
+
private_constant :PER_STEP_MAX_ATTEMPTS
|
|
23
15
|
|
|
24
|
-
|
|
16
|
+
attr_reader :spec, :max_steps, :min_steps, :max_shrink_attempts
|
|
25
17
|
|
|
26
|
-
|
|
27
|
-
if command.respond_to?(:name)
|
|
28
|
-
command.name.inspect
|
|
29
|
-
else
|
|
30
|
-
command.class.name || command.class.inspect
|
|
31
|
-
end
|
|
32
|
-
end
|
|
33
|
-
end
|
|
34
|
-
|
|
35
|
-
# @param model [Object]
|
|
36
|
-
# @param sut [Proc]
|
|
18
|
+
# @param spec [Spec]
|
|
37
19
|
# @param max_steps [Integer]
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
@model = model
|
|
45
|
-
@sut_factory = sut
|
|
20
|
+
# @param min_steps [Integer]
|
|
21
|
+
# @param max_shrink_attempts [Integer]
|
|
22
|
+
def initialize(spec:, max_steps: 20, min_steps: 1, max_shrink_attempts: 1_000)
|
|
23
|
+
validate_options!(max_steps: max_steps, min_steps: min_steps, max_shrink_attempts: max_shrink_attempts)
|
|
24
|
+
|
|
25
|
+
@spec = spec
|
|
46
26
|
@max_steps = max_steps
|
|
27
|
+
@min_steps = min_steps
|
|
28
|
+
@max_shrink_attempts = max_shrink_attempts
|
|
29
|
+
@shrinker = Shrinker.new(spec)
|
|
47
30
|
end
|
|
48
31
|
|
|
49
|
-
|
|
32
|
+
def stateful?
|
|
33
|
+
true
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# Generate a pure immutable `Pbt::Stateful::Run` (compatible with `Pbt.statistics`).
|
|
50
37
|
#
|
|
51
38
|
# @param rng [Random]
|
|
52
|
-
# @return [
|
|
39
|
+
# @return [Run]
|
|
53
40
|
def generate(rng = Random.new)
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
sequence = []
|
|
41
|
+
generate_for_check(rng).value
|
|
42
|
+
end
|
|
57
43
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
44
|
+
# Generate a `RunSample` carrying contextual shrink provenance for `Pbt.check` / `Pbt.assert`.
|
|
45
|
+
#
|
|
46
|
+
# @param rng [Random]
|
|
47
|
+
# @return [RunSample]
|
|
48
|
+
def generate_for_check(rng = Random.new)
|
|
49
|
+
target_length = (@min_steps == @max_steps) ? @min_steps : rng.rand(@min_steps..@max_steps)
|
|
50
|
+
initial_state, initial_state_sample, initial_eligible = draw_initial_state(rng, target_length)
|
|
51
|
+
|
|
52
|
+
state = initial_state
|
|
53
|
+
steps = []
|
|
54
|
+
step_samples = {}
|
|
55
|
+
next_id = 0
|
|
56
|
+
|
|
57
|
+
target_length.times do |step_index|
|
|
58
|
+
eligible = (step_index == 0) ? initial_eligible : eligible_commands(state)
|
|
59
|
+
break if eligible.empty?
|
|
60
|
+
|
|
61
|
+
selected = select_step(eligible, state, step_index, next_id, rng)
|
|
62
|
+
if selected.nil?
|
|
63
|
+
break unless steps.empty?
|
|
64
|
+
|
|
65
|
+
raise GenerationError,
|
|
66
|
+
"all eligible commands produced empty argument domains at step #{step_index} " \
|
|
67
|
+
"(eligible: #{eligible.map(&:name).inspect})"
|
|
68
|
+
end
|
|
61
69
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
70
|
+
step, sample, next_state = selected
|
|
71
|
+
next_id += 1
|
|
72
|
+
steps << step
|
|
73
|
+
step_samples[step.id] = sample
|
|
74
|
+
state = next_state
|
|
65
75
|
end
|
|
66
76
|
|
|
67
|
-
|
|
77
|
+
run = Run.new(initial_state: initial_state, steps: steps.freeze)
|
|
78
|
+
RunSample.new(
|
|
79
|
+
run,
|
|
80
|
+
initial_state_sample: initial_state_sample,
|
|
81
|
+
step_samples: step_samples,
|
|
82
|
+
session: ShrinkSession.new(max_attempts: @max_shrink_attempts)
|
|
83
|
+
)
|
|
68
84
|
end
|
|
69
85
|
|
|
70
|
-
#
|
|
86
|
+
# Execute a `Run` against a fresh SUT with full lifecycle, verification, and invariants.
|
|
71
87
|
#
|
|
72
|
-
# @param
|
|
73
|
-
# @return [
|
|
74
|
-
def
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
88
|
+
# @param run [Run]
|
|
89
|
+
# @return [void]
|
|
90
|
+
def run(run)
|
|
91
|
+
sym_state = Resolver.deep_freeze(Resolver.deep_dup(run.initial_state))
|
|
92
|
+
env = {}
|
|
93
|
+
sut_initial = Resolver.resolve(sym_state, env, detach: true)
|
|
78
94
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
95
|
+
@spec.with_sut(sut_initial) do |sut|
|
|
96
|
+
inv_initial = Resolver.resolve(sym_state, env)
|
|
97
|
+
check_invariants!(inv_initial, sut, step_index: nil, step: nil)
|
|
98
|
+
|
|
99
|
+
run.steps.each_with_index do |step, index|
|
|
100
|
+
command = @spec.command_by_name(step.command)
|
|
101
|
+
concrete_args = Resolver.resolve(step.args, env, detach: true)
|
|
102
|
+
|
|
103
|
+
result = execute_stage(stage: :run, target: command.name, step_index: index, step: step) do
|
|
104
|
+
command.run!(sut, concrete_args)
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
after_sym = command.next_state(sym_state, step.args, Ref.new(step_id: step.id))
|
|
108
|
+
env[step.id] = result
|
|
82
109
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
break unless applicable?(command, state, args, context: "shrink step #{index}")
|
|
110
|
+
before_concrete = Resolver.resolve(sym_state, env)
|
|
111
|
+
after_concrete = Resolver.resolve(after_sym, env)
|
|
112
|
+
verify_args = Resolver.resolve(step.args, env)
|
|
87
113
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
114
|
+
context = Context.new(
|
|
115
|
+
step: step,
|
|
116
|
+
before: before_concrete,
|
|
117
|
+
after: after_concrete,
|
|
118
|
+
args: verify_args,
|
|
119
|
+
result: result,
|
|
120
|
+
sut: sut
|
|
121
|
+
)
|
|
91
122
|
|
|
92
|
-
|
|
123
|
+
execute_stage(stage: :verify, target: command.name, step_index: index, step: step) do
|
|
124
|
+
command.verify!(context)
|
|
93
125
|
end
|
|
94
126
|
|
|
95
|
-
|
|
127
|
+
check_invariants!(after_concrete, sut, step_index: index, step: step)
|
|
128
|
+
sym_state = after_sym
|
|
96
129
|
end
|
|
97
130
|
end
|
|
98
131
|
end
|
|
99
132
|
|
|
100
|
-
#
|
|
101
|
-
def stateful?
|
|
102
|
-
true
|
|
103
|
-
end
|
|
104
|
-
|
|
105
|
-
# Stateful properties currently require sequential execution because the model,
|
|
106
|
-
# commands and SUT factory are ordinary Ruby objects and are not guaranteed to be
|
|
107
|
-
# Ractor-shareable.
|
|
133
|
+
# Deterministically replay a `Pbt::Stateful::Run` (e.g. `details.counterexample`).
|
|
108
134
|
#
|
|
109
|
-
# @param
|
|
110
|
-
# @
|
|
111
|
-
def
|
|
112
|
-
|
|
135
|
+
# @param run [Run]
|
|
136
|
+
# @return [void]
|
|
137
|
+
def replay(run)
|
|
138
|
+
unless run.is_a?(Run)
|
|
139
|
+
raise ArgumentError, "replay expects a Pbt::Stateful::Run, got #{run.class}"
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
validate_replay_run!(run)
|
|
143
|
+
self.run(run)
|
|
113
144
|
end
|
|
114
145
|
|
|
115
|
-
#
|
|
146
|
+
# Determine whether a shrink candidate failure matches the original failure signature.
|
|
147
|
+
# A shrink candidate that introduces a teardown error is always accepted so that `shrink_case`
|
|
148
|
+
# immediately halts shrinking and surfaces the cleanup breakage instead of masking it.
|
|
116
149
|
#
|
|
117
|
-
# @param
|
|
118
|
-
# @
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
command, args = unpack_step(step)
|
|
125
|
-
validate_command_protocol!(command, state:, context: "run step #{index}")
|
|
150
|
+
# @param initial_err [Exception]
|
|
151
|
+
# @param candidate_err [Exception]
|
|
152
|
+
# @return [Boolean]
|
|
153
|
+
def same_failure?(initial_err, candidate_err)
|
|
154
|
+
return false unless initial_err.is_a?(StepFailure) && candidate_err.is_a?(StepFailure)
|
|
155
|
+
return false if initial_err.teardown_error
|
|
156
|
+
return true if candidate_err.teardown_error
|
|
126
157
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
end
|
|
158
|
+
initial_err.signature == candidate_err.signature
|
|
159
|
+
end
|
|
130
160
|
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
161
|
+
# Shrink a failing runner `Case` while preserving provenance and shared shrink session.
|
|
162
|
+
#
|
|
163
|
+
# @param test_case [Pbt::Check::Case]
|
|
164
|
+
# @return [Enumerator<RunSample>]
|
|
165
|
+
def shrink_case(test_case)
|
|
166
|
+
return [].each if test_case.exception.is_a?(StepFailure) && test_case.exception.teardown_error
|
|
167
|
+
|
|
168
|
+
run_sample =
|
|
169
|
+
if test_case.sample.is_a?(RunSample)
|
|
170
|
+
test_case.sample
|
|
171
|
+
else
|
|
172
|
+
RunSample.new(
|
|
173
|
+
test_case.val,
|
|
174
|
+
initial_state_sample: nil,
|
|
175
|
+
step_samples: {},
|
|
176
|
+
session: ShrinkSession.new(max_attempts: @max_shrink_attempts)
|
|
142
177
|
)
|
|
143
|
-
rescue *Check::FATAL_EXCEPTIONS
|
|
144
|
-
raise
|
|
145
|
-
rescue Exception => e # standard:disable Lint/RescueException:
|
|
146
|
-
raise e.class,
|
|
147
|
-
"stateful step #{index} (#{command_name(command)}): #{e.message} [args=#{args.inspect}]",
|
|
148
|
-
e.backtrace
|
|
149
178
|
end
|
|
150
179
|
|
|
151
|
-
|
|
152
|
-
end
|
|
180
|
+
@shrinker.shrink(run_sample, failure: test_case.exception)
|
|
153
181
|
end
|
|
154
182
|
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
# @param
|
|
158
|
-
# @return [
|
|
159
|
-
def
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
183
|
+
# Plain-value shrink fallback yielding `Run` objects.
|
|
184
|
+
#
|
|
185
|
+
# @param run [Run]
|
|
186
|
+
# @return [Enumerator<Run>]
|
|
187
|
+
def shrink(run)
|
|
188
|
+
run_sample = RunSample.new(
|
|
189
|
+
run,
|
|
190
|
+
initial_state_sample: nil,
|
|
191
|
+
step_samples: {},
|
|
192
|
+
session: ShrinkSession.new(max_attempts: @max_shrink_attempts)
|
|
193
|
+
)
|
|
194
|
+
Enumerator.new do |y|
|
|
195
|
+
@shrinker.shrink(run_sample, failure: nil).each do |sample|
|
|
196
|
+
y << sample.value
|
|
197
|
+
end
|
|
167
198
|
end
|
|
168
199
|
end
|
|
169
200
|
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
def command_name(command)
|
|
173
|
-
command.respond_to?(:name) ? command.name.to_s : (command.class.name || command.class.inspect)
|
|
201
|
+
def run_in_ractor(_run)
|
|
202
|
+
raise Pbt::InvalidConfiguration, "Pbt.stateful does not support worker: :ractor. Use worker: :none instead."
|
|
174
203
|
end
|
|
175
204
|
|
|
176
|
-
|
|
177
|
-
# @return [void]
|
|
178
|
-
def validate_model!(model)
|
|
179
|
-
missing_methods = %i[initial_state commands].reject { |method_name| model.respond_to?(method_name) }
|
|
180
|
-
return if missing_methods.empty?
|
|
205
|
+
private
|
|
181
206
|
|
|
182
|
-
|
|
183
|
-
|
|
207
|
+
def validate_options!(max_steps:, min_steps:, max_shrink_attempts:)
|
|
208
|
+
unless min_steps.is_a?(Integer) && min_steps >= 0
|
|
209
|
+
raise InvalidSpec, "min_steps must be a non-negative Integer, got #{min_steps.inspect}"
|
|
210
|
+
end
|
|
211
|
+
unless max_steps.is_a?(Integer) && max_steps.positive? && max_steps >= min_steps
|
|
212
|
+
raise InvalidSpec, "max_steps must be a positive Integer >= min_steps (#{min_steps}), got #{max_steps.inspect}"
|
|
213
|
+
end
|
|
214
|
+
unless max_shrink_attempts.is_a?(Integer) && max_shrink_attempts >= 0
|
|
215
|
+
raise InvalidSpec, "max_shrink_attempts must be a non-negative Integer, got #{max_shrink_attempts.inspect}"
|
|
216
|
+
end
|
|
184
217
|
end
|
|
185
218
|
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
def commands_for(state, context:)
|
|
190
|
-
commands = @model.commands(state)
|
|
219
|
+
def draw_initial_state(rng, target_length)
|
|
220
|
+
initial_state, initial_state_sample = @spec.sample_initial_state(rng)
|
|
221
|
+
initial_eligible = eligible_commands(initial_state)
|
|
191
222
|
|
|
192
|
-
unless
|
|
193
|
-
|
|
194
|
-
|
|
223
|
+
unless @spec.arbitrary_initial_state?
|
|
224
|
+
if initial_eligible.empty?
|
|
225
|
+
raise InvalidSpec,
|
|
226
|
+
"no command guard is truthy at initial_state #{Resolver.format_value(initial_state)}"
|
|
227
|
+
end
|
|
228
|
+
return [initial_state, initial_state_sample, initial_eligible]
|
|
195
229
|
end
|
|
196
230
|
|
|
197
|
-
|
|
198
|
-
commands
|
|
199
|
-
end
|
|
231
|
+
return [initial_state, initial_state_sample, initial_eligible] unless initial_eligible.empty?
|
|
200
232
|
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
"Pbt.stateful command protocol mismatch for #{command.class} " \
|
|
210
|
-
"(name=#{safe_command_label(command)}, missing: #{missing_methods.join(", ")}, context=#{context})"
|
|
233
|
+
if target_length.zero?
|
|
234
|
+
probe_rng = rng.dup
|
|
235
|
+
PER_STEP_MAX_ATTEMPTS.times do
|
|
236
|
+
probed_state, = @spec.sample_initial_state(probe_rng)
|
|
237
|
+
return [initial_state, initial_state_sample, initial_eligible] unless eligible_commands(probed_state).empty?
|
|
238
|
+
end
|
|
239
|
+
raise GenerationError,
|
|
240
|
+
"no command guard was truthy across #{PER_STEP_MAX_ATTEMPTS} initial_state samples"
|
|
211
241
|
end
|
|
212
|
-
end
|
|
213
242
|
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
missing_methods = %i[generate shrink].reject { |method_name| arguments.respond_to?(method_name) }
|
|
220
|
-
return if missing_methods.empty?
|
|
243
|
+
(PER_STEP_MAX_ATTEMPTS - 1).times do
|
|
244
|
+
initial_state, initial_state_sample = @spec.sample_initial_state(rng)
|
|
245
|
+
initial_eligible = eligible_commands(initial_state)
|
|
246
|
+
return [initial_state, initial_state_sample, initial_eligible] unless initial_eligible.empty?
|
|
247
|
+
end
|
|
221
248
|
|
|
222
|
-
raise
|
|
223
|
-
"
|
|
224
|
-
"(name=#{safe_command_label(command)}, missing: #{missing_methods.join(", ")}, context=#{context})"
|
|
249
|
+
raise GenerationError,
|
|
250
|
+
"no command guard was truthy across #{PER_STEP_MAX_ATTEMPTS} initial_state samples"
|
|
225
251
|
end
|
|
226
252
|
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
253
|
+
def validate_replay_run!(run)
|
|
254
|
+
available_ids = Set.new
|
|
255
|
+
run.steps.each_with_index do |step, index|
|
|
256
|
+
@spec.command_by_name(step.command)
|
|
257
|
+
ref_ids = Resolver.collect_ref_ids(step.args)
|
|
258
|
+
unbound = ref_ids.reject { |id| available_ids.include?(id) }
|
|
259
|
+
unless unbound.empty?
|
|
260
|
+
refs_str = unbound.map { |id| "v#{id}" }.join(", ")
|
|
261
|
+
raise InvalidSpec,
|
|
262
|
+
"cannot replay run: unbound Ref #{refs_str} in step #{index + 1} #{step.format}"
|
|
263
|
+
end
|
|
264
|
+
available_ids << step.id
|
|
265
|
+
end
|
|
231
266
|
end
|
|
232
267
|
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
# @param context [String]
|
|
236
|
-
# @return [Array<Array<Object, Object>>]
|
|
237
|
-
def generate_candidates_for(state, rng, context:)
|
|
238
|
-
commands_for(state, context:).filter_map do |command|
|
|
239
|
-
args = generate_applicable_args(command, state, rng, context:)
|
|
240
|
-
next if args.equal?(NoApplicableArgs)
|
|
241
|
-
|
|
242
|
-
[command, args]
|
|
243
|
-
end
|
|
268
|
+
def eligible_commands(state)
|
|
269
|
+
@spec.commands.select { |cmd| cmd.guard?(state) }
|
|
244
270
|
end
|
|
245
271
|
|
|
246
|
-
#
|
|
247
|
-
#
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
272
|
+
# Returns `[step, sample, next_state]`, or `nil` if all eligible commands had empty domains.
|
|
273
|
+
# Raises `GenerationError` if eligible commands had non-empty domains but preconditions never passed.
|
|
274
|
+
def select_step(eligible, state, step_index, step_id, rng)
|
|
275
|
+
candidates = eligible.dup
|
|
276
|
+
attempts = 0
|
|
277
|
+
|
|
278
|
+
while attempts < PER_STEP_MAX_ATTEMPTS && !candidates.empty?
|
|
279
|
+
attempts += 1
|
|
280
|
+
command = pick_weighted(candidates, rng)
|
|
281
|
+
|
|
282
|
+
arb =
|
|
283
|
+
begin
|
|
284
|
+
command.arguments_for(state)
|
|
285
|
+
rescue Pbt::Arbitrary::EmptyDomainError
|
|
286
|
+
candidates.delete(command)
|
|
287
|
+
next
|
|
288
|
+
end
|
|
253
289
|
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
290
|
+
sample =
|
|
291
|
+
begin
|
|
292
|
+
Pbt::Arbitrary::Sample.capture(arb, rng)
|
|
293
|
+
rescue Pbt::Arbitrary::EmptyDomainError
|
|
294
|
+
candidates.delete(command)
|
|
295
|
+
next
|
|
296
|
+
end
|
|
257
297
|
|
|
258
|
-
|
|
298
|
+
frozen_args = Resolver.deep_freeze(Resolver.deep_dup(sample.value))
|
|
299
|
+
if command.precondition?(state, frozen_args)
|
|
300
|
+
ref = Ref.new(step_id: step_id)
|
|
301
|
+
next_state = command.next_state(state, frozen_args, ref)
|
|
302
|
+
step = Step.new(id: step_id, command: command.name, args: frozen_args)
|
|
303
|
+
return [step, sample, next_state]
|
|
304
|
+
end
|
|
259
305
|
end
|
|
260
306
|
|
|
261
|
-
|
|
262
|
-
rescue Pbt::Arbitrary::EmptyDomainError
|
|
263
|
-
NoApplicableArgs
|
|
264
|
-
end
|
|
307
|
+
return nil if candidates.empty?
|
|
265
308
|
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
# @return [Object]
|
|
270
|
-
def arguments_for(command, state, context:)
|
|
271
|
-
command.arguments(state)
|
|
309
|
+
raise GenerationError,
|
|
310
|
+
"could not satisfy preconditions at step #{step_index} after #{PER_STEP_MAX_ATTEMPTS} attempts " \
|
|
311
|
+
"(eligible: #{eligible.map(&:name).inspect})"
|
|
272
312
|
end
|
|
273
313
|
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
# @param context [String]
|
|
277
|
-
# @return [Object]
|
|
278
|
-
def arbitrary_for(command, state, context:)
|
|
279
|
-
arguments = arguments_for(command, state, context:)
|
|
280
|
-
validate_arguments_protocol!(command, arguments, context:)
|
|
281
|
-
arguments
|
|
282
|
-
end
|
|
314
|
+
def pick_weighted(commands, rng)
|
|
315
|
+
return commands.first if commands.size == 1
|
|
283
316
|
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
rescue Pbt::Arbitrary::EmptyDomainError
|
|
291
|
-
NoApplicableArgs
|
|
292
|
-
end
|
|
293
|
-
|
|
294
|
-
# @param command [Object]
|
|
295
|
-
# @param state [Object]
|
|
296
|
-
# @param args [Object]
|
|
297
|
-
# @param context [String]
|
|
298
|
-
# @return [Boolean]
|
|
299
|
-
def applicable?(command, state, args, context:)
|
|
300
|
-
command.applicable?(state, args)
|
|
301
|
-
end
|
|
302
|
-
|
|
303
|
-
# @param sequence [Array<Hash, Step>]
|
|
304
|
-
# @param index [Integer]
|
|
305
|
-
# @param command [Object]
|
|
306
|
-
# @param args [Object]
|
|
307
|
-
# @return [Array<Hash, Step>]
|
|
308
|
-
def replace_step(sequence, index, command:, args:)
|
|
309
|
-
candidate = sequence.dup
|
|
310
|
-
candidate[index] = rebuild_step(sequence[index], command:, args:)
|
|
311
|
-
candidate
|
|
312
|
-
end
|
|
313
|
-
|
|
314
|
-
# @param step [Hash, Step]
|
|
315
|
-
# @param command [Object]
|
|
316
|
-
# @param args [Object]
|
|
317
|
-
# @return [Hash, Step]
|
|
318
|
-
def rebuild_step(step, command:, args:)
|
|
319
|
-
case step
|
|
320
|
-
in Step
|
|
321
|
-
Step.new(command:, args:)
|
|
322
|
-
in Hash
|
|
323
|
-
{command:, args:}
|
|
324
|
-
else
|
|
325
|
-
raise ArgumentError, "invalid stateful step: #{step.inspect}"
|
|
317
|
+
total = commands.sum(&:weight)
|
|
318
|
+
ticket = rng.rand(total)
|
|
319
|
+
cumulative = 0
|
|
320
|
+
commands.each do |cmd|
|
|
321
|
+
cumulative += cmd.weight
|
|
322
|
+
return cmd if ticket < cumulative
|
|
326
323
|
end
|
|
324
|
+
commands.last
|
|
327
325
|
end
|
|
328
326
|
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
return false unless applicable?(command, state, args, context: "validate sequence")
|
|
337
|
-
|
|
338
|
-
state = command.next_state(state, args)
|
|
327
|
+
def check_invariants!(state, sut, step_index:, step:)
|
|
328
|
+
@spec.invariants.each_with_index do |inv, idx|
|
|
329
|
+
target = inv.name || :"invariant_#{idx}"
|
|
330
|
+
label = inv.name ? "invariant #{inv.name.inspect}" : "invariant"
|
|
331
|
+
execute_stage(stage: :invariant, target: target, target_label: label, step_index: step_index, step: step) do
|
|
332
|
+
Resolver.invoke_with_arity(inv.block, state, sut)
|
|
333
|
+
end
|
|
339
334
|
end
|
|
340
|
-
|
|
341
|
-
true
|
|
342
|
-
rescue
|
|
343
|
-
false
|
|
344
335
|
end
|
|
345
336
|
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
337
|
+
def execute_stage(stage:, target:, step_index:, step:, target_label: nil)
|
|
338
|
+
yield
|
|
339
|
+
rescue *Check::FATAL_EXCEPTIONS, InvalidSpec, ModelError, PreconditionNotMet
|
|
340
|
+
raise
|
|
341
|
+
rescue Exception => e # standard:disable Lint/RescueException
|
|
342
|
+
step_label = step_index ? "step #{step_index + 1} #{step.format}" : "initial"
|
|
343
|
+
label = target_label || stage.to_s
|
|
344
|
+
failure = StepFailure.new(
|
|
345
|
+
"#{step_label} [#{label}]: #{e.message}",
|
|
346
|
+
step_index: step_index,
|
|
347
|
+
step: step,
|
|
348
|
+
stage: stage,
|
|
349
|
+
target: target,
|
|
350
|
+
exception_class: e.class
|
|
351
|
+
)
|
|
352
|
+
failure.set_backtrace(e.backtrace) if e.backtrace
|
|
353
|
+
raise failure
|
|
355
354
|
end
|
|
356
|
-
|
|
357
|
-
NoApplicableArgs = Object.new
|
|
358
|
-
private_constant :NoApplicableArgs
|
|
359
355
|
end
|
|
360
356
|
end
|
|
361
357
|
end
|