-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample_make_test.go
61 lines (51 loc) · 1.29 KB
/
example_make_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package testdata_test
import (
"fmt"
"math/rand/v2"
"testing"
"github.com/kyuff/testdata"
)
// init the testdata genration
func init() {
testdata.Rand(rand.New(rand.NewPCG(1, 2))) // stable output
testdata.Values(knownDishes)
testdata.Generator(func(rand *rand.Rand) Name {
return Name(fmt.Sprintf("My own name: %d", rand.IntN(100)))
})
testdata.Generator(func(rand *rand.Rand) Age {
return Age(rand.IntN(100))
})
}
type Name string
type Age int
type City string
type Dish string
const (
Spaghetti Dish = "SPAGHETTI"
CaesarSalad Dish = "CAESAR_SALAD"
Milkshake Dish = "MILKSHAKE"
)
var knownDishes = []Dish{Spaghetti, CaesarSalad, Milkshake}
type Person struct {
Name Name
Age Age
Dish Dish
City City
Note string
}
func ExampleMake() {
var (
t = &testing.T{}
_ = testdata.MakeSticky[City](t)
a = testdata.Make[Person](t)
b = testdata.Make[Person](t, func(person Person) Person {
person.Age = 32
return person
})
)
fmt.Printf("A: %#v\n", a)
fmt.Printf("B: %#v\n", b)
// Output:
// A: testdata_test.Person{Name:"My own name: 73", Age:50, Dish:"SPAGHETTI", City:"City-LCMNe2ur8bFrW7oM", Note:"string-k3Dc1kHJPXsAFv0C"}
// B: testdata_test.Person{Name:"My own name: 94", Age:32, Dish:"SPAGHETTI", City:"City-LCMNe2ur8bFrW7oM", Note:"string-nUQnH3DqWyTPPTEi"}
}