@@ -2,9 +2,15 @@ package util
22
33import (
44 "fmt"
5+ "reflect"
56 "testing"
67)
78
9+ type testingStruct struct {
10+ foobar bool
11+ list []string
12+ }
13+
814func TestExpiredTokenError (t * testing.T ) {
915 if ok := IsExpiredTokenErr (fmt .Errorf ("error: invalid accessor custom_accesor_value" )); ! ok {
1016 t .Errorf ("Should be expired" )
@@ -19,3 +25,124 @@ func TestExpiredTokenError(t *testing.T) {
1925 t .Errorf ("Shouldn't be expired" )
2026 }
2127}
28+
29+ func TestSliceHasElement_scalar (t * testing.T ) {
30+ slice := []interface {}{1 , 2 , 3 , 4 , 5 }
31+
32+ found , index := SliceHasElement (slice , 2 )
33+ if ! found && index != 1 {
34+ t .Errorf ("Slice should find element" )
35+ }
36+
37+ found , index = SliceHasElement (slice , 10 )
38+ if found && index != - 1 {
39+ t .Errorf ("Slice should not find element" )
40+ }
41+ }
42+
43+ func TestSliceHasElement_struct (t * testing.T ) {
44+ slice := []interface {}{
45+ testingStruct {foobar : false , list : []string {"hello" , "world" }},
46+ testingStruct {foobar : true , list : []string {"best" , "line" , "on" , "the" , "citadel" }},
47+ testingStruct {foobar : true , list : []string {"I" , "gotta" , "go" }},
48+ }
49+
50+ found , index := SliceHasElement (slice , testingStruct {foobar : true , list : []string {"I" , "gotta" , "go" }})
51+ if ! found && index != 1 {
52+ t .Errorf ("Slice should find element" )
53+ }
54+
55+ found , index = SliceHasElement (slice , testingStruct {foobar : false , list : []string {}})
56+ if found && index != - 1 {
57+ t .Errorf ("Slice should not find element" )
58+ }
59+
60+ found , index = SliceHasElement (slice , 10 )
61+ if found && index != - 1 {
62+ t .Errorf ("Slice should not find element" )
63+ }
64+ }
65+
66+ func TestSliceAppendIfMissing_scalar (t * testing.T ) {
67+ slice := []interface {}{1 , 2 , 3 , 4 , 5 }
68+ expectedAppend := []interface {}{1 , 2 , 3 , 4 , 5 , 6 }
69+
70+ append := SliceAppendIfMissing (slice , 3 )
71+ if ! reflect .DeepEqual (slice , append ) {
72+ t .Errorf ("Slice should not be appended" )
73+ }
74+
75+ append = SliceAppendIfMissing (slice , 6 )
76+ if ! reflect .DeepEqual (expectedAppend , append ) {
77+ t .Errorf ("Slice should be appended" )
78+ }
79+ }
80+
81+ func TestSliceAppendIfMissing_struct (t * testing.T ) {
82+ slice := []interface {}{
83+ testingStruct {foobar : false , list : []string {"hello" , "world" }},
84+ testingStruct {foobar : true , list : []string {"best" , "line" , "on" , "the" , "citadel" }},
85+ }
86+ expectedAppend := []interface {}{
87+ testingStruct {foobar : false , list : []string {"hello" , "world" }},
88+ testingStruct {foobar : true , list : []string {"best" , "line" , "on" , "the" , "citadel" }},
89+ testingStruct {foobar : true , list : []string {"I" , "gotta" , "go" }},
90+ }
91+
92+ append := SliceAppendIfMissing (slice , testingStruct {foobar : false , list : []string {"hello" , "world" }})
93+ if ! reflect .DeepEqual (slice , append ) {
94+ t .Errorf ("Slice should not be appended" )
95+ }
96+
97+ append = SliceAppendIfMissing (slice , testingStruct {foobar : true , list : []string {"I" , "gotta" , "go" }})
98+ if ! reflect .DeepEqual (expectedAppend , append ) {
99+ t .Errorf ("Slice should be appended" )
100+ }
101+ }
102+
103+ func TestSliceRemoveIfPresent_scalar (t * testing.T ) {
104+ slice := []interface {}{1 , 2 , 3 , 4 , 5 }
105+ expected := []interface {}{1 , 2 , 5 , 4 }
106+
107+ removed := SliceRemoveIfPresent (slice , 10 )
108+ if ! reflect .DeepEqual (slice , removed ) {
109+ t .Errorf ("Slice should not be modified" )
110+ }
111+
112+ removed = SliceRemoveIfPresent (slice , 3 )
113+ if ! reflect .DeepEqual (expected , removed ) {
114+ t .Errorf ("Slice should be modified" )
115+ }
116+
117+ empty := make ([]interface {}, 0 )
118+ if len (SliceRemoveIfPresent (empty , 0 )) != 0 {
119+ t .Errorf ("Slice should be empty" )
120+ }
121+
122+ single := []interface {}{1 }
123+ if len (SliceRemoveIfPresent (single , 1 )) != 0 {
124+ t .Errorf ("Slice should be empty" )
125+ }
126+ }
127+
128+ func TestSliceRemoveIfPresent_struct (t * testing.T ) {
129+ slice := []interface {}{
130+ testingStruct {foobar : false , list : []string {"hello" , "world" }},
131+ testingStruct {foobar : true , list : []string {"best" , "line" , "on" , "the" , "citadel" }},
132+ testingStruct {foobar : true , list : []string {"I" , "gotta" , "go" }},
133+ }
134+ expected := []interface {}{
135+ testingStruct {foobar : true , list : []string {"I" , "gotta" , "go" }},
136+ testingStruct {foobar : true , list : []string {"best" , "line" , "on" , "the" , "citadel" }},
137+ }
138+
139+ removed := SliceRemoveIfPresent (slice , testingStruct {foobar : false , list : []string {}})
140+ if ! reflect .DeepEqual (slice , removed ) {
141+ t .Errorf ("Slice should not be modified" )
142+ }
143+
144+ removed = SliceRemoveIfPresent (slice , testingStruct {foobar : false , list : []string {"hello" , "world" }})
145+ if ! reflect .DeepEqual (expected , removed ) {
146+ t .Errorf ("Slice should be modified" )
147+ }
148+ }
0 commit comments