Skip to content

Commit

Permalink
moved MustOpenGif to testutils
Browse files Browse the repository at this point in the history
  • Loading branch information
logica0419 committed Dec 10, 2023
1 parent 3471bbf commit 113444a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 26 deletions.
43 changes: 17 additions & 26 deletions service/imaging/processor_default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ import (
"image"
"image/png"
"io"
"io/fs"
"os"
"testing"

"github.com/stretchr/testify/assert"
"github.com/traPtitech/traQ/testdata/gif"
"github.com/traPtitech/traQ/testutils"
"github.com/traPtitech/traQ/utils"
)

Expand All @@ -34,6 +33,15 @@ func setup() (Processor, *os.File) {
return processor, mustOpen("test.png")
}

func setupCustomConc(conc int) Processor {
processor := NewProcessor(Config{
MaxPixels: 500 * 500,
Concurrency: conc,
ThumbnailMaxSize: image.Point{X: 50, Y: 50},
})
return processor
}

func assertImg(t *testing.T, actualImg image.Image, expectedFilePath string) {
actualImgBytesBuffer := &bytes.Buffer{}
err := png.Encode(actualImgBytesBuffer, actualImg)
Expand Down Expand Up @@ -71,23 +79,6 @@ func TestProcessorDefault_Fit(t *testing.T) {
assertImg(t, actualImg, "test_fit.png")
}

func setupCustomConc(conc int) Processor {
processor := NewProcessor(Config{
MaxPixels: 500 * 500,
Concurrency: conc,
ThumbnailMaxSize: image.Point{X: 50, Y: 50},
})
return processor
}

func mustOpenGif(name string) fs.File {
f, err := gif.FS.Open(name)
if err != nil {
panic(err)
}
return f
}

func TestProcessorDefault_FitAnimationGIF(t *testing.T) {
t.Parallel()

Expand All @@ -108,38 +99,38 @@ func TestProcessorDefault_FitAnimationGIF(t *testing.T) {
},
{
name: "invalid (invalid gif)",
reader: io.LimitReader(mustOpenGif("cube.gif"), 10),
reader: io.LimitReader(testutils.MustOpenGif("cube.gif"), 10),
want: nil,
err: ErrInvalidImageSrc,
},
{
name: "success (cube 正方形、透明ピクセルあり)",
file: "cube.gif",
want: utils.MustIoReaderToBytes(mustOpenGif("cube_resized.gif")),
want: utils.MustIoReaderToBytes(testutils.MustOpenGif("cube_resized.gif")),
err: nil,
},
{
name: "success (miku 横長、差分最適化)",
file: "miku.gif",
want: utils.MustIoReaderToBytes(mustOpenGif("miku_resized.gif")),
want: utils.MustIoReaderToBytes(testutils.MustOpenGif("miku_resized.gif")),
err: nil,
},
{
name: "success (parapara 正方形、差分最適化)",
file: "parapara.gif",
want: utils.MustIoReaderToBytes(mustOpenGif("parapara_resized.gif")),
want: utils.MustIoReaderToBytes(testutils.MustOpenGif("parapara_resized.gif")),
err: nil,
},
{
name: "success (miku2 縦長、差分最適化)",
file: "miku2.gif",
want: utils.MustIoReaderToBytes(mustOpenGif("miku2_resized.gif")),
want: utils.MustIoReaderToBytes(testutils.MustOpenGif("miku2_resized.gif")),
err: nil,
},
{
name: "success (rabbit 小サイズ)",
file: "rabbit.gif",
want: utils.MustIoReaderToBytes(mustOpenGif("rabbit_resized.gif")),
want: utils.MustIoReaderToBytes(testutils.MustOpenGif("rabbit_resized.gif")),
err: nil,
},
}
Expand All @@ -159,7 +150,7 @@ func TestProcessorDefault_FitAnimationGIF(t *testing.T) {

processor := setupCustomConc(conc)
if tt.file != "" { // ファイルはこのタイミングで開かないと正常なデータにならない
tt.reader = mustOpenGif(tt.file)
tt.reader = testutils.MustOpenGif(tt.file)
}

actual, err := processor.FitAnimationGIF(tt.reader, 256, 256)
Expand Down
15 changes: 15 additions & 0 deletions testutils/gif.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package testutils

import (
"io/fs"

"github.com/traPtitech/traQ/testdata/gif"
)

func MustOpenGif(name string) fs.File {
f, err := gif.FS.Open(name)
if err != nil {
panic(err)
}
return f
}

0 comments on commit 113444a

Please sign in to comment.