-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_utils.go
40 lines (32 loc) · 1.05 KB
/
test_utils.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
package counters
import (
"bytes"
"io"
"os"
"testing"
"github.com/fogleman/gg"
"github.com/stretchr/testify/assert"
)
func TestImageContent(t *testing.T, expectedImagePath string, expectedImageLength int, canvas *gg.Context) {
byt := new(bytes.Buffer)
err := canvas.EncodePNG(byt)
assert.NoError(t, err)
if assert.Equal(t, expectedImageLength, byt.Len(), "The image should have %d bytes but has %d bytes",
expectedImageLength, byt.Len()) {
// Compare the buffer with the expected image
expectedImage, err := os.Open(expectedImagePath)
if assert.NoError(t, err, "The expected image should be present") {
defer expectedImage.Close()
expectedBytes, err := io.ReadAll(expectedImage)
if assert.NoError(t, err) {
if assert.Equal(t, expectedImageLength, len(expectedBytes), "The expected image (from the file, not the "+
"generated image) should have %d bytes but has %d bytes", expectedImageLength, byt.Len()) {
assert.ElementsMatch(t, expectedBytes, byt.Bytes())
}
}
}
}
}
func stringP(s string) *string {
return &s
}