-
Notifications
You must be signed in to change notification settings - Fork 5
/
utils_test.go
163 lines (134 loc) · 2.49 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
151
152
153
154
155
156
157
158
159
160
161
162
163
package gonertia
import (
"reflect"
"testing"
)
func Test_setOf(t *testing.T) {
t.Parallel()
t.Run("nil", func(t *testing.T) {
t.Parallel()
got := setOf[string](nil)
var want map[string]struct{}
if !reflect.DeepEqual(got, want) {
t.Fatalf("setOf()=%#v, want=%#v", got, want)
}
})
t.Run("empty", func(t *testing.T) {
t.Parallel()
got := setOf[string]([]string{})
var want map[string]struct{}
if !reflect.DeepEqual(got, want) {
t.Fatalf("setOf()=%#v, want=%#v", got, want)
}
})
t.Run("duplicates", func(t *testing.T) {
t.Parallel()
got := setOf[string]([]string{"foo", "foo"})
want := map[string]struct{}{
"foo": {},
}
if !reflect.DeepEqual(got, want) {
t.Fatalf("setOf()=%#v, want=%#v", got, want)
}
})
t.Run("strings", func(t *testing.T) {
t.Parallel()
got := setOf[string]([]string{"foo", "bar"})
want := map[string]struct{}{
"foo": {},
"bar": {},
}
if !reflect.DeepEqual(got, want) {
t.Fatalf("setOf()=%#v, want=%#v", got, want)
}
})
t.Run("integers", func(t *testing.T) {
t.Parallel()
got := setOf[int]([]int{123, 456})
want := map[int]struct{}{
123: {},
456: {},
}
if !reflect.DeepEqual(got, want) {
t.Fatalf("setOf()=%#v, want=%#v", got, want)
}
})
}
func Test_firstOr(t *testing.T) {
t.Parallel()
tests := []struct {
name string
items []string
fallback string
want string
}{
{
"nil",
nil,
"zoo",
"zoo",
},
{
"empty",
[]string{},
"zoo",
"zoo",
},
{
"not empty",
[]string{"foo", "bar"},
"zoo",
"foo",
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got := firstOr[string](tt.items, tt.fallback)
if got != tt.want {
t.Fatalf("firstOr()=%s, want=%s", got, tt.want)
}
})
}
}
func Test_md5(t *testing.T) {
t.Parallel()
tests := []struct {
name string
str string
want string
}{
{
"empty",
"",
"d41d8cd98f00b204e9800998ecf8427e",
},
{
"not empty",
"foo",
"acbd18db4cc2f85cedef654fccc4a4d8",
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got := md5(tt.str)
if got != tt.want {
t.Fatalf("md5()=%s, want=%s", got, tt.want)
}
})
}
}
func Test_md5File(t *testing.T) {
t.Parallel()
f := tmpFile(t, "foo")
got, err := md5File(f.Name())
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
if want := md5("foo"); got != want {
t.Fatalf("md5File()=%s, want=%s", got, want)
}
}