markitzero_test.go (view raw)
1package main
2
3import (
4 "strings"
5 "testing"
6)
7
8func doonezerotest(t *testing.T, input, output string) {
9 result := markitzero(input)
10 if result != output {
11 t.Errorf("\nexpected:\n%s\noutput:\n%s", output, result)
12 }
13}
14
15func TestBasictest(t *testing.T) {
16 input := `link to https://example.com/ with **bold** text`
17 output := `link to <a href="https://example.com/">https://example.com/</a> with <b>bold</b> text`
18 doonezerotest(t, input, output)
19}
20
21func TestMultibold(t *testing.T) {
22 input := `**in** out **in**`
23 output := `<b>in</b> out <b>in</b>`
24 doonezerotest(t, input, output)
25}
26
27func TestLinebreak1(t *testing.T) {
28 input := "hello\n> a quote\na comment"
29 output := "hello<blockquote>a quote</blockquote><p>a comment"
30 doonezerotest(t, input, output)
31}
32
33func TestLinebreak2(t *testing.T) {
34 input := "hello\n\n> a quote\n\na comment"
35 output := "hello<p><blockquote>a quote</blockquote><p>a comment"
36 doonezerotest(t, input, output)
37}
38
39func TestLinebreak3(t *testing.T) {
40 input := "hello\n\n```\nfunc(s string)\n```\n\ndoes it go?"
41 output := "hello<p><pre><code>func(s string)</code></pre><p>does it go?"
42 doonezerotest(t, input, output)
43}
44
45func TestCodeStyles(t *testing.T) {
46 input := "hello\n\n```go\nfunc(s string)\n```\n\ndoes it go?"
47 output := "hello<p><pre><code><span class=kw>func</span><span class=op>(</span>s <span class=tp>string</span><span class=op>)</span></code></pre><p>does it go?"
48 doonezerotest(t, input, output)
49}
50
51func TestSimplelink(t *testing.T) {
52 input := "This is a [link](https://example.com)."
53 output := `This is a <a href="https://example.com">link</a>.`
54 doonezerotest(t, input, output)
55}
56
57func TestSimplelink2(t *testing.T) {
58 input := "See (http://example.com) for examples."
59 output := `See (<a href="http://example.com">http://example.com</a>) for examples.`
60 doonezerotest(t, input, output)
61}
62
63func TestWikilink(t *testing.T) {
64 input := "I watched [Hackers](https://en.wikipedia.org/wiki/Hackers_(film))"
65 output := `I watched <a href="https://en.wikipedia.org/wiki/Hackers_(film)">Hackers</a>`
66 doonezerotest(t, input, output)
67}
68
69func TestQuotedlink(t *testing.T) {
70 input := `quoted "https://example.com/link" here`
71 output := `quoted "<a href="https://example.com/link">https://example.com/link</a>" here`
72 doonezerotest(t, input, output)
73}
74
75func TestLinkinQuote(t *testing.T) {
76 input := `> a quote and https://example.com/link`
77 output := `<blockquote>a quote and <a href="https://example.com/link">https://example.com/link</a></blockquote><p>`
78 doonezerotest(t, input, output)
79}
80
81func TestBoldLink(t *testing.T) {
82 input := `**b https://example.com/link b**`
83 output := `<b>b <a href="https://example.com/link">https://example.com/link</a> b</b>`
84 doonezerotest(t, input, output)
85}
86
87func TestHonklink(t *testing.T) {
88 input := `https://en.wikipedia.org/wiki/Honk!`
89 output := `<a href="https://en.wikipedia.org/wiki/Honk!">https://en.wikipedia.org/wiki/Honk!</a>`
90 doonezerotest(t, input, output)
91}
92
93func TestImagelink(t *testing.T) {
94 input := `an image <img alt="caption" src="https://example.com/wherever"> and linked [<img src="there">](example.com)`
95 output := `an image <img alt="caption" src="https://example.com/wherever"> and linked <a href="example.com"><img src="there"></a>`
96 doonezerotest(t, input, output)
97}
98
99func TestLists(t *testing.T) {
100 input := `hello
101+ a list
102+ the list
103
104para
105
106- singleton`
107 output := `hello<ul><li>a list<li>the list</ul><p>para<ul><li>singleton</ul><p>`
108 doonezerotest(t, input, output)
109}
110
111func TestTables(t *testing.T) {
112 input := `hello
113
114| col1 | col 2 |
115| row2 | cell4 |
116
117para
118`
119 output := `hello<table><tr><td>col1<td>col 2<tr><td>row2<td>cell4</table><p>para`
120 doonezerotest(t, input, output)
121}
122
123func TestHeaders(t *testing.T) {
124 input := `hello
125## fruits
126Love 'em. Eat 'em.
127`
128 output := `hello<h2>fruits</h2><p>Love 'em. Eat 'em.`
129 doonezerotest(t, input, output)
130}
131
132var benchData, simpleData string
133
134func init() {
135 benchData = strings.Repeat("hello there sir. It is a **fine** day for some testing!\n", 100)
136 simpleData = strings.Repeat("just a few words\n", 100)
137}
138
139func BenchmarkModerateSize(b *testing.B) {
140 for n := 0; n < b.N; n++ {
141 markitzero(benchData)
142 }
143}
144
145func BenchmarkSimpleData(b *testing.B) {
146 for n := 0; n < b.N; n++ {
147 markitzero(simpleData)
148 }
149}