Renamed Decoding and Encoding funcs

This commit is contained in:
Samuel Hawksby-Robinson 2020-10-09 12:17:04 +01:00 committed by Andrea Maria Piana
parent 0d6e6699ec
commit a6df8adeba
5 changed files with 11 additions and 77 deletions

View File

@ -12,7 +12,7 @@ import (
"golang.org/x/image/webp"
)
func Get(fileName string) (image.Image, error) {
func Decode(fileName string) (image.Image, error) {
file, err := os.Open(fileName)
if err != nil {
return nil, err

View File

@ -11,7 +11,7 @@ const (
path = "../_assets/tests/"
)
func TestGet(t *testing.T) {
func TestDecode(t *testing.T) {
cs := []struct {
Filepath string
@ -64,7 +64,7 @@ func TestGet(t *testing.T) {
}
for _, c := range cs {
img, err := Get(path + c.Filepath)
img, err := Decode(path + c.Filepath)
if c.Error {
require.Error(t, err)

View File

@ -4,28 +4,9 @@ import (
"image"
"image/jpeg"
"io"
"os"
)
func RenderAndMakeFile(img image.Image, imgDetail *Details) error {
out, err := os.Create(imgDetail.FileName)
if err != nil {
return err
}
defer out.Close()
err = Render(out, img, imgDetail)
if err != nil {
return err
}
fi, _ := out.Stat()
imgDetail.SizeFile = fi.Size()
return nil
}
func Render(w io.Writer, img image.Image, imgDetail *Details) error {
func Encode(w io.Writer, img image.Image, imgDetail *Details) error {
// Currently a wrapper for renderJpeg, but this function is useful if multiple render formats are needed
return renderJpeg(w, img, imgDetail)
}

View File

@ -2,13 +2,12 @@ package images
import (
"bytes"
"os"
"testing"
"github.com/stretchr/testify/require"
)
func TestRender(t *testing.T) {
func TestEncode(t *testing.T) {
cs := []struct {
FileName string
RenderSize int
@ -35,60 +34,13 @@ func TestRender(t *testing.T) {
}
for _, c := range cs {
img, err := Get(path + c.FileName)
img, err := Decode(path + c.FileName)
require.NoError(t, err)
bb := bytes.NewBuffer([]byte{})
err = Render(bb, img, &options)
err = Encode(bb, img, &options)
require.NoError(t, err)
require.Exactly(t, c.RenderSize, bb.Len())
}
}
func TestMakeAndRenderFile(t *testing.T) {
cs := []struct {
FileName string
OutName string
OutputSize int64
}{
{
"elephant.jpg",
"_elephant.jpg",
1447,
},
{
"rose.webp",
"_rose.jpg",
11119,
},
{
"spin.gif",
"_spin.jpg",
2263,
},
{
"status.png",
"_status.jpg",
5834,
},
}
for _, c := range cs {
img, err := Get(path + c.FileName)
require.NoError(t, err)
options := &Details{
FileName: path + c.OutName,
Quality: 70,
}
err = RenderAndMakeFile(img, options)
require.NoError(t, err)
require.Exactly(t, c.OutputSize, options.SizeFile)
// tidy up
err = os.Remove(options.FileName)
require.NoError(t, err)
}
}

View File

@ -3,9 +3,10 @@ package images
import (
"bytes"
"errors"
"github.com/stretchr/testify/require"
"image"
"testing"
"github.com/stretchr/testify/require"
)
func TestResize(t *testing.T) {
@ -76,7 +77,7 @@ func TestCrop(t *testing.T) {
}
for _, c := range cs {
img, err := Get(path + c.FileName)
img, err := Decode(path + c.FileName)
require.NoError(t, err)
for _, p := range c.Params {
@ -91,7 +92,7 @@ func TestCrop(t *testing.T) {
require.Exactly(t, p.OutputBound.Dy(), cImg.Bounds().Dy(), c.FileName)
bb := bytes.NewBuffer([]byte{})
err = Render(bb, cImg, options)
err = Encode(bb, cImg, options)
require.NoError(t, err)
require.Exactly(t, p.OutputSize, bb.Len())
}