factory

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2023 License: ISC Imports: 7 Imported by: 0

README

Go Doc

Factory

This is a helper package for testing purposes. It offers a set of functions for generating random values for standard built-in data types. It can also generate random values for user-defined structs through reflection.

Quick Start

package main

import (
  "fmt"

  "github.com/gardenbed/basil/factory"
)

func main() {
  name := factory.Name()
  email := factory.Email()
  fmt.Printf("%s <%s>\n", name, email)
}
package main

import (
  "fmt"
  "log"
  "net/url"
  "time"

  "github.com/gardenbed/basil/factory"
)

{
  object := struct {
    String     string
    Bool       bool
    Int        int
    Uint       uint
    Float64    float64
    Complex128 complex128
    Nested     struct {
      Duration time.Duration
      Time     *time.Time
      URL      *url.URL
    }
  }{}

  if err := factory.Populate(&object, false); err != nil {
    log.Fatalf("populate error: %s", err)
  }

  fmt.Printf("%+v\n", object)
}

Documentation

Overview

Package factory is used for generating random values for testing purposes.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bool

func Bool() bool

Bool generates a random bool value.

func BoolPtr

func BoolPtr() *bool

BoolPtr generates a random bool value and returns the pointer to it.

func BoolSlice

func BoolSlice() []bool

BoolSlice generates a random bool slice.

func Byte

func Byte() byte

Byte generates a random byte value.

func BytePtr

func BytePtr() *byte

BytePtr generates a random byte value and returns the pointer to it.

func ByteSlice

func ByteSlice() []byte

ByteSlice generates a random byte slice.

func Complex128

func Complex128() complex128

Complex128 generates a random complex128 value.

func Complex128Ptr

func Complex128Ptr() *complex128

Complex128Ptr generates a random complex128 value and returns the pointer to it.

func Complex128Slice

func Complex128Slice() []complex128

Complex128Slice generates a random complex128 slice.

func Complex64

func Complex64() complex64

Complex64 generates a random complex64 value.

func Complex64Ptr

func Complex64Ptr() *complex64

Complex64Ptr generates a random complex64 value and returns the pointer to it.

func Complex64Slice

func Complex64Slice() []complex64

Complex64Slice generates a random complex64 slice.

func Duration

func Duration() time.Duration

Duration generates a random time.Duration value.

func DurationPtr

func DurationPtr() *time.Duration

DurationPtr generates a random time.Duration value and returns the pointer to it.

func DurationSlice

func DurationSlice() []time.Duration

DurationSlice generates a random time.Duration slice.

func Email

func Email() string

Email generates a random email address.

Example
package main

import (
	"fmt"

	"github.com/gardenbed/basil/factory"
)

func main() {
	email := factory.Email()
	fmt.Printf("%s\n", email)
}
Output:

func Float32

func Float32() float32

Float32 generates a random float32 value.

func Float32Ptr

func Float32Ptr() *float32

Float32Ptr generates a random float32 value and returns the pointer to it.

func Float32Slice

func Float32Slice() []float32

Float32Slice generates a random float32 slice.

func Float64

func Float64() float64

Float64 generates a random float64 value.

func Float64Ptr

func Float64Ptr() *float64

Float64Ptr generates a random float64 value and returns the pointer to it.

func Float64Slice

func Float64Slice() []float64

Float64Slice generates a random float64 slice.

func Int

func Int() int

Int generates a random int value.

func Int16

func Int16() int16

Int16 generates a random int16 value.

func Int16Ptr

func Int16Ptr() *int16

Int16Ptr generates a random int16 value and returns the pointer to it.

func Int16Slice

func Int16Slice() []int16

Int16Slice generates a random int16 slice.

func Int32

func Int32() int32

Int32 generates a random int32 value.

func Int32Ptr

func Int32Ptr() *int32

Int32Ptr generates a random int32 value and returns the pointer to it.

func Int32Slice

func Int32Slice() []int32

Int32Slice generates a random int32 slice.

func Int64

func Int64() int64

Int64 generates a random int64 value.

func Int64Ptr

func Int64Ptr() *int64

Int64Ptr generates a random int64 value and returns the pointer to it.

func Int64Slice

func Int64Slice() []int64

Int64Slice generates a random int64 slice.

func Int8

func Int8() int8

Int8 generates a random int8 value.

func Int8Ptr

func Int8Ptr() *int8

Int8Ptr generates a random int8 value and returns the pointer to it.

func Int8Slice

func Int8Slice() []int8

Int8Slice generates a random int8 slice.

func IntPtr

func IntPtr() *int

IntPtr generates a random int value and returns the pointer to it.

func IntSlice

func IntSlice() []int

IntSlice generates a random int slice.

func Name

func Name() string

Name generates a random human name.

Example
package main

import (
	"fmt"

	"github.com/gardenbed/basil/factory"
)

func main() {
	name := factory.Name()
	fmt.Printf("%s\n", name)
}
Output:

func Populate

func Populate(s interface{}, skipUnsupported bool) error

Populate receives the pointer to a struct and populates all fields, including the nested fields, with random values. If skipUnsupported set to false, an error will be returned in case a type is not supported.

Example
package main

import (
	"fmt"
	"log"
	"net/url"
	"time"

	"github.com/gardenbed/basil/factory"
)

func main() {
	object := struct {
		String     string
		Bool       bool
		Int        int
		Uint       uint
		Float64    float64
		Complex128 complex128
		Nested     struct {
			Duration time.Duration
			Time     *time.Time
			URL      *url.URL
		}
	}{}

	if err := factory.Populate(&object, false); err != nil {
		log.Fatalf("populate error: %s", err)
	}

	fmt.Printf("%+v\n", object)
}
Output:

func Rune

func Rune() rune

Rune generates a random rune value.

func RunePtr

func RunePtr() *rune

RunePtr generates a random rune value and returns the pointer to it.

func RuneSlice

func RuneSlice() []rune

RuneSlice generates a random rune slice.

func String

func String() string

String generates a random string value.

Example
package main

import (
	"fmt"

	"github.com/gardenbed/basil/factory"
)

func main() {
	s := factory.String()
	fmt.Printf("%s\n", s)
}
Output:

func StringPtr

func StringPtr() *string

StringPtr generates a random string value and returns the pointer to it.

Example
package main

import (
	"fmt"

	"github.com/gardenbed/basil/factory"
)

func main() {
	s := factory.StringPtr()
	fmt.Printf("%s\n", *s)
}
Output:

func StringSlice

func StringSlice() []string

StringSlice generates a random string slice.

Example
package main

import (
	"fmt"

	"github.com/gardenbed/basil/factory"
)

func main() {
	s := factory.StringSlice()
	fmt.Printf("%s\n", s)
}
Output:

func Time

func Time() time.Time

Time generates a random time.Time value.

Example
package main

import (
	"fmt"

	"github.com/gardenbed/basil/factory"
)

func main() {
	t := factory.Time()
	fmt.Printf("%s\n", t)
}
Output:

func TimeAfter

func TimeAfter(t time.Time) time.Time

TimeAfter generates a random time.Time value after a given time.

Example
package main

import (
	"fmt"
	"time"

	"github.com/gardenbed/basil/factory"
)

func main() {
	t := factory.TimeAfter(time.Now())
	fmt.Printf("%s\n", t)
}
Output:

func TimeBefore

func TimeBefore(t time.Time) time.Time

TimeBefore generates a random time.Time value before a given time.

Example
package main

import (
	"fmt"
	"time"

	"github.com/gardenbed/basil/factory"
)

func main() {
	t := factory.TimeBefore(time.Now())
	fmt.Printf("%s\n", t)
}
Output:

func URL

func URL() url.URL

URL generates a random url.URL value.

Example
package main

import (
	"fmt"

	"github.com/gardenbed/basil/factory"
)

func main() {
	u := factory.URL()
	fmt.Printf("%s\n", &u)
}
Output:

func Uint

func Uint() uint

Uint generates a random uint value.

func Uint16

func Uint16() uint16

Uint16 generates a random uint16 value.

func Uint16Ptr

func Uint16Ptr() *uint16

Uint16Ptr generates a random uint16 value and returns the pointer to it.

func Uint16Slice

func Uint16Slice() []uint16

Uint16Slice generates a random uint16 slice.

func Uint32

func Uint32() uint32

Uint32 generates a random uint32 value.

func Uint32Ptr

func Uint32Ptr() *uint32

Uint32Ptr generates a random uint32 value and returns the pointer to it.

func Uint32Slice

func Uint32Slice() []uint32

Uint32Slice generates a random uint32 slice.

func Uint64

func Uint64() uint64

Uint64 generates a random uint64 value.

func Uint64Ptr

func Uint64Ptr() *uint64

Uint64Ptr generates a random uint64 value and returns the pointer to it.

func Uint64Slice

func Uint64Slice() []uint64

Uint64Slice generates a random uint64 slice.

func Uint8

func Uint8() uint8

Uint8 generates a random uint8 value.

func Uint8Ptr

func Uint8Ptr() *uint8

Uint8Ptr generates a random uint8 value and returns the pointer to it.

func Uint8Slice

func Uint8Slice() []uint8

Uint8Slice generates a random uint8 slice.

func UintPtr

func UintPtr() *uint

UintPtr generates a random uint value and returns the pointer to it.

func UintSlice

func UintSlice() []uint

UintSlice generates a random uint slice.

Types

This section is empty.

Jump to

Keyboard shortcuts

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