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,215 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "pbt/stateful/errors"
|
|
4
|
+
require "pbt/stateful/types"
|
|
5
|
+
|
|
6
|
+
module Pbt
|
|
7
|
+
module Stateful
|
|
8
|
+
# Immutable command specification built by `CommandBuilder`.
|
|
9
|
+
class Command
|
|
10
|
+
attr_reader :name, :weight
|
|
11
|
+
|
|
12
|
+
def initialize(name:, weight:, guard_block:, arguments_source:, precondition_block:, next_state_block:, run_block:, verify_block:)
|
|
13
|
+
@name = name
|
|
14
|
+
@weight = weight
|
|
15
|
+
@guard_block = guard_block
|
|
16
|
+
@arguments_source = arguments_source
|
|
17
|
+
@precondition_block = precondition_block
|
|
18
|
+
@next_state_block = next_state_block
|
|
19
|
+
@run_block = run_block
|
|
20
|
+
@verify_block = verify_block
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# @param state [Object]
|
|
24
|
+
# @return [Boolean]
|
|
25
|
+
def guard?(state)
|
|
26
|
+
return true unless @guard_block
|
|
27
|
+
|
|
28
|
+
!!Resolver.invoke_with_arity(@guard_block, state)
|
|
29
|
+
rescue *Check::FATAL_EXCEPTIONS
|
|
30
|
+
raise
|
|
31
|
+
rescue Exception => e # standard:disable Lint/RescueException
|
|
32
|
+
raise_model_error("guard", e)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# @param state [Object]
|
|
36
|
+
# @return [Object] arbitrary responding to generate/shrink (or sample)
|
|
37
|
+
def arguments_for(state)
|
|
38
|
+
arb =
|
|
39
|
+
if @arguments_source.nil?
|
|
40
|
+
Pbt.nil
|
|
41
|
+
elsif @arguments_source.is_a?(Proc)
|
|
42
|
+
Resolver.invoke_with_arity(@arguments_source, state)
|
|
43
|
+
else
|
|
44
|
+
@arguments_source
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
validate_arbitrary!(arb)
|
|
48
|
+
arb
|
|
49
|
+
rescue Pbt::Arbitrary::EmptyDomainError, InvalidSpec, *Check::FATAL_EXCEPTIONS
|
|
50
|
+
raise
|
|
51
|
+
rescue Exception => e # standard:disable Lint/RescueException
|
|
52
|
+
raise_model_error("arguments", e)
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# @param state [Object]
|
|
56
|
+
# @param args [Object]
|
|
57
|
+
# @return [Boolean]
|
|
58
|
+
def precondition?(state, args)
|
|
59
|
+
return true unless @precondition_block
|
|
60
|
+
|
|
61
|
+
!!Resolver.invoke_with_arity(@precondition_block, state, args)
|
|
62
|
+
rescue *Check::FATAL_EXCEPTIONS
|
|
63
|
+
raise
|
|
64
|
+
rescue Exception => e # standard:disable Lint/RescueException
|
|
65
|
+
raise_model_error("precondition", e)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# @param state [Object]
|
|
69
|
+
# @param args [Object]
|
|
70
|
+
# @param ref [Ref]
|
|
71
|
+
# @return [Object] deep-frozen next model state
|
|
72
|
+
def next_state(state, args, ref)
|
|
73
|
+
return state unless @next_state_block
|
|
74
|
+
|
|
75
|
+
next_s = Resolver.invoke_with_arity(@next_state_block, state, args, ref)
|
|
76
|
+
Resolver.deep_freeze(next_s)
|
|
77
|
+
rescue FrozenError => e
|
|
78
|
+
raise ModelError,
|
|
79
|
+
"command #{name.inspect} next_state attempted to mutate frozen model state: #{e.message}. " \
|
|
80
|
+
"Return a new state value instead (e.g. `state.with(...)`, `[*arr, x]`, `hash.merge(...)`)."
|
|
81
|
+
rescue *Check::FATAL_EXCEPTIONS
|
|
82
|
+
raise
|
|
83
|
+
rescue Exception => e # standard:disable Lint/RescueException
|
|
84
|
+
raise_model_error("next_state", e)
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
# @param sut [Object]
|
|
88
|
+
# @param concrete_args [Object]
|
|
89
|
+
# @return [Object]
|
|
90
|
+
def run!(sut, concrete_args)
|
|
91
|
+
Resolver.invoke_with_arity(@run_block, sut, concrete_args)
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# @param context [Context]
|
|
95
|
+
# @return [void]
|
|
96
|
+
def verify!(context)
|
|
97
|
+
return unless @verify_block
|
|
98
|
+
|
|
99
|
+
Resolver.invoke_with_arity(@verify_block, context)
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
private
|
|
103
|
+
|
|
104
|
+
def validate_arbitrary!(arb)
|
|
105
|
+
return if Resolver.arbitrary?(arb)
|
|
106
|
+
|
|
107
|
+
raise InvalidSpec,
|
|
108
|
+
"command #{name.inspect} arguments must return an arbitrary responding to generate and shrink, got #{arb.class}"
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def raise_model_error(callback_name, error)
|
|
112
|
+
err = ModelError.new("command #{name.inspect} #{callback_name} raised #{error.class}: #{error.message}")
|
|
113
|
+
err.set_backtrace(error.backtrace) if error.backtrace
|
|
114
|
+
raise err
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
# Yield-builder for defining a single stateful command.
|
|
119
|
+
class CommandBuilder
|
|
120
|
+
def initialize(name:, weight:)
|
|
121
|
+
raise InvalidSpec, "command name must be a Symbol, got #{name.inspect}" unless name.is_a?(Symbol)
|
|
122
|
+
unless weight.is_a?(Integer) && weight.positive?
|
|
123
|
+
raise InvalidSpec, "command #{name.inspect} weight must be a positive Integer, got #{weight.inspect}"
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
@name = name
|
|
127
|
+
@weight = weight
|
|
128
|
+
@guard_block = nil
|
|
129
|
+
@arguments_source = nil
|
|
130
|
+
@arguments_defined = false
|
|
131
|
+
@precondition_block = nil
|
|
132
|
+
@next_state_block = nil
|
|
133
|
+
@run_block = nil
|
|
134
|
+
@verify_block = nil
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
def guard(&block)
|
|
138
|
+
raise InvalidSpec, "command #{@name.inspect} guard requires a block" unless block
|
|
139
|
+
raise InvalidSpec, "command #{@name.inspect} guard already defined" if @guard_block
|
|
140
|
+
|
|
141
|
+
@guard_block = block
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
def arguments(*args, **kwargs, &block)
|
|
145
|
+
raise InvalidSpec, "command #{@name.inspect} arguments already defined" if @arguments_defined
|
|
146
|
+
|
|
147
|
+
@arguments_source =
|
|
148
|
+
if block
|
|
149
|
+
unless args.empty? && kwargs.empty?
|
|
150
|
+
raise InvalidSpec, "command #{@name.inspect} arguments accepts either a value, keyword arbitraries, or a block"
|
|
151
|
+
end
|
|
152
|
+
block
|
|
153
|
+
elsif !kwargs.empty?
|
|
154
|
+
unless args.empty?
|
|
155
|
+
raise InvalidSpec, "command #{@name.inspect} arguments cannot mix positional and keyword arguments"
|
|
156
|
+
end
|
|
157
|
+
Pbt.fixed_hash(kwargs)
|
|
158
|
+
elsif args.size == 1
|
|
159
|
+
arb = args.first
|
|
160
|
+
if !Resolver.arbitrary?(arb)
|
|
161
|
+
raise InvalidSpec, "command #{@name.inspect} arguments must be an arbitrary, got #{arb.class}"
|
|
162
|
+
end
|
|
163
|
+
arb
|
|
164
|
+
else
|
|
165
|
+
raise InvalidSpec, "command #{@name.inspect} arguments expects a single arbitrary, keyword arbitraries, or a block"
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
@arguments_defined = true
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
def precondition(&block)
|
|
172
|
+
raise InvalidSpec, "command #{@name.inspect} precondition requires a block" unless block
|
|
173
|
+
raise InvalidSpec, "command #{@name.inspect} precondition already defined" if @precondition_block
|
|
174
|
+
|
|
175
|
+
@precondition_block = block
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
def next_state(&block)
|
|
179
|
+
raise InvalidSpec, "command #{@name.inspect} next_state requires a block" unless block
|
|
180
|
+
raise InvalidSpec, "command #{@name.inspect} next_state already defined" if @next_state_block
|
|
181
|
+
|
|
182
|
+
@next_state_block = block
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
def run(&block)
|
|
186
|
+
raise InvalidSpec, "command #{@name.inspect} run requires a block" unless block
|
|
187
|
+
raise InvalidSpec, "command #{@name.inspect} run already defined" if @run_block
|
|
188
|
+
|
|
189
|
+
@run_block = block
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
def verify(&block)
|
|
193
|
+
raise InvalidSpec, "command #{@name.inspect} verify requires a block" unless block
|
|
194
|
+
raise InvalidSpec, "command #{@name.inspect} verify already defined" if @verify_block
|
|
195
|
+
|
|
196
|
+
@verify_block = block
|
|
197
|
+
end
|
|
198
|
+
|
|
199
|
+
def build
|
|
200
|
+
raise InvalidSpec, "command #{@name.inspect} must define run" unless @run_block
|
|
201
|
+
|
|
202
|
+
Command.new(
|
|
203
|
+
name: @name,
|
|
204
|
+
weight: @weight,
|
|
205
|
+
guard_block: @guard_block,
|
|
206
|
+
arguments_source: @arguments_source,
|
|
207
|
+
precondition_block: @precondition_block,
|
|
208
|
+
next_state_block: @next_state_block,
|
|
209
|
+
run_block: @run_block,
|
|
210
|
+
verify_block: @verify_block
|
|
211
|
+
)
|
|
212
|
+
end
|
|
213
|
+
end
|
|
214
|
+
end
|
|
215
|
+
end
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Pbt
|
|
4
|
+
class InvalidConfiguration < StandardError; end
|
|
5
|
+
|
|
6
|
+
module Stateful
|
|
7
|
+
# Raised when a stateful specification is invalid (missing sut/commands, invalid DSL options, etc.).
|
|
8
|
+
class InvalidSpec < Pbt::InvalidConfiguration; end
|
|
9
|
+
|
|
10
|
+
# Raised when the generator cannot produce a valid command sequence or step arguments within budget.
|
|
11
|
+
class GenerationError < StandardError; end
|
|
12
|
+
|
|
13
|
+
# Raised when a user model callback violates the pure-model contract (e.g. mutating frozen state).
|
|
14
|
+
class ModelError < StandardError; end
|
|
15
|
+
|
|
16
|
+
# Normalized failure identity used during shrinking to prevent slipping into an unrelated bug.
|
|
17
|
+
FailureSignature = Data.define(:stage, :target, :exception_class)
|
|
18
|
+
|
|
19
|
+
# Wraps a failure raised during SUT lifecycle, command execution, verification, or invariant checking.
|
|
20
|
+
class StepFailure < StandardError
|
|
21
|
+
attr_reader :step_index, :step, :stage, :target, :signature, :teardown_error
|
|
22
|
+
|
|
23
|
+
# @param message [String]
|
|
24
|
+
# @param step_index [Integer, nil] 0-based index of the failing step, or nil for sut/initial invariant/teardown
|
|
25
|
+
# @param step [Step, nil]
|
|
26
|
+
# @param stage [Symbol] :sut, :run, :verify, :invariant, or :teardown
|
|
27
|
+
# @param target [Symbol, String, nil] command name or invariant name
|
|
28
|
+
# @param exception_class [Class] original exception class
|
|
29
|
+
# @param teardown_error [Exception, nil] secondary error raised during teardown cleanup
|
|
30
|
+
def initialize(message, step_index:, step:, stage:, target:, exception_class:, teardown_error: nil)
|
|
31
|
+
frozen_target = target.is_a?(String) ? target.dup.freeze : target
|
|
32
|
+
@step_index = step_index
|
|
33
|
+
@step = step
|
|
34
|
+
@stage = stage
|
|
35
|
+
@target = frozen_target
|
|
36
|
+
@signature = FailureSignature.new(stage: stage, target: frozen_target, exception_class: exception_class)
|
|
37
|
+
@teardown_error = teardown_error
|
|
38
|
+
super(message)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# Attach a teardown error raised while this failure was already propagating.
|
|
42
|
+
#
|
|
43
|
+
# @param error [Exception]
|
|
44
|
+
# @return [self]
|
|
45
|
+
def attach_teardown_error(error)
|
|
46
|
+
@teardown_error = error
|
|
47
|
+
self
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def message
|
|
51
|
+
base = super
|
|
52
|
+
return base unless @teardown_error
|
|
53
|
+
|
|
54
|
+
"#{base}\n (teardown error: #{@teardown_error.class}: #{@teardown_error.message})"
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|