mirror of
https://github.com/talgo-cloud/bimg.git
synced 2026-04-23 11:11:51 -07:00
feat(test): better coverage for vips interface
This commit is contained in:
parent
b065e902ed
commit
2bc5756fad
3 changed files with 87 additions and 39 deletions
95
vips_test.go
95
vips_test.go
|
|
@ -18,30 +18,18 @@ func TestVipsRead(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, file := range files {
|
||||
img, _ := os.Open(path.Join("fixtures", file.name))
|
||||
buf, _ := ioutil.ReadAll(img)
|
||||
defer img.Close()
|
||||
|
||||
image, imageType, _ := vipsRead(buf)
|
||||
image, imageType, _ := vipsRead(readImage(file.name))
|
||||
if image == nil {
|
||||
t.Fatal("Empty image")
|
||||
}
|
||||
if imageType != file.expected {
|
||||
t.Fatal("Empty image")
|
||||
t.Fatal("Invalid image type")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestVipsSave(t *testing.T) {
|
||||
img, _ := os.Open(path.Join("fixtures", "test.jpg"))
|
||||
buf, _ := ioutil.ReadAll(img)
|
||||
defer img.Close()
|
||||
|
||||
image, _, _ := vipsRead(buf)
|
||||
if image == nil {
|
||||
t.Fatal("Empty image")
|
||||
}
|
||||
|
||||
image, _, _ := vipsRead(readImage("test.jpg"))
|
||||
options := vipsSaveOptions{Quality: 95, Type: JPEG}
|
||||
|
||||
buf, err := vipsSave(image, options)
|
||||
|
|
@ -52,3 +40,80 @@ func TestVipsSave(t *testing.T) {
|
|||
t.Fatal("Empty image")
|
||||
}
|
||||
}
|
||||
|
||||
func TestVipsRotate(t *testing.T) {
|
||||
image, _, _ := vipsRead(readImage("test.jpg"))
|
||||
|
||||
newImg, err := vipsRotate(image, D90)
|
||||
if err != nil {
|
||||
t.Fatal("Cannot save the image")
|
||||
}
|
||||
|
||||
buf, _ := vipsSave(newImg, vipsSaveOptions{Quality: 95})
|
||||
if len(buf) == 0 {
|
||||
t.Fatal("Empty image")
|
||||
}
|
||||
}
|
||||
|
||||
func TestVipsZoom(t *testing.T) {
|
||||
image, _, _ := vipsRead(readImage("test.jpg"))
|
||||
|
||||
newImg, err := vipsRotate(image, D90)
|
||||
if err != nil {
|
||||
t.Fatal("Cannot save the image")
|
||||
}
|
||||
|
||||
buf, _ := vipsSave(newImg, vipsSaveOptions{Quality: 95})
|
||||
if len(buf) == 0 {
|
||||
t.Fatal("Empty image")
|
||||
}
|
||||
}
|
||||
|
||||
func TestVipsWatermark(t *testing.T) {
|
||||
image, _, _ := vipsRead(readImage("test.jpg"))
|
||||
|
||||
watermark := Watermark{
|
||||
Text: "Copy me if you can",
|
||||
Font: "sans bold 12",
|
||||
Opacity: 0.5,
|
||||
Width: 200,
|
||||
DPI: 100,
|
||||
Margin: 100,
|
||||
Background: Color{255, 255, 255},
|
||||
}
|
||||
|
||||
newImg, err := vipsWatermark(image, watermark)
|
||||
if err != nil {
|
||||
t.Errorf("Cannot add watermark: %s", err)
|
||||
}
|
||||
|
||||
buf, _ := vipsSave(newImg, vipsSaveOptions{Quality: 95})
|
||||
if len(buf) == 0 {
|
||||
t.Fatal("Empty image")
|
||||
}
|
||||
}
|
||||
|
||||
func TestVipsImageType(t *testing.T) {
|
||||
imgType := vipsImageType(readImage("test.jpg"))
|
||||
if imgType != JPEG {
|
||||
t.Fatal("Invalid image type")
|
||||
}
|
||||
}
|
||||
|
||||
func TestVipsMemory(t *testing.T) {
|
||||
mem := VipsMemory()
|
||||
|
||||
if mem.Memory < 1024 {
|
||||
t.Fatal("Invalid memory")
|
||||
}
|
||||
if mem.Allocations == 0 {
|
||||
t.Fatal("Invalid memory allocations")
|
||||
}
|
||||
}
|
||||
|
||||
func readImage(file string) []byte {
|
||||
img, _ := os.Open(path.Join("fixtures", file))
|
||||
buf, _ := ioutil.ReadAll(img)
|
||||
defer img.Close()
|
||||
return buf
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue