gory

package module
v0.0.0-...-1a22577 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2018 License: MIT Imports: 3 Imported by: 1

README

gory

Build Status Coverage Status GoDoc

Factories for your Go structs. Think factory_girl.

Usage

To install, just go get it:

go get github.com/modocache/gory

gory is fully documented, but below are some examples to get you started.

Defining Factories

Define factories that may be used during any test. Works great in a global setup hook.

gory.Define("user", User{}, func(factory gory.Factory) {
    factory["FirstName"] = "John"
    factory["LastName"] = "Doe"
    factory["Admin"] = false

    // 'n' in email is incremented each time the factory is built
    factory["Email"] = gory.Sequence(func(n int) interface{} {
        return fmt.Sprintf("john-doe-%d@example.com", n)
    })

    // time.Now() is evaluated when the factory is built
    factory["Created"] = gory.Lazy(func() interface{} {
        return time.Now()
    })
})

See gory_suite_test.go for more examples of defining factories.

Using Factories
john := gory.Build("user").(*User)
fmt.Println(john.FirstName) // "John"

jane := gory.BuildWithParams("user", gory.Factory{
    "FirstName": "Jane"
}).(*User)
fmt.Println(jane.FirstName) // "Jane"

See gory_test.go for more examples of using factories.

Coming Soon

Documentation

Overview

gory is a fixtures replacement with a straightforward definition syntax. It's to Go what thoughtbot/factory_girl is to Ruby.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Build

func Build(definitionName string) interface{}

Returns an instance of the struct defined using the definitionName parameter and the Define() function. If no matching definition exists, this function panics. It also panics if an attempt to set an invalid field was made from within the Define() function.

Example:

person := gory.Build("person").(*Person)
fmt.Println(person.FirstName) // "Jane"

func BuildWithParams

func BuildWithParams(definitionName string, params Factory) interface{}

Returns an instance of the struct defined by definitionName, but using the specified parameters instead of the defined factory's values. If names of fields that don't exist or are not exported are specified, the function panics.

Example:

person := gory.BuildWithParams("person", Person{}, {
	"Name": "John"
}).(*Person)
fmt.Println(person.Name) // "John" instead of Factory value

func Define

func Define(definitionName string, instance interface{}, builder FactoryBuilder)

Define a factory. Factories can be retrieved later by using the definitionName parameter. You cannot define two factories with identical names.

The instance parameter must be a struct literal of the type you'd like the factory to return when building.

The builder parameter is a function executed when defining factories. Use the factory parameter as a map to store values for your struct fields. If this is nil, no fields on the returned struct are set.

func Lazy

func Lazy(callback func() interface{}) next

Used to set a lazily evaluated value on a Factory.

Example:

factory["Created"] = gory.Lazy(func() interface{} {
    return time.Now()
})

In the above example, time.Now() is not evaluated until the Factory is built, using the Build() function.

func Sequence

func Sequence(sequencer Sequencer) next

Used to set an sequenced value on a Factory.

Example:

factory["Email"] = gory.Sequence(func(n int) interface{} {
    return fmt.Sprintf("john-doe-%d@example.com", n)
})

Each time Build() is called, the sequenced value will use an incremented value for n.

Types

type Factory

type Factory map[string]interface{}

A Factory maps field names to values.

type FactoryBuilder

type FactoryBuilder func(factory Factory)

A function executed when defining factories. Use the factory parameter as a map to store values for your struct fields.

Example:

gory.Define("person", Person{}, func(factory gory.Factory) {
	factory["FirstName"] = "Jane"
	factory["LastName"] = "Doe"
	factory["Admin"] = false
})

Later, when you build the object, the FirstName, LastName, and Admin fields will be set to the parameters you specified. If you attempt to set fields that don't exist or are not exported, the Build() function will panic once called.

type Sequencer

type Sequencer func(n int) interface{}

A function that returns a value to be set on a Factory. n is incremented each time the Factory is built.

var IntSequencer Sequencer = func(n int) interface{} {
	return n
}

A Sequencer that simply returns the int value of n. In other words, the first time the factory is built, it will return 0, then 1, then 2, and so on.

Example:

factory["NumberOfChildren"] = gory.Sequence(gory.IntSequencer)

Jump to

Keyboard shortcuts

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