-
Notifications
You must be signed in to change notification settings - Fork 1
/
set_test.go
159 lines (124 loc) · 3.11 KB
/
set_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
package flagx
import (
"testing"
"time"
)
func TestSetInt(t *testing.T) {
mustEqual(t, (*SetOfInt)(nil).String(), "")
var sb SetOfInt
err := sb.Set("1,2,-3,4")
failIfErr(t, err)
want := map[int]struct{}{1: {}, 2: {}, -3: {}, 4: {}}
mustEqual(t, map[int]struct{}(sb), want)
str := sb.String()
wantStr := "-3,1,2,4"
mustEqual(t, str, wantStr)
}
func TestSetInt_Bad(t *testing.T) {
var sb SetOfInt
err := sb.Set("1,2,3.3,4")
if err == nil {
t.Fatal(err)
}
}
func TestSetInt64(t *testing.T) {
mustEqual(t, (*SetOfInt64)(nil).String(), "")
var sb SetOfInt64
err := sb.Set("1,2,-3,4")
failIfErr(t, err)
want := map[int64]struct{}{1: {}, 2: {}, -3: {}, 4: {}}
mustEqual(t, map[int64]struct{}(sb), want)
str := sb.String()
wantStr := "-3,1,2,4"
mustEqual(t, str, wantStr)
}
func TestSetInt64_Bad(t *testing.T) {
var sb SetOfInt64
err := sb.Set("1,2,3.3,4")
if err == nil {
t.Fatal(err)
}
}
func TestSetUint(t *testing.T) {
mustEqual(t, (*SetOfUint)(nil).String(), "")
var sb SetOfUint
err := sb.Set("1,2,3,4")
failIfErr(t, err)
want := map[uint]struct{}{1: {}, 2: {}, 3: {}, 4: {}}
mustEqual(t, map[uint]struct{}(sb), want)
str := sb.String()
wantStr := "1,2,3,4"
mustEqual(t, str, wantStr)
}
func TestSetUint_Bad(t *testing.T) {
var sb SetOfUint
err := sb.Set("1,2,-3,4")
if err == nil {
t.Fatal(err)
}
}
func TestSetUint64(t *testing.T) {
mustEqual(t, (*SetOfUint64)(nil).String(), "")
var sb SetOfUint64
err := sb.Set("1,2,3,4")
failIfErr(t, err)
want := map[uint64]struct{}{1: {}, 2: {}, 3: {}, 4: {}}
mustEqual(t, map[uint64]struct{}(sb), want)
str := sb.String()
wantStr := "1,2,3,4"
mustEqual(t, str, wantStr)
}
func TestSetUint64_Bad(t *testing.T) {
var sb SetOfUint64
err := sb.Set("1,2,-3,4")
if err == nil {
t.Fatal(err)
}
}
func TestSetString(t *testing.T) {
mustEqual(t, (*SetOfString)(nil).String(), "")
var ss SetOfString
err := ss.Set("1,2e20,3.78,rabbit,-4.20")
failIfErr(t, err)
want := map[string]struct{}{"1": {}, "2e20": {}, "3.78": {}, "rabbit": {}, "-4.20": {}}
mustEqual(t, map[string]struct{}(ss), want)
str := ss.String()
wantStr := "-4.20,1,2e20,3.78,rabbit"
mustEqual(t, str, wantStr)
}
func TestSetFloat64(t *testing.T) {
mustEqual(t, (*SetOfFloat64)(nil).String(), "")
var sb SetOfFloat64
err := sb.Set("1,2e20,3.78,-4.20")
failIfErr(t, err)
want := map[float64]struct{}{1: {}, 2e20: {}, 3.78: {}, -4.20: {}}
mustEqual(t, map[float64]struct{}(sb), want)
str := sb.String()
wantStr := "-4.2,1,2e+20,3.78"
mustEqual(t, str, wantStr)
}
func TestSetFloat64_Bad(t *testing.T) {
var sb SetOfFloat64
err := sb.Set("1,2,3/2,4")
if err == nil {
t.Fatal(err)
}
}
func TestSetDuration(t *testing.T) {
mustEqual(t, (*SetOfDuration)(nil).String(), "")
var sb SetOfDuration
err := sb.Set("1ns,1s,-1h")
failIfErr(t, err)
want := map[time.Duration]struct{}{1: {}, time.Second: {}, -time.Hour: {}}
mustEqual(t, map[time.Duration]struct{}(sb), want)
str := sb.String()
wantStr := "-1h0m0s,1ns,1s"
mustEqual(t, str, wantStr)
}
func TestSetDuration_Bad(t *testing.T) {
var sb SetOfDuration
err := sb.Set("1s,2day")
if err == nil {
t.Fatal(err)
}
}