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

     1  package arbitrary_test
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/leanovate/gopter"
     8  	"github.com/leanovate/gopter/arbitrary"
     9  )
    10  
    11  func commonGeneratorTest(t *testing.T, name string, gen gopter.Gen, valueCheck func(interface{}) bool) {
    12  	for i := 0; i < 100; i++ {
    13  		value, ok := gen.Sample()
    14  
    15  		if !ok || value == nil {
    16  			t.Errorf("Invalid generator result (%s): %#v", name, value)
    17  		} else if !valueCheck(value) {
    18  			t.Errorf("Invalid value (%s): %#v", name, value)
    19  		}
    20  
    21  		genResult := gen(gopter.DefaultGenParameters())
    22  		if genResult.Shrinker != nil {
    23  			value, ok := genResult.Retrieve()
    24  			if !ok || value == nil {
    25  				t.Errorf("Invalid generator result (%s): %#v", name, value)
    26  			} else {
    27  				shrink := genResult.Shrinker(value).Filter(genResult.Sieve)
    28  				shrunkValue, ok := shrink()
    29  				if ok && !valueCheck(shrunkValue) {
    30  					t.Errorf("Invalid shrunk value (%s): %#v -> %#v", name, value, shrunkValue)
    31  				}
    32  			}
    33  		}
    34  	}
    35  }
    36  
    37  func TestArbitrariesSimple(t *testing.T) {
    38  	arbitraries := arbitrary.DefaultArbitraries()
    39  
    40  	gen := arbitraries.GenForType(reflect.TypeOf(true))
    41  	commonGeneratorTest(t, "bool", gen, func(value interface{}) bool {
    42  		_, ok := value.(bool)
    43  		return ok
    44  	})
    45  
    46  	gen = arbitraries.GenForType(reflect.TypeOf(0))
    47  	commonGeneratorTest(t, "int", gen, func(value interface{}) bool {
    48  		_, ok := value.(int)
    49  		return ok
    50  	})
    51  
    52  	gen = arbitraries.GenForType(reflect.TypeOf(uint(0)))
    53  	commonGeneratorTest(t, "uint", gen, func(value interface{}) bool {
    54  		_, ok := value.(uint)
    55  		return ok
    56  	})
    57  
    58  	gen = arbitraries.GenForType(reflect.TypeOf(int8(0)))
    59  	commonGeneratorTest(t, "int8", gen, func(value interface{}) bool {
    60  		_, ok := value.(int8)
    61  		return ok
    62  	})
    63  
    64  	gen = arbitraries.GenForType(reflect.TypeOf(uint8(0)))
    65  	commonGeneratorTest(t, "uint8", gen, func(value interface{}) bool {
    66  		_, ok := value.(uint8)
    67  		return ok
    68  	})
    69  
    70  	gen = arbitraries.GenForType(reflect.TypeOf(int16(0)))
    71  	commonGeneratorTest(t, "int16", gen, func(value interface{}) bool {
    72  		_, ok := value.(int16)
    73  		return ok
    74  	})
    75  
    76  	gen = arbitraries.GenForType(reflect.TypeOf(uint16(0)))
    77  	commonGeneratorTest(t, "uint16", gen, func(value interface{}) bool {
    78  		_, ok := value.(uint16)
    79  		return ok
    80  	})
    81  
    82  	gen = arbitraries.GenForType(reflect.TypeOf(int32(0)))
    83  	commonGeneratorTest(t, "int32", gen, func(value interface{}) bool {
    84  		_, ok := value.(int32)
    85  		return ok
    86  	})
    87  
    88  	gen = arbitraries.GenForType(reflect.TypeOf(uint32(0)))
    89  	commonGeneratorTest(t, "uint32", gen, func(value interface{}) bool {
    90  		_, ok := value.(uint32)
    91  		return ok
    92  	})
    93  
    94  	gen = arbitraries.GenForType(reflect.TypeOf(int64(0)))
    95  	commonGeneratorTest(t, "int64", gen, func(value interface{}) bool {
    96  		_, ok := value.(int64)
    97  		return ok
    98  	})
    99  
   100  	gen = arbitraries.GenForType(reflect.TypeOf(uint64(0)))
   101  	commonGeneratorTest(t, "uint64", gen, func(value interface{}) bool {
   102  		_, ok := value.(uint64)
   103  		return ok
   104  	})
   105  
   106  	gen = arbitraries.GenForType(reflect.TypeOf(float32(0)))
   107  	commonGeneratorTest(t, "float32", gen, func(value interface{}) bool {
   108  		_, ok := value.(float32)
   109  		return ok
   110  	})
   111  
   112  	gen = arbitraries.GenForType(reflect.TypeOf(float64(0)))
   113  	commonGeneratorTest(t, "float64", gen, func(value interface{}) bool {
   114  		_, ok := value.(float64)
   115  		return ok
   116  	})
   117  
   118  	gen = arbitraries.GenForType(reflect.TypeOf(complex128(0)))
   119  	commonGeneratorTest(t, "complex128", gen, func(value interface{}) bool {
   120  		_, ok := value.(complex128)
   121  		return ok
   122  	})
   123  
   124  	gen = arbitraries.GenForType(reflect.TypeOf(complex64(0)))
   125  	commonGeneratorTest(t, "complex64", gen, func(value interface{}) bool {
   126  		_, ok := value.(complex64)
   127  		return ok
   128  	})
   129  
   130  	gen = arbitraries.GenForType(reflect.TypeOf(""))
   131  	commonGeneratorTest(t, "string", gen, func(value interface{}) bool {
   132  		_, ok := value.(string)
   133  		return ok
   134  	})
   135  }