Repository navigation
Expand file tree
/
Copy pathsqlcdebug_test.go
More file actions
107 lines (95 loc) · 2.39 KB
/
Copy pathsqlcdebug_test.go
File metadata and controls
107 lines (95 loc) · 2.39 KB
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
package sqlcdebug
import (
"sync"
"testing"
)
func resetForTest(t *testing.T) {
t.Helper()
registryMu.Lock()
registry = map[string]*Setting{}
envMap = nil
envOnce = sync.Once{}
registryMu.Unlock()
}
func TestParse(t *testing.T) {
tests := []struct {
input string
want map[string]string
}{
{"", map[string]string{}},
{"dumpast=1", map[string]string{"dumpast": "1"}},
{"dumpast=1,trace=trace.out", map[string]string{"dumpast": "1", "trace": "trace.out"}},
{" dumpast=1 , processplugins=0 ", map[string]string{"dumpast": "1", "processplugins": "0"}},
{"trace=", map[string]string{"trace": ""}},
{"bare", map[string]string{}},
}
for _, tt := range tests {
got := parse(tt.input)
if len(got) != len(tt.want) {
t.Errorf("parse(%q): got %v, want %v", tt.input, got, tt.want)
continue
}
for k, v := range tt.want {
if got[k] != v {
t.Errorf("parse(%q)[%q] = %q, want %q", tt.input, k, got[k], v)
}
}
}
}
func TestSettingValue(t *testing.T) {
resetForTest(t)
Update("dumpast=1,trace=foo.out")
if v := New("dumpast").Value(); v != "1" {
t.Errorf("dumpast = %q, want %q", v, "1")
}
if v := New("trace").Value(); v != "foo.out" {
t.Errorf("trace = %q, want %q", v, "foo.out")
}
if !New("dumpast").IsSet() {
t.Errorf("IsSet(dumpast) = false, want true")
}
// Unset key returns its registered default.
if v := New("processplugins").Value(); v != "1" {
t.Errorf("processplugins default = %q, want %q", v, "1")
}
if New("processplugins").IsSet() {
t.Errorf("IsSet(processplugins) = true, want false")
}
}
func TestUpdateRefreshesExistingSettings(t *testing.T) {
resetForTest(t)
dumpAST := New("dumpast")
if v := dumpAST.Value(); v != "" {
t.Errorf("initial dumpast = %q, want empty", v)
}
Update("dumpast=1")
if v := dumpAST.Value(); v != "1" {
t.Errorf("after Update dumpast = %q, want %q", v, "1")
}
Update("")
if v := dumpAST.Value(); v != "" {
t.Errorf("after reset dumpast = %q, want empty", v)
}
if dumpAST.IsSet() {
t.Errorf("after reset IsSet = true, want false")
}
}
func TestAny(t *testing.T) {
resetForTest(t)
if Any() {
t.Errorf("Any() = true, want false on empty env")
}
Update("trace=1")
if !Any() {
t.Errorf("Any() = false, want true after Update")
}
}
func TestNewPanicsOnUnknown(t *testing.T) {
resetForTest(t)
defer func() {
if r := recover(); r == nil {
t.Errorf("New(\"bogus\") did not panic")
}
}()
New("bogus")
}