Class: Minitest::Proptest::Property

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

Overview

Property evaluation - status, scoring, shrinking

Defined Under Namespace

Classes: InvalidProperty

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Assertions

#property

Constructor Details

#initialize(test_proc, filename, methodname, 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.



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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/minitest/proptest/property.rb', line 16

def initialize(
  # The function which proves the property
  test_proc,
  # The file in which our property lives
  filename,
  # The method containing our property
  methodname,
  # 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
  @filename          = filename
  @methodname        = methodname
  @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
  @assertions        = 0
  @valid_test_cases  = 0
  @generated         = []
  @arbitrary         = nil
  @previous_failure  = previous_failure.to_a
  @local_variables   = {}
end

Instance Attribute Details

#assertionsObject

Returns the value of attribute assertions.



14
15
16
# File 'lib/minitest/proptest/property.rb', line 14

def assertions
  @assertions
end

#callsObject (readonly)

Returns the value of attribute calls.



12
13
14
# File 'lib/minitest/proptest/property.rb', line 12

def calls
  @calls
end

#resultObject (readonly)

Returns the value of attribute result.



12
13
14
# File 'lib/minitest/proptest/property.rb', line 12

def result
  @result
end

#statusObject (readonly)

Returns the value of attribute status.



12
13
14
# File 'lib/minitest/proptest/property.rb', line 12

def status
  @status
end

#trivialObject (readonly)

Returns the value of attribute trivial.



12
13
14
# File 'lib/minitest/proptest/property.rb', line 12

def trivial
  @trivial
end

Instance Method Details

#explainObject



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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/minitest/proptest/property.rb', line 69

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?
           info = 'A counterexample to a property has been found after ' \
                  "#{@valid_test_cases} valid " \
                  "example#{@valid_test_cases == 1 ? '' : 's'}.\n"
           var_info = if @local_variables.empty?
                        'Variables local to the property were unable ' \
                          'to be determined.  This is usually a bug.'
                      else
                        "The values at the time of the failure were:\n"
                      end
           vars = @local_variables
                  .map { |k, v| "\t#{k}: #{v.inspect}" }
                  .join("\n")

           info + var_info + vars
         elsif @status.exhausted?
           "The property was unable to generate #{@max_success} test " \
             'cases before generating ' \
             "#{@max_success * @max_discard_ratio} rejected test " \
             "cases.  This might be a problem with the property's " \
             '`where` blocks.'
         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



63
64
65
66
67
# File 'lib/minitest/proptest/property.rb', line 63

def run!
  rerun!
  iterate!
  shrink!
end