-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpixelmatch_test.go
65 lines (53 loc) · 1.19 KB
/
pixelmatch_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
package pixelmatch
import (
"errors"
"image"
"image/png"
"os"
"path"
"testing"
)
func Test_Pixelmatch(t *testing.T) {
type testcase struct {
fNamePrefix string
shouldMatch bool
}
for _, tc := range []testcase{
{"01_matching", true},
{"02_nonmatching", false},
{"03_nonmatching_strideskip", false},
} {
t.Run(tc.fNamePrefix, func(t *testing.T) {
pathA := path.Join("testdata", tc.fNamePrefix+"_a.png")
pathB := path.Join("testdata", tc.fNamePrefix+"_b.png")
readpng := func(path string) image.Image {
f, err := os.Open(path)
if errors.Is(err, os.ErrNotExist) {
t.Fatalf("%s does not exist, please create it for this test to run: %v", path, err)
} else if err != nil {
t.Fatalf(err.Error())
}
img, err := png.Decode(f)
if err != nil {
t.Fatalf(err.Error())
}
return img
}
imgA := readpng(pathA)
imgB := readpng(pathB)
res, err := MatchPixel(imgA, imgB)
if err != nil {
t.Fatalf(err.Error())
}
if tc.shouldMatch {
if res != 0 {
t.Errorf("should have been 0 diff, but was %d", res)
}
} else {
if res == 0 {
t.Errorf("should have been >0 diff, but was %d", res)
}
}
})
}
}