-
Notifications
You must be signed in to change notification settings - Fork 5
/
pngquant_test.go
41 lines (36 loc) · 944 Bytes
/
pngquant_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
package pngquant
import (
"bytes"
"image/png"
"os"
"testing"
)
func TestCompress(t *testing.T) {
var file *os.File
file, _ = os.Open("gopher.png")
defer file.Close()
info, _ := file.Stat()
orgSize := info.Size()
orgImg, _ := png.Decode(file)
// Test with good data
newImg, err := Compress(orgImg, "1")
if err != nil {
t.Errorf("error has occurred: %v", err)
} else {
var w bytes.Buffer
png.Encode(&w, newImg)
if len(w.Bytes()) > int(orgSize) {
t.Error("image is not comppressed")
}
}
// Test with non-numerical data (no need to test encode as we expect nothing)
_, err = Compress(orgImg, "one")
if err == nil {
t.Errorf("ERROR: Expected failure on incompatible speed argument (one)")
}
// Test with number that exceeds the limit (no need to test encode as we expect nothing)
_, err = Compress(orgImg, "20")
if err == nil {
t.Errorf("ERROR: Expected failure on incompatible speed argument (20)")
}
}