github.com/leanovate/gopter@v0.2.9/example_labels_test.go (about)

     1  package gopter_test
     2  
     3  import (
     4  	"github.com/leanovate/gopter"
     5  	"github.com/leanovate/gopter/gen"
     6  	"github.com/leanovate/gopter/prop"
     7  )
     8  
     9  func spookyCalculation(a, b int) int {
    10  	if a < 0 {
    11  		a = -a
    12  	}
    13  	if b < 0 {
    14  		b = -b
    15  	}
    16  	return 2*b + 3*(2+(a+1)+b*(b+1))
    17  }
    18  
    19  // Example_labels demonstrates how labels may help, in case of more complex
    20  // conditions.
    21  // The output will be:
    22  //  ! Check spooky: Falsified after 0 passed tests.
    23  //  > Labels of failing property: even result
    24  //  a: 3
    25  //  a_ORIGINAL (44 shrinks): 861384713
    26  //  b: 0
    27  //  b_ORIGINAL (1 shrinks): -642623569
    28  func Example_labels() {
    29  	parameters := gopter.DefaultTestParameters()
    30  	parameters.Rng.Seed(1234) // Just for this example to generate reproducible results
    31  	parameters.MinSuccessfulTests = 10000
    32  
    33  	properties := gopter.NewProperties(parameters)
    34  
    35  	properties.Property("Check spooky", prop.ForAll(
    36  		func(a, b int) string {
    37  			result := spookyCalculation(a, b)
    38  			if result < 0 {
    39  				return "negative result"
    40  			}
    41  			if result%2 == 0 {
    42  				return "even result"
    43  			}
    44  			return ""
    45  		},
    46  		gen.Int().WithLabel("a"),
    47  		gen.Int().WithLabel("b"),
    48  	))
    49  
    50  	// When using testing.T you might just use: properties.TestingRun(t)
    51  	properties.Run(gopter.ConsoleReporter(false))
    52  	// Output:
    53  	// ! Check spooky: Falsified after 0 passed tests.
    54  	// > Labels of failing property: even result
    55  	// a: 3
    56  	// a_ORIGINAL (44 shrinks): 861384713
    57  	// b: 0
    58  	// b_ORIGINAL (1 shrinks): -642623569
    59  }