hypertext_test.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. package hypertext
  2. import (
  3. "servitor/ansi"
  4. "servitor/style"
  5. "testing"
  6. )
  7. func TestMergeText(t *testing.T) {
  8. lhs0 := "front"
  9. rhs0 := "back"
  10. output0 := mergeText(lhs0, rhs0)
  11. expected0 := "frontback"
  12. if expected0 != output0 {
  13. t.Fatalf("expected %s not %s", expected0, output0)
  14. }
  15. lhs1 := "front "
  16. rhs1 := " back"
  17. output1 := mergeText(lhs1, rhs1)
  18. expected1 := "front back"
  19. if expected1 != output1 {
  20. t.Fatalf("expected %s not %s", expected1, output1)
  21. }
  22. lhs2 := "front "
  23. rhs2 := " \n back"
  24. output2 := mergeText(lhs2, rhs2)
  25. expected2 := "front\nback"
  26. if expected2 != output2 {
  27. t.Fatalf("expected %s not %s", expected2, output2)
  28. }
  29. lhs3 := "front \n\n\n "
  30. rhs3 := " \n back"
  31. output3 := mergeText(lhs3, rhs3)
  32. expected3 := "front\n\nback"
  33. if expected3 != output3 {
  34. t.Fatalf("expected %s not %s", expected3, output3)
  35. }
  36. }
  37. func TestStyles(t *testing.T) {
  38. input := "<s>s</s><code>code</code><i>i</i><u>u</u><mark>mark</mark>"
  39. markup, _, err := NewMarkup(input)
  40. if err != nil {
  41. t.Fatal(err)
  42. }
  43. output := markup.Render(50)
  44. if err != nil {
  45. t.Fatal(err)
  46. }
  47. expected := style.Strikethrough("s") +
  48. style.Code("code") +
  49. style.Italic("i") +
  50. style.Underline("u") +
  51. style.Highlight("mark")
  52. if expected != output {
  53. t.Fatalf("excpected output to be %s not %s", expected, output)
  54. }
  55. }
  56. func TestSurroundingBlocks(t *testing.T) {
  57. input := "<p>first</p>in \t<mark>the</mark> \rmiddle<p>last</p>"
  58. markup, _, err := NewMarkup(input)
  59. if err != nil {
  60. t.Fatal(err)
  61. }
  62. output := markup.Render(50)
  63. if err != nil {
  64. t.Fatal(err)
  65. }
  66. expected := `first
  67. in ` + style.Highlight("the") + ` middle
  68. last`
  69. if expected != output {
  70. t.Fatalf("excpected output to be %s not %s", expected, output)
  71. }
  72. }
  73. func TestAdjacentBlocks(t *testing.T) {
  74. input := "\t<p>first</p>\n\t<p>second</p>"
  75. markup, _, err := NewMarkup(input)
  76. if err != nil {
  77. t.Fatal(err)
  78. }
  79. output := markup.Render(50)
  80. if err != nil {
  81. t.Fatal(err)
  82. }
  83. expected := `first
  84. second`
  85. if expected != output {
  86. t.Fatalf("excpected output to be %s not %s", expected, output)
  87. }
  88. }
  89. func TestPoetry(t *testing.T) {
  90. input := "he shouted\t\ta few words<br>at those annoying birds<br><br>and that they heard"
  91. markup, _, err := NewMarkup(input)
  92. if err != nil {
  93. t.Fatal(err)
  94. }
  95. output := markup.Render(50)
  96. if err != nil {
  97. t.Fatal(err)
  98. }
  99. expected := `he shouted a few words
  100. at those annoying birds
  101. and that they heard`
  102. if expected != output {
  103. t.Fatalf("excpected output to be %s not %s", expected, output)
  104. }
  105. }
  106. func TestPreservation(t *testing.T) {
  107. input := "<pre>multi-space \n\n\n\n\n far down</pre>"
  108. markup, _, err := NewMarkup(input)
  109. if err != nil {
  110. t.Fatal(err)
  111. }
  112. output := markup.Render(50)
  113. if err != nil {
  114. t.Fatal(err)
  115. }
  116. expected := style.CodeBlock(ansi.Pad(`multi-space
  117. far down`, 50))
  118. if expected != output {
  119. t.Fatalf("excpected output to be %s not %s", expected, output)
  120. }
  121. }
  122. func TestNestedBlocks(t *testing.T) {
  123. input := `<p>Once a timid child</p>
  124. <p> </p>
  125. <p><img src="https://i.snap.as/P8qpdMbM.jpg" alt=""/></p>`
  126. markup, _, err := NewMarkup(input)
  127. if err != nil {
  128. t.Fatal(err)
  129. }
  130. output := markup.Render(50)
  131. if err != nil {
  132. t.Fatal(err)
  133. }
  134. expected := `Once a timid child
  135. ` + style.LinkBlock("https://i.snap.as/P8qpdMbM.jpg", 1)
  136. if expected != output {
  137. t.Fatalf("excpected output to be %s not %s", expected, output)
  138. }
  139. }
  140. func TestAdjacentLists(t *testing.T) {
  141. input := `<ul><li>top list</li></ul><ul><li>bottom list</li></ul>`
  142. markup, _, err := NewMarkup(input)
  143. if err != nil {
  144. t.Fatal(err)
  145. }
  146. output := markup.Render(50)
  147. if err != nil {
  148. t.Fatal(err)
  149. }
  150. expected := style.Bullet("top list") + "\n\n" +
  151. style.Bullet("bottom list")
  152. if expected != output {
  153. t.Fatalf("excpected output to be %s not %s", expected, output)
  154. }
  155. }
  156. func TestNestedLists(t *testing.T) {
  157. input := `<ul><li>top list<ul><li>nested</li></ul></li></ul>`
  158. markup, _, err := NewMarkup(input)
  159. if err != nil {
  160. t.Fatal(err)
  161. }
  162. output := markup.Render(50)
  163. if err != nil {
  164. t.Fatal(err)
  165. }
  166. expected := style.Bullet("top list\n" + style.Bullet("nested"))
  167. if expected != output {
  168. t.Fatalf("excpected output to be %s not %s", expected, output)
  169. }
  170. }
  171. func TestBlockInList(t *testing.T) {
  172. input := `<ul><li>top list<p><ul><li>paragraph</li></ul></p></li></ul>`
  173. markup, _, err := NewMarkup(input)
  174. if err != nil {
  175. t.Fatal(err)
  176. }
  177. output := markup.Render(50)
  178. if err != nil {
  179. t.Fatal(err)
  180. }
  181. expected := style.Bullet("top list\n\n" + style.Bullet("paragraph"))
  182. if expected != output {
  183. t.Fatalf("excpected output to be %s not %s", expected, output)
  184. }
  185. }
  186. func TestWrapping(t *testing.T) {
  187. input := `<p>hello sir</p>`
  188. markup, _, err := NewMarkup(input)
  189. if err != nil {
  190. t.Fatal(err)
  191. }
  192. output := markup.Render(4)
  193. if err != nil {
  194. t.Fatal(err)
  195. }
  196. expected := "hell\no\nsir"
  197. if expected != output {
  198. t.Fatalf("excpected output to be %s not %s", expected, output)
  199. }
  200. }
  201. func TestLinks(t *testing.T) {
  202. input := `<a href="https://wikipedia.org">Great site</a>
  203. <img src="https://example.org" alt="What the heck">
  204. <iframe title="Music" src="https://spotify.com">`
  205. markup, links, err := NewMarkup(input)
  206. if err != nil {
  207. t.Fatal(err)
  208. }
  209. if links[0] != "https://wikipedia.org" {
  210. t.Fatalf("the first links should have been https://wikipedia.org not %s", links[0])
  211. }
  212. if links[1] != "https://example.org" {
  213. t.Fatalf("the first links should have been https://example.org not %s", links[1])
  214. }
  215. if links[2] != "https://spotify.com" {
  216. t.Fatalf("the first links should have been https://spotify.com not %s", links[2])
  217. }
  218. output := markup.Render(50)
  219. if err != nil {
  220. t.Fatal(err)
  221. }
  222. expected := style.Link("Great site", 1) + "\n\n" +
  223. style.LinkBlock("What the heck", 2) + "\n\n" +
  224. style.LinkBlock("Music", 3)
  225. if expected != output {
  226. t.Fatalf("excpected output to be %s not %s", expected, output)
  227. }
  228. }