-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimager_test.go
120 lines (98 loc) · 2.68 KB
/
imager_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
package imager
import (
"bytes"
"image"
"image/color"
"image/jpeg"
"os"
"testing"
)
// createTestImage creates a simple 100x100 red image for testing
func createTestImage() image.Image {
img := image.NewRGBA(image.Rect(0, 0, 100, 100))
for y := 0; y < 100; y++ {
for x := 0; x < 100; x++ {
img.Set(x, y, color.RGBA{255, 0, 0, 255})
}
}
return img
}
func TestNewImagerFromFile(t *testing.T) {
img := createTestImage()
// Save the test image to a temporary file
file, err := os.CreateTemp("", "test_image_*.jpg")
if err != nil {
t.Fatalf("failed to create temp file: %v", err)
}
defer os.Remove(file.Name())
err = jpeg.Encode(file, img, nil)
if err != nil {
t.Fatalf("failed to encode test image: %v", err)
}
file.Close()
// Test NewImagerFromFile
imgr, err := NewImagerFromFile(file.Name())
if err != nil {
t.Fatalf("NewImagerFromFile returned an error: %v", err)
}
if imgr.Image == nil {
t.Fatalf("NewImagerFromFile returned nil image")
}
if imgr.ImageType != "jpeg" {
t.Fatalf("NewImagerFromFile returned incorrect image type: %v", imgr.ImageType)
}
}
func TestNewImagerFromBytes(t *testing.T) {
img := createTestImage()
// Encode the test image to bytes
buf := new(bytes.Buffer)
err := jpeg.Encode(buf, img, nil)
if err != nil {
t.Fatalf("failed to encode test image: %v", err)
}
// Test NewImagerFromBytes
imgr, err := NewImagerFromBytes(buf.Bytes())
if err != nil {
t.Fatalf("NewImagerFromBytes returned an error: %v", err)
}
if imgr.Image == nil {
t.Fatalf("NewImagerFromBytes returned nil image")
}
if imgr.ImageType != "jpeg" {
t.Fatalf("NewImagerFromBytes returned incorrect image type: %v", imgr.ImageType)
}
}
func TestResize(t *testing.T) {
img := createTestImage()
imgr, err := NewImager(img)
if err != nil {
t.Fatalf("NewImager returned an error: %v", err)
}
resizedImgr := imgr.Resize(50, 50)
if resizedImgr.Image.Bounds().Dx() != 50 || resizedImgr.Image.Bounds().Dy() != 50 {
t.Fatalf("Resize did not return the expected dimensions: got %v", resizedImgr.Image.Bounds())
}
}
func TestBytes(t *testing.T) {
img := createTestImage()
imgr, err := NewImager(img)
if err != nil {
t.Fatalf("NewImager returned an error: %v", err)
}
imgr.ImageType = IMJPEG
data, err := imgr.Bytes()
if err != nil {
t.Fatalf("Bytes returned an error: %v", err)
}
if len(data) == 0 {
t.Fatalf("Bytes returned empty data")
}
// Decode the image to ensure it was encoded correctly
decodedImg, _, err := image.Decode(bytes.NewReader(data))
if err != nil {
t.Fatalf("Failed to decode image data: %v", err)
}
if decodedImg.Bounds() != img.Bounds() {
t.Fatalf("Decoded image bounds do not match original: got %v", decodedImg.Bounds())
}
}