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

     1  package gopter_test
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  	"sort"
     7  
     8  	"github.com/leanovate/gopter"
     9  	"github.com/leanovate/gopter/gen"
    10  	"github.com/leanovate/gopter/prop"
    11  )
    12  
    13  func wrong_biggest(ns []int) (int, error) {
    14  	if len(ns) == 0 {
    15  		return 0, fmt.Errorf("slice must have at least one element")
    16  	}
    17  	return ns[0], nil
    18  }
    19  
    20  func Example_biggest() {
    21  	parameters := gopter.DefaultTestParameters()
    22  	parameters.Rng.Seed(1234) // Just for this example to generate reproducible results
    23  
    24  	properties := gopter.NewProperties(parameters)
    25  	properties.Property("Non-zero length small int slice", prop.ForAll(
    26  		func(ns []int) bool {
    27  			result, _ := wrong_biggest(ns)
    28  			sort.Slice(ns, func(i, j int) bool {
    29  				return ns[i] > ns[j]
    30  			})
    31  			return result == ns[0]
    32  		},
    33  		gen.SliceOf(gen.IntRange(0, 20),
    34  			reflect.TypeOf(int(0))).
    35  			SuchThat(func(v interface{}) bool {
    36  				return len(v.([]int)) > 0
    37  			}),
    38  	))
    39  	// When using testing.T you might just use: properties.TestingRun(t)
    40  	properties.Run(gopter.ConsoleReporter(false))
    41  
    42  	// Output:
    43  	// ! Non-zero length small int slice: Falsified after 1 passed tests.
    44  	// ARG_0: [0 5 0]
    45  	// ARG_0_ORIGINAL (1 shrinks): [0 7 5]
    46  }