pbt 0.9.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.
@@ -32,6 +32,11 @@ module Pbt
32
32
  raise_model_error("guard", e)
33
33
  end
34
34
 
35
+ # @return [Boolean]
36
+ def state_dependent_arguments?
37
+ @arguments_source.is_a?(Proc)
38
+ end
39
+
35
40
  # @param state [Object]
36
41
  # @return [Object] arbitrary responding to generate/shrink (or sample)
37
42
  def arguments_for(state)
@@ -64,6 +64,7 @@ module Pbt
64
64
  run = run_sample.run
65
65
  session = run_sample.session
66
66
  session.mark_seen(run)
67
+ original_states = before_states_by_step_id(run)
67
68
 
68
69
  Enumerator.new do |y|
69
70
  next if session.exhausted?
@@ -86,24 +87,25 @@ module Pbt
86
87
  end
87
88
 
88
89
  # Stage 2: Chunk deletions (ddmin-style, back to front).
89
- next if shrink_by_chunk_deletions(y, run_sample, session) == :exhausted
90
+ next if shrink_by_chunk_deletions(y, run_sample, session, original_states) == :exhausted
90
91
 
91
92
  # Stage 3: Single-step deletions (back to front).
92
- next if shrink_by_single_deletions(y, run_sample, session) == :exhausted
93
+ next if shrink_by_single_deletions(y, run_sample, session, original_states) == :exhausted
93
94
 
94
95
  # Stage 4: Initial state shrinking (when initial_state is an Arbitrary).
95
- next if shrink_initial_state(y, run_sample, session) == :exhausted
96
+ next if shrink_initial_state(y, run_sample, session, original_states) == :exhausted
96
97
 
97
98
  # Stage 5: Step argument shrinking (step 0..n-1).
98
- shrink_step_arguments(y, run_sample, session)
99
+ shrink_step_arguments(y, run_sample, session, original_states)
99
100
  end
100
101
  end
101
102
 
102
103
  # Validate a candidate run purely against the model without touching the SUT.
103
104
  #
104
105
  # @param run [Run]
106
+ # @param original_states [Hash<Integer, Object>, nil]
105
107
  # @return [Boolean]
106
- def valid_run?(run)
108
+ def valid_run?(run, original_states: nil)
107
109
  state = Resolver.deep_freeze(run.initial_state)
108
110
  available_ids = Set.new
109
111
 
@@ -121,7 +123,12 @@ module Pbt
121
123
  return false
122
124
  end
123
125
 
124
- return false if Pbt::Arbitrary::Arbitrary.cover_result(arb, step.args) == false
126
+ covered = Pbt::Arbitrary::Arbitrary.cover_result(arb, step.args)
127
+ return false if covered == false
128
+ if covered.nil? && command.state_dependent_arguments? &&
129
+ original_states&.key?(step.id) && original_states[step.id] != state
130
+ return false
131
+ end
125
132
  return false unless command.precondition?(state, step.args)
126
133
 
127
134
  state = command.next_state(state, step.args, Ref.new(step_id: step.id))
@@ -133,6 +140,17 @@ module Pbt
133
140
 
134
141
  private
135
142
 
143
+ def before_states_by_step_id(run)
144
+ states = {}
145
+ state = Resolver.deep_freeze(run.initial_state)
146
+ run.steps.each do |step|
147
+ states[step.id] = state
148
+ command = @spec.command_by_name(step.command)
149
+ state = command.next_state(state, step.args, Ref.new(step_id: step.id))
150
+ end
151
+ states
152
+ end
153
+
136
154
  def post_failure_truncation(steps, failure)
137
155
  if failure.step_index.is_a?(Integer) && failure.step_index < steps.length - 1
138
156
  steps[0..failure.step_index]
@@ -141,7 +159,7 @@ module Pbt
141
159
  end
142
160
  end
143
161
 
144
- def shrink_by_chunk_deletions(y, run_sample, session)
162
+ def shrink_by_chunk_deletions(y, run_sample, session, original_states)
145
163
  run = run_sample.run
146
164
  steps = run.steps
147
165
  n = steps.length
@@ -156,7 +174,8 @@ module Pbt
156
174
  candidate,
157
175
  session: session,
158
176
  initial_state_sample: run_sample.initial_state_sample,
159
- step_samples: run_sample.step_samples
177
+ step_samples: run_sample.step_samples,
178
+ original_states: original_states
160
179
  )
161
180
  return :exhausted if status == :exhausted
162
181
  end
@@ -177,7 +196,7 @@ module Pbt
177
196
  starts
178
197
  end
179
198
 
180
- def shrink_by_single_deletions(y, run_sample, session)
199
+ def shrink_by_single_deletions(y, run_sample, session, original_states)
181
200
  run = run_sample.run
182
201
  steps = run.steps
183
202
  (steps.length - 1).downto(0) do |idx|
@@ -188,7 +207,8 @@ module Pbt
188
207
  candidate,
189
208
  session: session,
190
209
  initial_state_sample: run_sample.initial_state_sample,
191
- step_samples: run_sample.step_samples
210
+ step_samples: run_sample.step_samples,
211
+ original_states: original_states
192
212
  )
193
213
  return :exhausted if status == :exhausted
194
214
  end
@@ -196,7 +216,7 @@ module Pbt
196
216
  :ok
197
217
  end
198
218
 
199
- def shrink_initial_state(y, run_sample, session)
219
+ def shrink_initial_state(y, run_sample, session, original_states)
200
220
  initial_sample = run_sample.initial_state_sample
201
221
  return :ok unless initial_sample
202
222
 
@@ -209,7 +229,8 @@ module Pbt
209
229
  candidate,
210
230
  session: session,
211
231
  initial_state_sample: child_sample,
212
- step_samples: run_sample.step_samples
232
+ step_samples: run_sample.step_samples,
233
+ original_states: original_states
213
234
  )
214
235
  return :exhausted if status == :exhausted
215
236
  end
@@ -217,7 +238,7 @@ module Pbt
217
238
  :ok
218
239
  end
219
240
 
220
- def shrink_step_arguments(y, run_sample, session)
241
+ def shrink_step_arguments(y, run_sample, session, original_states)
221
242
  run = run_sample.run
222
243
  steps = run.steps
223
244
  step_samples = run_sample.step_samples
@@ -239,7 +260,8 @@ module Pbt
239
260
  candidate,
240
261
  session: session,
241
262
  initial_state_sample: run_sample.initial_state_sample,
242
- step_samples: step_samples.merge(step.id => child_sample)
263
+ step_samples: step_samples.merge(step.id => child_sample),
264
+ original_states: original_states
243
265
  )
244
266
  return :exhausted if status == :exhausted
245
267
  end
@@ -261,7 +283,8 @@ module Pbt
261
283
  candidate,
262
284
  session: session,
263
285
  initial_state_sample: run_sample.initial_state_sample,
264
- step_samples: step_samples
286
+ step_samples: step_samples,
287
+ original_states: original_states
265
288
  )
266
289
  return :exhausted if status == :exhausted
267
290
  end
@@ -273,14 +296,14 @@ module Pbt
273
296
  :ok
274
297
  end
275
298
 
276
- def emit_candidate(y, candidate, session:, initial_state_sample:, step_samples:, validate: true)
299
+ def emit_candidate(y, candidate, session:, initial_state_sample:, step_samples:, validate: true, original_states: nil)
277
300
  return :exhausted if session.exhausted?
278
301
  return :duplicate if session.seen?(candidate)
279
302
 
280
303
  session.mark_seen(candidate)
281
304
  session.consume_attempt!
282
305
 
283
- return :invalid if validate && !valid_run?(candidate)
306
+ return :invalid if validate && !valid_run?(candidate, original_states: original_states)
284
307
 
285
308
  y << RunSample.new(
286
309
  candidate,
data/lib/pbt/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Pbt
4
- VERSION = "0.9.0"
4
+ VERSION = "1.0.0"
5
5
  end
data/lib/pbt.rb CHANGED
@@ -65,6 +65,8 @@ module Pbt
65
65
  # @param predicate [Proc] Test code that receives generated values and runs the test.
66
66
  # @return [Check::Property]
67
67
  def self.property(*args, **kwargs, &predicate)
68
+ raise ArgumentError, "Pbt.property requires a predicate block" unless predicate
69
+
68
70
  arb = to_arbitrary(args, kwargs)
69
71
  Check::Property.new(
70
72
  arb,
@@ -79,7 +81,8 @@ module Pbt
79
81
  # `Pbt.statistics`, and `property.replay(run)`.
80
82
  #
81
83
  # @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).
84
+ # @param min_steps [Integer] Target minimum number of commands per generated run (default: 1).
85
+ # A run may stop earlier if it reaches a terminal state where no command guard is truthy.
83
86
  # @param max_shrink_attempts [Integer] Total shrink attempt budget per failure (default: 1000).
84
87
  # @yieldparam m [Pbt::Stateful::SpecBuilder] Model builder for `initial_state`, `sut`/`teardown`/`around`, `command`, and `invariant`.
85
88
  # @return [Pbt::Stateful::Property]
@@ -118,10 +121,14 @@ module Pbt
118
121
  # @raise [ArgumentError] When both positional and keyword arguments are given
119
122
  def to_arbitrary(args, kwargs)
120
123
  if args == [] && kwargs != {}
124
+ validate_arbitraries!(kwargs.values)
121
125
  fixed_hash(kwargs)
122
126
  elsif args != [] && kwargs == {}
127
+ validate_arbitraries!(args)
123
128
  # wrap by tuple arbitrary so that property class doesn't have to take care of an array
124
129
  (args.size == 1) ? args.first : tuple(*args)
130
+ elsif args == [] && kwargs == {}
131
+ raise ArgumentError, "Pbt.property requires at least one arbitrary"
125
132
  else
126
133
  raise ArgumentError, <<~MSG
127
134
  It's not supported to use both positional and keyword arguments at the same time.
@@ -129,5 +136,13 @@ module Pbt
129
136
  MSG
130
137
  end
131
138
  end
139
+
140
+ def validate_arbitraries!(arbs)
141
+ arbs.each do |arb|
142
+ unless arb.respond_to?(:generate) && arb.respond_to?(:shrink)
143
+ raise ArgumentError, "Pbt.property expects arbitraries responding to generate and shrink, got #{arb.class}"
144
+ end
145
+ end
146
+ end
132
147
  end
133
148
  end
@@ -45,6 +45,10 @@ module Pbt
45
45
  def map: [U] () { (T) -> U } -> MapArbitrary[U]
46
46
  | [U] (^(T) -> U mapper, ?(^(untyped) -> untyped | _Callable)? unmapper) -> MapArbitrary[U]
47
47
  | (_Callable mapper, ?(^(untyped) -> untyped | _Callable)? unmapper) -> MapArbitrary[untyped]
48
+ def bind: [U] () { (T) -> _Arbitrary[U] } -> BindArbitrary[U]
49
+ | [U] (^(T) -> _Arbitrary[U] binder) -> BindArbitrary[U]
50
+ | (_Callable binder) -> BindArbitrary[untyped]
51
+ alias flat_map bind
48
52
  def filter: (?max_attempts: Integer) { (T) -> boolish } -> FilterArbitrary[T]
49
53
  end
50
54
 
@@ -53,8 +57,8 @@ module Pbt
53
57
  def shrink: (Integer current, ?target: Integer?) -> Enumerator[Integer, untyped]
54
58
  end
55
59
 
56
- class ChooseArbitrary < Arbitrary[Integer | Float]
57
- def initialize: (Range[Integer] | Range[Float] range) -> void
60
+ class ChooseArbitrary[out T = Integer | Float] < Arbitrary[T]
61
+ def initialize: (Range[T] range) -> void
58
62
  end
59
63
 
60
64
  class ConstantArbitrary[out T = untyped] < Arbitrary[T]
@@ -89,8 +93,13 @@ module Pbt
89
93
  def initialize: (_Arbitrary[untyped] arb, ^(untyped) -> untyped | _Callable mapper, ?(^(untyped) -> untyped | _Callable)? unmapper, ?bijective: bool) -> void
90
94
  end
91
95
 
92
- class FloatArbitrary < MapArbitrary[Float]
93
- def initialize: () -> void
96
+ class BindArbitrary[out T = untyped] < Arbitrary[T]
97
+ def initialize: (_Arbitrary[untyped] arb, ^(untyped) -> untyped | _Callable binder) -> void
98
+ end
99
+
100
+ class FloatArbitrary < Arbitrary[Float]
101
+ def initialize: (?min: (Integer | Float)?, ?max: (Integer | Float)?, ?allow_nan: bool, ?allow_infinity: bool) -> void
102
+ def shrink: (untyped current, ?target: (Integer | Float)?) -> Enumerator[Float, untyped]
94
103
  end
95
104
 
96
105
  class FilterArbitrary[out T = untyped] < Arbitrary[T]
@@ -105,7 +114,8 @@ module Pbt
105
114
  def array: [T] (_Arbitrary[T] arb, ?min: Integer, ?max: Integer, ?empty: bool) -> ArrayArbitrary[T]
106
115
  def tuple: (*_Arbitrary[untyped] arbs) -> TupleArbitrary
107
116
  def fixed_hash: (Hash[untyped, _Arbitrary[untyped]] hash) -> FixedHashArbitrary
108
- def choose: (Range[Integer] | Range[Float] range) -> ChooseArbitrary
117
+ def choose: (Range[Integer] range) -> ChooseArbitrary[Integer]
118
+ | (Range[Float] range) -> ChooseArbitrary[Float]
109
119
  def constant_from: [T] (*T choices) -> ConstantFromArbitrary[T]
110
120
  def one_of: [T] (*_Arbitrary[T] arbitraries) -> OneOfArbitrary[T]
111
121
  def frequency: [T] (*[Integer, _Arbitrary[T]] choices) -> FrequencyArbitrary[T]
@@ -117,11 +127,12 @@ module Pbt
117
127
  def ascii_char: () -> ConstantFromArbitrary[String]
118
128
  def ascii_string: (?min: Integer, ?max: Integer, ?empty: bool) -> MapArbitrary[String]
119
129
  def printable_ascii_char: () -> ConstantFromArbitrary[String]
130
+ def string: (?min: Integer, ?max: Integer, ?empty: bool) -> MapArbitrary[String]
120
131
  def printable_ascii_string: (?min: Integer, ?max: Integer, ?empty: bool) -> MapArbitrary[String]
121
- def printable_char: () -> ConstantFromArbitrary[String]
132
+ def printable_char: () -> MapArbitrary[String]
122
133
  def printable_string: (?min: Integer, ?max: Integer, ?empty: bool) -> MapArbitrary[String]
123
134
  def symbol: (?min: Integer, ?max: Integer, ?empty: bool) -> MapArbitrary[Symbol]
124
- def float: () -> FloatArbitrary
135
+ def float: (?min: (Integer | Float)?, ?max: (Integer | Float)?, ?allow_nan: bool, ?allow_infinity: bool) -> FloatArbitrary
125
136
  def set: [T] (_Arbitrary[T] arb, ?min: Integer, ?max: Integer, ?empty: bool) -> MapArbitrary[Set[T]]
126
137
  def hash: () -> Integer
127
138
  | [K, V] (_Arbitrary[K] key_arbitrary, _Arbitrary[V] value_arbitrary, ?min: Integer, ?max: Integer, ?empty: bool) -> MapArbitrary[Hash[K, V]]
data/sig/pbt/stateful.rbs CHANGED
@@ -103,6 +103,7 @@ module Pbt
103
103
  ) -> Context
104
104
  end
105
105
 
106
+ # Internal helper module (not part of the stable SemVer public API).
106
107
  module Resolver
107
108
  def self.arbitrary?: (untyped obj) -> bool
108
109
  def self.deep_dup: (untyped obj, ?Hash[untyped, untyped] cache) -> untyped
@@ -130,6 +131,7 @@ module Pbt
130
131
  ) -> void
131
132
 
132
133
  def guard?: (untyped state) -> bool
134
+ def state_dependent_arguments?: () -> bool
133
135
  def arguments_for: (untyped state) -> Arbitrary::_Arbitrary[untyped]
134
136
  def precondition?: (untyped state, untyped args) -> bool
135
137
  def next_state: (untyped state, untyped args, Ref ref) -> untyped
@@ -188,6 +190,7 @@ module Pbt
188
190
  def build: () -> Spec
189
191
  end
190
192
 
193
+ # Internal shrinking classes (not part of the stable SemVer public API).
191
194
  class ShrinkSession
192
195
  attr_reader attempts: Integer
193
196
  attr_reader max_attempts: Integer
@@ -217,7 +220,7 @@ module Pbt
217
220
  class Shrinker
218
221
  def initialize: (Spec spec) -> void
219
222
  def shrink: (RunSample run_sample, failure: Exception?) -> Enumerator[RunSample, untyped]
220
- def valid_run?: (Run run) -> bool
223
+ def valid_run?: (Run run, ?original_states: Hash[Integer, untyped]?) -> bool
221
224
  end
222
225
 
223
226
  class Property
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pbt
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ohbarye
@@ -23,6 +23,7 @@ files:
23
23
  - lib/pbt/arbitrary/arbitrary.rb
24
24
  - lib/pbt/arbitrary/arbitrary_methods.rb
25
25
  - lib/pbt/arbitrary/array_arbitrary.rb
26
+ - lib/pbt/arbitrary/bind_arbitrary.rb
26
27
  - lib/pbt/arbitrary/choose_arbitrary.rb
27
28
  - lib/pbt/arbitrary/constant.rb
28
29
  - lib/pbt/arbitrary/constant_arbitrary.rb
@@ -63,9 +64,11 @@ homepage: https://github.com/ohbarye/pbt
63
64
  licenses:
64
65
  - MIT
65
66
  metadata:
66
- homepage_uri: https://github.com/ohbarye/pbt
67
67
  source_code_uri: https://github.com/ohbarye/pbt
68
- changelog_uri: https://github.com/ohbarye/pbt/releases
68
+ changelog_uri: https://github.com/ohbarye/pbt/blob/main/CHANGELOG.md
69
+ bug_tracker_uri: https://github.com/ohbarye/pbt/issues
70
+ documentation_uri: https://www.rubydoc.info/gems/pbt
71
+ rubygems_mfa_required: 'true'
69
72
  rdoc_options: []
70
73
  require_paths:
71
74
  - lib
@@ -82,6 +85,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
82
85
  requirements: []
83
86
  rubygems_version: 4.0.20
84
87
  specification_version: 4
85
- summary: Property-Based Testing tool for Ruby, utilizing Ractor for parallelizing
86
- test cases.
88
+ summary: Property-Based Testing for Ruby with shrinking, stateful testing, and optional
89
+ Ractor parallelism.
87
90
  test_files: []