Class: Minitest::Proptest::Property

Inherits:
Object
  • Object
show all
Defined in:
lib/minitest/proptest/property.rb

Overview

Property evaluation - status, scoring, shrinking

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(test_proc, random: Random.method(:new), max_success: 100, max_discard_ratio: 10, max_size: 0x100, max_shrinks: 0x7fffffffffffffff, previous_failure: []) ⇒ Property

Returns a new instance of Property.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/minitest/proptest/property.rb', line 9

def initialize(
  # The function which proves the property
  test_proc,
  # Any class which provides `rand` accepting both an Integer and a Range
  # is acceptable.  The default value is Ruby's standard Mersenne Twister
  # implementation.
  random: Random.method(:new),
  # Maximum number of successful cases before considering the test a
  # success.
  max_success: 100,
  # Maximum ratio of discarded tests per successful test before giving up.
  max_discard_ratio: 10,
  # Maximum amount of entropy to generate in a single run
  max_size: 0x100,
  # Maximum number of shrink attempts (default of half of max unsigned int
  # on the system architecture adopted from QuickCheck
  max_shrinks: 0x7fffffffffffffff,
  # Previously discovered counter-example.  If this exists, it should be
  # run before any test cases are generated.
  previous_failure: []
)
  @test_proc         = test_proc
  @random            = random.call
  @generator         = ::Minitest::Proptest::Gen.new(@random)
  @max_success       = max_success
  @max_discard_ratio = max_discard_ratio
  @max_size          = max_size
  @max_shrinks       = max_shrinks
  @status            = Status.unknown
  @trivial           = false
  @result            = nil
  @exception         = nil
  @calls             = 0
  @valid_test_cases  = 0
  @generated         = []
  @arbitrary         = nil
  @previous_failure  = previous_failure.to_a
end

Instance Attribute Details

#resultObject (readonly)

Returns the value of attribute result.



7
8
9
# File 'lib/minitest/proptest/property.rb', line 7

def result
  @result
end

#statusObject (readonly)

Returns the value of attribute status.



7
8
9
# File 'lib/minitest/proptest/property.rb', line 7

def status
  @status
end

#trivialObject (readonly)

Returns the value of attribute trivial.



7
8
9
# File 'lib/minitest/proptest/property.rb', line 7

def trivial
  @trivial
end

Instance Method Details

#arbitrary(*classes) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/minitest/proptest/property.rb', line 54

def arbitrary(*classes)
  if @arbitrary
    @arbitrary.call(*classes)
  else
    a = @generator.for(*classes)
    @generated << a
    @status = Status.overrun unless @generated.length <= @max_size
    a.value
  end
end

#explainObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/minitest/proptest/property.rb', line 65

def explain
  prop = if @status.valid?
           'The property was proved to satsfaction across ' \
             "#{@valid_test_cases} assertions."
         elsif @status.invalid?
           'The property was determined to be invalid due to ' \
             "#{@exception.class.name}: #{@exception.message}\n" \
             "#{@exception.backtrace.map { |l| "    #{l}" }.join("\n")}"
         elsif @status.overrun?
           "The property attempted to generate more than #{@max_size} " \
             "bytes of entropy, violating the property's maximum " \
             'size.  This might be rectified by increasing max_size.'
         elsif @status.unknown?
           'The property has not yet been tested.'
         elsif @status.interesting?
           'The property has found the following counterexample after ' \
             "#{@valid_test_cases} valid " \
             "example#{@valid_test_cases == 1 ? '' : 's'}:\n" \
             "#{@generated.map(&:value).inspect}"
         end
  trivial = if @trivial
              "\nThe test does not appear to use any generated values " \
                'and as such is likely not generating much value.  ' \
                'Consider reworking this test to make use of arbitrary ' \
                'data.'
            else
              ''
            end
  prop + trivial
end

#run!Object



48
49
50
51
52
# File 'lib/minitest/proptest/property.rb', line 48

def run!
  rerun!
  iterate!
  shrink!
end