Debian Codegraph @

example_test.go

raw symbols SCIP
1 package petname_test
2
3 import (
4 "fmt"
5 "math/rand"
6
7 "github.com/dustinkirkland/golang-petname"
8 )
9
10 // Example of using the traditional package-level functions (backward compatible)
11 func ExampleGenerate() {
12 // Old API - still works exactly as before
13 // Uses global rand (auto-seeded in Go 1.20+)
14 name := petname.Generate(2, "-")
15 fmt.Println(name)
16 }
17
18 // Example of using the new Generator API with deterministic seed
19 func ExampleNew_deterministic() {
20 // New API - deterministic generation for testing
21 gen := petname.New(rand.New(rand.NewSource(42)))
22
23 // Generate predictable names
24 fmt.Println(gen.Generate(2, "-"))
25 fmt.Println(gen.Generate(2, "-"))
26 fmt.Println(gen.Generate(2, "-"))
27 // Output:
28 // guiding-dodo
29 // relieved-bass
30 // mature-zebra
31 }
32
33 // Example of using the new Generator API for concurrent use
34 func ExampleNew_concurrent() {
35 // Each goroutine can have its own generator for thread-safe concurrent use
36 gen1 := petname.New(rand.New(rand.NewSource(1)))
37 gen2 := petname.New(rand.New(rand.NewSource(2)))
38
39 // These can be safely used concurrently
40 name1 := gen1.Generate(2, "-")
41 name2 := gen2.Generate(2, "-")
42
43 fmt.Println(name1)
44 fmt.Println(name2)
45 // Output:
46 // touched-shark
47 // relaxing-muskox
48 }
49
50 // Example of generating individual words with deterministic behavior
51 func ExampleGenerator_Adjective() {
52 gen := petname.New(rand.New(rand.NewSource(100)))
53
54 fmt.Println(gen.Adjective())
55 fmt.Println(gen.Adverb())
56 fmt.Println(gen.Name())
57 // Output:
58 // skilled
59 // apparently
60 // prawn
61 }