type_test.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package imagetype
  2. import (
  3. "os"
  4. "testing"
  5. "github.com/stretchr/testify/require"
  6. )
  7. func TestDefaultTypesRegistered(t *testing.T) {
  8. // Test that all default types are properly registered by init()
  9. defaultTypes := []Type{
  10. JPEG, JXL, PNG, WEBP, GIF, ICO, SVG, HEIC, AVIF, BMP, TIFF,
  11. }
  12. for _, typ := range defaultTypes {
  13. t.Run(typ.String(), func(t *testing.T) {
  14. desc := GetTypeDesc(typ)
  15. require.NotNil(t, desc)
  16. // Verify that the description has non-empty fields
  17. require.NotEmpty(t, desc.String)
  18. require.NotEmpty(t, desc.Ext)
  19. require.NotEqual(t, "application/octet-stream", desc.Mime)
  20. })
  21. }
  22. }
  23. func TestDetect(t *testing.T) {
  24. tests := []struct {
  25. name string
  26. file string
  27. want Type
  28. }{
  29. {"JPEG", "../testdata/test-images/jpg/jpg.jpg", JPEG},
  30. {"JXL", "../testdata/test-images/jxl/jxl.jxl", JXL},
  31. {"PNG", "../testdata/test-images/png/png.png", PNG},
  32. {"WEBP", "../testdata/test-images/webp/webp.webp", WEBP},
  33. {"GIF", "../testdata/test-images/gif/gif.gif", GIF},
  34. {"ICO", "../testdata/test-images/ico/png-256x256.ico", ICO},
  35. {"SVG", "../testdata/test-images/svg/svg.svg", SVG},
  36. {"HEIC", "../testdata/test-images/heif/heif.heif", HEIC},
  37. {"BMP", "../testdata/test-images/bmp/24-bpp.bmp", BMP},
  38. {"TIFF", "../testdata/test-images/tiff/tiff.tiff", TIFF},
  39. {"SVG", "../testdata/test-images/svg/svg.svg", SVG},
  40. }
  41. for _, tt := range tests {
  42. t.Run(tt.name, func(t *testing.T) {
  43. f, err := os.Open(tt.file)
  44. require.NoError(t, err)
  45. defer f.Close()
  46. got, err := Detect(f)
  47. require.NoError(t, err)
  48. require.Equal(t, tt.want, got)
  49. })
  50. }
  51. }