truncate_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package sanitizer
  4. import "testing"
  5. func TestTruncateHTMWithTextLowerThanLimitL(t *testing.T) {
  6. input := `This is a <strong>bug 🐛</strong>.`
  7. expected := `This is a bug 🐛.`
  8. output := TruncateHTML(input, 50)
  9. if expected != output {
  10. t.Errorf(`Wrong output: %q != %q`, expected, output)
  11. }
  12. }
  13. func TestTruncateHTMLWithTextAboveLimit(t *testing.T) {
  14. input := `This is <strong>HTML</strong>.`
  15. expected := `This…`
  16. output := TruncateHTML(input, 4)
  17. if expected != output {
  18. t.Errorf(`Wrong output: %q != %q`, expected, output)
  19. }
  20. }
  21. func TestTruncateHTMLWithUnicodeTextAboveLimit(t *testing.T) {
  22. input := `This is a <strong>bike 🚲</strong>.`
  23. expected := `This…`
  24. output := TruncateHTML(input, 4)
  25. if expected != output {
  26. t.Errorf(`Wrong output: %q != %q`, expected, output)
  27. }
  28. }
  29. func TestTruncateHTMLWithMultilineTextAboveLimit(t *testing.T) {
  30. input := `
  31. This is a <strong>bike
  32. 🚲</strong>.
  33. `
  34. expected := `This is a bike…`
  35. output := TruncateHTML(input, 15)
  36. if expected != output {
  37. t.Errorf(`Wrong output: %q != %q`, expected, output)
  38. }
  39. }
  40. func TestTruncateHTMLWithMultilineTextLowerThanLimit(t *testing.T) {
  41. input := `
  42. This is a <strong>bike
  43. 🚲</strong>.
  44. `
  45. expected := `This is a bike 🚲.`
  46. output := TruncateHTML(input, 20)
  47. if expected != output {
  48. t.Errorf(`Wrong output: %q != %q`, expected, output)
  49. }
  50. }
  51. func TestTruncateHTMLWithMultipleSpaces(t *testing.T) {
  52. tests := []struct {
  53. name string
  54. input string
  55. maxLen int
  56. expected string
  57. }{
  58. {
  59. name: "multiple spaces",
  60. input: "hello world test",
  61. maxLen: 20,
  62. expected: "hello world test",
  63. },
  64. {
  65. name: "tabs and newlines",
  66. input: "hello\t\tworld\n\ntest",
  67. maxLen: 20,
  68. expected: "hello world test",
  69. },
  70. {
  71. name: "truncation with unicode",
  72. input: "hello world 你好",
  73. maxLen: 11,
  74. expected: "hello world…",
  75. },
  76. {
  77. name: "html stripping",
  78. input: "<p>hello <b>world</b> test</p>",
  79. maxLen: 20,
  80. expected: "hello world test",
  81. },
  82. {
  83. name: "no truncation needed",
  84. input: "hello world",
  85. maxLen: 20,
  86. expected: "hello world",
  87. },
  88. }
  89. for _, tt := range tests {
  90. t.Run(tt.name, func(t *testing.T) {
  91. result := TruncateHTML(tt.input, tt.maxLen)
  92. if result != tt.expected {
  93. t.Errorf("TruncateHTML(%q, %d) = %q, want %q",
  94. tt.input, tt.maxLen, result, tt.expected)
  95. }
  96. })
  97. }
  98. }