/
config_file_test.go
117 lines (92 loc) · 2.18 KB
/
config_file_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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package cfg_test
import (
"io/ioutil"
"testing"
"github.com/walle/cfg"
)
const configContents = `
# This is a comment
# An integer value
answer = 42
# A float value
pi = 3.14
# A boolean value
is_active = true
# A string value
quotes = Alea iacta est\nEt tu, Brute?`
func Test_ConfigFile(t *testing.T) {
f, err := ioutil.TempFile("", "cfg-test")
if err != nil {
t.Errorf("Error creating tmp file: %s\n", err)
}
path := f.Name()
f.WriteString(configContents)
f.Close()
configFile, err := cfg.NewConfigFile(path)
if err != nil {
t.Errorf("Error parsing config: %s\n", err)
}
a, _ := configFile.GetInt("answer")
if a != 42 {
t.Errorf("Expected %v got %v\n", 42, a)
}
p, _ := configFile.GetFloat("pi")
if p != 3.14 {
t.Errorf("Expected %v got %v\n", 3.14, p)
}
i, _ := configFile.GetBool("is_active")
if i != true {
t.Errorf("Expected %v got %v\n", true, i)
}
quotes := "Alea iacta est\nEt tu, Brute?"
q, _ := configFile.GetString("quotes")
if q != quotes {
t.Errorf("Expected %s got %s\n", quotes, q)
}
}
func Test_ConfigFilePersist(t *testing.T) {
f, err := ioutil.TempFile("", "cfg-test")
if err != nil {
t.Errorf("Error creating tmp file: %s\n", err)
}
path := f.Name()
f.WriteString(configContents)
f.Close()
configFile, err := cfg.NewConfigFile(path)
if err != nil {
t.Errorf("Error parsing config: %s\n", err)
}
a, _ := configFile.GetInt("answer")
if a != 42 {
t.Errorf("Expected %v got %v\n", 42, a)
}
configFile.SetInt("answer", 314)
err = configFile.Persist()
if err != nil {
t.Errorf("Error persisting config: %s\n", err)
}
c2, err := cfg.NewConfigFile(path)
if err != nil {
t.Errorf("Error parsing config: %s\n", err)
}
a2, _ := c2.GetInt("answer")
if a2 != 314 {
t.Errorf("Expected %v got %v\n", 314, a2)
}
}
func Test_ConfigFilePath(t *testing.T) {
f, err := ioutil.TempFile("", "cfg-test")
if err != nil {
t.Errorf("Error creating tmp file: %s\n", err)
}
path := f.Name()
f.WriteString(configContents)
f.Close()
configFile, err := cfg.NewConfigFile(path)
if err != nil {
t.Errorf("Error parsing config: %s\n", err)
}
if configFile.Path() != path {
t.Errorf("Expected %s, got %s\n", path, configFile.Path())
}
}