/
encode_test.go
91 lines (75 loc) · 1.78 KB
/
encode_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
package cfg_test
import (
"testing"
"github.com/walle/cfg"
)
var myConfig = &MyConfig{
Answer: 42,
Pi: 3.14,
IsActive: true,
Quotes: "Alea iacta est\nEt tu, Brute?",
unexported: "foo",
NotUsed: "bar",
}
var myConfigWithEmpty = &MyConfig{
Answer: 42,
IsActive: false,
}
const myConfigEncoded = `Answer = 42
Pi = 3.14
is_active = true
quotes = Alea iacta est\nEt tu, Brute?
`
const myConfigEncodedWithEmpty = `Answer = 42
Pi = 0
is_active = false
quotes =
`
func Test_Marshal(t *testing.T) {
data, err := cfg.Marshal(myConfig)
if err != nil {
t.Errorf("Error encoding data: %s\n", err)
}
if string(data) != myConfigEncoded {
t.Errorf("Expected %q got %q\n", myConfigEncoded, string(data))
}
}
func Test_MarshalWithEmpty(t *testing.T) {
data, err := cfg.Marshal(myConfigWithEmpty)
if err != nil {
t.Errorf("Error encoding data: %s\n", err)
}
if string(data) != myConfigEncodedWithEmpty {
t.Errorf("Expected %q got %q\n", myConfigEncodedWithEmpty, string(data))
}
}
func Test_MarshalNotStruct(t *testing.T) {
i := 0
_, err := cfg.Marshal(i)
if err == nil {
t.Errorf("Did not get error when trying to marshal int\n")
}
}
func Test_MarshalToConfig(t *testing.T) {
config, err := cfg.MarshalToConfig(myConfig)
if err != nil {
t.Errorf("Error encoding data: %s\n", err)
}
answer, _ := config.GetInt("Answer")
if answer != 42 {
t.Errorf("Expected %v, got %v\n", 42, answer)
}
pi, _ := config.GetFloat("Pi")
if pi != 3.14 {
t.Errorf("Expected %v, got %v\n", 3.14, pi)
}
isActive, _ := config.GetBool("is_active")
if isActive != true {
t.Errorf("Expected %v, got %v\n", true, isActive)
}
quotes, _ := config.GetString("quotes")
q := "Alea iacta est\nEt tu, Brute?"
if quotes != q {
t.Errorf("Expected %q, got %q\n", q, quotes)
}
}