Class: Pbt::Check::Property

Inherits:
Object
  • Object
show all
Defined in:
lib/pbt/check/property.rb

Overview

Represents a property to be tested. This class holds an arbitrary to generate values and a predicate to test them.

Instance Method Summary collapse

Constructor Details

#initialize(arb, &predicate) ⇒ Property

Returns a new instance of Property.

Parameters:

  • arb (Array<Arbitrary>)
  • predicate (Proc)

    Predicate proc to test the generated values. Library users write this.

[View source]

10
11
12
13
# File 'lib/pbt/check/property.rb', line 10

def initialize(arb, &predicate)
  @arb = arb
  @predicate = predicate
end

Instance Method Details

#generate(rng) ⇒ Object

Generate a next value to test.

Parameters:

  • rng (Random)

    Random number generator.

Returns:

  • (Object)
[View source]

19
20
21
# File 'lib/pbt/check/property.rb', line 19

def generate(rng)
  @arb.generate(rng)
end

#run(val) ⇒ void

This method returns an undefined value.

Run the predicate with the generated ‘val`.

Parameters:

  • val (Object)
[View source]

36
37
38
39
40
41
42
# File 'lib/pbt/check/property.rb', line 36

def run(val)
  if val.is_a?(Hash)
    @predicate.call(**val)
  else
    @predicate.call(val)
  end
end

#run_in_ractor(val) ⇒ Ractor

Run the predicate with the generated ‘val` in a Ractor. This is used only for parallel testing with Ractors.

Parameters:

  • val (Object)

Returns:

  • (Ractor)
[View source]

49
50
51
# File 'lib/pbt/check/property.rb', line 49

def run_in_ractor(val)
  Ractor.new(val, &@predicate)
end

#shrink(val) ⇒ Enumerator<Object>

Shrink the ‘val` to a smaller one. This is used to find the smallest failing case after a failure.

Parameters:

  • val (Object)

Returns:

  • (Enumerator<Object>)
[View source]

28
29
30
# File 'lib/pbt/check/property.rb', line 28

def shrink(val)
  @arb.shrink(val)
end