-
Notifications
You must be signed in to change notification settings - Fork 0
/
io_test.go
55 lines (48 loc) · 1.36 KB
/
io_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
package main
import (
"errors"
"regexp"
"testing"
)
func TestGetRandIndexInArray(t *testing.T) {
type randIndexTest struct {
list []string
}
tests := []randIndexTest{
{[]string{"pen"}},
{[]string{"pom", "sam"}},
{[]string{}},
}
for _, tt := range tests {
list := tt.list
rInd, err := getRandIndexInArray(list)
if len(list) == 0 && err == nil {
t.Errorf("getRandIndexInArray(%#v) = %d want error (%s)", list, rInd, err)
}
if len(list) != 0 && (rInd >= len(list) || rInd < 0) {
t.Errorf("getRandIndexInArray(%#v) = %d want between %d and %d", list, rInd, 0, len(list))
}
}
}
func TestGetJpegPathInDirectory(t *testing.T) {
type getJpegPathInDirectoryTest struct {
directory string
err error
imageRegex string
}
tests := map[string]getJpegPathInDirectoryTest{
"non-existent dir": {"bob", errEmptyDir, ""},
"images dir": {"images", nil, ".*\\.jpg"},
}
for name, tt := range tests {
want := regexp.MustCompile(tt.imageRegex)
t.Run(name, func(t *testing.T) {
path, err := GetJpegPathInDirectory(tt.directory)
if tt.err != nil && !errors.Is(err, tt.err) {
t.Errorf("GetJpegPathInDirectory(%s) = %s with error %q want error %q", tt.directory, path, tt.err, err)
} else if !want.MatchString(path) {
t.Errorf("GetJpegPathInDirectory(%s) = %s want match for %#q", tt.directory, path, tt.imageRegex)
}
})
}
}