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

     1  package arbitrary_test
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  	"unicode"
     7  
     8  	"github.com/leanovate/gopter/arbitrary"
     9  	"github.com/leanovate/gopter/gen"
    10  )
    11  
    12  type DemoStruct struct {
    13  	Value1 int64
    14  	Value2 string
    15  	Value3 []uint
    16  	Value4 int32
    17  }
    18  
    19  func TestArbitrariesStructs(t *testing.T) {
    20  	arbitraries := arbitrary.DefaultArbitraries()
    21  
    22  	arbitraries.RegisterGen(gen.Int64Range(10, 100))
    23  	arbitraries.RegisterGen(gen.Int32Range(1, 10))
    24  	arbitraries.RegisterGen(gen.Const([]uint{1, 2, 3}))
    25  	arbitraries.RegisterGen(gen.AlphaString())
    26  
    27  	gen := arbitraries.GenForType(reflect.TypeOf(&DemoStruct{}))
    28  	for i := 0; i < 100; i++ {
    29  		raw, ok := gen.Sample()
    30  		if !ok {
    31  			t.Errorf("Invalid value: %#v", raw)
    32  		}
    33  		value, ok := raw.(*DemoStruct)
    34  		if !ok {
    35  			t.Errorf("Invalid value: %#v", raw)
    36  		}
    37  		if value.Value1 < 10 || value.Value1 > 100 {
    38  			t.Errorf("Invalid value.Value1 out of bounds: %#v", raw)
    39  		}
    40  		for _, ch := range value.Value2 {
    41  			if !unicode.IsLetter(ch) {
    42  				t.Errorf("Invalid value.Value2: %#v", raw)
    43  			}
    44  		}
    45  		if !reflect.DeepEqual(value.Value3, []uint{1, 2, 3}) {
    46  			t.Errorf("Invalid value.Value3: %#v", raw)
    47  		}
    48  		if value.Value4 < 1 || value.Value4 > 10 {
    49  			t.Errorf("Invalid value.Value4 out of bounds: %#v", raw)
    50  		}
    51  	}
    52  }