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

     1  package arbitrary
     2  
     3  import (
     4  	"reflect"
     5  	"time"
     6  
     7  	"github.com/leanovate/gopter"
     8  	"github.com/leanovate/gopter/gen"
     9  )
    10  
    11  // Arbitraries defines a context to generate arbitrary values of any kind.
    12  // Values are generated by either providing a generator for a specific type
    13  // or by creating a generator on the fly using golang reflection.
    14  type Arbitraries struct {
    15  	generators map[reflect.Type]gopter.Gen
    16  }
    17  
    18  // DefaultArbitraries creates a default arbitrary context with the widest
    19  // possible ranges for all types.
    20  func DefaultArbitraries() *Arbitraries {
    21  	return &Arbitraries{
    22  		generators: map[reflect.Type]gopter.Gen{
    23  			reflect.TypeOf(time.Time{}):  gen.Time(),
    24  			reflect.TypeOf(&time.Time{}): gen.PtrOf(gen.Time()),
    25  		},
    26  	}
    27  }
    28  
    29  // GenForType gets a generator for a generator for a type
    30  func (a *Arbitraries) GenForType(rt reflect.Type) gopter.Gen {
    31  	if gen, ok := a.generators[rt]; ok {
    32  		return gen
    33  	}
    34  	return a.genForKind(rt)
    35  }
    36  
    37  // RegisterGen registers a generator
    38  func (a *Arbitraries) RegisterGen(gen gopter.Gen) {
    39  	result := gen(gopter.MinGenParams)
    40  	rt := result.ResultType
    41  	a.generators[rt] = gen
    42  }