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

     1  package gen_test
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/leanovate/gopter"
     8  	"github.com/leanovate/gopter/gen"
     9  )
    10  
    11  type testStruct struct {
    12  	Value1 string
    13  	Value2 int64
    14  	Value3 []int8
    15  	Value4 string
    16  	Value5 *string
    17  	Value6 interface{}
    18  	value7 string
    19  }
    20  
    21  func TestStruct(t *testing.T) {
    22  	structGen := gen.Struct(reflect.TypeOf(&testStruct{}), map[string]gopter.Gen{
    23  		"Value1":   gen.Identifier(),
    24  		"Value2":   gen.Int64(),
    25  		"Value3":   gen.SliceOf(gen.Int8()),
    26  		"NotThere": gen.AnyString(),
    27  		"Value5":   gen.PtrOf(gen.Const("v5")),
    28  		"Value6":   gen.AnyString(),
    29  		"value7":   gen.AnyString(),
    30  	})
    31  	for i := 0; i < 100; i++ {
    32  		value, ok := structGen.Sample()
    33  
    34  		if !ok {
    35  			t.Errorf("Invalid value: %#v", value)
    36  		}
    37  		v, ok := value.(testStruct)
    38  		if !ok || v.Value1 == "" || v.Value3 == nil || v.Value4 != "" || !(v.Value5 == nil || *v.Value5 == "v5") {
    39  			t.Errorf("Invalid value: %#v", value)
    40  		}
    41  	}
    42  }
    43  
    44  func TestStructWithDifferentValueTypesInSameField(t *testing.T) {
    45  	structGen := gen.Struct(reflect.TypeOf(&testStruct{}), map[string]gopter.Gen{
    46  		"Value6": gen.OneGenOf(gen.AnyString(), gen.Int()),
    47  	})
    48  	for i := 0; i < 100; i++ {
    49  		value, ok := structGen.Sample()
    50  
    51  		if !ok {
    52  			t.Errorf("Invalid value: %#v", value)
    53  		}
    54  	}
    55  }
    56  
    57  func TestStructDeterminism(t *testing.T) {
    58  	structGen := gen.Struct(reflect.TypeOf(&testStruct{}), map[string]gopter.Gen{
    59  		"Value1": gen.Identifier(),
    60  		"Value2": gen.Int64(),
    61  		"Value3": gen.SliceOf(gen.Int8()),
    62  	})
    63  	for i := 0; i < 100; i++ {
    64  		parameters := gopter.DefaultGenParameters().CloneWithSeed(1234)
    65  		for _, expected := range []testStruct{
    66  			{
    67  				Value1: "hUeNzDbtiF4xxkidfvLaiczgpwsqfyvbbuhrjjoez4jtewulIKwzMguttazo3qwi5ufIfi6izpqT4evzrmgtmk1gQo",
    68  				Value2: -2282921689139609493,
    69  				Value3: []int8{-93, -96, -23, -58, 65, -108, 56, 63, -64, 26, -69, 62, 61, -93, -107, 52, -95},
    70  			},
    71  			{
    72  				Value1: "ubJrJEawwnoh63jv1lxd7xhtaqqrEnjawudgiixhhkw6sdmqdgxbabyoxcoE0uviwDupccvYvxcqOv0z8opjk",
    73  				Value2: -1611599231975617329,
    74  				Value3: []int8{15, -41, -106, 37, 3, 76, -65, -87, 113, -115, 76, 61, 41, 65, 11, -90, -4, 43, 110, -121, 65, 112, -128, 51, -86, 50, 30, 33, -73, -88, 94, 101, 63, -113, 45, 110, 46, 21, 115, 78, -58, 47, -110, 7, -14, -18, 2, -26, 63, -33, 77, 82, -52, -57, -105},
    75  			},
    76  			{
    77  				Value1: "axnbggD6Hgsxyxd6ZwcZ4Bn1uM7hzd0azvsuLvj3wvfvoramjcltivmditt5qhmHYfn0egagcFpuAffzaWxvalEaniojczez",
    78  				Value2: -345052727922296584,
    79  				Value3: []int8{-61, 94, 67, 9, 39, 119, 23, 1, 57, -66, 57, -94, 38, -122, 16, 82, -119, 21, -74, -66, -111, 55, -96, 8, -79, 13, -41, 124, 71, -63, 56, 16, 62, 55, -13, -35, -27, 68, -82, 22, -63, -76, 96, 60, -89, -10, -65, -102, -97, 45, 124, 117, -37, 21, 58, -87, 116, 60, -111, 27, 102, -102, -81, -123, -86, -95},
    80  			},
    81  			{
    82  				Value1: "lr",
    83  				Value2: -6442088894944465291,
    84  				Value3: []int8{-20, 38, 25, -76, -110, -98, -61, 65, -14, 52, -47, 22, -90},
    85  			},
    86  			{
    87  				Value1: "un23mggozHs4txZtydz6mIBymnxjxklkjyNzf",
    88  				Value2: -26686468742269553,
    89  				Value3: []int8{78, 91, -22, -126, -93, 35, -14, 67, -97, 13, -25, 73, -111, 26, 14, -67, 50, -23, -15, -63, -40, -103, 126, 60, -63, -83, -126, 64, 52, -50, 86, -25, -1, 108, 7, 62, 79, 89, 45, -73, 52, -7, -85, -111, -120, -21, 116, -8, -22, 34, 85, 36, 124, 12, -111, -114, -115, 91, -94, 82, -3, -46, 94, -73, 62, -117, -7, 84, -94, 13, 71, -5, 21, 32, 106, -44, 46},
    90  			},
    91  		} {
    92  			value, ok := structGen(parameters).Retrieve()
    93  
    94  			if !ok {
    95  				t.Errorf("Invalid value: %#v", value)
    96  			}
    97  			v, ok := value.(testStruct)
    98  			if !reflect.DeepEqual(expected, v) {
    99  				t.Errorf("Invalid value: %#v; expected: %#v", v, expected)
   100  			}
   101  		}
   102  	}
   103  }
   104  
   105  func TestStructPropageEmpty(t *testing.T) {
   106  	fail := gen.Struct(reflect.TypeOf(&testStruct{}), map[string]gopter.Gen{
   107  		"Value1": gen.Identifier().SuchThat(func(str string) bool {
   108  			return false
   109  		}),
   110  	})
   111  
   112  	if _, ok := fail.Sample(); ok {
   113  		t.Errorf("Failing field generator in Struct generated a value")
   114  	}
   115  }
   116  
   117  func TestStructNoStruct(t *testing.T) {
   118  	fail := gen.Struct(reflect.TypeOf(""), map[string]gopter.Gen{})
   119  
   120  	if _, ok := fail.Sample(); ok {
   121  		t.Errorf("Invalid Struct generated a value")
   122  	}
   123  }
   124  
   125  func TestStructPtr(t *testing.T) {
   126  	structGen := gen.StructPtr(reflect.TypeOf(&testStruct{}), map[string]gopter.Gen{
   127  		"Value1":   gen.Identifier(),
   128  		"Value2":   gen.Int64(),
   129  		"Value3":   gen.SliceOf(gen.Int8()),
   130  		"NotThere": gen.AnyString(),
   131  	})
   132  	for i := 0; i < 100; i++ {
   133  		value, ok := structGen.Sample()
   134  
   135  		if !ok || value == nil {
   136  			t.Errorf("Invalid value: %#v", value)
   137  		}
   138  		v, ok := value.(*testStruct)
   139  		if !ok || v.Value1 == "" || v.Value3 == nil || v.Value4 != "" {
   140  			t.Errorf("Invalid value: %#v", value)
   141  		}
   142  	}
   143  }
   144  
   145  func TestStructPtrPropageEmpty(t *testing.T) {
   146  	fail := gen.StructPtr(reflect.TypeOf(&testStruct{}), map[string]gopter.Gen{
   147  		"Value1": gen.Identifier().SuchThat(func(str string) bool {
   148  			return false
   149  		}),
   150  	})
   151  
   152  	if _, ok := fail.Sample(); ok {
   153  		t.Errorf("Failing field generator in StructPtr generated a value")
   154  	}
   155  }
   156  
   157  func TestStructPtrNoStruct(t *testing.T) {
   158  	fail := gen.StructPtr(reflect.TypeOf(""), map[string]gopter.Gen{})
   159  
   160  	if _, ok := fail.Sample(); ok {
   161  		t.Errorf("Invalid StructPtr generated a value")
   162  	}
   163  }