| 31 | } |
| 32 | |
| 33 | func TestDetect(t *testing.T) { |
| 34 | tests := []struct { |
| 35 | name string |
| 36 | file string |
| 37 | want imagetype.Type |
| 38 | }{ |
| 39 | {"JPEG", "../testdata/test-images/jpg/jpg.jpg", imagetype.JPEG}, |
| 40 | {"JXL", "../testdata/test-images/jxl/jxl.jxl", imagetype.JXL}, |
| 41 | {"PNG", "../testdata/test-images/png/png.png", imagetype.PNG}, |
| 42 | {"WEBP", "../testdata/test-images/webp/webp.webp", imagetype.WEBP}, |
| 43 | {"GIF", "../testdata/test-images/gif/gif.gif", imagetype.GIF}, |
| 44 | {"ICO", "../testdata/test-images/ico/png-256x256.ico", imagetype.ICO}, |
| 45 | {"SVG", "../testdata/test-images/svg/svg.svg", imagetype.SVG}, |
| 46 | {"HEIC", "../testdata/test-images/heif/heif.heif", imagetype.HEIC}, |
| 47 | {"BMP", "../testdata/test-images/bmp/24-bpp.bmp", imagetype.BMP}, |
| 48 | {"TIFF", "../testdata/test-images/tiff/tiff.tiff", imagetype.TIFF}, |
| 49 | {"SVG", "../testdata/test-images/svg/svg.svg", imagetype.SVG}, |
| 50 | {"RAW", "../testdata/test-images/raw/RAW_CANON_1DM2.CR2", imagetype.Unknown}, // RAW is not supported |
| 51 | } |
| 52 | |
| 53 | for _, tt := range tests { |
| 54 | t.Run(tt.name, func(t *testing.T) { |
| 55 | f, err := os.Open(tt.file) |
| 56 | require.NoError(t, err) |
| 57 | defer f.Close() |
| 58 | |
| 59 | got, err := imagetype.Detect(f, "", path.Ext(tt.file)) |
| 60 | if tt.want == imagetype.Unknown { |
| 61 | require.Error(t, err) |
| 62 | } else { |
| 63 | require.NoError(t, err) |
| 64 | } |
| 65 | require.Equal(t, tt.want, got) |
| 66 | }) |
| 67 | } |
| 68 | } |