github.com/leanovate/gopter@v0.2.9/gen/slice_of_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  func TestSliceOf(t *testing.T) {
    12  	genParams := gopter.DefaultGenParameters()
    13  	genParams.MaxSize = 50
    14  	elementGen := gen.Const("element")
    15  	sliceGen := gen.SliceOf(elementGen)
    16  
    17  	for i := 0; i < 100; i++ {
    18  		sample, ok := sliceGen(genParams).Retrieve()
    19  
    20  		if !ok {
    21  			t.Error("Sample was not ok")
    22  		}
    23  		strings, ok := sample.([]string)
    24  		if !ok {
    25  			t.Errorf("Sample not slice of string: %#v", sample)
    26  		} else {
    27  			if len(strings) > 50 {
    28  				t.Errorf("Sample has invalid length: %#v", len(strings))
    29  			}
    30  			for _, str := range strings {
    31  				if str != "element" {
    32  					t.Errorf("Sample contains invalid value: %#v", sample)
    33  				}
    34  			}
    35  		}
    36  	}
    37  
    38  	genParams.MinSize = 10
    39  
    40  	for i := 0; i < 100; i++ {
    41  		sample, ok := sliceGen(genParams).Retrieve()
    42  
    43  		if !ok {
    44  			t.Error("Sample was not ok")
    45  		}
    46  		strings, ok := sample.([]string)
    47  		if !ok {
    48  			t.Errorf("Sample not slice of string: %#v", sample)
    49  		} else {
    50  			if len(strings) > 50 || len(strings) < 10 {
    51  				t.Errorf("Sample has invalid length: %#v", len(strings))
    52  			}
    53  			for _, str := range strings {
    54  				if str != "element" {
    55  					t.Errorf("Sample contains invalid value: %#v", sample)
    56  				}
    57  			}
    58  		}
    59  	}
    60  
    61  	genParams.MaxSize = 10
    62  
    63  	for i := 0; i < 100; i++ {
    64  		sample, ok := sliceGen(genParams).Retrieve()
    65  
    66  		if !ok {
    67  			t.Error("Sample was not ok")
    68  		}
    69  		strings, ok := sample.([]string)
    70  		if !ok {
    71  			t.Errorf("Sample not slice of string: %#v", sample)
    72  		} else {
    73  			if len(strings) != 10 {
    74  				t.Errorf("Sample has invalid length: %#v", len(strings))
    75  			}
    76  			for _, str := range strings {
    77  				if str != "element" {
    78  					t.Errorf("Sample contains invalid value: %#v", sample)
    79  				}
    80  			}
    81  		}
    82  	}
    83  
    84  	genParams.MaxSize = 0
    85  	genParams.MinSize = 0
    86  
    87  	for i := 0; i < 100; i++ {
    88  		sample, ok := sliceGen(genParams).Retrieve()
    89  
    90  		if !ok {
    91  			t.Error("Sample was not ok")
    92  		}
    93  		strings, ok := sample.([]string)
    94  		if !ok {
    95  			t.Errorf("Sample not slice of string: %#v", sample)
    96  		} else {
    97  			if len(strings) != 0 {
    98  				t.Errorf("Sample has invalid length: %#v", len(strings))
    99  			}
   100  		}
   101  	}
   102  }
   103  
   104  func TestSliceOfPanic(t *testing.T) {
   105  	genParams := gopter.DefaultGenParameters()
   106  	genParams.MaxSize = 0
   107  	genParams.MinSize = 1
   108  	elementGen := gen.Const("element")
   109  	sliceGen := gen.SliceOf(elementGen)
   110  
   111  	defer func() {
   112  		if r := recover(); r == nil {
   113  			t.Error("SliceOf did not panic when MinSize was > MaxSize")
   114  		}
   115  	}()
   116  
   117  	sliceGen(genParams).Retrieve()
   118  }
   119  
   120  func TestSliceOfN(t *testing.T) {
   121  	elementGen := gen.Const("element")
   122  	sliceGen := gen.SliceOfN(10, elementGen)
   123  
   124  	for i := 0; i < 100; i++ {
   125  		sample, ok := sliceGen.Sample()
   126  
   127  		if !ok {
   128  			t.Error("Sample was not ok")
   129  		}
   130  		strings, ok := sample.([]string)
   131  		if !ok {
   132  			t.Errorf("Sample not slice of string: %#v", sample)
   133  		} else {
   134  			if len(strings) != 10 {
   135  				t.Errorf("Sample has invalid length: %#v", len(strings))
   136  			}
   137  			for _, str := range strings {
   138  				if str != "element" {
   139  					t.Errorf("Sample contains invalid value: %#v", sample)
   140  				}
   141  			}
   142  		}
   143  	}
   144  }
   145  
   146  func TestSliceOfNSieve(t *testing.T) {
   147  	var called int
   148  	elementSieve := func(v interface{}) bool {
   149  		called++
   150  		return v == "element"
   151  	}
   152  	elementGen := gen.Const("element").SuchThat(elementSieve)
   153  	sliceGen := gen.SliceOfN(10, elementGen)
   154  	result := sliceGen(gopter.DefaultGenParameters())
   155  	value, ok := result.Retrieve()
   156  	if !ok || value == nil {
   157  		t.Errorf("Invalid value: %#v", value)
   158  	}
   159  	strs, ok := value.([]string)
   160  	if !ok || len(strs) != 10 {
   161  		t.Errorf("Invalid value: %#v", value)
   162  	}
   163  	if called != 20 {
   164  		t.Errorf("Invalid called: %d", called)
   165  	}
   166  	if result.Sieve(strs[0:9]) {
   167  		t.Error("Sieve must not allow array len < 10")
   168  	}
   169  	strs[0] = "bla"
   170  	if result.Sieve(strs) {
   171  		t.Error("Sieve must not allow array with invalid element")
   172  	}
   173  }
   174  
   175  func TestSliceOfOverride(t *testing.T) {
   176  	genParams := gopter.DefaultGenParameters()
   177  	genParams.MaxSize = 50
   178  	sliceType := reflect.TypeOf((*baseType)(nil)).Elem()
   179  	sliceGen := gen.SliceOf(gen.OneGenOf(genA(), genB()), sliceType)
   180  	result := sliceGen(gopter.DefaultGenParameters())
   181  	value, ok := result.Retrieve()
   182  	if !ok || value == nil {
   183  		t.Errorf("Invalid value: %#v", value)
   184  	}
   185  	_, okType := value.([]baseType)
   186  	if !okType {
   187  		t.Errorf("Invalid type: %#v", value)
   188  	}
   189  }
   190  
   191  type baseType interface {
   192  	String() string
   193  }
   194  
   195  type specA struct{}
   196  type specB struct{}
   197  
   198  func (a specA) String() string { return "specA" }
   199  func (b specB) String() string { return "specB" }
   200  func genA() gopter.Gen         { return gen.Const(specA{}) }
   201  func genB() gopter.Gen         { return gen.Const(specB{}) }