-
Notifications
You must be signed in to change notification settings - Fork 0
/
toml_test.go
82 lines (67 loc) · 1.34 KB
/
toml_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
// Copyright (c) 2018 Timo Savola. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package confi
import (
"bytes"
"strings"
"testing"
)
func TestRead(t *testing.T) {
c := newTestConfig()
if err := Read(strings.NewReader(testConfigTOML), c); err != nil {
t.Fatalf("%v", err)
}
testConfigValues(t, c)
}
func TestReadKeepDefaults(t *testing.T) {
var c struct {
Foo string
Bar string
Baz struct {
A string
B string
}
}
c.Bar = "default value"
c.Baz.A = "preserve me please"
if err := Read(strings.NewReader(`foo = "hello"
[baz]
b = "goodbye"
`), &c); err != nil {
t.Fatalf("%v", err)
}
if c.Foo != "hello" {
t.Error(c.Foo)
}
if c.Bar != "default value" {
t.Error(c.Bar)
}
if c.Baz.A != "preserve me please" {
t.Error(c.Baz.A)
}
if c.Baz.B != "goodbye" {
t.Error(c.Baz.B)
}
}
func TestReadFileIfExists(t *testing.T) {
if err := ReadFileIfExists("/nonexistent", nil); err != nil {
t.Error(err)
}
if ReadFileIfExists("/etc/issue", nil) == nil {
t.Fail()
}
}
func TestWrite(t *testing.T) {
c := newTestConfig()
if err := Read(strings.NewReader(testConfigTOML), c); err != nil {
t.Fatal(err)
}
b := new(bytes.Buffer)
if err := Write(b, c); err != nil {
t.Fatal(err)
}
if s := b.String(); s != testConfigTOML {
t.Error(s)
}
}