structmommy

package module
v0.0.0-...-c64e652 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 1, 2014 License: MIT Imports: 8 Imported by: 0

README

Struct Mommy

GoDoc

Documentation

Overview

Package structmommy implement object generator for test purposes

Call Make to fill in your struct with random data. Use PostMake functions to define some of fields (Define), save struct to DB or whatever you please.

ToDo: Let generate multiple objects at once!

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Define

func Define(keyValue ...interface{}) func(interface{}) error

Define is post Make function. Lets define values of struct fields

Example
obj := struct {
	FieldA float32
	FieldB uint8
}{}

structmommy.SetSeed(26)
structmommy.Make(&obj, structmommy.Define("FieldA", 2.0))

fmt.Println("FieldA:", obj.FieldA)
fmt.Println("FieldB:", obj.FieldB)
Output:

FieldA: 2
FieldB: 42

func Make

func Make(obj interface{}, postFuncs ...func(interface{}) error) (err error)

Make fills in given object with radom data. Afterwords, if specified, PostMake functions are applied in order on the object. If any function will return no nil error, no other function is execute and error is returned

Example
obj := struct {
	FieldA float32
	FieldB uint8
}{}

structmommy.SetSeed(26)
structmommy.Make(&obj)

fmt.Println("FieldA:", obj.FieldA)
fmt.Println("FieldB:", obj.FieldB)
Output:

FieldA: -1.5426473e+38
FieldB: 42

func PartialEqual

func PartialEqual(obj interface{}, keyValue ...interface{}) (err error)

Checks values of subset of fields in a given struct. Accepts both object or it's refference.

Example (NotEqual)
obj := struct {
	FieldA float32
	FieldB uint8
}{
	FieldA: 1.2,
	FieldB: 42,
}

err := structmommy.PartialEqual(obj, "FieldA", 123)
if err != nil {
	fmt.Println("Error:", err)
} else {
	fmt.Println("All fields as expected!")
}

fmt.Println("FieldA:", obj.FieldA)
fmt.Println("FieldB:", obj.FieldB)
Output:

Error: value of field 'FieldA' equals 1.2, expected 123
FieldA: 1.2
FieldB: 42
Example (Ok)
obj := struct {
	FieldA float32
	FieldB uint8
}{
	FieldA: 1.2,
	FieldB: 42,
}

err := structmommy.PartialEqual(obj, "FieldA", 1.2, "FieldB", 42)
if err != nil {
	fmt.Println("Error:", err)
} else {
	fmt.Println("All fields as expected!")
}

fmt.Println("FieldA:", obj.FieldA)
fmt.Println("FieldB:", obj.FieldB)
Output:

All fields as expected!
FieldA: 1.2
FieldB: 42

func SetSeed

func SetSeed(seed int64)

SetSeed let you set/reset seed on rand object used to generate data.

Using it is completely optional as on init seed is set to current timesstamp (UnixNano).

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL