-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbool_slice_test.go
146 lines (113 loc) · 3.51 KB
/
bool_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
package dynflags_test
import (
"testing"
"github.com/containeroo/dynflags"
"github.com/stretchr/testify/assert"
)
func TestBoolSlicesValue(t *testing.T) {
t.Parallel()
t.Run("Parse valid bool value", func(t *testing.T) {
t.Parallel()
boolSlicesValue := dynflags.BoolSlicesValue{Bound: &[]bool{}}
parsed, err := boolSlicesValue.Parse("true")
assert.NoError(t, err)
assert.Equal(t, true, parsed)
})
t.Run("Parse invalid bool value", func(t *testing.T) {
t.Parallel()
boolSlicesValue := dynflags.BoolSlicesValue{Bound: &[]bool{}}
parsed, err := boolSlicesValue.Parse("invalid")
assert.Error(t, err)
assert.Nil(t, parsed)
})
t.Run("Set valid bool value", func(t *testing.T) {
t.Parallel()
bound := []bool{true}
boolSlicesValue := dynflags.BoolSlicesValue{Bound: &bound}
err := boolSlicesValue.Set(false)
assert.NoError(t, err)
assert.Equal(t, []bool{true, false}, bound)
})
t.Run("Set invalid type", func(t *testing.T) {
t.Parallel()
bound := []bool{}
boolSlicesValue := dynflags.BoolSlicesValue{Bound: &bound}
err := boolSlicesValue.Set("invalid")
assert.Error(t, err)
assert.EqualError(t, err, "invalid value type: expected bool")
})
}
func TestGroupConfigBoolSlices(t *testing.T) {
t.Parallel()
t.Run("Define bool slices flag", func(t *testing.T) {
t.Parallel()
group := &dynflags.ConfigGroup{Flags: make(map[string]*dynflags.Flag)}
defaultValue := []bool{true, false}
group.BoolSlices("boolSliceFlag", defaultValue, "A bool slices flag")
assert.Contains(t, group.Flags, "boolSliceFlag")
assert.Equal(t, "A bool slices flag", group.Flags["boolSliceFlag"].Usage)
assert.Equal(t, "true,false", group.Flags["boolSliceFlag"].Default)
})
}
func TestGetBoolSlices(t *testing.T) {
t.Parallel()
t.Run("Retrieve []bool value", func(t *testing.T) {
t.Parallel()
parsedGroup := &dynflags.ParsedGroup{
Name: "testGroup",
Values: map[string]interface{}{
"flag1": []bool{true, false, true},
},
}
result, err := parsedGroup.GetBoolSlices("flag1")
assert.NoError(t, err)
assert.Equal(t, []bool{true, false, true}, result)
})
t.Run("Retrieve single bool value as []bool", func(t *testing.T) {
t.Parallel()
parsedGroup := &dynflags.ParsedGroup{
Name: "testGroup",
Values: map[string]interface{}{
"flag1": true,
},
}
result, err := parsedGroup.GetBoolSlices("flag1")
assert.NoError(t, err)
assert.Equal(t, []bool{true}, result)
})
t.Run("Flag not found", func(t *testing.T) {
t.Parallel()
parsedGroup := &dynflags.ParsedGroup{
Name: "testGroup",
Values: map[string]interface{}{},
}
result, err := parsedGroup.GetBoolSlices("nonExistentFlag")
assert.Error(t, err)
assert.Nil(t, result)
assert.EqualError(t, err, "flag 'nonExistentFlag' not found in group 'testGroup'")
})
t.Run("Flag value is invalid type", func(t *testing.T) {
t.Parallel()
parsedGroup := &dynflags.ParsedGroup{
Name: "testGroup",
Values: map[string]interface{}{
"flag1": "invalid",
},
}
result, err := parsedGroup.GetBoolSlices("flag1")
assert.Error(t, err)
assert.Nil(t, result)
assert.EqualError(t, err, "flag 'flag1' is not a []bool")
})
}
func TestBoolSlicesGetBound(t *testing.T) {
t.Run("BoolSlicesValue - GetBound", func(t *testing.T) {
var slices *[]bool
val := []bool{true, false, true}
slices = &val
boolSlicesValue := dynflags.BoolSlicesValue{Bound: slices}
assert.Equal(t, val, boolSlicesValue.GetBound())
boolSlicesValue = dynflags.BoolSlicesValue{Bound: nil}
assert.Nil(t, boolSlicesValue.GetBound())
})
}