forked from jinzhu/configor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unmarshal_json_test.go
72 lines (55 loc) · 2.14 KB
/
unmarshal_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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package configor_test
import (
"encoding/json"
"io/ioutil"
"os"
"strings"
"testing"
"github.com/jinzhu/configor"
)
func TestUnmatchedKeyInJsonConfigFile(t *testing.T) {
type configStruct struct {
Name string
}
type configFile struct {
Name string
Test string
}
config := configFile{Name: "test", Test: "ATest"}
file, err := ioutil.TempFile("/tmp", "configor")
if err != nil {
t.Fatal("Could not create temp file")
}
defer os.Remove(file.Name())
defer file.Close()
filename := file.Name()
if err := json.NewEncoder(file).Encode(config); err == nil {
var result configStruct
// Do not return error when there are unmatched keys but ErrorOnUnmatchedKeys is false
if err := configor.New(&configor.Config{}).Load(&result, filename); err != nil {
t.Errorf("Should NOT get error when loading configuration with extra keys. Error: %v", err)
}
// Return an error when there are unmatched keys and ErrorOnUnmatchedKeys is true
if err := configor.New(&configor.Config{ErrorOnUnmatchedKeys: true}).Load(&result, filename); err == nil || !strings.Contains(err.Error(), "json: unknown field") {
t.Errorf("Should get unknown field error when loading configuration with extra keys. Instead got error: %v", err)
}
} else {
t.Errorf("failed to marshal config")
}
// Add .json to the file name and test
err = os.Rename(file.Name(), file.Name()+".json")
if err != nil {
t.Errorf("Could not add suffix to file")
}
filename = file.Name() + ".json"
defer os.Remove(filename)
var result configStruct
// Do not return error when there are unmatched keys but ErrorOnUnmatchedKeys is false
if err := configor.New(&configor.Config{}).Load(&result, filename); err != nil {
t.Errorf("Should NOT get error when loading configuration with extra keys. Error: %v", err)
}
// Return an error when there are unmatched keys and ErrorOnUnmatchedKeys is true
if err := configor.New(&configor.Config{ErrorOnUnmatchedKeys: true}).Load(&result, filename); err == nil || !strings.Contains(err.Error(), "json: unknown field") {
t.Errorf("Should get unknown field error when loading configuration with extra keys. Instead got error: %v", err)
}
}