-
Notifications
You must be signed in to change notification settings - Fork 9
/
builder_test.go
61 lines (49 loc) · 1.4 KB
/
builder_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 faker_test
import (
"fmt"
"testing"
"github.com/pioz/faker"
"github.com/stretchr/testify/assert"
)
func TestRegisterBuilder(t *testing.T) {
err := faker.UnregisterBuilder("foo", "string")
assert.NotNil(t, err)
assert.Equal(t, "builder not registered", err.Error())
err = faker.RegisterBuilder("foo", "string", func(...string) (interface{}, error) {
return "bar", nil
})
assert.Nil(t, err)
err = faker.RegisterBuilder("foo", "string", func(...string) (interface{}, error) {
return "bar", nil
})
assert.NotNil(t, err)
assert.Equal(t, "builder already registered", err.Error())
err = faker.UnregisterBuilder("foo", "string")
assert.Nil(t, err)
}
func ExampleRegisterBuilder() {
faker.SetSeed(1802)
// Define a new builder
builder := func(params ...string) (interface{}, error) {
if len(params) > 0 && params[0] == "melee" {
return faker.Pick("Barbarian", "Bard", "Fighter", "Monk", "Paladin", "Rogue"), nil
}
return faker.Pick("Cleric", "Druid", "Ranger", "Sorcerer", "Warlock", "Wizard"), nil
}
// Register a new builder named "dndClass" for string type
err := faker.RegisterBuilder("dndClass", "string", builder)
if err != nil {
panic(err)
}
player := &struct {
Class string `faker:"dndClass(melee)"`
// other fields ...
}{}
// Build a struct with fake data
err = faker.Build(&player)
if err != nil {
panic(err)
}
fmt.Println(player.Class)
// Output: Paladin
}