media_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Copyright 2019 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package media // import "miniflux.app/reader/media"
  5. import "testing"
  6. func TestContentMimeType(t *testing.T) {
  7. scenarios := []struct {
  8. inputType, inputMedium, expectedMimeType string
  9. }{
  10. {"image/png", "image", "image/png"},
  11. {"", "image", "image/*"},
  12. {"", "video", "video/*"},
  13. {"", "audio", "audio/*"},
  14. {"", "", "application/octet-stream"},
  15. }
  16. for _, scenario := range scenarios {
  17. content := &Content{Type: scenario.inputType, Medium: scenario.inputMedium}
  18. result := content.MimeType()
  19. if result != scenario.expectedMimeType {
  20. t.Errorf(`Unexpected mime type, got %q instead of %q for type=%q medium=%q`,
  21. result,
  22. scenario.expectedMimeType,
  23. scenario.inputType,
  24. scenario.inputMedium,
  25. )
  26. }
  27. }
  28. }
  29. func TestContentSize(t *testing.T) {
  30. scenarios := []struct {
  31. inputSize string
  32. expectedSize int64
  33. }{
  34. {"", 0},
  35. {"123", int64(123)},
  36. }
  37. for _, scenario := range scenarios {
  38. content := &Content{FileSize: scenario.inputSize}
  39. result := content.Size()
  40. if result != scenario.expectedSize {
  41. t.Errorf(`Unexpected size, got %d instead of %d for %q`,
  42. result,
  43. scenario.expectedSize,
  44. scenario.inputSize,
  45. )
  46. }
  47. }
  48. }
  49. func TestPeerLinkType(t *testing.T) {
  50. scenarios := []struct {
  51. inputType string
  52. expectedMimeType string
  53. }{
  54. {"", "application/octet-stream"},
  55. {"application/x-bittorrent", "application/x-bittorrent"},
  56. }
  57. for _, scenario := range scenarios {
  58. peerLink := &PeerLink{Type: scenario.inputType}
  59. result := peerLink.MimeType()
  60. if result != scenario.expectedMimeType {
  61. t.Errorf(`Unexpected mime type, got %q instead of %q for %q`,
  62. result,
  63. scenario.expectedMimeType,
  64. scenario.inputType,
  65. )
  66. }
  67. }
  68. }
  69. func TestDescription(t *testing.T) {
  70. scenarios := []struct {
  71. inputType string
  72. inputContent string
  73. expectedDescription string
  74. }{
  75. {"", "", ""},
  76. {"html", "a <b>c</b>", "a <b>c</b>"},
  77. {"plain", "a\nhttp://www.example.org/", `a<br><a href="http://www.example.org/">http://www.example.org/</a>`},
  78. }
  79. for _, scenario := range scenarios {
  80. desc := &Description{Type: scenario.inputType, Description: scenario.inputContent}
  81. result := desc.HTML()
  82. if result != scenario.expectedDescription {
  83. t.Errorf(`Unexpected description, got %q instead of %q for %q`,
  84. result,
  85. scenario.expectedDescription,
  86. scenario.inputType,
  87. )
  88. }
  89. }
  90. }
  91. func TestFirstDescription(t *testing.T) {
  92. var descList DescriptionList
  93. descList = append(descList, Description{})
  94. descList = append(descList, Description{Description: "Something"})
  95. if descList.First() != "Something" {
  96. t.Errorf(`Unexpected description`)
  97. }
  98. }