-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathutils_test.go
150 lines (131 loc) · 3.79 KB
/
utils_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
package unleash
import (
"testing"
"github.com/stretchr/testify/assert"
)
// TestGetFetchURLPath verifies that getFetchURLPath returns the correct path
func TestGetFetchURLPath(t *testing.T) {
assert := assert.New(t)
res := getFetchURLPath("")
assert.Equal("./client/features", res)
res = getFetchURLPath("myProject")
assert.Equal("./client/features?project=myProject", res)
}
func TestEvery(t *testing.T) {
t.Run("All Even Integers", func(t *testing.T) {
numbers := []int{2, 4, 6, 8, 10}
allEven := every(numbers, func(item interface{}) bool {
num, ok := item.(int)
if !ok {
t.Errorf("Expected an integer, got %T", item)
return false
}
return num%2 == 0
})
if !allEven {
t.Errorf("Expected all numbers to be even, but got false")
}
})
t.Run("All Long Strings", func(t *testing.T) {
words := []string{"apple", "banana", "cherry"}
allLong := every(words, func(item interface{}) bool {
str, ok := item.(string)
if !ok {
t.Errorf("Expected a string, got %T", item)
return false
}
return len(str) > 3
})
if !allLong {
t.Errorf("Expected all words to be long, but got false")
}
})
t.Run("Empty Slice", func(t *testing.T) {
emptySlice := []int{}
allEmpty := every(emptySlice, func(item interface{}) bool {
// This condition should not be reached for an empty slice.
t.Errorf("Unexpected condition reached")
return false
})
if allEmpty {
t.Errorf("Expected an empty slice to return false, but got true")
}
})
t.Run("invalid inout", func(t *testing.T) {
invalidInput := "string"
result := every(invalidInput, func(item interface{}) bool {
// This condition should not be reached for an empty slice.
return true
})
if result == true {
t.Errorf("Expected result to be false")
}
})
t.Run("Result should be false if one doesn't match the predicate", func(t *testing.T) {
words := []string{"apple", "banana", "cherry", "he"}
allLong := every(words, func(item interface{}) bool {
str, ok := item.(string)
if !ok {
t.Errorf("Expected a string, got %T", item)
return false
}
return len(str) > 3
})
if allLong == true {
t.Errorf("Expected all words to be long, but got false")
}
})
}
func TestContains(t *testing.T) {
t.Run("Element is present in the slice", func(t *testing.T) {
arr := []string{"apple", "banana", "cherry", "date", "fig"}
str := "banana"
result := contains(arr, str)
if !result {
t.Errorf("Expected '%s' to be in the slice, but it was not found", str)
}
})
t.Run("Element is not present in the slice", func(t *testing.T) {
arr := []string{"apple", "banana", "cherry", "date", "fig"}
str := "grape"
result := contains(arr, str)
if result {
t.Errorf("Expected '%s' not to be in the slice, but it was found", str)
}
})
t.Run("Empty slice should return false", func(t *testing.T) {
arr := []string{}
str := "apple"
result := contains(arr, str)
if result {
t.Errorf("Expected an empty slice to return false, but it returned true")
}
})
}
func TestGetConnectionId(t *testing.T) {
for i := 0; i < 100; i++ {
uuid := getConnectionId()
t.Run("Correct length", func(t *testing.T) {
if len(uuid) != 36 {
t.Errorf("Expected UUID length to be 36, but got %d in %s", len(uuid), uuid)
}
})
t.Run("UUIDv4 version", func(t *testing.T) {
if uuid[14] != '4' {
t.Errorf("Expected version 4, but got %c in %s", uuid[14], uuid)
}
})
t.Run("UUIDv4 variant", func(t *testing.T) {
variant := uuid[19]
if variant != '8' && variant != '9' && variant != 'a' && variant != 'b' {
t.Errorf("Expected variant 10xx, but got %c in %s", variant, uuid)
}
})
t.Run("Uniqueness", func(t *testing.T) {
uuid2 := getConnectionId()
if uuid == uuid2 {
t.Errorf("Generated UUIDs are not unique: '%s' and '%s'", uuid, uuid2)
}
})
}
}