Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
nicksanford committed Jan 6, 2025
1 parent e850ece commit b2a7095
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions rimage/lazy_encoded_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package rimage
import (
"bytes"
"image"
"image/jpeg"
"image/png"
"testing"

Expand All @@ -15,10 +16,14 @@ func TestLazyEncodedImage(t *testing.T) {
img := image.NewNRGBA(image.Rect(0, 0, 4, 8))
img.Set(3, 3, Red)

var buf bytes.Buffer
test.That(t, png.Encode(&buf, img), test.ShouldBeNil)
var pngBuf bytes.Buffer
test.That(t, png.Encode(&pngBuf, img), test.ShouldBeNil)
var jpegBuf bytes.Buffer
test.That(t, jpeg.Encode(&jpegBuf, img, &jpeg.Options{Quality: 100}), test.ShouldBeNil)
jpegImg, err := jpeg.Decode(bytes.NewBuffer(jpegBuf.Bytes()))
test.That(t, err, test.ShouldBeNil)

imgLazy := NewLazyEncodedImage(buf.Bytes(), utils.MimeTypePNG)
imgLazy := NewLazyEncodedImage(pngBuf.Bytes(), utils.MimeTypePNG)

test.That(t, imgLazy.(*LazyEncodedImage).MIMEType(), test.ShouldEqual, utils.MimeTypePNG)
test.That(t, NewColorFromColor(imgLazy.At(0, 0)), test.ShouldEqual, Black)
Expand Down Expand Up @@ -46,4 +51,26 @@ func TestLazyEncodedImage(t *testing.T) {
test.That(t, func() { imgLazy.ColorModel() }, test.ShouldPanic)
test.That(t, func() { NewColorFromColor(imgLazy.At(0, 0)) }, test.ShouldPanic)
test.That(t, func() { NewColorFromColor(imgLazy.At(4, 4)) }, test.ShouldPanic)

// png without a mime type
imgLazy = NewLazyEncodedImage(pngBuf.Bytes(), "")
test.That(t, imgLazy.(*LazyEncodedImage).MIMEType(), test.ShouldEqual, utils.MimeTypePNG)
test.That(t, NewColorFromColor(imgLazy.At(0, 0)), test.ShouldEqual, Black)
test.That(t, NewColorFromColor(imgLazy.At(3, 3)), test.ShouldEqual, Red)
test.That(t, imgLazy.Bounds(), test.ShouldResemble, img.Bounds())
test.That(t, imgLazy.ColorModel(), test.ShouldResemble, img.ColorModel())

img2, err = png.Decode(bytes.NewBuffer(imgLazy.(*LazyEncodedImage).RawData()))
test.That(t, err, test.ShouldBeNil)
test.That(t, img2, test.ShouldResemble, img)

// jpeg without a mime type
imgLazy = NewLazyEncodedImage(jpegBuf.Bytes(), "")
test.That(t, imgLazy.(*LazyEncodedImage).MIMEType(), test.ShouldEqual, utils.MimeTypeJPEG)
test.That(t, imgLazy.Bounds(), test.ShouldResemble, jpegImg.Bounds())
test.That(t, imgLazy.ColorModel(), test.ShouldResemble, jpegImg.ColorModel())

img2, err = jpeg.Decode(bytes.NewBuffer(imgLazy.(*LazyEncodedImage).RawData()))
test.That(t, err, test.ShouldBeNil)
test.That(t, img2, test.ShouldResemble, jpegImg)
}

0 comments on commit b2a7095

Please sign in to comment.