rewriter_test.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Copyright 2017 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 rewrite
  5. import "testing"
  6. func TestRewriteWithNoMatchingRule(t *testing.T) {
  7. output := Rewriter("https://example.org/article", `Some text.`, ``)
  8. expected := `Some text.`
  9. if expected != output {
  10. t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected)
  11. }
  12. }
  13. func TestRewriteWithYoutubeLink(t *testing.T) {
  14. output := Rewriter("https://www.youtube.com/watch?v=1234", `Video Description`, ``)
  15. expected := `<iframe width="650" height="350" frameborder="0" src="https://www.youtube-nocookie.com/embed/1234" allowfullscreen></iframe><p>Video Description</p>`
  16. if expected != output {
  17. t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected)
  18. }
  19. }
  20. func TestRewriteWithInexistingCustomRule(t *testing.T) {
  21. output := Rewriter("https://www.youtube.com/watch?v=1234", `Video Description`, `some rule`)
  22. expected := `Video Description`
  23. if expected != output {
  24. t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected)
  25. }
  26. }
  27. func TestRewriteWithXkcdLink(t *testing.T) {
  28. description := `<img src="https://imgs.xkcd.com/comics/thermostat.png" title="Your problem is so terrible, I worry that, if I help you, I risk drawing the attention of whatever god of technology inflicted it on you." alt="Your problem is so terrible, I worry that, if I help you, I risk drawing the attention of whatever god of technology inflicted it on you." />`
  29. output := Rewriter("https://xkcd.com/1912/", description, ``)
  30. expected := description + `<blockquote cite="https://xkcd.com/1912/">Your problem is so terrible, I worry that, if I help you, I risk drawing the attention of whatever god of technology inflicted it on you.</blockquote>`
  31. if expected != output {
  32. t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected)
  33. }
  34. }
  35. func TestRewriteWithXkcdLinkAndNoImage(t *testing.T) {
  36. description := "test"
  37. output := Rewriter("https://xkcd.com/1912/", description, ``)
  38. expected := description
  39. if expected != output {
  40. t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected)
  41. }
  42. }
  43. func TestRewriteWithXkcdAndNoImage(t *testing.T) {
  44. description := "test"
  45. output := Rewriter("https://xkcd.com/1912/", description, ``)
  46. expected := description
  47. if expected != output {
  48. t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected)
  49. }
  50. }
  51. func TestRewriteWithPDFLink(t *testing.T) {
  52. description := "test"
  53. output := Rewriter("https://example.org/document.pdf", description, ``)
  54. expected := `<a href="https://example.org/document.pdf">PDF</a><br>test`
  55. if expected != output {
  56. t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected)
  57. }
  58. }