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

     1  package gen_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/leanovate/gopter"
     7  )
     8  
     9  func commonGeneratorTest(t *testing.T, name string, gen gopter.Gen, valueCheck func(interface{}) bool) {
    10  	for i := 0; i < 100; i++ {
    11  		value, ok := gen.Sample()
    12  
    13  		if !ok || value == nil {
    14  			t.Errorf("Invalid generator result (%s): %#v", name, value)
    15  		} else if !valueCheck(value) {
    16  			t.Errorf("Invalid value (%s): %#v", name, value)
    17  		}
    18  
    19  		genResult := gen(gopter.DefaultGenParameters())
    20  		if genResult.Shrinker != nil {
    21  			value, ok := genResult.Retrieve()
    22  			if !ok || value == nil {
    23  				t.Errorf("Invalid generator result (%s): %#v", name, value)
    24  			} else {
    25  				shrink := genResult.Shrinker(value).Filter(genResult.Sieve)
    26  				shrunkValue, ok := shrink()
    27  				if ok && !valueCheck(shrunkValue) {
    28  					t.Errorf("Invalid shrunk value (%s): %#v -> %#v", name, value, shrunkValue)
    29  				}
    30  			}
    31  		}
    32  	}
    33  }