-
Notifications
You must be signed in to change notification settings - Fork 24
/
iniflags_test.go
178 lines (165 loc) · 4.82 KB
/
iniflags_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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package iniflags
import (
"flag"
"fmt"
"testing"
"time"
)
func TestRemoveTrailingComments(t *testing.T) {
hashCommented := "v = v # test_comment"
clean := removeTrailingComments(hashCommented)
if clean != "v = v" {
t.Fatalf("Supposed to get 'v = v ', got '%s'", clean)
}
colonCommented := "v = v ; test_comment"
clean = removeTrailingComments(colonCommented)
if clean != "v = v" {
t.Fatalf("Supposed to get 'v = v ', got '%s'", clean)
}
}
func TestGetTrailingComments(t *testing.T) {
hashCommented := "v = v # test_comment"
comment := getTrailingComment(hashCommented)
if comment != " test_comment" {
t.Fatalf("Supposed to get ' test_comment', got '%s'", comment)
}
colonCommented := "v = v ; test_comment"
comment = getTrailingComment(colonCommented)
if comment != " test_comment" {
t.Fatalf("Supposed to get ' test_comment', got '%s'", comment)
}
}
func TestBOM(t *testing.T) {
args, ok := getArgsFromConfig("test_bom.ini")
if !ok {
t.Fail()
}
if len(args) != 1 {
t.Fatalf("Unexpected number of args parsed: %d. Expected 1", len(args))
}
if args[0].Key != "bom" {
t.Fatalf("Unexpected key name parsed: %q. Expected \"bom\"", args[0].Key)
}
if args[0].Value != "привет" {
t.Fatalf("Unexpected value parsed: %q. Expected \"привет\"", args[0].Value)
}
}
func TestUnquoteValue(t *testing.T) {
val := "\"val#;\\\"\\n\" # test\n"
fixedVal, comment, ok := unquoteValue(val, 0, "")
if !ok || fixedVal != "val#;\"\n" {
t.Fatalf("Value should be unquoted and stripped, got '%s'", fixedVal)
}
expected := " test\n"
if comment != expected {
t.Fatalf("Supposed to get '%q', got '%q'", expected, comment)
}
}
func TestGetFlags(t *testing.T) {
parsed = false
Parse()
missingFlags := getMissingFlags()
if _, found := missingFlags["config"]; !found {
t.Fatalf("'config' flag should be missing in tests")
}
}
func TestGetArgsFromConfig(t *testing.T) {
args, ok := getArgsFromConfig("test_config.ini")
if !ok {
t.Fail()
}
var expected string
var checkedVar0, checkedVar1, checkedVar2, checkedVar3, checkedVar4 bool
for _, arg := range args {
t.Log(arg.Key, fmt.Sprintf("%q", arg.Value))
switch arg.Key {
case "var0":
expected = "val0"
if arg.Value != expected {
t.Fatalf("Val of 'var0' should be '%q', got %q", expected, arg.Value)
}
expected = " comment"
if arg.Comment != expected {
t.Fatalf("Comment of 'var0' should be ' comment', got %q", expected, arg.Comment)
}
checkedVar0 = true
case "var1":
expected = "val#1\n\\\"\nx"
if arg.Value != expected {
t.Fatalf("Invalid val for var1 should be '%q', got '%q'", expected, arg.Value)
}
expected = " this is a test comment"
if arg.Comment != expected {
t.Fatalf("Comment of 'var1' should be '%q', got %q", expected, arg.Comment)
}
checkedVar1 = true
case "var2":
expected = "1234"
if arg.Value != expected {
t.Fatalf("Val of 'var2' should be '%q', got %q", expected, arg.Value)
}
checkedVar2 = true
case "var3":
expected = ""
if arg.Value != expected {
t.Fatalf("Val of 'var3' should be '%q', got %q", expected, arg.Value)
}
expected = " empty value"
if arg.Comment != expected {
t.Fatalf("Comment of 'var3' should be '%q', got %q", expected, arg.Comment)
}
checkedVar3 = true
case "var4":
expected = "multi,var|12345"
if arg.Value != expected {
t.Fatalf("Val of 'var4' should be '%q', got %q", expected, arg.Value)
}
checkedVar4 = true
}
}
if !checkedVar0 || !checkedVar1 || !checkedVar2 || !checkedVar3 || !checkedVar4 {
t.Fatalf("Not all vals checked: args=[%v], %v, %v, %v, %v, %v",
args, checkedVar0, checkedVar1, checkedVar2, checkedVar3, checkedVar4)
}
}
func TestIsHTTP(t *testing.T) {
if !isHTTP("http://example.com") {
t.Fatalf("http://example.com should must be recognized as http path")
}
if !isHTTP("hTtpS://example.com") {
t.Fatalf("hTtpS://example.com should must be recognized as http path")
}
}
var x = flag.String("x", "baz", "for TestSetConfigFile")
func TestSetConfigFile(t *testing.T) {
parsed = false
SetConfigFile("./test_setconfigfile.ini")
Parse()
if *x != "foobar" {
t.Fatalf("Unexpected x=[%s]. Expected [foobar]", *x)
}
}
func TestSetAllowMissingConfigFile(t *testing.T) {
parsed = false
*allowMissingConfig = false
SetAllowMissingConfigFile(true)
if *allowMissingConfig != true {
t.Fatal("SetAllowUnknownFlags failed to update global.")
}
}
func TestSetAllowUnknownFlags(t *testing.T) {
parsed = false
*allowUnknownFlags = false
SetAllowUnknownFlags(true)
if *allowUnknownFlags != true {
t.Fatal("SetAllowUnknownFlags failed to update global.")
}
}
func TestSetConfigUpdateInterval(t *testing.T) {
parsed = false
*configUpdateInterval = time.Second
SetConfigUpdateInterval(time.Minute)
if *configUpdateInterval != time.Minute {
t.Fatal("SetConfigUpdateInterval failed to update global.")
}
}