Class: Quick::Sampler::Base

Inherits:
Enumerator::Lazy
  • Object
show all
Defined in:
lib/quick/sampler/base.rb

Overview

A sampler base class, delegating most work to the underlying lazy enumerator.

Readable #inspect

One superficial extra that Base provides is a description attribute which is returned by #inspect. This can help keep test output readable.

Shrinking

Sampler::Base also provides an api for shrinking failed inputs.

From stackoverflow discussion of shrinking (follow the link for an example) in the original QuickCheck:

When QuickCheck finds an input that violates a property, it will first try to find smaller inputs that also violate the property, in order to give the developer a better message about the nature of the failure.

What it means to be „small“ of course depends on the datatype in question; to QuickCheck it is anything that comes out from from the shrink function.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, description: "Quick Sampler") ⇒ Base

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Not supposed to be used directly, use Quick::Sampler.compile instead.



37
38
39
40
# File 'lib/quick/sampler/base.rb', line 37

def initialize source, description: "Quick Sampler"
  @description = description
  super(source_to_lazy(source))
end

Instance Attribute Details

#descriptionObject

Returns the value of attribute description.



28
29
30
# File 'lib/quick/sampler/base.rb', line 28

def description
  @description
end

Instance Method Details

#inspectString



43
44
45
# File 'lib/quick/sampler/base.rb', line 43

def inspect
  description
end

#shrink(samples) {|sample| ... } ⇒ Array

A very preliminary API for QuickCheck-like input shrinking. A sampler is responsible for shrinking its failed samples.

Examples:

Detecting and then shrinking failing examples:


# Generate random input data
all_inputs = sampler.first(100)

# Find failures (by rejecting inputs satisfying the property)
failed_inputs = all_inputs.reject{ |input| property(input) }

# Shrink failed inputs, continue shrinking as long as property does not hold
shrunk_inputs = sampler.shrink(failed_inputs) {|input| !property(input)}

Yield Parameters:

  • sample (<Sample>)

    shrunk value to check the property again. If property holds, the value will be discarded, and the one it was shrunk from will be added to the set of "minimal" failing examples.

Yield Returns:

  • (Boolean)

    true to keep on shrinking, meaning "property still fails for shrunken value, try to shrink more"



72
73
74
# File 'lib/quick/sampler/base.rb', line 72

def shrink samples, &block
  Quick::Sampler::Shrink.while(samples, &block)
end