-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlflag_test.go
166 lines (145 loc) · 3.38 KB
/
lflag_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
package lflag
import (
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
// these are used in the cli and env tests
var testParams = []Param{
{ParamType: ParamTypeString, Name: "foo"},
{ParamType: ParamTypeString, Name: "bar", Usage: "wut"},
{ParamType: ParamTypeString, Name: "baz", Usage: "wut", Default: "wat"},
{ParamType: ParamTypeBool, Name: "flag1"},
{ParamType: ParamTypeBool, Name: "flag2"},
{ParamType: ParamTypeString, Name: "foo-bar"},
}
type jstruct struct {
Foo string `json:"foo"`
Bar int `json:"bar"`
}
func TestParse(t *testing.T) {
s := String("str", "default", "Some string")
i := Int("int", 5, "Some int")
b := Bool("bool", false, "Some bool")
bf := Bool("bool-false", true, "Some bool")
d := Duration("dur", 10*time.Second, "Some duration")
var j jstruct
JSON(&j, "json", jstruct{Foo: "foo", Bar: 5}, "Some json")
Parse(SourceStub{
"str": "hello",
"int": "8",
"bool": "true",
"bool-false": "false",
"dur": "5m",
"json": `{"foo":"FOO","bar":10}`,
})
assert.Equal(t, "hello", *s)
assert.Equal(t, 8, *i)
assert.True(t, *b)
assert.False(t, *bf)
assert.Equal(t, 5*time.Minute, *d)
assert.Equal(t, jstruct{Foo: "FOO", Bar: 10}, j)
}
func TestParseDefault(t *testing.T) {
s := String("str", "default", "Some string")
i := Int("int", 5, "Some int")
b := Bool("bool", true, "Some bool")
d := Duration("dur", 10*time.Second, "Some duration")
var j jstruct
JSON(&j, "json", jstruct{Foo: "foo", Bar: 5}, "Some json")
Parse(SourceStub{})
assert.Equal(t, "default", *s)
assert.Equal(t, 5, *i)
assert.True(t, *b)
assert.Equal(t, 10*time.Second, *d)
assert.Equal(t, jstruct{Foo: "foo", Bar: 5}, j)
}
func TestParseRequired(t *testing.T) {
s := RequiredString("str", "Some string")
i := RequiredInt("int", "Some int")
b := RequiredBool("bool", "Some bool")
d := RequiredDuration("dur", "Some duration")
var j jstruct
RequiredJSON(&j, "json", "Some json")
Parse(SourceStub{
"str": "hello",
"int": "8",
"bool": "true",
"dur": "5h",
"json": `{"foo":"FOO","bar":10}`,
})
assert.Equal(t, "hello", *s)
assert.Equal(t, 8, *i)
assert.True(t, *b)
assert.Equal(t, 5*time.Hour, *d)
assert.Equal(t, jstruct{Foo: "FOO", Bar: 10}, j)
}
func TestDuplicateJSON(t *testing.T) {
var j jstruct
JSON(&j, "json", jstruct{Foo: "foo", Bar: 5}, "Some json")
JSON(&j, "json", jstruct{Foo: "foo", Bar: 5}, "Some json")
Parse(SourceStub{
"json": `{"bar":10}`,
})
assert.Equal(t, jstruct{Foo: "", Bar: 10}, j)
assert.Panics(t, func() {
var j jstruct
JSON(&j, "json", jstruct{}, "Some json")
var k jstruct
JSON(&k, "json", jstruct{}, "Some json")
})
}
func TestDo(t *testing.T) {
Reset()
// we shouldn't need a lock but we do because the race detector is sensitive
var i int
var il sync.Mutex
Do(func() {
il.Lock()
assert.Equal(t, 0, i)
i++
il.Unlock()
Do(func() {
il.Lock()
assert.Equal(t, 2, i)
i++
il.Unlock()
})
})
Do(func() {
il.Lock()
assert.Equal(t, 1, i)
i++
il.Unlock()
Do(func() {
il.Lock()
assert.Equal(t, 3, i)
i++
il.Unlock()
Do(func() {
il.Lock()
assert.Equal(t, 4, i)
i++
il.Unlock()
})
})
})
Parse(SourceStub{})
ch := make(chan bool)
Do(func() {
il.Lock()
assert.Equal(t, 5, i)
i++
il.Unlock()
Do(func() {
il.Lock()
assert.Equal(t, 6, i)
i++
il.Unlock()
close(ch)
})
})
<-ch
assert.Equal(t, 7, i)
}