-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathjson_test.go
48 lines (42 loc) · 1.03 KB
/
json_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
package config_test
import (
"encoding/json"
"os"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/kayac/go-config"
)
var testsJSON = []string{
`{"foo":"bar"}`,
`{"foo":"b\nar"}`,
`{"foo":"b\"ar"}`,
`{"foo":"b\u1234ar\t"}`,
`{"foo":"\u2029"}`,
`["A", "B", "C"]`,
`"string"`,
}
var templateTestJSON = []byte(`{
"json": "{{ env "JSON" | json_escape }}"
}`)
func TestJSONEncode(t *testing.T) {
defer os.Unsetenv("JSON")
for _, s := range testsJSON {
t.Setenv("JSON", s)
var before interface{}
if err := json.Unmarshal([]byte(s), &before); err != nil {
t.Error("failed to unmarshal before", err)
}
conf := make(map[string]string, 0)
if err := config.LoadWithEnvJSONBytes(&conf, templateTestJSON); err != nil {
t.Error("failed to LoadWithEnvJSONBytes", err)
}
t.Logf("%#v", conf)
var after interface{}
if err := json.Unmarshal([]byte(conf["json"]), &after); err != nil {
t.Error("failed to unmarshal after", err)
}
if cmp.Diff(before, after) != "" {
t.Errorf("%v != %v", before, after)
}
}
}