forked from brettlangdon/forge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
forge_test.go
154 lines (133 loc) · 3.87 KB
/
forge_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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package forge_test
import (
"bytes"
"fmt"
"os"
"testing"
"github.com/go-aah/forge"
)
var testConfigBytes = []byte(`
# Global stuff
global = "global value";
# Primary stuff
primary {
string = "primary string value";
string_with_quote = "some \"quoted\" str\\ing";
single = 'hello world';
empty = '';
single_with_quote = '\'hello\' "world"';
# Semicolons are optional
integer500 = 500
float = 80.80
negative = -50
boolean = true
not_true = FALSE
nothing = NULL
list = [
TRUE,
FALSE,
50.5,
"hello",
'list',
]
# Reference secondary._under (which hasn't been defined yet)
sec_ref = secondary._under;
# Primary-sub stuff
sub {
key = "primary sub key value";
include "./test_include.cfg";
}
sub_section {
# Testing of a special case that had previous caused failures
# Was caused by an array with no ending semicolon, followed directly by another setting
nested_array_no_semi_colon = ["a", "b"]
another = true
}
}
secondary {
another = "secondary another value";
global_reference = global;
primary_sub_key = primary.sub.key;
another_again = .another; # References secondary.another
_under = 50;
path = $PATH;
}
`)
var testConfigString = string(testConfigBytes)
var testConfigReader = bytes.NewReader(testConfigBytes)
var expectedPath = os.Getenv("PATH")
func assertEqual(a interface{}, b interface{}, t *testing.T) {
if a != b {
t.Fatal(fmt.Sprintf("'%v' != '%v'", a, b))
}
}
func assertDirectives(values map[string]interface{}, t *testing.T) {
// Global
assertEqual(values["global"], "global value", t)
// Primary
primary := values["primary"].(map[string]interface{})
assertEqual(primary["string"], "primary string value", t)
assertEqual(primary["string_with_quote"], "some \"quoted\" str\\ing", t)
assertEqual(primary["single"], "hello world", t)
assertEqual(primary["empty"], "", t)
assertEqual(primary["single_with_quote"], "'hello' \"world\"", t)
assertEqual(primary["integer500"], int64(500), t)
assertEqual(primary["float"], float64(80.80), t)
assertEqual(primary["negative"], int64(-50), t)
assertEqual(primary["boolean"], true, t)
assertEqual(primary["not_true"], false, t)
assertEqual(primary["nothing"], nil, t)
assertEqual(primary["sec_ref"], int64(50), t)
// Primary list
list := primary["list"].([]interface{})
assertEqual(len(list), 5, t)
assertEqual(list[0], true, t)
assertEqual(list[1], false, t)
assertEqual(list[2], float64(50.5), t)
assertEqual(list[3], "hello", t)
assertEqual(list[4], "list", t)
// Primary Sub
sub := primary["sub"].(map[string]interface{})
assertEqual(sub["key"], "primary sub key value", t)
assertEqual(sub["included_setting"], "primary sub included_setting value", t)
// Secondary
secondary := values["secondary"].(map[string]interface{})
assertEqual(secondary["another"], "secondary another value", t)
assertEqual(secondary["global_reference"], "global value", t)
assertEqual(secondary["primary_sub_key"], "primary sub key value", t)
assertEqual(secondary["another_again"], "secondary another value", t)
assertEqual(secondary["_under"], int64(50), t)
assertEqual(secondary["path"], expectedPath, t)
}
func TestParseBytes(t *testing.T) {
settings, err := forge.ParseBytes(testConfigBytes)
if err != nil {
t.Fatal(err)
}
values := settings.ToMap()
assertDirectives(values, t)
}
func TestParseString(t *testing.T) {
settings, err := forge.ParseString(testConfigString)
if err != nil {
t.Fatal(err)
}
values := settings.ToMap()
assertDirectives(values, t)
}
func TestParseReader(t *testing.T) {
settings, err := forge.ParseReader(testConfigReader)
if err != nil {
t.Fatal(err)
}
values := settings.ToMap()
assertDirectives(values, t)
}
func TestParseFile(t *testing.T) {
settings, err := forge.ParseFile("./test.cfg")
if err != nil {
t.Fatal(err)
}
values := settings.ToMap()
assertDirectives(values, t)
}