Skip to content
Snippets Groups Projects
Commit f9b86f44 authored by ensiouel's avatar ensiouel
Browse files

обновление тестов

parent 3f7418b1
No related branches found
No related tags found
No related merge requests found
......@@ -20,9 +20,9 @@ const (
)
func init() {
RegisterFormatEncoder(JPEG, func(w io.Writer, img image.Image) error { return jpeg.Encode(w, img, nil) }, "jpg", "jpeg")
RegisterFormatEncoder(PNG, png.Encode, "png")
RegisterFormatEncoder(JPEG, func(w io.Writer, img image.Image) error { return jpeg.Encode(w, img, nil) }, "jpg")
RegisterFormatEncoder(PNG, png.Encode)
RegisterFormatEncoder(GIF, func(w io.Writer, img image.Image) error { return gif.Encode(w, img, nil) })
RegisterFormatEncoder(TIFF, func(w io.Writer, img image.Image) error { return tiff.Encode(w, img, nil) })
RegisterFormatEncoder(TIFF, func(w io.Writer, img image.Image) error { return tiff.Encode(w, img, nil) }, "tif")
RegisterFormatEncoder(BMP, bmp.Encode)
}
......@@ -21,6 +21,7 @@ var (
func RegisterFormatEncoder(format Format, fn EncodeFunc, extensions ...string) {
defaultFormatEncoderRegistry[format] = fn
formatExtensions[string(format)] = format
for _, ext := range extensions {
formatExtensions[strings.TrimPrefix(strings.ToLower(ext), ".")] = format
}
......@@ -66,12 +67,3 @@ func FormatFromExtension(ext string) (Format, error) {
}
return "", errors.Errorf("unsupported format")
}
//func NormalizeFormat(format string) string {
// format = strings.ToLower(format)
// format = strings.TrimPrefix(format, ".")
// //if v, ok := builtinFormats[format]; ok {
// // return v
// //}
// return format
//}
......@@ -15,24 +15,52 @@ func TestOpen(t *testing.T) {
return
}
require.NoError(t, err)
require.Equal(t, JPEG, ext)
require.Equal(t, "jpeg", ext)
}
func TestNormalizeFormat(t *testing.T) {
require.Equal(t, NormalizeFormat("jpg"), JPEG)
require.Equal(t, NormalizeFormat("png"), PNG)
require.Equal(t, NormalizeFormat("tif"), TIFF)
require.Equal(t, NormalizeFormat("any"), "any")
require.Equal(t, NormalizeFormat(".jpg"), JPEG)
require.Equal(t, NormalizeFormat(".gif"), GIF)
require.Equal(t, NormalizeFormat(".bmp"), BMP)
require.Equal(t, NormalizeFormat(".any"), "any")
func TestFormatFromExtension(t *testing.T) {
var tests = []struct {
name string
input string
output Format
wantErr bool
}{
{
name: "correct jpeg",
input: "jpg",
output: JPEG,
wantErr: false,
},
{
name: "correct png",
input: "png",
output: PNG,
wantErr: false,
},
{
name: "incorrect any",
input: "any",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
format, err := FormatFromExtension(tt.input)
if tt.wantErr {
require.Error(t, err)
} else {
require.NoError(t, err)
require.Equal(t, tt.output, format)
}
})
}
}
func TestEncode(t *testing.T) {
var tests = []struct {
name string
input string
input Format
wantErr bool
}{
{
......@@ -95,31 +123,31 @@ func TestDecode(t *testing.T) {
{
name: "jpeg format",
input: "testdata/1.jpeg",
output: JPEG,
output: "jpeg",
wantErr: false,
},
{
name: "png format",
input: "testdata/1.png",
output: PNG,
output: "png",
wantErr: false,
},
{
name: "gif format",
input: "testdata/1.gif",
output: GIF,
output: "gif",
wantErr: false,
},
{
name: "tiff format",
input: "testdata/1.tiff",
output: TIFF,
output: "tiff",
wantErr: false,
},
{
name: "bmp format",
input: "testdata/1.bmp",
output: BMP,
output: "bmp",
wantErr: false,
},
}
......
......@@ -26,5 +26,5 @@ func TestDecodeWebP(t *testing.T) {
defer file.Close()
_, ext, err := Decode(file)
require.NoError(t, err)
require.Equal(t, WEBP, ext)
require.Equal(t, "webp", ext)
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment