generators

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2022 License: BSD-3-Clause Imports: 7 Imported by: 0

Documentation

Overview

Package generators helps to quickly generate data needed for unit tests. The generation of all supported different types is based on a passed rand.Rand. When using the same value here the generated data will be the same when repeating tests. So generators.FixedRand() delivers such a fixed value.

Index

Constants

View Source
const (
	// MinWordLen is the length of the shortest word.
	MinWordLen = 1

	// MaxWordLen is the length of the longest word.
	MaxWordLen = 14
)

Variables

This section is empty.

Functions

func BuildEMail

func BuildEMail(first, last, domain string) string

BuildEMail creates an e-mail address out of first and last name and the domain.

func BuildTime

func BuildTime(layout string, offset time.Duration) (string, time.Time)

BuildTime returns the current time plus or minus the passed offset formatted as string and as Time. The returned time is the parsed formatted one to avoid parsing troubles in tests.

func FixedRand

func FixedRand() *rand.Rand

FixedRand returns a random number generator with a fixed source so that tests using the generate functions can be repeated with the same result.

func SimpleRand

func SimpleRand() *rand.Rand

SimpleRand returns a random number generator with a source using the the current time as seed. It's not the best random, but ok to generate test data.

func ToUpperFirst

func ToUpperFirst(s string) string

ToUpperFirst returns the passed string with the first rune converted to uppercase.

func UUIDString

func UUIDString(uuid [16]byte) string

UUIDString creates a formatted string out of a generated pseudo UUID.

Types

type Generator

type Generator struct {
	// contains filtered or unexported fields
}

Generator is responsible for generating different random data based on a random number generator.

func New

func New(rand *rand.Rand) *Generator

New returns a new generator using the passed random number generator.

func (*Generator) Byte

func (g *Generator) Byte(lo, hi byte) byte

Byte generates a byte between lo and hi including those values.

func (*Generator) Bytes

func (g *Generator) Bytes(lo, hi byte, count int) []byte

Bytes generates a slice of random bytes.

func (*Generator) Domain

func (g *Generator) Domain() string

Domain generates domain out of name and top level domain.

func (*Generator) Duration

func (g *Generator) Duration(lo, hi time.Duration) time.Duration

Duration generates a duration between lo and hi including those values.

func (*Generator) EMail

func (g *Generator) EMail() string

EMail returns a random e-mail address.

func (*Generator) FemaleName

func (g *Generator) FemaleName() (first, middle, last string)

FemaleName generates a female name consisting out of first, middle and last name.

func (*Generator) FlipCoin

func (g *Generator) FlipCoin(percent int) bool

FlipCoin returns true if the internal generated percentage is equal or greater than the passed percentage.

func (*Generator) Int

func (g *Generator) Int(lo, hi int) int

Int generates an int between lo and hi including those values.

func (*Generator) Ints

func (g *Generator) Ints(lo, hi, count int) []int

Ints generates a slice of random ints.

func (*Generator) LimitedWord

func (g *Generator) LimitedWord(lo, hi int) string

LimitedWord generates a random word with a length between lo and hi.

func (*Generator) MaleName

func (g *Generator) MaleName() (first, middle, last string)

MaleName generates a male name consisting out of first, middle and last name.

func (*Generator) Name

func (g *Generator) Name() (first, middle, last string)

Name generates a male or female name consisting out of first, middle and last name.

func (*Generator) Names

func (g *Generator) Names(count int) []string

Names generates a set of names to be used in other generators.

func (*Generator) OneByteOf

func (g *Generator) OneByteOf(values ...byte) byte

OneByteOf returns one of the passed bytes.

func (*Generator) OneDurationOf

func (g *Generator) OneDurationOf(values ...time.Duration) time.Duration

OneDurationOf returns one of the passed durations.

func (*Generator) OneIntOf

func (g *Generator) OneIntOf(values ...int) int

OneIntOf returns one of the passed ints.

func (*Generator) OneOf added in v0.5.0

func (g *Generator) OneOf(values ...any) any

OneOf returns one of the passed empty interfaces (aka values).

func (*Generator) OneRuneOf

func (g *Generator) OneRuneOf(values string) rune

OneRuneOf returns one of the runes of the passed string.

func (*Generator) OneStringOf

func (g *Generator) OneStringOf(values ...string) string

OneStringOf returns one of the passed strings.

func (*Generator) Paragraph

func (g *Generator) Paragraph() string

Paragraph generates a paragraph between 2 and 10 sentences.

func (*Generator) ParagraphWithNames

func (g *Generator) ParagraphWithNames(names []string) string

ParagraphWithNames workes like Paragraph but inserts randomly names of the passed set. They can be generated by NameSet().

func (*Generator) Pattern

func (g *Generator) Pattern(pattern string) string

Pattern generates a string based on a pattern. Here different escape chars are replaced by according random chars while all others are left as they are. Escape chars start with a caret (^) followed by specializer. Those are:

  • ^ for a caret
  • 0 for a number between 0 and 9
  • 1 for a number between 1 and 9
  • o for an octal number
  • h for a hexadecimal number (lower-case)
  • H for a hexadecimal number (upper-case)
  • a for any char between a and z
  • A for any char between A and Z
  • c for a consonant (lower-case)
  • C for a consonant (upper-case)
  • v for a vowel (lower-case)
  • V for a vowel (upper-case)

func (*Generator) Percent

func (g *Generator) Percent() int

Percent generates an int between 0 and 100.

func (*Generator) Sentence

func (g *Generator) Sentence() string

Sentence generates a sentence between 2 and 15 words and possibly containing commas.

func (*Generator) SentenceWithNames

func (g *Generator) SentenceWithNames(names []string) string

SentenceWithNames works like Sentence but inserts randomly names of the passed set. They can be generated by NameSet().

func (*Generator) SleepOneOf

func (g *Generator) SleepOneOf(sleeps ...time.Duration) time.Duration

SleepOneOf chooses randomely one of the passed durations and lets the goroutine sleep for this time.

func (*Generator) Time

func (g *Generator) Time(loc *time.Location, base time.Time, dur time.Duration) time.Time

Time generates a time between the given one and that time plus the given duration. The result will have the passed location.

func (*Generator) URL

func (g *Generator) URL() string

URL generates a http, https or ftp URL, some of the leading to a file.

func (*Generator) UUID

func (g *Generator) UUID() [16]byte

UUID generates a 16 byte long random byte array. So it's no real UUID, even no v4, but it looks like.

func (*Generator) Word

func (g *Generator) Word() string

Word generates a random word.

func (*Generator) Words

func (g *Generator) Words(count int) []string

Words generates a slice of random words

Jump to

Keyboard shortcuts

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