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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
|
package utils
import (
"testing"
"time"
"github.com/stretchr/testify/require"
)
func Test_PrettyTimeAgo(t *testing.T) {
cases := map[string]string{
"1s": "less than a minute ago",
"30s": "less than a minute ago",
"1m08s": "about 1 minute ago",
"15m0s": "about 15 minutes ago",
"59m10s": "about 59 minutes ago",
"1h10m02s": "about 1 hour ago",
"15h0m01s": "about 15 hours ago",
"30h10m": "about 1 day ago",
"50h": "about 2 days ago",
"720h05m": "about 1 month ago",
"3000h10m": "about 4 months ago",
"8760h59m": "about 1 year ago",
"17601h59m": "about 2 years ago",
"262800h19m": "about 30 years ago",
}
for duration, expected := range cases {
d, e := time.ParseDuration(duration)
if e != nil {
t.Errorf("failed to create a duration: %s", e)
}
fuzzy := PrettyTimeAgo(d)
require.Equal(t, fuzzy, expected, "unexpected fuzzy duration value: %s for %s")
}
}
func Test_Pluralize(t *testing.T) {
testCases := []struct {
name string
word string
amount int
want string
}{
{
name: "singular",
word: "label",
amount: 1,
want: "1 label",
},
{
name: "plural",
word: "label",
amount: 3,
want: "3 labels",
},
}
for _, tC := range testCases {
t.Run(tC.name, func(t *testing.T) {
got := Pluralize(tC.amount, tC.word)
require.Equal(t, got, tC.want, "Pluralize() got = %s, want = %s")
})
}
}
func Test_PresentInStringSlice(t *testing.T) {
testCases := []struct {
name string
hay []string
needle string
want bool
}{
{"simple true", []string{"foo", "bar", "baz"}, "bar", true},
{"simple false", []string{"foo", "bar", "baz"}, "qux", false},
}
for _, tC := range testCases {
t.Run(tC.name, func(t *testing.T) {
got := PresentInStringSlice(tC.hay, tC.needle)
require.Equal(t, got, tC.want, "PresentInStringSlice() got = %t, want = %t")
})
}
}
func Test_PresentInIntSlice(t *testing.T) {
testCases := []struct {
name string
hay []int
needle int
want bool
}{
{"simple true", []int{1, 2, 3}, 2, true},
{"simple false", []int{1, 2, 3}, 4, false},
}
for _, tC := range testCases {
t.Run(tC.name, func(t *testing.T) {
got := PresentInIntSlice(tC.hay, tC.needle)
require.Equal(t, got, tC.want, "PresentInIntSlice() got = %t, want = %t")
})
}
}
func Test_SanitizePathName(t *testing.T) {
tests := []struct {
name string
filename string
want string
}{
{
name: "A regular filename",
filename: "cli-v1.22.0.json",
want: "/cli-v1.22.0.json",
},
{
name: "A regular filename in a directory",
filename: "cli/cli-v1.22.0.json",
want: "/cli/cli-v1.22.0.json",
},
{
name: "A filename with directory traversal",
filename: "cli-v1.../../22.0.zip",
want: "/22.0.zip",
},
{
name: "A particularly nasty filename",
filename: "..././..././..././etc/password_file",
want: "/.../.../.../etc/password_file",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
filePathWanted := SanitizePathName(tt.filename)
require.Equal(t, filePathWanted, tt.want, "SanitizePathName() got = %s, want = %s")
})
}
}
func Test_CommonElementsInStringSlice(t *testing.T) {
testCases := []struct {
name string
array1 []string
array2 []string
want []string
}{
{
name: "simple no matching elements",
array1: []string{"foo", "bar", "baz"},
array2: []string{"qux", "quux", "quz"},
want: []string{},
},
{
name: "simple matching elements",
array1: []string{"foo", "quux", "baz"},
array2: []string{"qux", "quux", "baz"},
want: []string{"quux", "baz"},
},
}
for _, tC := range testCases {
t.Run(tC.name, func(t *testing.T) {
got := CommonElementsInStringSlice(tC.array1, tC.array2)
require.Equal(t, len(got), len(tC.want), "CommonElementsInStringSlice() size of got (%d) and wanted (%d) arrays differ")
for i := range got {
require.Equal(t, got[i], tC.want[i], "CommonElementsInStringSlice() got = %s, want = %s")
}
})
}
}
func Test_Map(t *testing.T) {
type SomeType struct {
name string
}
type Tests[T1, T2 any] struct {
name string
slice []T1
fn func(T1) T2
want []T2
}
tests := []Tests[any, any]{
{
"list of strings",
[]any{"foo", "bar", "baz"},
func(e any) any { return e },
[]any{"foo", "bar", "baz"},
},
{
"list of structs",
[]any{SomeType{"foo"}, SomeType{"bar"}, SomeType{"baz"}},
func(e any) any { return e.(SomeType).name },
[]any{"foo", "bar", "baz"},
},
{
"no elements",
[]any{},
func(e any) any { return e },
[]any{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := Map(tt.slice, tt.fn)
require.Equal(t, got, tt.want, "Test_Map() want %v; but got %v")
})
}
}
func TestIsValidURL(t *testing.T) {
tests := []struct {
name string
input string
expected bool
}{
{
name: "invalid empty url",
input: "",
expected: false,
},
{
name: "invalid url without correct scheme",
input: "https:/gitlab.com/group",
expected: false,
},
{
name: "ok with correct url",
input: "https://gitlab.com/group",
expected: true,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
actualIsValid := IsValidURL(test.input)
require.Equal(t, test.expected, actualIsValid, "TestIsValidURL() got = %s, want = %s")
})
}
}
func TestPtr(t *testing.T) {
tests := []struct {
name string
val any
}{
{"string", "GitLab"},
{"int", 503},
{"float", 50.3},
{"time", time.Now()},
{"struct", struct{}{}},
}
for _, tt := range tests {
require.Equal(t, Ptr(tt.val), &tt.val, "TestPtr() got = %s want = %s")
}
}
|