mirror of
https://github.com/talgo-cloud/bimg.git
synced 2026-03-08 23:18:19 -07:00
feat(test): add vertical image fixtures with multiple test cases
This commit is contained in:
parent
22f7c36eb1
commit
e7feecf376
4 changed files with 59 additions and 14 deletions
BIN
fixtures/vertical.jpg
Normal file
BIN
fixtures/vertical.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 2.9 MiB |
1
image.go
1
image.go
|
|
@ -1,5 +1,6 @@
|
||||||
package bimg
|
package bimg
|
||||||
|
|
||||||
|
// Image encapsulates the whole image buffer
|
||||||
type Image struct {
|
type Image struct {
|
||||||
buffer []byte
|
buffer []byte
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,11 +6,13 @@ package bimg
|
||||||
*/
|
*/
|
||||||
import "C"
|
import "C"
|
||||||
|
|
||||||
|
// ImageSize represents the image width and height values
|
||||||
type ImageSize struct {
|
type ImageSize struct {
|
||||||
Width int
|
Width int
|
||||||
Height int
|
Height int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ImageMedatada represents the basic metadata fields
|
||||||
type ImageMetadata struct {
|
type ImageMetadata struct {
|
||||||
Orientation int
|
Orientation int
|
||||||
Channels int
|
Channels int
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
"strconv"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -28,27 +29,68 @@ func TestResize(t *testing.T) {
|
||||||
Write("fixtures/test_out.jpg", newImg)
|
Write("fixtures/test_out.jpg", newImg)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestResizeCustomSizes(t *testing.T) {
|
func TestResizeVerticalImage(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
file string
|
|
||||||
format ImageType
|
format ImageType
|
||||||
options Options
|
options Options
|
||||||
}{
|
}{
|
||||||
{"test.jpg", JPEG, Options{Width: 800, Height: 600}},
|
{JPEG, Options{Width: 800, Height: 600}},
|
||||||
{"test.jpg", JPEG, Options{Width: 1000, Height: 1000}},
|
{JPEG, Options{Width: 1000, Height: 1000}},
|
||||||
{"test.jpg", JPEG, Options{Width: 100, Height: 50}},
|
{JPEG, Options{Width: 1000, Height: 1500}},
|
||||||
{"test.jpg", JPEG, Options{Width: 2000, Height: 2000}},
|
{JPEG, Options{Width: 100, Height: 50}},
|
||||||
{"test.jpg", JPEG, Options{Width: 500, Height: 1000}},
|
{JPEG, Options{Width: 2000, Height: 2000}},
|
||||||
{"test.jpg", JPEG, Options{Width: 500}},
|
{JPEG, Options{Width: 500, Height: 1000}},
|
||||||
{"test.jpg", JPEG, Options{Height: 500}},
|
{JPEG, Options{Width: 500}},
|
||||||
{"test.jpg", JPEG, Options{Crop: true, Width: 500, Height: 1000}},
|
{JPEG, Options{Height: 500}},
|
||||||
{"test.jpg", JPEG, Options{Crop: true, Enlarge: true, Width: 2000, Height: 1400}},
|
{JPEG, Options{Crop: true, Width: 500, Height: 1000}},
|
||||||
{"test.jpg", JPEG, Options{Enlarge: true, Force: true, Width: 2000, Height: 2000}},
|
{JPEG, Options{Crop: true, Enlarge: true, Width: 2000, Height: 1400}},
|
||||||
{"test.jpg", JPEG, Options{Force: true, Width: 2000, Height: 2000}},
|
{JPEG, Options{Enlarge: true, Force: true, Width: 2000, Height: 2000}},
|
||||||
|
{JPEG, Options{Force: true, Width: 2000, Height: 2000}},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
buf, _ := Read("fixtures/vertical.jpg")
|
||||||
|
for _, test := range tests {
|
||||||
|
image, err := Resize(buf, test.options)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Resize(imgData, %#v) error: %#v", test.options, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if DetermineImageType(image) != test.format {
|
||||||
|
t.Fatal("Image format is invalid. Expected: %s", test.format)
|
||||||
|
}
|
||||||
|
|
||||||
|
size, _ := Size(image)
|
||||||
|
if test.options.Height > 0 && size.Height != test.options.Height {
|
||||||
|
t.Fatalf("Invalid height: %d", size.Height)
|
||||||
|
}
|
||||||
|
if test.options.Width > 0 && size.Width != test.options.Width {
|
||||||
|
t.Fatalf("Invalid width: %d", size.Width)
|
||||||
|
}
|
||||||
|
|
||||||
|
Write("fixtures/test_vertical_"+strconv.Itoa(test.options.Width)+"x"+strconv.Itoa(test.options.Height)+".jpg", image)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestResizeCustomSizes(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
format ImageType
|
||||||
|
options Options
|
||||||
|
}{
|
||||||
|
{JPEG, Options{Width: 800, Height: 600}},
|
||||||
|
{JPEG, Options{Width: 1000, Height: 1000}},
|
||||||
|
{JPEG, Options{Width: 100, Height: 50}},
|
||||||
|
{JPEG, Options{Width: 2000, Height: 2000}},
|
||||||
|
{JPEG, Options{Width: 500, Height: 1000}},
|
||||||
|
{JPEG, Options{Width: 500}},
|
||||||
|
{JPEG, Options{Height: 500}},
|
||||||
|
{JPEG, Options{Crop: true, Width: 500, Height: 1000}},
|
||||||
|
{JPEG, Options{Crop: true, Enlarge: true, Width: 2000, Height: 1400}},
|
||||||
|
{JPEG, Options{Enlarge: true, Force: true, Width: 2000, Height: 2000}},
|
||||||
|
{JPEG, Options{Force: true, Width: 2000, Height: 2000}},
|
||||||
|
}
|
||||||
|
|
||||||
|
buf, _ := Read("fixtures/test.jpg")
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
buf, _ := Read("fixtures/" + test.file)
|
|
||||||
image, err := Resize(buf, test.options)
|
image, err := Resize(buf, test.options)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Resize(imgData, %#v) error: %#v", test.options, err)
|
t.Errorf("Resize(imgData, %#v) error: %#v", test.options, err)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue