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
|
@@ -25,24 +25,58 @@ module Pbt
|
|
|
25
25
|
length.times.map { @value_arb.generate(rng) }
|
|
26
26
|
end
|
|
27
27
|
|
|
28
|
+
def contextual?
|
|
29
|
+
Sample.contextual?(@value_arb)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def sample(rng = Random.new)
|
|
33
|
+
samples = Array.new(@length_arb.generate(rng)) { Sample.capture(@value_arb, rng) }
|
|
34
|
+
Sample.array(samples, @length_arb, @min_length)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# @see Arbitrary#cover?
|
|
38
|
+
def cover?(value)
|
|
39
|
+
return false unless value.is_a?(Array)
|
|
40
|
+
return false if value.size < @min_length
|
|
41
|
+
return false if !@max_length.nil? && value.size > @max_length
|
|
42
|
+
|
|
43
|
+
unknown = false
|
|
44
|
+
value.each do |item|
|
|
45
|
+
covered = Arbitrary.cover_result(@value_arb, item)
|
|
46
|
+
return false if covered == false
|
|
47
|
+
|
|
48
|
+
unknown = true if covered.nil?
|
|
49
|
+
end
|
|
50
|
+
unknown ? nil : true
|
|
51
|
+
end
|
|
52
|
+
|
|
28
53
|
# @see Arbitrary#shrink
|
|
29
54
|
def shrink(current)
|
|
30
|
-
return Enumerator.new { |_| } if current.size == @min_length
|
|
31
|
-
|
|
32
55
|
Enumerator.new do |y|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
56
|
+
if current.size > @min_length
|
|
57
|
+
# First, shrink the length of the array via contiguous slices.
|
|
58
|
+
emitted_n_minus_1 = false
|
|
59
|
+
@length_arb.shrink(current.size).each do |length|
|
|
60
|
+
emitted_n_minus_1 = true if length == current.size - 1
|
|
61
|
+
if length == 0
|
|
62
|
+
y.yield []
|
|
63
|
+
next
|
|
64
|
+
end
|
|
65
|
+
current.each_cons(length) do |con|
|
|
66
|
+
y.yield con
|
|
67
|
+
end
|
|
39
68
|
end
|
|
40
|
-
|
|
41
|
-
|
|
69
|
+
|
|
70
|
+
# Second, try deleting individual elements (including non-contiguous interior elements).
|
|
71
|
+
if current.size >= 3
|
|
72
|
+
indices = emitted_n_minus_1 ? (1...(current.size - 1)) : (0...current.size)
|
|
73
|
+
indices.each do |i|
|
|
74
|
+
y.yield [*current[...i], *current[i + 1..]]
|
|
75
|
+
end
|
|
42
76
|
end
|
|
43
77
|
end
|
|
44
78
|
|
|
45
|
-
#
|
|
79
|
+
# Third, shrink each element of the array (even when already at min_length).
|
|
46
80
|
current.each_with_index do |item, i|
|
|
47
81
|
@value_arb.shrink(item).each do |val|
|
|
48
82
|
y.yield [*current[...i], val, *current[i + 1..]]
|
|
@@ -12,6 +12,23 @@ module Pbt
|
|
|
12
12
|
# @see Arbitrary#generate
|
|
13
13
|
def generate(rng = Random.new)
|
|
14
14
|
rng.rand(@range)
|
|
15
|
+
rescue ArgumentError => e
|
|
16
|
+
raise EmptyDomainError, e.message if empty_range?
|
|
17
|
+
|
|
18
|
+
raise
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# @see Arbitrary#cover?
|
|
22
|
+
def cover?(value)
|
|
23
|
+
return false if @range.begin.nil? || @range.end.nil?
|
|
24
|
+
|
|
25
|
+
if integer_range?
|
|
26
|
+
return false unless value.is_a?(Integer)
|
|
27
|
+
else
|
|
28
|
+
return false unless value.is_a?(Float) && value.finite?
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
@range.cover?(value)
|
|
15
32
|
end
|
|
16
33
|
|
|
17
34
|
# @see Arbitrary#shrink
|
|
@@ -20,6 +37,16 @@ module Pbt
|
|
|
20
37
|
min, max = [@range.begin, @range.end].sort
|
|
21
38
|
IntegerArbitrary.new(min, max).shrink(current, target: min)
|
|
22
39
|
end
|
|
40
|
+
|
|
41
|
+
private
|
|
42
|
+
|
|
43
|
+
def integer_range?
|
|
44
|
+
@range.begin.is_a?(Integer) && @range.end.is_a?(Integer)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def empty_range?
|
|
48
|
+
!@range.begin.nil? && !@range.end.nil? && !@range.cover?(@range.begin)
|
|
49
|
+
end
|
|
23
50
|
end
|
|
24
51
|
end
|
|
25
52
|
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Pbt
|
|
4
|
+
module Arbitrary
|
|
5
|
+
# Generates one of the given constant values.
|
|
6
|
+
class ConstantFromArbitrary < Arbitrary
|
|
7
|
+
# @param choices [Array<Object>] Non-empty list of constant choices.
|
|
8
|
+
def initialize(choices)
|
|
9
|
+
raise EmptyDomainError, "constant_from requires at least one choice" if choices.empty?
|
|
10
|
+
|
|
11
|
+
@choices = choices.dup
|
|
12
|
+
@idx_arb = IntegerArbitrary.new(0, @choices.size - 1)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
# @see Arbitrary#generate
|
|
16
|
+
def generate(rng = Random.new)
|
|
17
|
+
@choices[@idx_arb.generate(rng)]
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# @see Arbitrary#cover?
|
|
21
|
+
def cover?(value)
|
|
22
|
+
@choices.any? { |choice| choice.equal?(value) || choice.eql?(value) }
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# @see Arbitrary#shrink
|
|
26
|
+
def shrink(current)
|
|
27
|
+
index = @choices.index { |choice| choice.equal?(current) || choice.eql?(current) }
|
|
28
|
+
return Enumerator.new {} if index.nil?
|
|
29
|
+
|
|
30
|
+
# Shrinks to values earlier in the list of `choices`.
|
|
31
|
+
Enumerator.new do |y|
|
|
32
|
+
@idx_arb.shrink(index).each do |idx|
|
|
33
|
+
y << @choices[idx]
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -4,19 +4,57 @@ module Pbt
|
|
|
4
4
|
module Arbitrary
|
|
5
5
|
# Generates values from another arbitrary, but only if they pass a predicate.
|
|
6
6
|
class FilterArbitrary < Arbitrary
|
|
7
|
+
DEFAULT_MAX_ATTEMPTS = 1_000
|
|
8
|
+
|
|
7
9
|
# @param arb [Arbitrary] Arbitrary to generate values to be filtered.
|
|
10
|
+
# @param max_attempts [Integer] Maximum generation attempts before raising `FilterExhaustedError`.
|
|
8
11
|
# @param refinement [Proc] Predicate proc to test each produced element. Return true to keep the element, false otherwise.
|
|
9
|
-
def initialize(arb, &refinement)
|
|
12
|
+
def initialize(arb, max_attempts: DEFAULT_MAX_ATTEMPTS, &refinement)
|
|
13
|
+
raise ArgumentError, "filter requires a block" unless refinement
|
|
14
|
+
unless max_attempts.is_a?(Integer) && max_attempts.positive?
|
|
15
|
+
raise ArgumentError, "max_attempts must be a positive Integer"
|
|
16
|
+
end
|
|
17
|
+
|
|
10
18
|
@arb = arb
|
|
19
|
+
@max_attempts = max_attempts
|
|
11
20
|
@refinement = refinement
|
|
12
21
|
end
|
|
13
22
|
|
|
14
23
|
# @see Arbitrary#generate
|
|
15
24
|
def generate(rng = Random.new)
|
|
16
|
-
|
|
25
|
+
@max_attempts.times do
|
|
17
26
|
val = @arb.generate(rng)
|
|
18
27
|
return val if @refinement.call(val)
|
|
19
28
|
end
|
|
29
|
+
|
|
30
|
+
raise FilterExhaustedError, "FilterArbitrary failed to generate a matching value after #{@max_attempts} attempts"
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def contextual?
|
|
34
|
+
Sample.contextual?(@arb)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def sample(rng = Random.new)
|
|
38
|
+
@max_attempts.times do
|
|
39
|
+
sample = Sample.capture(@arb, rng)
|
|
40
|
+
return sample.filter(&@refinement) if @refinement.call(sample.value)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
raise FilterExhaustedError, "FilterArbitrary failed to generate a matching value after #{@max_attempts} attempts"
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# @see Arbitrary#cover?
|
|
47
|
+
def cover?(value)
|
|
48
|
+
base = Arbitrary.cover_result(@arb, value)
|
|
49
|
+
return false if base == false
|
|
50
|
+
|
|
51
|
+
passed = begin
|
|
52
|
+
!!@refinement.call(value)
|
|
53
|
+
rescue
|
|
54
|
+
return nil
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
passed ? base : false
|
|
20
58
|
end
|
|
21
59
|
|
|
22
60
|
# @see Arbitrary#shrink
|
|
@@ -16,6 +16,22 @@ module Pbt
|
|
|
16
16
|
@keys.zip(values).to_h
|
|
17
17
|
end
|
|
18
18
|
|
|
19
|
+
def contextual?
|
|
20
|
+
Sample.contextual?(@arb)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def sample(rng = Random.new)
|
|
24
|
+
@arb.sample(rng).map { |values| @keys.zip(values).to_h }
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# @see Arbitrary#cover?
|
|
28
|
+
def cover?(value)
|
|
29
|
+
return false unless value.is_a?(Hash) && value.size == @keys.size
|
|
30
|
+
return false unless @keys.all? { |k| value.key?(k) }
|
|
31
|
+
|
|
32
|
+
@arb.cover?(@keys.map { |k| value[k] })
|
|
33
|
+
end
|
|
34
|
+
|
|
19
35
|
# @see Arbitrary#shrink
|
|
20
36
|
def shrink(current)
|
|
21
37
|
# This is not the most comprehensive but allows a reasonable number of entries in the shrink
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Pbt
|
|
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
|
|
11
|
+
|
|
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
|
+
end
|
|
19
|
+
|
|
20
|
+
# @see Arbitrary#cover?
|
|
21
|
+
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
|
|
26
|
+
|
|
27
|
+
nil
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Pbt
|
|
4
|
+
module Arbitrary
|
|
5
|
+
# Selects a generator using nonnegative integer weights.
|
|
6
|
+
class FrequencyArbitrary < Arbitrary
|
|
7
|
+
def initialize(choices)
|
|
8
|
+
unless choices.all? { |pair|
|
|
9
|
+
pair.is_a?(Array) && pair.size == 2 && pair[0].is_a?(Integer) && pair[0] >= 0 &&
|
|
10
|
+
pair[1].respond_to?(:generate) && pair[1].respond_to?(:shrink)
|
|
11
|
+
}
|
|
12
|
+
raise ArgumentError, "frequency expects [nonnegative Integer weight, arbitrary] pairs"
|
|
13
|
+
end
|
|
14
|
+
@choices = choices.select { |weight, _| weight.positive? }.map(&:dup)
|
|
15
|
+
@total = @choices.sum(&:first)
|
|
16
|
+
raise ArgumentError, "frequency requires at least one positive weight" if @total.zero?
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def generate(rng = Random.new)
|
|
20
|
+
select(rng).generate(rng)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# Preserve the selected branch for shrinking, even after subsequent generation.
|
|
24
|
+
def sample(rng = Random.new)
|
|
25
|
+
Sample.capture(select(rng), rng)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def contextual?
|
|
29
|
+
true
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# @see Arbitrary#cover?
|
|
33
|
+
def cover?(value)
|
|
34
|
+
unknown = false
|
|
35
|
+
@choices.each do |_weight, arb|
|
|
36
|
+
covered = Arbitrary.cover_result(arb, value)
|
|
37
|
+
return true if covered == true
|
|
38
|
+
|
|
39
|
+
unknown = true if covered.nil?
|
|
40
|
+
end
|
|
41
|
+
unknown ? nil : false
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# A bare value cannot identify its branch. Use sample or Pbt.property to shrink.
|
|
45
|
+
def shrink(current)
|
|
46
|
+
[].each
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
private
|
|
50
|
+
|
|
51
|
+
def select(rng)
|
|
52
|
+
ticket = rng.rand(@total)
|
|
53
|
+
@choices.each do |weight, arbitrary|
|
|
54
|
+
return arbitrary if ticket < weight
|
|
55
|
+
|
|
56
|
+
ticket -= weight
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
@@ -10,6 +10,8 @@ module Pbt
|
|
|
10
10
|
# @param min [Integer] Minimum value to generate.
|
|
11
11
|
# @param max [Integer] Maximum value to generate.
|
|
12
12
|
def initialize(min, max)
|
|
13
|
+
raise ArgumentError, "min and max cannot be nil" if min.nil? || max.nil?
|
|
14
|
+
|
|
13
15
|
@min = min
|
|
14
16
|
@max = max
|
|
15
17
|
end
|
|
@@ -23,6 +25,15 @@ module Pbt
|
|
|
23
25
|
raise
|
|
24
26
|
end
|
|
25
27
|
|
|
28
|
+
# @see Arbitrary#cover?
|
|
29
|
+
def cover?(value)
|
|
30
|
+
return false unless value.is_a?(Integer)
|
|
31
|
+
return false if !@min.nil? && value < @min
|
|
32
|
+
return false if !@max.nil? && value > @max
|
|
33
|
+
|
|
34
|
+
true
|
|
35
|
+
end
|
|
36
|
+
|
|
26
37
|
# @see Arbitrary#shrink
|
|
27
38
|
def shrink(current, target: nil)
|
|
28
39
|
# If no target is specified, use the appropriate bound as target
|
|
@@ -6,11 +6,16 @@ module Pbt
|
|
|
6
6
|
class MapArbitrary < Arbitrary
|
|
7
7
|
# @param arb [Arbitrary] Arbitrary to generate values to be mapped.
|
|
8
8
|
# @param mapper [Proc] Proc to map generated values. Mainly used for generation.
|
|
9
|
-
# @param unmapper [Proc]
|
|
10
|
-
|
|
9
|
+
# @param unmapper [Proc, nil] Optional proc to unmap generated values for bare `#shrink`.
|
|
10
|
+
# @param bijective [Boolean] Internal flag indicating `mapper`/`unmapper` form a bijection on the codomain.
|
|
11
|
+
def initialize(arb, mapper, unmapper = nil, bijective: false)
|
|
12
|
+
raise ArgumentError, "mapper must be callable" unless mapper.respond_to?(:call)
|
|
13
|
+
raise ArgumentError, "unmapper must be callable or nil" if !unmapper.nil? && !unmapper.respond_to?(:call)
|
|
14
|
+
|
|
11
15
|
@arb = arb
|
|
12
16
|
@mapper = mapper
|
|
13
17
|
@unmapper = unmapper
|
|
18
|
+
@bijective = bijective
|
|
14
19
|
end
|
|
15
20
|
|
|
16
21
|
# @see Arbitrary#generate
|
|
@@ -18,8 +23,41 @@ module Pbt
|
|
|
18
23
|
@mapper.call(@arb.generate(rng))
|
|
19
24
|
end
|
|
20
25
|
|
|
26
|
+
# @private
|
|
27
|
+
def contextual?
|
|
28
|
+
@unmapper.nil? || Sample.contextual?(@arb)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# @see Arbitrary#sample
|
|
32
|
+
def sample(rng = Random.new)
|
|
33
|
+
Sample.capture(@arb, rng).map { |value| @mapper.call(value) }
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# @see Arbitrary#cover?
|
|
37
|
+
def cover?(value)
|
|
38
|
+
return nil if @unmapper.nil?
|
|
39
|
+
|
|
40
|
+
unmapped, mapped = begin
|
|
41
|
+
u = @unmapper.call(value)
|
|
42
|
+
[u, @mapper.call(u)]
|
|
43
|
+
rescue
|
|
44
|
+
return nil
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
round_trip = mapped.equal?(value) || mapped.eql?(value)
|
|
48
|
+
return @bijective ? false : nil unless round_trip
|
|
49
|
+
|
|
50
|
+
inner = Arbitrary.cover_result(@arb, unmapped)
|
|
51
|
+
return true if inner == true
|
|
52
|
+
return false if @bijective && inner == false
|
|
53
|
+
|
|
54
|
+
nil
|
|
55
|
+
end
|
|
56
|
+
|
|
21
57
|
# @see Arbitrary#shrink
|
|
22
58
|
def shrink(current)
|
|
59
|
+
return [].each if @unmapper.nil?
|
|
60
|
+
|
|
23
61
|
Enumerator.new do |y|
|
|
24
62
|
@arb.shrink(@unmapper.call(current)).each do |v|
|
|
25
63
|
y.yield @mapper.call(v)
|
|
@@ -2,27 +2,60 @@
|
|
|
2
2
|
|
|
3
3
|
module Pbt
|
|
4
4
|
module Arbitrary
|
|
5
|
-
#
|
|
5
|
+
# Uniformly selects one of the given arbitraries and delegates generation/shrinking to it.
|
|
6
6
|
class OneOfArbitrary < Arbitrary
|
|
7
|
-
# @param
|
|
8
|
-
def initialize(
|
|
9
|
-
|
|
10
|
-
|
|
7
|
+
# @param arbitraries [Array<Arbitrary>] Non-empty list of arbitraries to choose from.
|
|
8
|
+
def initialize(arbitraries)
|
|
9
|
+
raise ArgumentError, "one_of expects an Array of arbitraries" unless arbitraries.is_a?(Array)
|
|
10
|
+
raise EmptyDomainError, "one_of requires at least one arbitrary" if arbitraries.empty?
|
|
11
|
+
|
|
12
|
+
unless arbitraries.all? { |arb| arb.respond_to?(:generate) && arb.respond_to?(:shrink) }
|
|
13
|
+
raise ArgumentError, "one_of expects arbitraries (use Pbt.constant_from for constant values)"
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
@arbitraries = arbitraries.dup
|
|
11
17
|
end
|
|
12
18
|
|
|
13
19
|
# @see Arbitrary#generate
|
|
14
20
|
def generate(rng = Random.new)
|
|
15
|
-
|
|
21
|
+
select(rng).generate(rng)
|
|
16
22
|
end
|
|
17
23
|
|
|
18
|
-
#
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
24
|
+
# Preserve the selected branch for shrinking, even after subsequent generation.
|
|
25
|
+
#
|
|
26
|
+
# @see Arbitrary#sample
|
|
27
|
+
def sample(rng = Random.new)
|
|
28
|
+
Sample.capture(select(rng), rng)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# @private
|
|
32
|
+
def contextual?
|
|
33
|
+
true
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# @see Arbitrary#cover?
|
|
37
|
+
def cover?(value)
|
|
38
|
+
unknown = false
|
|
39
|
+
@arbitraries.each do |arb|
|
|
40
|
+
covered = Arbitrary.cover_result(arb, value)
|
|
41
|
+
return true if covered == true
|
|
42
|
+
|
|
43
|
+
unknown = true if covered.nil?
|
|
25
44
|
end
|
|
45
|
+
unknown ? nil : false
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# A bare value cannot identify its source arbitrary. Use `sample` or `Pbt.property` to shrink.
|
|
49
|
+
#
|
|
50
|
+
# @see Arbitrary#shrink
|
|
51
|
+
def shrink(_current)
|
|
52
|
+
[].each
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
private
|
|
56
|
+
|
|
57
|
+
def select(rng)
|
|
58
|
+
@arbitraries[rng.rand(@arbitraries.size)]
|
|
26
59
|
end
|
|
27
60
|
end
|
|
28
61
|
end
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Pbt
|
|
4
|
+
module Arbitrary
|
|
5
|
+
# A generated value and its lazy shrink tree. Each sample owns its provenance.
|
|
6
|
+
class Sample
|
|
7
|
+
attr_reader :value
|
|
8
|
+
|
|
9
|
+
def initialize(value, &shrinker)
|
|
10
|
+
@value = value
|
|
11
|
+
@shrinker = shrinker
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def shrink
|
|
15
|
+
Enumerator.new { |y| @shrinker.call.each { |child| y << child } }
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def map(&mapper)
|
|
19
|
+
Sample.new(mapper.call(value)) do
|
|
20
|
+
shrink.lazy.map { |child| child.map(&mapper) }
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def filter(&predicate)
|
|
25
|
+
Sample.new(value) do
|
|
26
|
+
shrink.lazy.select { |child| predicate.call(child.value) }.map { |child| child.filter(&predicate) }
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Compatibility with user-defined generate/shrink objects.
|
|
31
|
+
def self.capture(arbitrary, rng)
|
|
32
|
+
if arbitrary.respond_to?(:sample)
|
|
33
|
+
arbitrary.sample(rng)
|
|
34
|
+
else
|
|
35
|
+
from_value(arbitrary, arbitrary.generate(rng))
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def self.from_value(arbitrary, value)
|
|
40
|
+
Sample.new(value) do
|
|
41
|
+
arbitrary.shrink(value).lazy.map { |child| from_value(arbitrary, child) }
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def self.contextual?(arbitrary)
|
|
46
|
+
arbitrary.respond_to?(:contextual?) && arbitrary.contextual?
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def self.tuple(samples)
|
|
50
|
+
Sample.new(samples.map(&:value)) do
|
|
51
|
+
Enumerator.new do |y|
|
|
52
|
+
samples.each_with_index do |sample, index|
|
|
53
|
+
sample.shrink.each do |child|
|
|
54
|
+
next_samples = samples.dup
|
|
55
|
+
next_samples[index] = child
|
|
56
|
+
y << tuple(next_samples)
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def self.array(samples, length_arbitrary, min_length)
|
|
64
|
+
Sample.new(samples.map(&:value)) do
|
|
65
|
+
Enumerator.new do |y|
|
|
66
|
+
if samples.size > min_length
|
|
67
|
+
emitted_n_minus_1 = false
|
|
68
|
+
length_arbitrary.shrink(samples.size).each do |length|
|
|
69
|
+
emitted_n_minus_1 = true if length == samples.size - 1
|
|
70
|
+
if length.zero?
|
|
71
|
+
y << array([], length_arbitrary, min_length)
|
|
72
|
+
else
|
|
73
|
+
samples.each_cons(length) { |items| y << array(items, length_arbitrary, min_length) }
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
if samples.size >= 3
|
|
78
|
+
indices = emitted_n_minus_1 ? (1...(samples.size - 1)) : (0...samples.size)
|
|
79
|
+
indices.each do |i|
|
|
80
|
+
y << array([*samples[...i], *samples[i + 1..]], length_arbitrary, min_length)
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
samples.each_with_index do |sample, index|
|
|
86
|
+
sample.shrink.each do |child|
|
|
87
|
+
next_samples = samples.dup
|
|
88
|
+
next_samples[index] = child
|
|
89
|
+
y << array(next_samples, length_arbitrary, min_length)
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
end
|
|
@@ -14,6 +14,28 @@ module Pbt
|
|
|
14
14
|
@arbs.map { |arb| arb.generate(rng) }
|
|
15
15
|
end
|
|
16
16
|
|
|
17
|
+
def contextual?
|
|
18
|
+
@arbs.any? { |arb| Sample.contextual?(arb) }
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def sample(rng = Random.new)
|
|
22
|
+
Sample.tuple(@arbs.map { |arb| Sample.capture(arb, rng) })
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# @see Arbitrary#cover?
|
|
26
|
+
def cover?(value)
|
|
27
|
+
return false unless value.is_a?(Array) && value.size == @arbs.size
|
|
28
|
+
|
|
29
|
+
unknown = false
|
|
30
|
+
@arbs.each_with_index do |arb, idx|
|
|
31
|
+
covered = Arbitrary.cover_result(arb, value[idx])
|
|
32
|
+
return false if covered == false
|
|
33
|
+
|
|
34
|
+
unknown = true if covered.nil?
|
|
35
|
+
end
|
|
36
|
+
unknown ? nil : true
|
|
37
|
+
end
|
|
38
|
+
|
|
17
39
|
# @see Arbitrary#shrink
|
|
18
40
|
def shrink(current)
|
|
19
41
|
# This is not the most comprehensive but allows a reasonable number of entries in the shrink.
|
data/lib/pbt/check/case.rb
CHANGED
|
@@ -9,19 +9,21 @@ module Pbt
|
|
|
9
9
|
:num_runs,
|
|
10
10
|
:seed,
|
|
11
11
|
:thread_report_on_exception,
|
|
12
|
-
|
|
12
|
+
:max_skips
|
|
13
13
|
) do
|
|
14
14
|
# @param verbose [Boolean] Whether to print verbose output. Default is `false`.
|
|
15
|
-
# @param worker [Symbol] The concurrency method to use.
|
|
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
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.
|
|
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
|
+
# @param max_skips [Integer, nil] Maximum number of skipped cases allowed via `Pbt.assume`. Defaults to `num_runs * 10` when `nil`.
|
|
19
20
|
def initialize(
|
|
20
21
|
verbose: false,
|
|
21
22
|
worker: :none,
|
|
22
23
|
num_runs: 100,
|
|
23
24
|
seed: Random.new_seed,
|
|
24
|
-
thread_report_on_exception: false
|
|
25
|
+
thread_report_on_exception: false,
|
|
26
|
+
max_skips: nil
|
|
25
27
|
)
|
|
26
28
|
super
|
|
27
29
|
end
|