-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema_test.go
102 lines (85 loc) · 2.11 KB
/
schema_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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package cfg_test
import (
"context"
"fmt"
"io"
"testing"
"github.com/zpatrick/cfg"
"github.com/zpatrick/testx/assert"
)
func ExampleSchema_validation() {
var userName string
schema := cfg.Schema[string]{
Dest: &userName,
Validator: cfg.OneOf("admin", "guest"),
Provider: cfg.StaticProvider("other", false),
}
if err := schema.Load(context.Background()); err != nil {
fmt.Println(err)
}
// Output: validation failed: input other not contained in [admin guest]
}
func TestSchemaLoad_populatesDest(t *testing.T) {
var out int
port := cfg.Schema[int]{
Dest: &out,
Provider: cfg.StaticProvider(8080, false),
}
assert.NilError(t, port.Load(context.Background()))
assert.Equal(t, out, 8080)
}
func TestSchemaLoad_returnsUnhandledProviderError(t *testing.T) {
var out int
port := cfg.Schema[int]{
Dest: &out,
Default: cfg.Addr(8080),
Provider: cfg.ProviderFunc[int](func(context.Context) (int, error) {
return 0, io.EOF
}),
}
assert.ErrorIs(t, port.Load(context.Background()), io.EOF)
}
func TestSchemaLoad_usesDefaultWhenHandlingNoValueProvidedError(t *testing.T) {
var out int
port := cfg.Schema[int]{
Dest: &out,
Default: cfg.Addr(8080),
Provider: cfg.ProviderFunc[int](func(context.Context) (int, error) {
return 0, cfg.NoValueProvidedError
}),
}
assert.NilError(t, port.Load(context.Background()))
assert.Equal(t, out, 8080)
}
func TestSchemaLoad_returnsValidationError(t *testing.T) {
var (
out int
called bool
)
port := cfg.Schema[int]{
Dest: &out,
Provider: cfg.StaticProvider(8080, false),
Validator: cfg.ValidatorFunc[int](func(i int) error {
called = true
return io.EOF
}),
}
assert.ErrorIs(t, port.Load(context.Background()), io.EOF)
assert.Equal(t, called, true)
}
func TestSchemaLoad_validationSuccess(t *testing.T) {
var (
out int
called bool
)
port := cfg.Schema[int]{
Dest: &out,
Provider: cfg.StaticProvider(8080, false),
Validator: cfg.ValidatorFunc[int](func(i int) error {
called = true
return nil
}),
}
assert.NilError(t, port.Load(context.Background()))
assert.Equal(t, called, true)
}