media_test.go 2.7 KB

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