-
Notifications
You must be signed in to change notification settings - Fork 0
/
yaml_test.go
84 lines (67 loc) · 2.17 KB
/
yaml_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
package cfgloader_test
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/ashep/go-cfgloader"
)
func TestLoadYAML(tt *testing.T) {
tt.Run("InvalidInYAML", func(t *testing.T) {
out := cfgStruct{}
err := cfgloader.LoadYAML([]byte("foo"), &out, nil)
assert.EqualError(t, err, "yaml: unmarshal errors:\n line 1: cannot unmarshal !!str `foo` into cfgloader_test.cfgStruct")
})
tt.Run("InvalidSchema", func(t *testing.T) {
out := cfgStruct{}
err := cfgloader.LoadYAML([]byte("foo: bar"), &out, []byte(`{]`))
assert.NotErrorIs(t, err, cfgloader.SchemaValidationError{})
assert.EqualError(t, err, "invalid character ']' looking for beginning of object key string")
})
tt.Run("SchemaCheckFailed", func(t *testing.T) {
out := cfgStruct{}
err := cfgloader.LoadYAML([]byte("foo: bar"), &out, testSchema)
assert.ErrorIs(t, err, cfgloader.SchemaValidationError{})
assert.EqualError(t, err, "bar: String length must be greater than or equal to 1; foo: String length must be less than or equal to 2")
})
tt.Run("OkEmptySchema", func(t *testing.T) {
out := cfgStruct{}
err := cfgloader.LoadYAML([]byte("foo: bar"), &out, nil)
require.NoError(t, err)
assert.Equal(t, "bar", out.Foo)
assert.Equal(t, "", out.Bar)
})
tt.Run("Ok", func(t *testing.T) {
out := cfgStruct{}
err := cfgloader.LoadYAML([]byte(`
foo: ba
bar: baz
int: 123
float: 123.456
baz:
foo: baz_foo
bar: baz_bar
int: 234
float: 234.567
`), &out, testSchema)
require.NoError(t, err)
assert.Equal(t, "ba", out.Foo)
assert.Equal(t, "baz", out.Bar)
assert.Equal(t, 123, out.Int)
assert.Equal(t, 123.456, out.Float)
assert.Equal(t, "baz_foo", out.Baz.Foo)
assert.Equal(t, "baz_bar", out.Baz.Bar)
assert.Equal(t, 234, out.Baz.Int)
assert.Equal(t, 234.567, out.Baz.Float)
})
}
func TestLoadYAMLFromFile(tt *testing.T) {
tt.Run("OkEmptySchema", func(t *testing.T) {
p, err := writeTempFile(t, []byte("foo: ba\nbar: baz"), "")
require.NoError(t, err)
out := cfgStruct{}
err = cfgloader.LoadYAMLFromPath(p, &out, testSchema)
require.NoError(t, err)
assert.Equal(t, "ba", out.Foo)
assert.Equal(t, "baz", out.Bar)
})
}