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.
@@ -2,29 +2,251 @@
2
2
 
3
3
  module Pbt
4
4
  module Arbitrary
5
- # Generates finite Float values via `x.to_f / (y.to_f.abs + 1.0)` over default integer bounds
6
- # and shrinks them toward zero.
7
- class FloatArbitrary < MapArbitrary
8
- MAX_ABS = 1_000_000.0
9
- MIN_NONZERO_ABS = 1.0 / 1_000_001.0
10
- private_constant :MAX_ABS, :MIN_NONZERO_ABS
5
+ # Generates Float values within optional finite `Integer` / `Float` bounds, with opt-in
6
+ # NaN / Infinity support and float-aware binary-search shrinking toward zero (or the nearest bound).
7
+ #
8
+ # Omitted bounds default finite generation to `-1_000_000.0..1_000_000.0` (or a scaled span for
9
+ # large one-sided bounds), while `cover?` only enforces explicitly specified bounds.
10
+ class FloatArbitrary < Arbitrary
11
+ DEFAULT_MIN = -1_000_000.0
12
+ DEFAULT_MAX = 1_000_000.0
13
+ DEFAULT_SPAN = 1_000_000.0
14
+ EDGE_CASE_BIAS_DENOMINATOR = 5
15
+ MAX_SHRINK_STEPS = 16
16
+ private_constant :DEFAULT_MIN, :DEFAULT_MAX, :DEFAULT_SPAN, :EDGE_CASE_BIAS_DENOMINATOR, :MAX_SHRINK_STEPS
11
17
 
12
- def initialize
13
- super(
14
- TupleArbitrary.new(IntegerArbitrary.new(-1_000_000, 1_000_000), IntegerArbitrary.new(-1_000_000, 1_000_000)),
15
- FLOAT_MAPPER,
16
- FLOAT_UNMAPPER
17
- )
18
+ # @param min [Integer, Float, nil] Inclusive finite lower bound.
19
+ # @param max [Integer, Float, nil] Inclusive finite upper bound.
20
+ # @param allow_nan [Boolean] Whether `Float::NAN` may be generated. Default is `false`.
21
+ # @param allow_infinity [Boolean] Whether `+/-Float::INFINITY` may be generated when the corresponding bound is unbounded. Default is `false`.
22
+ def initialize(min: nil, max: nil, allow_nan: false, allow_infinity: false)
23
+ @raw_min = validate_bound!(:min, min)
24
+ @raw_max = validate_bound!(:max, max)
25
+ @min = round_inward_min(@raw_min)
26
+ @max = round_inward_max(@raw_max)
27
+ @allow_nan = !!allow_nan
28
+ @allow_infinity = !!allow_infinity
29
+ @empty = empty_domain?
30
+ @generation_bounds = @empty ? nil : compute_generation_bounds
31
+ @special_values = compute_special_values
32
+ @edge_cases = @empty ? [].freeze : compute_finite_edge_cases
33
+ end
34
+
35
+ # @see Arbitrary#generate
36
+ def generate(rng = Random.new)
37
+ if @empty
38
+ raise EmptyDomainError, "no representable Float in range #{@raw_min.inspect}..#{@raw_max.inspect}"
39
+ end
40
+
41
+ lo, hi = @generation_bounds
42
+
43
+ if !@special_values.empty? && rng.rand(20).zero?
44
+ return @special_values[rng.rand(@special_values.size)]
45
+ end
46
+
47
+ if !@edge_cases.empty? && rng.rand(EDGE_CASE_BIAS_DENOMINATOR).zero?
48
+ return @edge_cases[rng.rand(@edge_cases.size)]
49
+ end
50
+
51
+ if @min.nil? && @max.nil? && rng.rand(2).zero?
52
+ x = rng.rand(-1_000_000..1_000_000)
53
+ y = rng.rand(-1_000_000..1_000_000)
54
+ return x.to_f / (y.to_f.abs + 1.0)
55
+ end
56
+
57
+ sample_finite(lo, hi, rng)
18
58
  end
19
59
 
20
60
  # @see Arbitrary#cover?
21
61
  def cover?(value)
22
- return false unless value.is_a?(Float) && value.finite?
23
- return false if value.abs > MAX_ABS
24
- return true if value == 0.0
25
- return false if value.abs < MIN_NONZERO_ABS
62
+ return false unless value.is_a?(Float)
63
+ return false if @empty
64
+ return @allow_nan if value.nan?
65
+
66
+ if value.infinite?
67
+ return false unless @allow_infinity
68
+
69
+ return value.positive? ? @max.nil? : @min.nil?
70
+ end
71
+
72
+ return false if @min && value < @min
73
+ return false if @max && value > @max
74
+
75
+ true
76
+ end
77
+
78
+ # @see Arbitrary#shrink
79
+ def shrink(current, target: nil)
80
+ return [].each if @empty
81
+
82
+ lo, hi = shrink_bounds(current)
83
+ self.class.shrink_float(current, min: lo, max: hi, target: target)
84
+ end
85
+
86
+ # Float-aware binary-search shrink helper shared with `ChooseArbitrary`.
87
+ # @api private
88
+ #
89
+ # @param current [Object]
90
+ # @param min [Float]
91
+ # @param max [Float]
92
+ # @param target [Integer, Float, nil]
93
+ # @param exclude_end [Boolean]
94
+ # @return [Enumerator<Float>]
95
+ def self.shrink_float(current, min:, max:, target: nil, exclude_end: false)
96
+ return [].each unless current.is_a?(Float)
97
+ return [].each if min > max || (exclude_end && min >= max)
98
+
99
+ t = (target.nil? ? 0.0 : target.to_f).clamp(min, max)
100
+ t = min if exclude_end && t >= max
101
+
102
+ if current.nan?
103
+ return Enumerator.new { |y| y.yield t }
104
+ end
105
+
106
+ if current.infinite?
107
+ return Enumerator.new do |y|
108
+ y.yield t
109
+ finite_edge = current.positive? ? max : min
110
+ y.yield finite_edge if finite_edge != t && !(exclude_end && finite_edge >= max)
111
+ end
112
+ end
113
+
114
+ return [].each if current == t
115
+
116
+ Enumerator.new do |y|
117
+ seen = {current => true}
118
+
119
+ emit = lambda do |val|
120
+ clamped = val.clamp(min, max)
121
+ return if exclude_end && clamped >= max
122
+ return if seen.key?(clamped)
123
+
124
+ seen[clamped] = true
125
+ y.yield clamped
126
+ end
127
+
128
+ emit.call(t)
129
+
130
+ truncated = current.truncate.to_f
131
+ if truncated != current && truncated != t &&
132
+ ((t < current && truncated > t && truncated < current) ||
133
+ (t > current && truncated < t && truncated > current))
134
+ emit.call(truncated)
135
+ end
136
+
137
+ step = ((current / 2.0) - (t / 2.0)).abs
138
+ positive = current > t
139
+
140
+ MAX_SHRINK_STEPS.times do
141
+ break unless step.finite? && step > 0.0
142
+
143
+ candidate = positive ? (current - step) : (current + step)
144
+ break if candidate == current
145
+
146
+ emit.call(candidate)
147
+ step /= 2.0
148
+ end
149
+ end
150
+ end
151
+
152
+ private
153
+
154
+ def validate_bound!(name, value)
155
+ return nil if value.nil?
156
+
157
+ valid_type = value.is_a?(Integer) || value.is_a?(Float)
158
+ unless valid_type && value.to_f.finite?
159
+ raise ArgumentError, "#{name} must be a finite Integer, Float, or nil, got #{value.inspect}"
160
+ end
161
+
162
+ value
163
+ end
164
+
165
+ def round_inward_min(value)
166
+ return nil if value.nil?
167
+
168
+ f = value.to_f
169
+ f = f.next_float if value.is_a?(Integer) && f < value
170
+ f
171
+ end
172
+
173
+ def round_inward_max(value)
174
+ return nil if value.nil?
175
+
176
+ f = value.to_f
177
+ f = f.prev_float if value.is_a?(Integer) && f > value
178
+ f
179
+ end
180
+
181
+ def empty_domain?
182
+ return true if @raw_min && @raw_max && @raw_min > @raw_max
183
+ return true if @min && !@min.finite?
184
+ return true if @max && !@max.finite?
185
+ return true if @min && @max && @min > @max
186
+
187
+ false
188
+ end
189
+
190
+ def compute_generation_bounds
191
+ if @min && @max
192
+ [@min, @max].freeze
193
+ elsif @min
194
+ span = [DEFAULT_SPAN, @min.abs].max
195
+ hi = [@min + span, DEFAULT_MAX].max
196
+ [@min, hi.finite? ? hi : Float::MAX].freeze
197
+ elsif @max
198
+ span = [DEFAULT_SPAN, @max.abs].max
199
+ lo = [@max - span, DEFAULT_MIN].min
200
+ [lo.finite? ? lo : -Float::MAX, @max].freeze
201
+ else
202
+ [DEFAULT_MIN, DEFAULT_MAX].freeze
203
+ end
204
+ end
205
+
206
+ def shrink_bounds(current)
207
+ lo, hi = @generation_bounds
208
+ if current.is_a?(Float) && current.finite?
209
+ lo = [lo, current].min if @min.nil?
210
+ hi = [hi, current].max if @max.nil?
211
+ end
212
+ [lo, hi]
213
+ end
214
+
215
+ def compute_special_values
216
+ values = []
217
+ values << Float::NAN if @allow_nan
218
+ if @allow_infinity
219
+ values << Float::INFINITY if @max.nil?
220
+ values << -Float::INFINITY if @min.nil?
221
+ end
222
+ values.freeze
223
+ end
224
+
225
+ def compute_finite_edge_cases
226
+ lo, hi = @generation_bounds
227
+ return [].freeze if lo >= hi
228
+
229
+ candidates = [0.0, 1.0, -1.0]
230
+ candidates << -0.0 if lo < 0.0 && hi >= 0.0
231
+ candidates << @min if @min
232
+ candidates << @max if @max
233
+ candidates
234
+ .select { |v| v.between?(lo, hi) }
235
+ .uniq { |v| v.zero? ? (1.0 / v) : v }
236
+ .freeze
237
+ end
238
+
239
+ def sample_finite(lo, hi, rng)
240
+ return lo if lo == hi
26
241
 
27
- nil
242
+ span = hi - lo
243
+ if span.finite?
244
+ rng.rand(lo..hi)
245
+ else
246
+ # Avoid Float overflow when hi - lo exceeds Float::MAX.
247
+ mid = (lo / 2.0) + (hi / 2.0)
248
+ rng.rand(2).zero? ? rng.rand(lo..mid) : rng.rand(mid..hi)
249
+ end
28
250
  end
29
251
  end
30
252
  end
@@ -13,7 +13,7 @@ module Pbt
13
13
  end
14
14
  @choices = choices.select { |weight, _| weight.positive? }.map(&:dup)
15
15
  @total = @choices.sum(&:first)
16
- raise ArgumentError, "frequency requires at least one positive weight" if @total.zero?
16
+ raise EmptyDomainError, "frequency requires at least one positive weight" if @total.zero?
17
17
  end
18
18
 
19
19
  def generate(rng = Random.new)
@@ -5,7 +5,9 @@ module Pbt
5
5
  # Generates random integers between `min` and `max`.
6
6
  class IntegerArbitrary < Arbitrary
7
7
  DEFAULT_TARGET = 0
8
- private_constant :DEFAULT_TARGET
8
+ EDGE_CASE_DOMAIN_THRESHOLD = 20
9
+ EDGE_CASE_BIAS_DENOMINATOR = 5
10
+ private_constant :DEFAULT_TARGET, :EDGE_CASE_DOMAIN_THRESHOLD, :EDGE_CASE_BIAS_DENOMINATOR
9
11
 
10
12
  # @param min [Integer] Minimum value to generate.
11
13
  # @param max [Integer] Maximum value to generate.
@@ -14,10 +16,15 @@ module Pbt
14
16
 
15
17
  @min = min
16
18
  @max = max
19
+ @edge_cases = build_edge_cases
17
20
  end
18
21
 
19
22
  # @see Arbitrary#generate
20
23
  def generate(rng = Random.new)
24
+ if @edge_cases && rng.rand(EDGE_CASE_BIAS_DENOMINATOR).zero?
25
+ return @edge_cases[rng.rand(@edge_cases.size)]
26
+ end
27
+
21
28
  rng.rand(@min..@max)
22
29
  rescue ArgumentError => e
23
30
  raise EmptyDomainError, e.message if @min > @max
@@ -36,10 +43,7 @@ module Pbt
36
43
 
37
44
  # @see Arbitrary#shrink
38
45
  def shrink(current, target: nil)
39
- # If no target is specified, use the appropriate bound as target
40
46
  target ||= DEFAULT_TARGET.clamp(@min, @max)
41
-
42
- # Ensure target is within bounds
43
47
  target = target.clamp(@min, @max)
44
48
 
45
49
  gap = current - target
@@ -48,19 +52,32 @@ module Pbt
48
52
  is_positive_gap = gap > 0
49
53
 
50
54
  Enumerator.new do |y|
51
- while (diff = (current - target).abs) > 1
52
- halved = diff / 2
53
- current -= is_positive_gap ? halved : -halved
54
- # Ensure current stays within bounds
55
- current = current.clamp(@min, @max)
56
- y.yield current
57
- end
58
- # Only yield target if it's different from current
59
- if current != target
60
- y.yield target
55
+ last_yielded = target
56
+ y.yield target
57
+
58
+ step = gap.abs
59
+ while step > 1
60
+ step = step.is_a?(Integer) ? ((step + 1) / 2) : (step / 2.0)
61
+ break if step < 1
62
+
63
+ candidate = (is_positive_gap ? (current - step) : (current + step)).clamp(@min, @max)
64
+ if candidate != current && candidate != last_yielded
65
+ y.yield candidate
66
+ last_yielded = candidate
67
+ end
61
68
  end
62
69
  end
63
70
  end
71
+
72
+ private
73
+
74
+ def build_edge_cases
75
+ return nil unless @min.is_a?(Integer) && @max.is_a?(Integer)
76
+ return nil if @min > @max
77
+ return nil if (@max - @min + 1) <= EDGE_CASE_DOMAIN_THRESHOLD
78
+
79
+ [0, 1, -1, @min, @max].uniq.select { |v| v.between?(@min, @max) }.freeze
80
+ end
64
81
  end
65
82
  end
66
83
  end
@@ -60,7 +60,10 @@ module Pbt
60
60
 
61
61
  Enumerator.new do |y|
62
62
  @arb.shrink(@unmapper.call(current)).each do |v|
63
- y.yield @mapper.call(v)
63
+ mapped = @mapper.call(v)
64
+ next if mapped.equal?(current) || mapped.eql?(current)
65
+
66
+ y.yield mapped
64
67
  end
65
68
  end
66
69
  end
@@ -46,6 +46,41 @@ module Pbt
46
46
  arbitrary.respond_to?(:contextual?) && arbitrary.contextual?
47
47
  end
48
48
 
49
+ def self.validate_bound_arbitrary!(arb)
50
+ return arb if arb.is_a?(Arbitrary) || (arb.respond_to?(:generate) && arb.respond_to?(:shrink))
51
+
52
+ raise ArgumentError, "bind block must return an arbitrary responding to generate and shrink, got #{arb.class}"
53
+ end
54
+
55
+ def self.bind(outer_sample, inner_seed, &binder)
56
+ inner_arb = validate_bound_arbitrary!(binder.call(outer_sample.value))
57
+ inner_sample = capture(inner_arb, Random.new(inner_seed))
58
+ bind_samples(outer_sample, inner_sample, inner_seed, binder)
59
+ end
60
+
61
+ def self.bind_samples(outer_sample, inner_sample, inner_seed, binder)
62
+ Sample.new(inner_sample.value) do
63
+ Enumerator.new do |y|
64
+ outer_sample.shrink.each do |shrunk_outer|
65
+ shrunk_inner =
66
+ begin
67
+ shrunk_inner_arb = validate_bound_arbitrary!(binder.call(shrunk_outer.value))
68
+ capture(shrunk_inner_arb, Random.new(inner_seed))
69
+ rescue EmptyDomainError, FilterExhaustedError
70
+ next
71
+ end
72
+
73
+ y << bind_samples(shrunk_outer, shrunk_inner, inner_seed, binder)
74
+ end
75
+
76
+ inner_sample.shrink.each do |shrunk_inner|
77
+ y << bind_samples(outer_sample, shrunk_inner, inner_seed, binder)
78
+ end
79
+ end
80
+ end
81
+ end
82
+ private_class_method :bind_samples
83
+
49
84
  def self.tuple(samples)
50
85
  Sample.new(samples.map(&:value)) do
51
86
  Enumerator.new do |y|
@@ -14,7 +14,7 @@ module Pbt
14
14
  # @param verbose [Boolean] Whether to print verbose output. Default is `false`.
15
15
  # @param worker [Symbol] The concurrency method to use. `:ractor` and `:none` are supported. Default is `:none`.
16
16
  # @param num_runs [Integer] The number of runs to perform. Default is `100`.
17
- # @param seed [Integer] The seed to use for random number generation. It's useful to reproduce failed test with the seed you'd pick up from failure messages. Default is a random seed.
17
+ # @param seed [Integer] The seed to use for random number generation. Initialized once per process by default; override via `Pbt.assert(seed: ...)` / `Pbt.check(seed: ...)` to vary or reproduce a specific failure.
18
18
  # @param thread_report_on_exception [Boolean] Whether to report exceptions in threads. It's useful to suppress error logs on Ractor that reports many errors. Default is `false`.
19
19
  # @param max_skips [Integer, nil] Maximum number of skipped cases allowed via `Pbt.assume`. Defaults to `num_runs * 10` when `nil`.
20
20
  def initialize(
@@ -10,6 +10,11 @@ module Pbt
10
10
  # @param splat_args [Boolean] Whether generated tuple values should be splatted as positional arguments.
11
11
  # @param predicate [Proc] Predicate proc to test the generated values. Library users write this.
12
12
  def initialize(arb, keyword_args: false, splat_args: false, &predicate)
13
+ unless arb.respond_to?(:generate) && arb.respond_to?(:shrink)
14
+ raise ArgumentError, "Property arbitrary must respond to generate and shrink, got #{arb.class}"
15
+ end
16
+ raise ArgumentError, "Property requires a predicate block" unless predicate
17
+
13
18
  @arb = arb
14
19
  @keyword_args = keyword_args
15
20
  @splat_args = splat_args
@@ -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
- def initialize(source_values, property, verbose, target_runs: nil, max_skips: nil)
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 @property.respond_to?(:shrink_case)
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
- @property.shrink(c.val)
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,8 +41,12 @@ 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
51
  if property.respond_to?(:stateful?) && property.stateful?
43
52
  if options.key?(:worker) && config[:worker] == :ractor
@@ -47,11 +56,6 @@ module Pbt
47
56
  config[:worker] = :none unless options.key?(:worker)
48
57
  end
49
58
 
50
- max_skips = config[:max_skips]
51
- if !max_skips.nil? && !(max_skips.is_a?(Integer) && max_skips >= 0)
52
- raise Pbt::InvalidConfiguration, "max_skips must be a non-negative Integer or nil"
53
- end
54
-
55
59
  initial_values = toss(property, config[:seed])
56
60
 
57
61
  setup_for_ractor(config, property) do
@@ -61,6 +65,35 @@ module Pbt
61
65
 
62
66
  private
63
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
+
64
97
  # If using Ractor, some extra configurations are available and they need to be set up.
65
98
  #
66
99
  # - `:thread_report_on_exception`
@@ -133,25 +166,45 @@ module Pbt
133
166
  # @param runner [RunnerIterator]
134
167
  # @return [void]
135
168
  def run_it_in_ractors(property, runner)
136
- runner.map.with_index { |val, index|
137
- c = build_case(val, runner.case_index(index))
138
- c.ractor = property.run_in_ractor(c.val)
139
- c
140
- }.each do |c|
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|
141
186
  wait_ractor_result(c.ractor)
142
187
  runner.handle_result(c)
143
188
  rescue => e
144
189
  cause = unwrap_ractor_exception(e)
145
190
  if cause.is_a?(PreconditionNotMet)
146
- break if runner.handle_skip
191
+ if runner.handle_skip
192
+ stopped = true
193
+ break
194
+ end
147
195
 
148
196
  next
149
197
  end
150
198
  raise cause if FATAL_EXCEPTIONS.any? { |klass| cause.is_a?(klass) }
151
199
 
152
200
  c.exception = cause
153
- break if runner.handle_result(c) # Ignore the rest of the cases when failure is accepted.
201
+ if runner.handle_result(c) # Ignore the rest of the cases when failure is accepted.
202
+ stopped = true
203
+ break
204
+ end
154
205
  end
206
+
207
+ raise generation_error if generation_error && !stopped
155
208
  end
156
209
 
157
210
  def build_case(value, index)
@@ -71,10 +71,14 @@ module Pbt
71
71
  indented = formatted.lines.map { |line| " #{line.chomp}" }.join("\n")
72
72
  "\n#{indented}"
73
73
  else
74
- " #{ce}"
74
+ " #{format_value(ce)}"
75
75
  end
76
76
  end
77
77
 
78
+ def format_value(value)
79
+ value.is_a?(Pbt::Stateful::Run) ? value.to_s : value.inspect
80
+ end
81
+
78
82
  def error_backtrace
79
83
  locations =
80
84
  if @run_details.error_instance.is_a?(Pbt::Stateful::StepFailure)
@@ -92,7 +96,7 @@ module Pbt
92
96
  def verbose_details
93
97
  [
94
98
  "\nEncountered failures were:",
95
- @run_details.failures.map { |f| "- #{f.val}" },
99
+ @run_details.failures.map { |f| "- #{format_value(f.val)}" },
96
100
  format_execution_summary
97
101
  ].join("\n")
98
102
  end
@@ -119,7 +123,7 @@ module Pbt
119
123
  "\x1b[31m\u00D7\x1b[0m" # red "×"
120
124
  end
121
125
  left_padding = ". " * current_depth
122
- summary_lines << "#{left_padding}#{result_icon} #{current_tree.value}"
126
+ summary_lines << "#{left_padding}#{result_icon} #{format_value(current_tree.value)}"
123
127
 
124
128
  # push its children to the queue
125
129
  current_tree.children.reverse_each do |tree|