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
|
@@ -10,6 +10,9 @@ module Pbt
|
|
|
10
10
|
# @private
|
|
11
11
|
# @!attribute [r] run_execution [Reporter::RunExecution]
|
|
12
12
|
class RunnerIterator < DelegateClass(Array)
|
|
13
|
+
DEFAULT_MAX_SHRINK_STEPS = 10_000
|
|
14
|
+
private_constant :DEFAULT_MAX_SHRINK_STEPS
|
|
15
|
+
|
|
13
16
|
attr_reader :run_execution
|
|
14
17
|
|
|
15
18
|
# @param source_values [Enumerator] Enumerator of generated values by arbitrary.
|
|
@@ -17,12 +20,16 @@ module Pbt
|
|
|
17
20
|
# @param verbose [Boolean] Controls the verbosity of the output.
|
|
18
21
|
# @param target_runs [Integer, nil] Number of non-skipped runs to complete before stopping.
|
|
19
22
|
# @param max_skips [Integer, nil] Maximum number of skipped initial runs allowed via `Pbt.assume`.
|
|
20
|
-
|
|
23
|
+
# @param max_shrink_steps [Integer] Safety cap on accepted shrink transitions for non-stateful shrinking.
|
|
24
|
+
def initialize(source_values, property, verbose, target_runs: nil, max_skips: nil, max_shrink_steps: DEFAULT_MAX_SHRINK_STEPS)
|
|
21
25
|
@run_execution = Reporter::RunExecution.new(verbose)
|
|
22
26
|
@property = property
|
|
23
27
|
@source_stream = source_values
|
|
24
28
|
@target_runs = target_runs
|
|
25
29
|
@max_skips = max_skips
|
|
30
|
+
@max_shrink_steps = max_shrink_steps
|
|
31
|
+
@shrink_steps = 0
|
|
32
|
+
@seen_bare_failures = {}
|
|
26
33
|
@initial_failure = nil
|
|
27
34
|
@initial_offset = 0
|
|
28
35
|
@next_values = @target_runs ? next_initial_batch(@target_runs) : source_values
|
|
@@ -100,15 +107,21 @@ module Pbt
|
|
|
100
107
|
end
|
|
101
108
|
|
|
102
109
|
# failed run
|
|
110
|
+
shrinking = !@initial_failure.nil?
|
|
103
111
|
@initial_failure ||= c.exception
|
|
104
112
|
@run_execution.record_failure(c)
|
|
113
|
+
@shrink_steps += 1 if shrinking
|
|
114
|
+
|
|
105
115
|
@next_values =
|
|
106
|
-
if @
|
|
116
|
+
if @shrink_steps >= @max_shrink_steps
|
|
117
|
+
[].each
|
|
118
|
+
elsif @property.respond_to?(:shrink_case)
|
|
107
119
|
@property.shrink_case(c)
|
|
108
120
|
elsif c.sample
|
|
109
121
|
c.sample.shrink
|
|
110
122
|
else
|
|
111
|
-
|
|
123
|
+
mark_bare_seen(c.val)
|
|
124
|
+
filter_seen_bare_candidates(@property.shrink(c.val))
|
|
112
125
|
end
|
|
113
126
|
true
|
|
114
127
|
else
|
|
@@ -120,6 +133,28 @@ module Pbt
|
|
|
120
133
|
|
|
121
134
|
private
|
|
122
135
|
|
|
136
|
+
def mark_bare_seen(value)
|
|
137
|
+
@seen_bare_failures[value] = true
|
|
138
|
+
rescue
|
|
139
|
+
nil
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
def bare_seen?(value)
|
|
143
|
+
@seen_bare_failures.key?(value)
|
|
144
|
+
rescue
|
|
145
|
+
false
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
def filter_seen_bare_candidates(enum)
|
|
149
|
+
Enumerator.new do |y|
|
|
150
|
+
enum.each do |candidate|
|
|
151
|
+
next if bare_seen?(candidate)
|
|
152
|
+
|
|
153
|
+
y << candidate
|
|
154
|
+
end
|
|
155
|
+
end
|
|
156
|
+
end
|
|
157
|
+
|
|
123
158
|
def next_initial_batch(count)
|
|
124
159
|
@initial_offset = @run_execution.num_successes + @run_execution.num_skips
|
|
125
160
|
Enumerator.new(count) do |y|
|
|
@@ -14,6 +14,11 @@ module Pbt
|
|
|
14
14
|
module RunnerMethods
|
|
15
15
|
include Check::Tosser
|
|
16
16
|
|
|
17
|
+
private :toss
|
|
18
|
+
|
|
19
|
+
SUPPORTED_WORKERS = [:none, :ractor].freeze
|
|
20
|
+
private_constant :SUPPORTED_WORKERS
|
|
21
|
+
|
|
17
22
|
# Run a property based test and report its result.
|
|
18
23
|
#
|
|
19
24
|
# @see Check::Configuration
|
|
@@ -36,16 +41,19 @@ module Pbt
|
|
|
36
41
|
# @param property [Proc] Proc that returns Property instance.
|
|
37
42
|
# @return [RunDetails]
|
|
38
43
|
def check(**options, &property)
|
|
44
|
+
raise ArgumentError, "Pbt.check requires a block returning a property" unless property
|
|
45
|
+
|
|
46
|
+
validate_option_keys!(options)
|
|
39
47
|
property = property.call
|
|
40
48
|
config = Pbt.configuration.to_h.merge(options.to_h)
|
|
49
|
+
validate_config!(config)
|
|
41
50
|
|
|
42
|
-
if property.respond_to?(:stateful?) && property.stateful?
|
|
43
|
-
config[:worker]
|
|
44
|
-
|
|
51
|
+
if property.respond_to?(:stateful?) && property.stateful?
|
|
52
|
+
if options.key?(:worker) && config[:worker] == :ractor
|
|
53
|
+
raise Pbt::InvalidConfiguration, "Pbt.stateful does not support worker: :ractor. Use worker: :none instead."
|
|
54
|
+
end
|
|
45
55
|
|
|
46
|
-
|
|
47
|
-
if !max_skips.nil? && !(max_skips.is_a?(Integer) && max_skips >= 0)
|
|
48
|
-
raise Pbt::InvalidConfiguration, "max_skips must be a non-negative Integer or nil"
|
|
56
|
+
config[:worker] = :none unless options.key?(:worker)
|
|
49
57
|
end
|
|
50
58
|
|
|
51
59
|
initial_values = toss(property, config[:seed])
|
|
@@ -57,6 +65,35 @@ module Pbt
|
|
|
57
65
|
|
|
58
66
|
private
|
|
59
67
|
|
|
68
|
+
def validate_option_keys!(options)
|
|
69
|
+
unknown = options.keys - Configuration.members
|
|
70
|
+
return if unknown.empty?
|
|
71
|
+
|
|
72
|
+
raise Pbt::InvalidConfiguration, "unknown option(s): #{unknown.map(&:inspect).join(", ")}"
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def validate_config!(config)
|
|
76
|
+
num_runs = config[:num_runs]
|
|
77
|
+
unless num_runs.is_a?(Integer) && num_runs.positive?
|
|
78
|
+
raise Pbt::InvalidConfiguration, "num_runs must be a positive Integer, got #{num_runs.inspect}"
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
worker = config[:worker]
|
|
82
|
+
unless SUPPORTED_WORKERS.include?(worker)
|
|
83
|
+
raise Pbt::InvalidConfiguration, "worker must be one of #{SUPPORTED_WORKERS.inspect}, got #{worker.inspect}"
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
seed = config[:seed]
|
|
87
|
+
unless seed.is_a?(Integer)
|
|
88
|
+
raise Pbt::InvalidConfiguration, "seed must be an Integer, got #{seed.inspect}"
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
max_skips = config[:max_skips]
|
|
92
|
+
if !max_skips.nil? && !(max_skips.is_a?(Integer) && max_skips >= 0)
|
|
93
|
+
raise Pbt::InvalidConfiguration, "max_skips must be a non-negative Integer or nil"
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
|
|
60
97
|
# If using Ractor, some extra configurations are available and they need to be set up.
|
|
61
98
|
#
|
|
62
99
|
# - `:thread_report_on_exception`
|
|
@@ -129,25 +166,45 @@ module Pbt
|
|
|
129
166
|
# @param runner [RunnerIterator]
|
|
130
167
|
# @return [void]
|
|
131
168
|
def run_it_in_ractors(property, runner)
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
169
|
+
launched = []
|
|
170
|
+
generation_error = nil
|
|
171
|
+
|
|
172
|
+
begin
|
|
173
|
+
runner.each_with_index do |val, index|
|
|
174
|
+
c = build_case(val, runner.case_index(index))
|
|
175
|
+
c.ractor = property.run_in_ractor(c.val)
|
|
176
|
+
launched << c
|
|
177
|
+
end
|
|
178
|
+
rescue *FATAL_EXCEPTIONS
|
|
179
|
+
raise
|
|
180
|
+
rescue Exception => e # standard:disable Lint/RescueException
|
|
181
|
+
generation_error = e
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
stopped = false
|
|
185
|
+
launched.each do |c|
|
|
137
186
|
wait_ractor_result(c.ractor)
|
|
138
187
|
runner.handle_result(c)
|
|
139
188
|
rescue => e
|
|
140
189
|
cause = unwrap_ractor_exception(e)
|
|
141
190
|
if cause.is_a?(PreconditionNotMet)
|
|
142
|
-
|
|
191
|
+
if runner.handle_skip
|
|
192
|
+
stopped = true
|
|
193
|
+
break
|
|
194
|
+
end
|
|
143
195
|
|
|
144
196
|
next
|
|
145
197
|
end
|
|
146
198
|
raise cause if FATAL_EXCEPTIONS.any? { |klass| cause.is_a?(klass) }
|
|
147
199
|
|
|
148
200
|
c.exception = cause
|
|
149
|
-
|
|
201
|
+
if runner.handle_result(c) # Ignore the rest of the cases when failure is accepted.
|
|
202
|
+
stopped = true
|
|
203
|
+
break
|
|
204
|
+
end
|
|
150
205
|
end
|
|
206
|
+
|
|
207
|
+
raise generation_error if generation_error && !stopped
|
|
151
208
|
end
|
|
152
209
|
|
|
153
210
|
def build_case(value, index)
|
|
@@ -43,27 +43,60 @@ 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
|
+
" #{format_value(ce)}"
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def format_value(value)
|
|
79
|
+
value.is_a?(Pbt::Stateful::Run) ? value.to_s : value.inspect
|
|
80
|
+
end
|
|
81
|
+
|
|
55
82
|
def error_backtrace
|
|
56
|
-
|
|
83
|
+
locations =
|
|
84
|
+
if @run_details.error_instance.is_a?(Pbt::Stateful::StepFailure)
|
|
85
|
+
@run_details.error_instance.backtrace || @run_details.error_instance.backtrace_locations
|
|
86
|
+
else
|
|
87
|
+
@run_details.error_instance.backtrace_locations
|
|
88
|
+
end
|
|
89
|
+
return "" if locations.nil? # It can be nil.
|
|
57
90
|
|
|
58
91
|
i = @run_details.verbose ? -1 : 3
|
|
59
|
-
" #{
|
|
92
|
+
" #{locations[..i].join("\n ")}"
|
|
60
93
|
end
|
|
61
94
|
|
|
62
95
|
# @return [String]
|
|
63
96
|
def verbose_details
|
|
64
97
|
[
|
|
65
98
|
"\nEncountered failures were:",
|
|
66
|
-
@run_details.failures.map { |f| "- #{f.val}" },
|
|
99
|
+
@run_details.failures.map { |f| "- #{format_value(f.val)}" },
|
|
67
100
|
format_execution_summary
|
|
68
101
|
].join("\n")
|
|
69
102
|
end
|
|
@@ -90,7 +123,7 @@ module Pbt
|
|
|
90
123
|
"\x1b[31m\u00D7\x1b[0m" # red "×"
|
|
91
124
|
end
|
|
92
125
|
left_padding = ". " * current_depth
|
|
93
|
-
summary_lines << "#{left_padding}#{result_icon} #{current_tree.value}"
|
|
126
|
+
summary_lines << "#{left_padding}#{result_icon} #{format_value(current_tree.value)}"
|
|
94
127
|
|
|
95
128
|
# push its children to the queue
|
|
96
129
|
current_tree.children.reverse_each do |tree|
|
|
@@ -0,0 +1,220 @@
|
|
|
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
|
+
# @return [Boolean]
|
|
36
|
+
def state_dependent_arguments?
|
|
37
|
+
@arguments_source.is_a?(Proc)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# @param state [Object]
|
|
41
|
+
# @return [Object] arbitrary responding to generate/shrink (or sample)
|
|
42
|
+
def arguments_for(state)
|
|
43
|
+
arb =
|
|
44
|
+
if @arguments_source.nil?
|
|
45
|
+
Pbt.nil
|
|
46
|
+
elsif @arguments_source.is_a?(Proc)
|
|
47
|
+
Resolver.invoke_with_arity(@arguments_source, state)
|
|
48
|
+
else
|
|
49
|
+
@arguments_source
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
validate_arbitrary!(arb)
|
|
53
|
+
arb
|
|
54
|
+
rescue Pbt::Arbitrary::EmptyDomainError, InvalidSpec, *Check::FATAL_EXCEPTIONS
|
|
55
|
+
raise
|
|
56
|
+
rescue Exception => e # standard:disable Lint/RescueException
|
|
57
|
+
raise_model_error("arguments", e)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# @param state [Object]
|
|
61
|
+
# @param args [Object]
|
|
62
|
+
# @return [Boolean]
|
|
63
|
+
def precondition?(state, args)
|
|
64
|
+
return true unless @precondition_block
|
|
65
|
+
|
|
66
|
+
!!Resolver.invoke_with_arity(@precondition_block, state, args)
|
|
67
|
+
rescue *Check::FATAL_EXCEPTIONS
|
|
68
|
+
raise
|
|
69
|
+
rescue Exception => e # standard:disable Lint/RescueException
|
|
70
|
+
raise_model_error("precondition", e)
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# @param state [Object]
|
|
74
|
+
# @param args [Object]
|
|
75
|
+
# @param ref [Ref]
|
|
76
|
+
# @return [Object] deep-frozen next model state
|
|
77
|
+
def next_state(state, args, ref)
|
|
78
|
+
return state unless @next_state_block
|
|
79
|
+
|
|
80
|
+
next_s = Resolver.invoke_with_arity(@next_state_block, state, args, ref)
|
|
81
|
+
Resolver.deep_freeze(next_s)
|
|
82
|
+
rescue FrozenError => e
|
|
83
|
+
raise ModelError,
|
|
84
|
+
"command #{name.inspect} next_state attempted to mutate frozen model state: #{e.message}. " \
|
|
85
|
+
"Return a new state value instead (e.g. `state.with(...)`, `[*arr, x]`, `hash.merge(...)`)."
|
|
86
|
+
rescue *Check::FATAL_EXCEPTIONS
|
|
87
|
+
raise
|
|
88
|
+
rescue Exception => e # standard:disable Lint/RescueException
|
|
89
|
+
raise_model_error("next_state", e)
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
# @param sut [Object]
|
|
93
|
+
# @param concrete_args [Object]
|
|
94
|
+
# @return [Object]
|
|
95
|
+
def run!(sut, concrete_args)
|
|
96
|
+
Resolver.invoke_with_arity(@run_block, sut, concrete_args)
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
# @param context [Context]
|
|
100
|
+
# @return [void]
|
|
101
|
+
def verify!(context)
|
|
102
|
+
return unless @verify_block
|
|
103
|
+
|
|
104
|
+
Resolver.invoke_with_arity(@verify_block, context)
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
private
|
|
108
|
+
|
|
109
|
+
def validate_arbitrary!(arb)
|
|
110
|
+
return if Resolver.arbitrary?(arb)
|
|
111
|
+
|
|
112
|
+
raise InvalidSpec,
|
|
113
|
+
"command #{name.inspect} arguments must return an arbitrary responding to generate and shrink, got #{arb.class}"
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
def raise_model_error(callback_name, error)
|
|
117
|
+
err = ModelError.new("command #{name.inspect} #{callback_name} raised #{error.class}: #{error.message}")
|
|
118
|
+
err.set_backtrace(error.backtrace) if error.backtrace
|
|
119
|
+
raise err
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
# Yield-builder for defining a single stateful command.
|
|
124
|
+
class CommandBuilder
|
|
125
|
+
def initialize(name:, weight:)
|
|
126
|
+
raise InvalidSpec, "command name must be a Symbol, got #{name.inspect}" unless name.is_a?(Symbol)
|
|
127
|
+
unless weight.is_a?(Integer) && weight.positive?
|
|
128
|
+
raise InvalidSpec, "command #{name.inspect} weight must be a positive Integer, got #{weight.inspect}"
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
@name = name
|
|
132
|
+
@weight = weight
|
|
133
|
+
@guard_block = nil
|
|
134
|
+
@arguments_source = nil
|
|
135
|
+
@arguments_defined = false
|
|
136
|
+
@precondition_block = nil
|
|
137
|
+
@next_state_block = nil
|
|
138
|
+
@run_block = nil
|
|
139
|
+
@verify_block = nil
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
def guard(&block)
|
|
143
|
+
raise InvalidSpec, "command #{@name.inspect} guard requires a block" unless block
|
|
144
|
+
raise InvalidSpec, "command #{@name.inspect} guard already defined" if @guard_block
|
|
145
|
+
|
|
146
|
+
@guard_block = block
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
def arguments(*args, **kwargs, &block)
|
|
150
|
+
raise InvalidSpec, "command #{@name.inspect} arguments already defined" if @arguments_defined
|
|
151
|
+
|
|
152
|
+
@arguments_source =
|
|
153
|
+
if block
|
|
154
|
+
unless args.empty? && kwargs.empty?
|
|
155
|
+
raise InvalidSpec, "command #{@name.inspect} arguments accepts either a value, keyword arbitraries, or a block"
|
|
156
|
+
end
|
|
157
|
+
block
|
|
158
|
+
elsif !kwargs.empty?
|
|
159
|
+
unless args.empty?
|
|
160
|
+
raise InvalidSpec, "command #{@name.inspect} arguments cannot mix positional and keyword arguments"
|
|
161
|
+
end
|
|
162
|
+
Pbt.fixed_hash(kwargs)
|
|
163
|
+
elsif args.size == 1
|
|
164
|
+
arb = args.first
|
|
165
|
+
if !Resolver.arbitrary?(arb)
|
|
166
|
+
raise InvalidSpec, "command #{@name.inspect} arguments must be an arbitrary, got #{arb.class}"
|
|
167
|
+
end
|
|
168
|
+
arb
|
|
169
|
+
else
|
|
170
|
+
raise InvalidSpec, "command #{@name.inspect} arguments expects a single arbitrary, keyword arbitraries, or a block"
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
@arguments_defined = true
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
def precondition(&block)
|
|
177
|
+
raise InvalidSpec, "command #{@name.inspect} precondition requires a block" unless block
|
|
178
|
+
raise InvalidSpec, "command #{@name.inspect} precondition already defined" if @precondition_block
|
|
179
|
+
|
|
180
|
+
@precondition_block = block
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
def next_state(&block)
|
|
184
|
+
raise InvalidSpec, "command #{@name.inspect} next_state requires a block" unless block
|
|
185
|
+
raise InvalidSpec, "command #{@name.inspect} next_state already defined" if @next_state_block
|
|
186
|
+
|
|
187
|
+
@next_state_block = block
|
|
188
|
+
end
|
|
189
|
+
|
|
190
|
+
def run(&block)
|
|
191
|
+
raise InvalidSpec, "command #{@name.inspect} run requires a block" unless block
|
|
192
|
+
raise InvalidSpec, "command #{@name.inspect} run already defined" if @run_block
|
|
193
|
+
|
|
194
|
+
@run_block = block
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
def verify(&block)
|
|
198
|
+
raise InvalidSpec, "command #{@name.inspect} verify requires a block" unless block
|
|
199
|
+
raise InvalidSpec, "command #{@name.inspect} verify already defined" if @verify_block
|
|
200
|
+
|
|
201
|
+
@verify_block = block
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
def build
|
|
205
|
+
raise InvalidSpec, "command #{@name.inspect} must define run" unless @run_block
|
|
206
|
+
|
|
207
|
+
Command.new(
|
|
208
|
+
name: @name,
|
|
209
|
+
weight: @weight,
|
|
210
|
+
guard_block: @guard_block,
|
|
211
|
+
arguments_source: @arguments_source,
|
|
212
|
+
precondition_block: @precondition_block,
|
|
213
|
+
next_state_block: @next_state_block,
|
|
214
|
+
run_block: @run_block,
|
|
215
|
+
verify_block: @verify_block
|
|
216
|
+
)
|
|
217
|
+
end
|
|
218
|
+
end
|
|
219
|
+
end
|
|
220
|
+
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
|