markgone_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // This source code is dedicated to the Public Domain.
  2. package markgone
  3. import (
  4. "os"
  5. "testing"
  6. "strings"
  7. )
  8. var text string = `First line as the title
  9. MarkGone uses the minimal formatting syntax from godoc.
  10. MarkGone processes plain text instead of comments in go source code.
  11. Paragraphs are separated by one or more blank lines.
  12. Heading
  13. A heading is a single line
  14. followed by another paragraph,
  15. beginning with a capital letter,
  16. and containing no punctuation.
  17. To produce a pre-formatted blocks,
  18. simply indent every line of the block.
  19. Common indent prefix will be removed in output.
  20. http://example.com/urls_are_auto-linked
  21. Tags: space-separated tags on last line following at least one blank-line`
  22. func ExampleDoc() {
  23. ToHTML(os.Stdout, text, nil)
  24. // Output:
  25. /*
  26. <h2>First line as the title</h2>
  27. <p>
  28. MarkGone uses the minimal formatting syntax from godoc.
  29. MarkGone processes plain text instead of comments in go source code.
  30. </p>
  31. <p>
  32. Paragraphs are separated by one or more blank lines.
  33. </p>
  34. <h3 id="hdr-Heading">Heading</h3>
  35. <p>
  36. A heading is a single line
  37. followed by another paragraph,
  38. beginning with a capital letter,
  39. and containing no punctuation.
  40. </p>
  41. <pre>To produce a pre-formatted blocks,
  42. simply indent every line of the block.
  43. Common indent prefix will be removed in output.
  44. </pre>
  45. <p>
  46. <a href="http://example.com/urls_are_auto-linked">http://example.com/urls_are_auto-linked</a></p>
  47. <div class="taglist">
  48. <strong>Tags:</strong>
  49. <a href="/tag/space-separated" rel="tag">space-separated</a>
  50. <a href="/tag/tags" rel="tag">tags</a>
  51. <a href="/tag/on" rel="tag">on</a>
  52. <a href="/tag/last" rel="tag">last</a>
  53. <a href="/tag/line" rel="tag">line</a>
  54. <a href="/tag/following" rel="tag">following</a>
  55. <a href="/tag/at" rel="tag">at</a>
  56. <a href="/tag/least" rel="tag">least</a>
  57. <a href="/tag/one" rel="tag">one</a>
  58. <a href="/tag/blank-line" rel="tag">blank-line</a>
  59. </div>
  60. */
  61. }
  62. var preparedBodyTests = []struct {
  63. in string
  64. out string
  65. }{
  66. {"\na leading blank line", "a leading blank line"},
  67. {"\n\n\nleading blank lines", "leading blank lines"},
  68. {"a trailing blank line\n", "a trailing blank line"},
  69. {"trailing blank lines\n\n\n", "trailing blank lines"},
  70. {"a trailing space \n", "a trailing space"},
  71. {"trailing spaces \n", "trailing spaces"},
  72. {"\n\n\nmultiple \n\n\nlines\n\n\n", "multiple\n\n\nlines"},
  73. }
  74. func TestPreparedBody(t *testing.T) {
  75. for i, tt := range preparedBodyTests {
  76. outLines, _ := preparedBody(strings.Split(tt.in, "\n"))
  77. out := strings.Join(outLines, "\n")
  78. if out != tt.out {
  79. t.Errorf("#%d FAIL\n Actual value: %v\nExpected value: %v", i, out, tt.out)
  80. }
  81. }
  82. }
  83. var formattingTitleTests = []struct{
  84. in string
  85. out string
  86. }{
  87. {
  88. "title\n",
  89. "<h2>title</h2>\n",
  90. },
  91. {
  92. "not a title (no newline)",
  93. "<p>\nnot a title (no newline)</p>\n",
  94. },
  95. {
  96. "another title\n\n",
  97. "<h2>another title</h2>\n",
  98. },
  99. {
  100. "title\n\nbody\n",
  101. "<h2>title</h2>\n<p>\nbody</p>\n",
  102. },
  103. {
  104. "two continuous lines\nare not title\n",
  105. "<p>\ntwo continuous lines\nare not title</p>\n",
  106. },
  107. {
  108. "\n\ntitle must be the first line\n",
  109. "<p>\ntitle must be the first line</p>\n",
  110. },
  111. }
  112. func TestFormattingTitle(t *testing.T) {
  113. for i, tt := range formattingTitleTests {
  114. out := ToHTMLString(tt.in, nil)
  115. if out != tt.out {
  116. t.Errorf("#%d FAIL\n Actual value: %v\nExpected value: %v", i, out, tt.out)
  117. }
  118. }
  119. }
  120. var taggingTests = []struct{
  121. in string
  122. out string
  123. }{
  124. {
  125. "\nparagraph\n\nTags: one-tag",
  126. `<p>
  127. paragraph</p>
  128. <div class="taglist">
  129. <strong>Tags:</strong>
  130. <a href="/tag/one-tag" rel="tag">one-tag</a>
  131. </div>
  132. `,
  133. },
  134. {
  135. "\nparagraph\n\nTags: multiple tags",
  136. `<p>
  137. paragraph</p>
  138. <div class="taglist">
  139. <strong>Tags:</strong>
  140. <a href="/tag/multiple" rel="tag">multiple</a>
  141. <a href="/tag/tags" rel="tag">tags</a>
  142. </div>
  143. `,
  144. },
  145. {
  146. "\nparagraph\nTags: not following blank lines",
  147. "<p>\nparagraph\nTags: not following blank lines</p>\n",
  148. },
  149. {
  150. "\nparagraph\n\nTags:no space",
  151. "<p>\nparagraph\n</p>\n<p>\nTags:no space</p>\n",
  152. },
  153. {
  154. "\nempty list\n\nTags: ",
  155. "<p>\nempty list\n</p>\n<p>\nTags:</p>\n",
  156. },
  157. }
  158. func TestTagging(t *testing.T) {
  159. for i, tt := range taggingTests {
  160. out := ToHTMLString(tt.in, nil)
  161. if out != tt.out {
  162. t.Errorf("#%d FAIL\n Actual value: %v\nExpected value: %v", i, out, tt.out)
  163. }
  164. }
  165. }