github.com/leanovate/gopter@v0.2.9/gen/one_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 TestOneConstOf(t *testing.T) {
    12  	consts := gen.OneConstOf("one", "two", "three", "four")
    13  	commonOneOfTest(t, consts)
    14  
    15  	fail := gen.OneConstOf()
    16  	if _, ok := fail.Sample(); ok {
    17  		t.Errorf("Empty OneConstOf generated a value")
    18  	}
    19  }
    20  
    21  func TestOneGenOf(t *testing.T) {
    22  	consts := gen.OneGenOf(gen.Const("one"), gen.Const("two"), gen.Const("three"), gen.Const("four"))
    23  	commonOneOfTest(t, consts)
    24  
    25  	fail := gen.OneGenOf()
    26  	if _, ok := fail.Sample(); ok {
    27  		t.Errorf("Empty OneGenOf generated a value")
    28  	}
    29  }
    30  
    31  func commonOneOfTest(t *testing.T, gen gopter.Gen) {
    32  	generated := make(map[string]bool, 0)
    33  	for i := 0; i < 100; i++ {
    34  		value, ok := gen.Sample()
    35  
    36  		if !ok || value == nil {
    37  			t.Errorf("Invalid consts: %#v", value)
    38  		}
    39  		v, ok := value.(string)
    40  		if !ok {
    41  			t.Errorf("Invalid consts: %#v", value)
    42  		}
    43  		generated[v] = true
    44  	}
    45  	if !reflect.DeepEqual(generated, map[string]bool{
    46  		"one":   true,
    47  		"two":   true,
    48  		"three": true,
    49  		"four":  true,
    50  	}) {
    51  		t.Errorf("Not all consts where generated: %#v", generated)
    52  	}
    53  }