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

     1  package gen_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/leanovate/gopter"
     7  	"github.com/leanovate/gopter/gen"
     8  )
     9  
    10  func TestMapOf(t *testing.T) {
    11  	genParams := gopter.DefaultGenParameters()
    12  	genParams.MaxSize = 50
    13  	keyGen := gen.Identifier()
    14  	elementGen := gen.Const("element")
    15  	mapGen := gen.MapOf(keyGen, elementGen)
    16  
    17  	for i := 0; i < 100; i++ {
    18  		sample, ok := mapGen(genParams).Retrieve()
    19  
    20  		if !ok {
    21  			t.Error("Sample was not ok")
    22  		}
    23  		strings, ok := sample.(map[string]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 _, value := range strings {
    31  				if value != "element" {
    32  					t.Errorf("Sample contains invalid value: %#v", sample)
    33  				}
    34  			}
    35  		}
    36  	}
    37  
    38  	genParams.MaxSize = 10
    39  
    40  	for i := 0; i < 100; i++ {
    41  		sample, ok := mapGen(genParams).Retrieve()
    42  
    43  		if !ok {
    44  			t.Error("Sample was not ok")
    45  		}
    46  		strings, ok := sample.(map[string]string)
    47  		if !ok {
    48  			t.Errorf("Sample not slice of string: %#v", sample)
    49  		} else {
    50  			if len(strings) > 10 {
    51  				t.Errorf("Sample has invalid length: %#v", len(strings))
    52  			}
    53  			for _, value := range strings {
    54  				if value != "element" {
    55  					t.Errorf("Sample contains invalid value: %#v", sample)
    56  				}
    57  			}
    58  		}
    59  	}
    60  
    61  	genParams.MaxSize = 0
    62  	genParams.MinSize = 0
    63  
    64  	for i := 0; i < 100; i++ {
    65  		sample, ok := mapGen(genParams).Retrieve()
    66  
    67  		if !ok {
    68  			t.Error("Sample was not ok")
    69  		}
    70  		strings, ok := sample.(map[string]string)
    71  		if !ok {
    72  			t.Errorf("Sample not slice of string: %#v", sample)
    73  		} else {
    74  			if len(strings) != 0 {
    75  				t.Errorf("Sample has invalid length: %#v", len(strings))
    76  			}
    77  		}
    78  	}
    79  }
    80  
    81  func TestMapOfPanic(t *testing.T) {
    82  	genParams := gopter.DefaultGenParameters()
    83  	genParams.MaxSize = 0
    84  	genParams.MinSize = 1
    85  	keyGen := gen.Identifier()
    86  	elementGen := gen.Const("element")
    87  	mapGen := gen.MapOf(keyGen, elementGen)
    88  
    89  	defer func() {
    90  		if r := recover(); r == nil {
    91  			t.Error("SliceOf did not panic when MinSize was > MaxSize")
    92  		}
    93  	}()
    94  
    95  	mapGen(genParams).Retrieve()
    96  }