Class: Minitest::Proptest::Gen::ValueGenerator
- Inherits:
-
Object
- Object
- Minitest::Proptest::Gen::ValueGenerator
- Defined in:
- lib/minitest/proptest/gen/value_generator.rb
Overview
Methods for value generation
Instance Attribute Summary collapse
-
#entropy ⇒ Object
Returns the value of attribute entropy.
-
#type_parameters ⇒ Object
writeonly
Sets the attribute type_parameters.
Class Method Summary collapse
- .bound_max ⇒ Object
- .bound_min ⇒ Object
- .empty(gen) ⇒ Object
- .with_append(bound_min, bound_max, &f) ⇒ Object
- .with_empty(&f) ⇒ Object
- .with_score_function(&f) ⇒ Object
- .with_shrink_function(&f) ⇒ Object
Instance Method Summary collapse
-
#append(_other) ⇒ Object
append is not expected to be called unless overridden.
- #force(v) ⇒ Object
- #generate_value ⇒ Object
- #one_of(r) ⇒ Object
- #prefix_entropy_generation(vals) ⇒ Object
- #score ⇒ Object
- #score_function(v) ⇒ Object
- #shrink_candidates ⇒ Object
- #shrink_function(x) ⇒ Object
- #shrink_parameter(x) ⇒ Object
-
#sized(n) ⇒ Object
Generator helpers.
- #value ⇒ Object
Instance Attribute Details
#entropy ⇒ Object
Returns the value of attribute entropy.
8 9 10 |
# File 'lib/minitest/proptest/gen/value_generator.rb', line 8 def entropy @entropy end |
#type_parameters=(value) ⇒ Object (writeonly)
Sets the attribute type_parameters
9 10 11 |
# File 'lib/minitest/proptest/gen/value_generator.rb', line 9 def type_parameters=(value) @type_parameters = value end |
Class Method Details
.bound_max ⇒ Object
40 41 42 |
# File 'lib/minitest/proptest/gen/value_generator.rb', line 40 def self.bound_max 1 end |
.bound_min ⇒ Object
44 45 46 |
# File 'lib/minitest/proptest/gen/value_generator.rb', line 44 def self.bound_min 0 end |
.empty(gen) ⇒ Object
53 54 55 |
# File 'lib/minitest/proptest/gen/value_generator.rb', line 53 def self.empty(gen) self.new(gen) end |
.with_append(bound_min, bound_max, &f) ⇒ Object
21 22 23 24 25 26 27 28 29 |
# File 'lib/minitest/proptest/gen/value_generator.rb', line 21 def self.with_append(bound_min, bound_max, &f) define_singleton_method(:bound_min) { bound_min } define_singleton_method(:bound_max) { bound_max } define_method(:append) do |other| @value = f.call(value, other.value) self end self end |
.with_empty(&f) ⇒ Object
31 32 33 34 35 36 37 38 |
# File 'lib/minitest/proptest/gen/value_generator.rb', line 31 def self.with_empty(&f) define_singleton_method(:empty) do |gen| temp = new(gen) temp.instance_variable_set(:@value, f.call) temp end self end |
.with_score_function(&f) ⇒ Object
16 17 18 19 |
# File 'lib/minitest/proptest/gen/value_generator.rb', line 16 def self.with_score_function(&f) define_method(:score_function, &f) self end |
.with_shrink_function(&f) ⇒ Object
11 12 13 14 |
# File 'lib/minitest/proptest/gen/value_generator.rb', line 11 def self.with_shrink_function(&f) define_method(:shrink_function, &f) self end |
Instance Method Details
#append(_other) ⇒ Object
append is not expected to be called unless overridden
49 50 51 |
# File 'lib/minitest/proptest/gen/value_generator.rb', line 49 def append(_other) self end |
#force(v) ⇒ Object
57 58 59 60 61 62 |
# File 'lib/minitest/proptest/gen/value_generator.rb', line 57 def force(v) temp = self.class.new(ArgumentError) temp.instance_variable_set(:@value, v) temp.type_parameters = @type_parameters temp end |
#generate_value ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/minitest/proptest/gen/value_generator.rb', line 64 def generate_value gen = @generated.reduce(@generator) do |g, val| g.call(val) g end while gen.is_a?(Proc) || gen.is_a?(Method) gen = gen.call(*@type_parameters.map(&:value)) gen = gen.value if gen.is_a?(ValueGenerator) end gen end |
#one_of(r) ⇒ Object
127 128 129 |
# File 'lib/minitest/proptest/gen/value_generator.rb', line 127 def one_of(r) r.to_a[sized(r.to_a.length - 1)] end |
#prefix_entropy_generation(vals) ⇒ Object
84 85 86 |
# File 'lib/minitest/proptest/gen/value_generator.rb', line 84 def prefix_entropy_generation(vals) @generated = vals + @generated end |
#score ⇒ Object
88 89 90 91 92 |
# File 'lib/minitest/proptest/gen/value_generator.rb', line 88 def score value fs = @type_parameters.map { |x| x.method(:score_function) } score_function(*fs, value) end |
#score_function(v) ⇒ Object
94 95 96 |
# File 'lib/minitest/proptest/gen/value_generator.rb', line 94 def score_function(v) v.to_i.abs end |
#shrink_candidates ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/minitest/proptest/gen/value_generator.rb', line 98 def shrink_candidates fs = @type_parameters.map { |x| x.method(:shrink_function) } os = score # Ensure that the end of the shrink attempt will contain the original # value. This is necessary to guarantee that the shrink process # produces at least one failure for the purpose of capturing variable # assignment. candidates = shrink_function(*fs, value) + [value] candidates .map { |c| [force(c).score, c] } .reject { |(s, _)| s > os } .sort { |x, y| x.first <=> y.first } .uniq end |
#shrink_function(x) ⇒ Object
113 114 115 |
# File 'lib/minitest/proptest/gen/value_generator.rb', line 113 def shrink_function(x) [x.itself] end |
#shrink_parameter(x) ⇒ Object
117 118 119 |
# File 'lib/minitest/proptest/gen/value_generator.rb', line 117 def shrink_parameter(x) @shrink_parameter.call(x) end |
#sized(n) ⇒ Object
Generator helpers
123 124 125 |
# File 'lib/minitest/proptest/gen/value_generator.rb', line 123 def sized(n) entropy.call(n + 1) end |
#value ⇒ Object
78 79 80 81 82 |
# File 'lib/minitest/proptest/gen/value_generator.rb', line 78 def value return false if @value == false @value ||= generate_value end |