-
Notifications
You must be signed in to change notification settings - Fork 1
/
slice_test.go
168 lines (135 loc) · 3.37 KB
/
slice_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
package flagx
import (
"testing"
"time"
)
const sep = ","
func TestSliceBool(t *testing.T) {
s := boolSlice{sep: sep, value: new([]bool)}
err := s.Set("true,false,t,f,1,0")
failIfErr(t, err)
want := []bool{true, false, true, false, true, false}
mustEqual(t, *s.value, want)
str := s.String()
wantStr := "true,false,true,false,true,false"
mustEqual(t, str, wantStr)
}
func TestSliceBool_Bad(t *testing.T) {
s := boolSlice{sep: sep, value: new([]bool)}
err := s.Set("true,false,nono,yes")
if err == nil {
t.Fatal(err)
}
}
func TestSliceInt(t *testing.T) {
s := intSlice{sep: sep, value: new([]int)}
err := s.Set("1,2,-3,4")
failIfErr(t, err)
want := []int{1, 2, -3, 4}
mustEqual(t, *s.value, want)
str := s.String()
wantStr := "1,2,-3,4"
mustEqual(t, str, wantStr)
}
func TestSliceInt_Bad(t *testing.T) {
s := intSlice{sep: sep, value: new([]int)}
err := s.Set("1,2,3.3,4")
if err == nil {
t.Fatal(err)
}
}
func TestSliceInt64(t *testing.T) {
s := int64Slice{sep: sep, value: new([]int64)}
err := s.Set("1,2,-3,4")
failIfErr(t, err)
want := []int64{1, 2, -3, 4}
mustEqual(t, *s.value, want)
str := s.String()
wantStr := "1,2,-3,4"
mustEqual(t, str, wantStr)
}
func TestSliceInt64_Bad(t *testing.T) {
s := int64Slice{sep: sep, value: new([]int64)}
err := s.Set("1,2,3.3,4")
if err == nil {
t.Fatal(err)
}
}
func TestSliceUint(t *testing.T) {
s := uintSlice{sep: sep, value: new([]uint)}
err := s.Set("1,2,3,4")
failIfErr(t, err)
want := []uint{1, 2, 3, 4}
mustEqual(t, *s.value, want)
str := s.String()
wantStr := "1,2,3,4"
mustEqual(t, str, wantStr)
}
func TestSliceUint_Bad(t *testing.T) {
s := uintSlice{sep: sep, value: new([]uint)}
err := s.Set("1,2,-3,4")
if err == nil {
t.Fatal(err)
}
}
func TestSliceUint64(t *testing.T) {
s := uint64Slice{sep: sep, value: new([]uint64)}
err := s.Set("1,2,3,4")
failIfErr(t, err)
want := []uint64{1, 2, 3, 4}
mustEqual(t, *s.value, want)
str := s.String()
wantStr := "1,2,3,4"
mustEqual(t, str, wantStr)
}
func TestSliceUint64_Bad(t *testing.T) {
s := uint64Slice{sep: sep, value: new([]uint64)}
err := s.Set("1,2,-3,4")
if err == nil {
t.Fatal(err)
}
}
func TestSliceString(t *testing.T) {
s := stringSlice{sep: sep, value: new([]string)}
err := s.Set("1,2e20,3.78,rabbit,-4.20")
failIfErr(t, err)
want := []string{"1", "2e20", "3.78", "rabbit", "-4.20"}
mustEqual(t, *s.value, want)
str := s.String()
wantStr := "1,2e20,3.78,rabbit,-4.20"
mustEqual(t, str, wantStr)
}
func TestSliceFloat64(t *testing.T) {
s := float64Slice{sep: sep, value: new([]float64)}
err := s.Set("1,2e20,3.78,-4.20")
failIfErr(t, err)
want := []float64{1, 2e20, 3.78, -4.20}
mustEqual(t, *s.value, want)
str := s.String()
wantStr := "1,2e+20,3.78,-4.2"
mustEqual(t, str, wantStr)
}
func TestSliceFloat64_Bad(t *testing.T) {
s := float64Slice{sep: sep, value: new([]float64)}
err := s.Set("1,2,3/2,4")
if err == nil {
t.Fatal(err)
}
}
func TestSliceDuration(t *testing.T) {
s := durationSlice{sep: sep, value: new([]time.Duration)}
err := s.Set("1ns,1s,-1h")
failIfErr(t, err)
want := []time.Duration{1, time.Second, -time.Hour}
mustEqual(t, *s.value, want)
str := s.String()
wantStr := "1ns,1s,-1h0m0s"
mustEqual(t, str, wantStr)
}
func TestSliceDuration_Bad(t *testing.T) {
s := durationSlice{sep: sep, value: new([]time.Duration)}
err := s.Set("1s,2day")
if err == nil {
t.Fatal(err)
}
}