rewriter_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 := `<figure><img src="https://imgs.xkcd.com/comics/thermostat.png" 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."/><figcaption><p>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.</p></figcaption></figure>`
  31. if expected != output {
  32. t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected)
  33. }
  34. }
  35. func TestRewriteWithXkcdLinkAndImageNoTitle(t *testing.T) {
  36. description := `<img src="https://imgs.xkcd.com/comics/thermostat.png" 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." />`
  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 TestRewriteWithXkcdLinkAndNoImage(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 TestRewriteWithXkcdAndNoImage(t *testing.T) {
  52. description := "test"
  53. output := Rewriter("https://xkcd.com/1912/", description, ``)
  54. expected := description
  55. if expected != output {
  56. t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected)
  57. }
  58. }
  59. func TestRewriteWithPDFLink(t *testing.T) {
  60. description := "test"
  61. output := Rewriter("https://example.org/document.pdf", description, ``)
  62. expected := `<a href="https://example.org/document.pdf">PDF</a><br>test`
  63. if expected != output {
  64. t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected)
  65. }
  66. }