utils_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package processor // import "miniflux.app/v2/internal/reader/processor"
  4. import (
  5. "testing"
  6. "time"
  7. )
  8. func TestISO8601DurationParsing(t *testing.T) {
  9. var scenarios = []struct {
  10. duration string
  11. expected time.Duration
  12. }{
  13. // Live streams and radio.
  14. {"PT0M0S", 0},
  15. // https://www.youtube.com/watch?v=HLrqNhgdiC0
  16. {"PT6M20S", (6 * time.Minute) + (20 * time.Second)},
  17. // https://www.youtube.com/watch?v=LZa5KKfqHtA
  18. {"PT5M41S", (5 * time.Minute) + (41 * time.Second)},
  19. // https://www.youtube.com/watch?v=yIxEEgEuhT4
  20. {"PT51M52S", (51 * time.Minute) + (52 * time.Second)},
  21. // https://www.youtube.com/watch?v=bpHf1XcoiFs
  22. {"PT80M42S", (1 * time.Hour) + (20 * time.Minute) + (42 * time.Second)},
  23. // Hours only
  24. {"PT2H", 2 * time.Hour},
  25. // Seconds only
  26. {"PT30S", 30 * time.Second},
  27. // Hours and minutes
  28. {"PT1H30M", (1 * time.Hour) + (30 * time.Minute)},
  29. // Hours and seconds
  30. {"PT2H45S", (2 * time.Hour) + (45 * time.Second)},
  31. // Empty duration
  32. {"PT", 0},
  33. }
  34. for _, tc := range scenarios {
  35. result, err := parseISO8601Duration(tc.duration)
  36. if err != nil {
  37. t.Errorf("Got an error when parsing %q: %v", tc.duration, err)
  38. }
  39. if tc.expected != result {
  40. t.Errorf(`Unexpected result, got %v for duration %q`, result, tc.duration)
  41. }
  42. }
  43. }
  44. func TestISO8601DurationParsingErrors(t *testing.T) {
  45. var errorScenarios = []struct {
  46. duration string
  47. expectedErr string
  48. }{
  49. // Missing PT prefix
  50. {"6M20S", "the period doesn't start with PT"},
  51. // Unsupported Year specifier
  52. {"PT1Y", "the 'Y' specifier isn't supported"},
  53. // Unsupported Week specifier
  54. {"PT2W", "the 'W' specifier isn't supported"},
  55. // Unsupported Day specifier
  56. {"PT3D", "the 'D' specifier isn't supported"},
  57. // Invalid number for hours (letter at start of number)
  58. {"PTaH", "invalid character in the period"},
  59. // Invalid number for minutes (letter at start of number)
  60. {"PTbM", "invalid character in the period"},
  61. // Invalid number for seconds (letter at start of number)
  62. {"PTcS", "invalid character in the period"},
  63. // Invalid character in the middle of a number
  64. {"PT1a2H", "invalid character in the period"},
  65. {"PT3b4M", "invalid character in the period"},
  66. {"PT5c6S", "invalid character in the period"},
  67. // Test cases for actual ParseFloat errors (empty number before specifier)
  68. {"PTH", "strconv.ParseFloat: parsing \"\": invalid syntax"},
  69. {"PTM", "strconv.ParseFloat: parsing \"\": invalid syntax"},
  70. {"PTS", "strconv.ParseFloat: parsing \"\": invalid syntax"},
  71. // Invalid character
  72. {"PT1X", "invalid character in the period"},
  73. // Invalid character mixed
  74. {"PT1H@M", "invalid character in the period"},
  75. }
  76. for _, tc := range errorScenarios {
  77. _, err := parseISO8601Duration(tc.duration)
  78. if err == nil {
  79. t.Errorf("Expected an error when parsing %q, but got none", tc.duration)
  80. } else if err.Error() != tc.expectedErr {
  81. t.Errorf("Expected error %q when parsing %q, but got %q", tc.expectedErr, tc.duration, err.Error())
  82. }
  83. }
  84. }
  85. func TestMinifyEntryContentWithWhitespace(t *testing.T) {
  86. input := `<p> Some text with a <a href="http://example.org/"> link </a> </p>`
  87. expected := `<p>Some text with a <a href="http://example.org/">link</a></p>`
  88. result := minifyContent(input)
  89. if expected != result {
  90. t.Errorf(`Unexpected result, got %q`, result)
  91. }
  92. }
  93. func TestMinifyContentWithDefaultAttributes(t *testing.T) {
  94. input := `<script type="application/javascript">console.log("Hello, World!");</script>`
  95. expected := `<script>console.log("Hello, World!");</script>`
  96. result := minifyContent(input)
  97. if expected != result {
  98. t.Errorf(`Unexpected result, got %q`, result)
  99. }
  100. }
  101. func TestMinifyContentWithComments(t *testing.T) {
  102. input := `<p>Some text<!-- This is a comment --> with a <a href="http://example.org/">link</a>.</p>`
  103. expected := `<p>Some text with a <a href="http://example.org/">link</a>.</p>`
  104. result := minifyContent(input)
  105. if expected != result {
  106. t.Errorf(`Unexpected result, got %q`, result)
  107. }
  108. }
  109. func TestMinifyContentWithSpecialComments(t *testing.T) {
  110. input := `<p>Some text <!--[if IE 6]><p>IE6</p><![endif]--> with a <a href="http://example.org/">link</a>.</p>`
  111. expected := `<p>Some text with a <a href="http://example.org/">link</a>.</p>`
  112. result := minifyContent(input)
  113. if expected != result {
  114. t.Errorf(`Unexpected result, got %q`, result)
  115. }
  116. }