Class: Minitest::Proptest::Property

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

Overview

Property evaluation - status, scoring, shrinking

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Assertions

#property

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.



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
47
48
49
50
51
52
53
# File 'lib/minitest/proptest/property.rb', line 14

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
  @valid_test_case   = true
  @result            = nil
  @exception         = nil
  @calls             = 0
  @assertions        = 0
  @valid_test_cases  = 0
  @generated         = []
  @arbitrary         = nil
  @previous_failure  = previous_failure.to_a
end

Instance Attribute Details

#assertionsObject

Returns the value of attribute assertions.



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

def assertions
  @assertions
end

#callsObject (readonly)

Returns the value of attribute calls.



10
11
12
# File 'lib/minitest/proptest/property.rb', line 10

def calls
  @calls
end

#resultObject (readonly)

Returns the value of attribute result.



10
11
12
# File 'lib/minitest/proptest/property.rb', line 10

def result
  @result
end

#statusObject (readonly)

Returns the value of attribute status.



10
11
12
# File 'lib/minitest/proptest/property.rb', line 10

def status
  @status
end

#trivialObject (readonly)

Returns the value of attribute trivial.



10
11
12
# File 'lib/minitest/proptest/property.rb', line 10

def trivial
  @trivial
end

Instance Method Details

#arbitrary(*classes) ⇒ Object



61
62
63
64
65
66
67
68
69
70
# File 'lib/minitest/proptest/property.rb', line 61

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



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
# File 'lib/minitest/proptest/property.rb', line 76

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}"
         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



55
56
57
58
59
# File 'lib/minitest/proptest/property.rb', line 55

def run!
  rerun!
  iterate!
  shrink!
end

#where(&b) ⇒ Object



72
73
74
# File 'lib/minitest/proptest/property.rb', line 72

def where(&b)
  @valid_test_case &= b.call
end