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
- func BuildEMail(first, last, domain string) string
- func BuildTime(layout string, offset time.Duration) (string, time.Time)
- func FixedRand() *rand.Rand
- func SimpleRand() *rand.Rand
- func ToUpperFirst(s string) string
- func UUIDString(uuid [16]byte) string
- type Generator
- func (g *Generator) Byte(lo, hi byte) byte
- func (g *Generator) Bytes(lo, hi byte, count int) []byte
- func (g *Generator) Domain() string
- func (g *Generator) Duration(lo, hi time.Duration) time.Duration
- func (g *Generator) EMail() string
- func (g *Generator) FemaleName() (first, middle, last string)
- func (g *Generator) FlipCoin(percent int) bool
- func (g *Generator) Int(lo, hi int) int
- func (g *Generator) Ints(lo, hi, count int) []int
- func (g *Generator) LimitedWord(lo, hi int) string
- func (g *Generator) MaleName() (first, middle, last string)
- func (g *Generator) Name() (first, middle, last string)
- func (g *Generator) Names(count int) []string
- func (g *Generator) OneByteOf(values ...byte) byte
- func (g *Generator) OneDurationOf(values ...time.Duration) time.Duration
- func (g *Generator) OneIntOf(values ...int) int
- func (g *Generator) OneOf(values ...any) any
- func (g *Generator) OneRuneOf(values string) rune
- func (g *Generator) OneStringOf(values ...string) string
- func (g *Generator) Paragraph() string
- func (g *Generator) ParagraphWithNames(names []string) string
- func (g *Generator) Pattern(pattern string) string
- func (g *Generator) Percent() int
- func (g *Generator) Sentence() string
- func (g *Generator) SentenceWithNames(names []string) string
- func (g *Generator) SleepOneOf(sleeps ...time.Duration) time.Duration
- func (g *Generator) Time(loc *time.Location, base time.Time, dur time.Duration) time.Time
- func (g *Generator) URL() string
- func (g *Generator) UUID() [16]byte
- func (g *Generator) Word() string
- func (g *Generator) Words(count int) []string
Constants ¶
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 ¶
BuildEMail creates an e-mail address out of first and last name and the domain.
func BuildTime ¶
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 ¶
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 ¶
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 ¶
ToUpperFirst returns the passed string with the first rune converted to uppercase.
func UUIDString ¶
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 (*Generator) Duration ¶
Duration generates a duration between lo and hi including those values.
func (*Generator) FemaleName ¶
FemaleName generates a female name consisting out of first, middle and last name.
func (*Generator) FlipCoin ¶
FlipCoin returns true if the internal generated percentage is equal or greater than the passed percentage.
func (*Generator) LimitedWord ¶
LimitedWord generates a random word with a length between lo and hi.
func (*Generator) MaleName ¶
MaleName generates a male name consisting out of first, middle and last name.
func (*Generator) Name ¶
Name generates a male or female name consisting out of first, middle and last name.
func (*Generator) OneDurationOf ¶
OneDurationOf returns one of the passed durations.
func (*Generator) OneOf ¶ added in v0.5.0
OneOf returns one of the passed empty interfaces (aka values).
func (*Generator) OneStringOf ¶
OneStringOf returns one of the passed strings.
func (*Generator) ParagraphWithNames ¶
ParagraphWithNames workes like Paragraph but inserts randomly names of the passed set. They can be generated by NameSet().
func (*Generator) Pattern ¶
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) Sentence ¶
Sentence generates a sentence between 2 and 15 words and possibly containing commas.
func (*Generator) SentenceWithNames ¶
SentenceWithNames works like Sentence but inserts randomly names of the passed set. They can be generated by NameSet().
func (*Generator) SleepOneOf ¶
SleepOneOf chooses randomely one of the passed durations and lets the goroutine sleep for this time.
func (*Generator) Time ¶
Time generates a time between the given one and that time plus the given duration. The result will have the passed location.