5
5
package collection
6
6
7
7
import (
8
+ "maps"
8
9
"math/rand"
9
10
"testing"
10
11
"time"
11
12
12
13
"github.com/go-faker/faker/v4"
13
14
"github.com/stretchr/testify/assert"
14
15
"github.com/stretchr/testify/require"
15
- "golang.org/x/exp/maps"
16
16
17
17
"github.com/ARM-software/golang-utils/utils/commonerrors"
18
18
"github.com/ARM-software/golang-utils/utils/commonerrors/errortest"
@@ -23,47 +23,59 @@ var (
23
23
)
24
24
25
25
func TestParseCommaSeparatedListWordsOnly (t * testing.T ) {
26
- stringList := ""
27
- stringArray := []string {}
28
- // we don't need cryptographically secure random numbers for generating a number of elements in a list
29
- lengthOfList := random .Intn (10 ) //nolint:gosec
30
- for i := 0 ; i < lengthOfList ; i ++ {
31
- word := faker .Word ()
32
- stringList += word
33
- stringArray = append (stringArray , word )
34
- numSpacesToAdd := random .Intn (5 ) //nolint:gosec
35
- for j := 0 ; j < numSpacesToAdd ; j ++ {
36
- stringList += " "
26
+ t .Run ("simple" , func (t * testing.T ) {
27
+ stringArray := []string {faker .Word (), faker .Word (), faker .Word (), faker .UUIDDigit (), faker .URL (), faker .Username (), faker .DomainName ()}
28
+ require .Equal (t , stringArray , ParseCommaSeparatedList (ConvertSliceToCommaSeparatedList [string ](stringArray )))
29
+ })
30
+ t .Run ("with whitespaces" , func (t * testing.T ) {
31
+ stringList := ""
32
+ var stringArray []string
33
+ // we don't need cryptographically secure random numbers for generating a number of elements in a list
34
+ lengthOfList := random .Intn (10 ) //nolint:gosec
35
+ for i := 0 ; i < lengthOfList ; i ++ {
36
+ word := faker .Word ()
37
+ stringList += word
38
+ stringArray = append (stringArray , word )
39
+ numSpacesToAdd := random .Intn (5 ) //nolint:gosec
40
+ for j := 0 ; j < numSpacesToAdd ; j ++ {
41
+ stringList += " "
42
+ }
43
+ stringList += ","
37
44
}
38
- stringList += ","
39
- }
40
- finalList := ParseCommaSeparatedList (stringList )
41
- require .Equal (t , stringArray , finalList )
45
+ finalList := ParseCommaSeparatedList (stringList )
46
+ require .Equal (t , stringArray , finalList )
47
+ })
42
48
}
43
49
44
50
// Test to make sure that spaces that show up within the words aren't removed
45
51
func TestParseCommaSeparatedListWithSpacesBetweenWords (t * testing.T ) {
46
- stringList := ""
47
- stringArray := []string {}
48
- // we don't need cryptographically secure random numbers for generating a number of elements in a list
49
- lengthOfList := random .Intn (10 ) //nolint:gosec
50
- for i := 0 ; i < lengthOfList ; i ++ {
51
- word := faker .Sentence ()
52
- stringList += word
53
- stringArray = append (stringArray , word )
54
- numSpacesToAdd := random .Intn (5 ) //nolint:gosec
55
- for j := 0 ; j < numSpacesToAdd ; j ++ {
56
- stringList += " "
52
+ t .Run ("simple" , func (t * testing.T ) {
53
+ stringArray := []string {faker .Paragraph (), faker .Word (), faker .Sentence (), faker .UUIDDigit (), faker .Name (), faker .Username (), faker .DomainName ()}
54
+ require .Equal (t , stringArray , ParseCommaSeparatedList (ConvertSliceToCommaSeparatedList [string ](stringArray )))
55
+ })
56
+ t .Run ("with whitespaces" , func (t * testing.T ) {
57
+ stringList := ""
58
+ var stringArray []string
59
+ // we don't need cryptographically secure random numbers for generating a number of elements in a list
60
+ lengthOfList := random .Intn (10 ) //nolint:gosec
61
+ for i := 0 ; i < lengthOfList ; i ++ {
62
+ word := faker .Sentence ()
63
+ stringList += word
64
+ stringArray = append (stringArray , word )
65
+ numSpacesToAdd := random .Intn (5 ) //nolint:gosec
66
+ for j := 0 ; j < numSpacesToAdd ; j ++ {
67
+ stringList += " "
68
+ }
69
+ stringList += ","
57
70
}
58
- stringList += ","
59
- }
60
- finalList := ParseCommaSeparatedList (stringList )
61
- require .Equal (t , stringArray , finalList )
71
+ finalList := ParseCommaSeparatedList (stringList )
72
+ require .Equal (t , stringArray , finalList )
73
+ })
62
74
}
63
75
64
76
func TestParseCommaSeparatedListWithSpacesBetweenWordsKeepBlanks (t * testing.T ) {
65
77
stringList := ""
66
- stringArray := []string {}
78
+ var stringArray []string
67
79
// we don't need cryptographically secure random numbers for generating a number of elements in a list
68
80
lengthOfList := random .Intn (10 ) + 8 //nolint:gosec
69
81
for i := 0 ; i < lengthOfList ; i ++ {
@@ -94,6 +106,17 @@ func TestParseCommaSeparatedListWithSpacesBetweenWordsKeepBlanks(t *testing.T) {
94
106
}
95
107
96
108
func TestParseCommaSeparatedListToMap (t * testing.T ) {
109
+ randomMap := map [string ]string {
110
+ faker .Sentence (): faker .Sentence (),
111
+ faker .Word (): faker .Paragraph (),
112
+ faker .Name (): faker .Sentence (),
113
+ faker .Sentence (): faker .Sentence (),
114
+ faker .Word (): faker .Paragraph (),
115
+ faker .Name (): faker .Sentence (),
116
+ faker .Sentence (): faker .Sentence (),
117
+ faker .Word (): faker .Paragraph (),
118
+ faker .Name (): faker .Sentence (),
119
+ }
97
120
for _ , test := range []struct {
98
121
Name string
99
122
Input string
@@ -111,6 +134,7 @@ func TestParseCommaSeparatedListToMap(t *testing.T) {
111
134
{"Normal 8" , "," , map [string ]string {}, nil },
112
135
{"Normal 9" , ",,,,," , map [string ]string {}, nil },
113
136
{"Normal 10" , ",, ,, ," , map [string ]string {}, nil },
137
+ {"Normal 11" , ConvertMapToCommaSeparatedList [string , string ](randomMap ), randomMap , nil },
114
138
{"Bad 1" , "one" , nil , commonerrors .ErrInvalid },
115
139
{"Bad 1" , "one, two, three" , nil , commonerrors .ErrInvalid },
116
140
{"Bad 2" , "one element with spaces" , nil , commonerrors .ErrInvalid },
@@ -125,3 +149,51 @@ func TestParseCommaSeparatedListToMap(t *testing.T) {
125
149
})
126
150
}
127
151
}
152
+
153
+ func TestParseCommaSeparatedPairListToMap (t * testing.T ) {
154
+ randomMap := map [string ]string {
155
+ faker .Sentence (): faker .Sentence (),
156
+ faker .Word (): faker .Paragraph (),
157
+ faker .Name (): faker .Sentence (),
158
+ faker .Sentence (): faker .Sentence (),
159
+ faker .Word (): faker .Paragraph (),
160
+ faker .Name (): faker .Sentence (),
161
+ faker .Sentence (): faker .Sentence (),
162
+ faker .Word (): faker .Paragraph (),
163
+ faker .Name (): faker .Sentence (),
164
+ }
165
+ for _ , test := range []struct {
166
+ Name string
167
+ Input string
168
+ Expected map [string ]string
169
+ Err error
170
+ PairSeparator string
171
+ }{
172
+ {"Normal 1" , "hello=world" , map [string ]string {"hello" : "world" }, nil , "=" },
173
+ {"Normal 2" , "hello+world,adrien+cabarbaye" , map [string ]string {"hello" : "world" , "adrien" : "cabarbaye" }, nil , "+" },
174
+ {"Normal 2" , "hello, world, adrien, cabarbaye" , map [string ]string {"hello" : "world" , "adrien" : "cabarbaye" }, nil , "," },
175
+ {"Normal 2.5" , "hello= world, adrien = cabarbaye" , map [string ]string {"hello" : "world" , "adrien" : "cabarbaye" }, nil , "=" },
176
+ {"Normal 3" , "hello&world,adrien&cabarbaye," , map [string ]string {"hello" : "world" , "adrien" : "cabarbaye" }, nil , "&" },
177
+ {"Normal 4" , "hello%%world,,,,adrien%%cabarbaye,,," , map [string ]string {"hello" : "world" , "adrien" : "cabarbaye" }, nil , "%%" },
178
+ {"Normal 5" , "hello$$$world,this$$$value has spaces" , map [string ]string {"hello" : "world" , "this" : "value has spaces" }, nil , "$$$" },
179
+ {"Normal 6" , "hello__world,,,this__value has spaces,,," , map [string ]string {"hello" : "world" , "this" : "value has spaces" }, nil , "__" },
180
+ {"Normal 7" , "" , map [string ]string {}, nil , "+" },
181
+ {"Normal 8" , "," , map [string ]string {}, nil , "+" },
182
+ {"Normal 9" , ",,,,," , map [string ]string {}, nil , "^" },
183
+ {"Normal 10" , ",, ,, ," , map [string ]string {}, nil , "+" },
184
+ {"Normal 11" , ConvertMapToCommaSeparatedPairsList [string , string ](randomMap , "/" ), randomMap , nil , "/" },
185
+ {"Normal 12" , ConvertMapToCommaSeparatedPairsList [string , string ](randomMap , " " ), randomMap , nil , " " },
186
+ {"Bad 1" , "one" , nil , commonerrors .ErrInvalid , "+" },
187
+ {"Bad 1" , "one, two, three" , nil , commonerrors .ErrInvalid , "+" },
188
+ {"Bad 2" , "one element with spaces" , nil , commonerrors .ErrInvalid , "+" },
189
+ {"Bad 3" , "one element with spaces and end comma," , nil , commonerrors .ErrInvalid , "+" },
190
+ {"Bad 4" , "one element with spaces and multiple end commas,,," , nil , commonerrors .ErrInvalid , "+" },
191
+ {"Bad 5" , ",,,one element with spaces and multiple end/beginning commas,,," , nil , commonerrors .ErrInvalid , "=" },
192
+ } {
193
+ t .Run (test .Name , func (t * testing.T ) {
194
+ pairs , err := ParseCommaSeparatedListOfPairsToMap (test .Input , test .PairSeparator )
195
+ errortest .AssertError (t , err , test .Err )
196
+ assert .True (t , maps .Equal (test .Expected , pairs ))
197
+ })
198
+ }
199
+ }
0 commit comments