truncate_test.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Copyright 2022 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 sanitizer
  5. import "testing"
  6. func TestTruncateHTMWithTextLowerThanLimitL(t *testing.T) {
  7. input := `This is a <strong>bug 🐛</strong>.`
  8. expected := `This is a bug 🐛.`
  9. output := TruncateHTML(input, 50)
  10. if expected != output {
  11. t.Errorf(`Wrong output: %q != %q`, expected, output)
  12. }
  13. }
  14. func TestTruncateHTMLWithTextAboveLimit(t *testing.T) {
  15. input := `This is <strong>HTML</strong>.`
  16. expected := `This…`
  17. output := TruncateHTML(input, 4)
  18. if expected != output {
  19. t.Errorf(`Wrong output: %q != %q`, expected, output)
  20. }
  21. }
  22. func TestTruncateHTMLWithUnicodeTextAboveLimit(t *testing.T) {
  23. input := `This is a <strong>bike 🚲</strong>.`
  24. expected := `This…`
  25. output := TruncateHTML(input, 4)
  26. if expected != output {
  27. t.Errorf(`Wrong output: %q != %q`, expected, output)
  28. }
  29. }
  30. func TestTruncateHTMLWithMultilineTextAboveLimit(t *testing.T) {
  31. input := `
  32. This is a <strong>bike
  33. 🚲</strong>.
  34. `
  35. expected := `This is a bike…`
  36. output := TruncateHTML(input, 15)
  37. if expected != output {
  38. t.Errorf(`Wrong output: %q != %q`, expected, output)
  39. }
  40. }
  41. func TestTruncateHTMLWithMultilineTextLowerThanLimit(t *testing.T) {
  42. input := `
  43. This is a <strong>bike
  44. 🚲</strong>.
  45. `
  46. expected := `This is a bike 🚲.`
  47. output := TruncateHTML(input, 20)
  48. if expected != output {
  49. t.Errorf(`Wrong output: %q != %q`, expected, output)
  50. }
  51. }