Documentation
¶
Overview ¶
Package randkit provides random test helpers for generating strings, file names, identifiers, and other test fixtures. The default source is math/rand/v2's automatically-seeded global PRNG, which is fast and sufficiently random for test use. Pass WithSeed to switch to a deterministic source with stable output.
Index ¶
- Constants
- func FileName(dir string, opts ...func(*options)) string
- func Int(maximum int, opts ...func(*options)) int
- func Password(n int, opts ...func(*options)) string
- func Str(opts ...func(*options)) string
- func WithChars(list ...string) func(*options)
- func WithExt(ext string) func(*options)
- func WithLen(n int) func(*options)
- func WithPrefix(prefix string) func(*options)
- func WithSeed(seed int64) func(*options)
- func WithSuffix(suffix string) func(*options)
Examples ¶
Constants ¶
const ( // Uppercase is the list of uppercase letters. Uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" // Lowercase is the list of lowercase letters. Lowercase = "abcdefghijklmnopqrstuvwxyz" // Letters is a list of lowercase and uppercase letters. Letters = Lowercase + Uppercase // Digits is a list of digits 0 to 9. Digits = "0123456789" )
Character sets used to generate random strings.
Variables ¶
This section is empty.
Functions ¶
func FileName ¶
FileName returns a random file name. By default, the file name is 7 letters long [a-zA-Z] with the prefix "file-" and extension ".txt".
Example ¶
package main
import (
"fmt"
"github.com/ctx42/testkit/pkg/randkit"
)
func main() {
fmt.Println(randkit.FileName("/tmp", randkit.WithSeed(1)))
}
Output: /tmp/file-qLKZasg.txt
Example (WithExt) ¶
package main
import (
"fmt"
"github.com/ctx42/testkit/pkg/randkit"
)
func main() {
name := randkit.FileName("/tmp", randkit.WithExt(".json"), randkit.WithSeed(1))
fmt.Println(name)
}
Output: /tmp/file-qLKZasg.json
func Int ¶
Int generates a random integer in the range [1, max]. It panics if maximum is not positive.
Example ¶
package main
import (
"fmt"
"github.com/ctx42/testkit/pkg/randkit"
)
func main() {
fmt.Println(randkit.Int(100, randkit.WithSeed(1)))
}
Output: 32
func Password ¶
Password returns an n-character random password drawn from letters [a-zA-Z] and digits [0-9]. No special characters are included.
Example ¶
package main
import (
"fmt"
"github.com/ctx42/testkit/pkg/randkit"
)
func main() {
fmt.Println(randkit.Password(16, randkit.WithSeed(1)))
}
Output: tSR9avhesITXkYun
func Str ¶
func Str(opts ...func(*options)) string
Str returns a random string based on provided options. When no options are given, the generated string will be 10 characters long containing only letters. It panics if the character set is empty (e.g. WithChars("")).
Example ¶
package main
import (
"fmt"
"github.com/ctx42/testkit/pkg/randkit"
)
func main() {
fmt.Println(randkit.Str(randkit.WithSeed(1)))
}
Output: qLKZasgepC
Example (WithChars) ¶
package main
import (
"fmt"
"github.com/ctx42/testkit/pkg/randkit"
)
func main() {
s := randkit.Str(
randkit.WithChars(randkit.Digits),
randkit.WithLen(8),
randkit.WithSeed(1),
)
fmt.Println(s)
}
Output: 37790310
Example (WithLen) ¶
package main
import (
"fmt"
"github.com/ctx42/testkit/pkg/randkit"
)
func main() {
fmt.Println(randkit.Str(randkit.WithSeed(1), randkit.WithLen(6)))
}
Output: qLKZas
Example (WithPrefixSuffix) ¶
package main
import (
"fmt"
"github.com/ctx42/testkit/pkg/randkit"
)
func main() {
s := randkit.Str(
randkit.WithPrefix("test-"),
randkit.WithSuffix("-end"),
randkit.WithLen(6),
randkit.WithSeed(1),
)
fmt.Println(s)
}
Output: test-qLKZas-end
func WithChars ¶
func WithChars(list ...string) func(*options)
WithChars is a Str option setting the list of characters to use when generating random strings. All passed strings are concatenated in the order they were passed.
func WithExt ¶
func WithExt(ext string) func(*options)
WithExt is a Str option alias for WithSuffix.
func WithLen ¶
func WithLen(n int) func(*options)
WithLen is a Str option setting the length for the generated string.
func WithPrefix ¶
func WithPrefix(prefix string) func(*options)
WithPrefix is a Str option setting prefix for generated strings.
func WithSeed ¶ added in v0.4.0
func WithSeed(seed int64) func(*options)
WithSeed sets this call's source to a deterministic ChaCha8 PRNG seeded by seed. Use only when a test must assert exact generated values — never in production code where unpredictability is required.
func WithSuffix ¶
func WithSuffix(suffix string) func(*options)
WithSuffix is a Str option setting suffix for generated strings.
Types ¶
This section is empty.