metadata_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package bimg
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "path"
  6. "testing"
  7. )
  8. func TestSize(t *testing.T) {
  9. files := []struct {
  10. name string
  11. width int
  12. height int
  13. }{
  14. {"test.jpg", 1680, 1050},
  15. {"test.png", 400, 300},
  16. {"test.webp", 550, 368},
  17. }
  18. for _, file := range files {
  19. size, err := Size(readFile(file.name))
  20. if err != nil {
  21. t.Fatalf("Cannot read the image: %#v", err)
  22. }
  23. if size.Width != file.width || size.Height != file.height {
  24. t.Fatalf("Unexpected image size: %dx%d", size.Width, size.Height)
  25. }
  26. }
  27. }
  28. func TestMetadata(t *testing.T) {
  29. files := []struct {
  30. name string
  31. format string
  32. orientation int
  33. alpha bool
  34. profile bool
  35. space string
  36. }{
  37. {"test.jpg", "jpeg", 0, false, false, "srgb"},
  38. {"test_icc_prophoto.jpg", "jpeg", 0, false, true, "srgb"},
  39. {"test.png", "png", 0, true, false, "srgb"},
  40. {"test.webp", "webp", 0, false, false, "srgb"},
  41. }
  42. for _, file := range files {
  43. metadata, err := Metadata(readFile(file.name))
  44. if err != nil {
  45. t.Fatalf("Cannot read the image: %s -> %s", file.name, err)
  46. }
  47. if metadata.Type != file.format {
  48. t.Fatalf("Unexpected image format: %s", file.format)
  49. }
  50. if metadata.Orientation != file.orientation {
  51. t.Fatalf("Unexpected image orientation: %d != %d", metadata.Orientation, file.orientation)
  52. }
  53. if metadata.Alpha != file.alpha {
  54. t.Fatalf("Unexpected image alpha: %t != %t", metadata.Alpha, file.alpha)
  55. }
  56. if metadata.Profile != file.profile {
  57. t.Fatalf("Unexpected image profile: %t != %t", metadata.Profile, file.profile)
  58. }
  59. if metadata.Space != file.space {
  60. t.Fatalf("Unexpected image profile: %t != %t", metadata.Profile, file.profile)
  61. }
  62. }
  63. }
  64. func TestImageInterpretation(t *testing.T) {
  65. files := []struct {
  66. name string
  67. interpretation Interpretation
  68. }{
  69. {"test.jpg", InterpretationSRGB},
  70. {"test.png", InterpretationSRGB},
  71. {"test.webp", InterpretationSRGB},
  72. }
  73. for _, file := range files {
  74. interpretation, err := ImageInterpretation(readFile(file.name))
  75. if err != nil {
  76. t.Fatalf("Cannot read the image: %s -> %s", file.name, err)
  77. }
  78. if interpretation != file.interpretation {
  79. t.Fatalf("Unexpected image interpretation")
  80. }
  81. }
  82. }
  83. func TestColourspaceIsSupported(t *testing.T) {
  84. files := []struct {
  85. name string
  86. }{
  87. {"test.jpg"},
  88. {"test.png"},
  89. {"test.webp"},
  90. }
  91. for _, file := range files {
  92. supported, err := ColourspaceIsSupported(readFile(file.name))
  93. if err != nil {
  94. t.Fatalf("Cannot read the image: %s -> %s", file.name, err)
  95. }
  96. if supported != true {
  97. t.Fatalf("Unsupported image colourspace")
  98. }
  99. }
  100. supported, err := initImage("test.jpg").ColourspaceIsSupported()
  101. if err != nil {
  102. t.Errorf("Cannot process the image: %#v", err)
  103. }
  104. if supported != true {
  105. t.Errorf("Non-supported colourspace")
  106. }
  107. }
  108. func readFile(file string) []byte {
  109. data, _ := os.Open(path.Join("fixtures", file))
  110. buf, _ := ioutil.ReadAll(data)
  111. return buf
  112. }