-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_test.go
54 lines (42 loc) · 1.14 KB
/
config_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
// SPDX-FileCopyrightText: 2019-2024 caixw
//
// SPDX-License-Identifier: MIT
package config
import (
"encoding/json"
"encoding/xml"
"io/fs"
"strings"
"testing"
"github.com/issue9/assert/v4"
)
var _ fs.FS = &Config{}
type conf struct {
XMLName struct{} `json:"-" xml:"config"`
Debug bool `json:"debug" xml:"debug,attr"`
Port int `json:"port" xml:"port,attr"`
Cert string `json:"cert" xml:"cert"`
}
func TestAppDir(t *testing.T) {
a := assert.New(t, false)
c, err := AppDir(nil, "config")
a.NotError(err).NotNil(c).
True(strings.HasSuffix(c.Dir(), "config"))
}
func TestConfig(t *testing.T) {
a := assert.New(t, false)
c := Dir(nil, "./testdata")
a.NotNil(c)
a.True(c.Exists("config.xml")).False(c.Exists("not-exists.xml"))
data, err := c.Read("config.xml")
a.NotError(err).NotNil(data)
obj := &conf{}
a.ErrorString(c.Load("config.json", obj), "not found serializer for ")
c.Serializer().Add(xml.Marshal, xml.Unmarshal, ".xml").
Add(json.Marshal, json.Unmarshal, ".json", ".js")
obj1 := &conf{}
a.NotError(c.Load("config.json", obj))
obj2 := &conf{}
a.NotError(c.Load("config.xml", obj))
a.Equal(obj1, obj2)
}