-
Notifications
You must be signed in to change notification settings - Fork 1
/
gophig_test.go
89 lines (75 loc) · 1.86 KB
/
gophig_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
package gophig_test
import (
"github.com/restartfu/gophig"
"github.com/stretchr/testify/require"
"os"
"testing"
)
type MockMarshaler struct{}
func (m MockMarshaler) Marshal(interface{}) ([]byte, error) {
return []byte{}, nil
}
func (MockMarshaler) Unmarshal([]byte, interface{}) error {
return nil
}
func TestNewGophig(t *testing.T) {
t.Run("gophig creation is successful", func(t *testing.T) {
g := gophig.NewGophig[any]("", MockMarshaler{}, os.ModePerm)
require.NotNil(t, g)
})
}
type Sample struct {
Name string `json,toml,yaml:"name"`
Surname string `json,toml,yaml:"surname"`
Age int `json,toml,yaml:"age"`
}
func TestGophig_GetConf(t *testing.T) {
for _, ext := range []string{
"json",
"toml",
"yaml",
} {
t.Run("sample unmarshals successfully into "+ext+" sample struct", func(t *testing.T) {
marshaler, err := gophig.MarshalerFromExtension(ext)
require.NoError(t, err)
g := gophig.NewGophig[Sample]("tests/assets/sample."+ext, marshaler, os.ModePerm)
require.NotNil(t, g)
sample, err := g.LoadConf()
require.NoError(t, err)
require.Equal(t,
Sample{
Name: "jane",
Surname: "doe",
Age: 20,
},
sample,
)
})
}
}
func TestGophig_SetConf(t *testing.T) {
for _, ext := range []string{
"json",
"toml",
"yaml",
} {
t.Run("sample marshals successfully into "+ext+" sample data", func(t *testing.T) {
marshaler, err := gophig.MarshalerFromExtension(ext)
require.NoError(t, err)
err = os.MkdirAll("tests/tmp", os.ModePerm)
require.NoError(t, err)
defer func() {
require.NoError(t, os.RemoveAll("tests/tmp"))
}()
g := gophig.NewGophig[Sample]("tests/tmp/sample."+ext, marshaler, os.ModePerm)
require.NotNil(t, g)
sample := Sample{
Name: "jane",
Surname: "doe",
Age: 20,
}
err = g.SaveConf(sample)
require.NoError(t, err)
})
}
}