-
Notifications
You must be signed in to change notification settings - Fork 0
/
rnzml_test.go
210 lines (201 loc) · 5.57 KB
/
rnzml_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package rnzml
import (
"fmt"
"strings"
"testing"
)
var r = NewRenderer()
func ExampleRenderer_Render() {
r := NewRenderer()
out := &strings.Builder{}
_ = r.Render(strings.NewReader("*Hello*, `world`"), out)
fmt.Print(out.String())
// Output:
// <p><strong>Hello</strong>, <code>world</code>
// </p>
//
}
func TestRender(t *testing.T) {
t.Run("Should render rnzml to HTML", func(t *testing.T) {
in := strings.Join([]string{
"Here is the first line",
"```",
"p := *b",
"",
"`var`",
"```",
"Here `code` is *bold*",
}, "\n")
expected := strings.Join([]string{
"<p>Here is the first line",
"</p>",
"<pre><code>p := *b",
"",
"`var`",
"</code></pre>",
"<p>Here <code>code</code> is <strong>bold</strong>",
"</p>\n",
}, "\n")
out := &strings.Builder{}
err := r.Render(strings.NewReader(in), out)
if err != nil {
t.Error(err)
} else if expected != out.String() {
t.Errorf("expected: '%s'(%x) got: '%s'(%x)", expected, []byte(expected), out.String(), []byte(out.String()))
}
})
t.Run("Should encase text blocks in paragraph tags", func(t *testing.T) {
out := &strings.Builder{}
expected := "<p>a\n</p>\n"
err := r.Render(strings.NewReader("a"), out)
if err != nil {
t.Error(err)
} else if expected != out.String() {
t.Errorf("expected: '%s'(%x) got: '%s'(%x)", expected, []byte(expected), out.String(), []byte(out.String()))
}
})
t.Run("Should encase code blocks in <pre><code>tags", func(t *testing.T) {
out := &strings.Builder{}
expected := "<pre><code>a\n</code></pre>\n"
err := r.Render(strings.NewReader("```\na\n```"), out)
if err != nil {
t.Error(err)
} else if expected != out.String() {
t.Errorf("expected: '%s'(%x) got: '%s'(%x)", expected, []byte(expected), out.String(), []byte(out.String()))
}
})
t.Run("Should check for closing code blocks", func(t *testing.T) {
out := &strings.Builder{}
err := r.Render(strings.NewReader("```"), out)
if err == nil {
t.Error("expected error")
}
})
t.Run("Should pass line and char error information in error", func(t *testing.T) {
out := &strings.Builder{}
expected := "line 2: unclosed bold text (*) at position: 1"
err := r.Render(strings.NewReader("a\nb*\nc"), out)
if err == nil {
t.Error("expected error")
}
if expected != err.Error() {
t.Errorf("expected: '%s' got: '%s'", expected, err.Error())
}
})
}
func TestRenderLine(t *testing.T) {
t.Run("Should make text encased in '*' bold", func(t *testing.T) {
out := &strings.Builder{}
expected := "a <strong>bold</strong> word"
err := r.renderLine("a *bold* word", out)
if err != nil {
t.Error(err)
} else if expected != out.String() {
t.Errorf("expected: '%s' got: %s", expected, out.String())
}
})
t.Run("Should check for unclosed '*'", func(t *testing.T) {
out := &strings.Builder{}
err := r.renderLine("a *unclosed bold", out)
if err == nil {
t.Errorf("expected error")
}
})
t.Run("Should make text incased in '`' code", func(t *testing.T) {
out := &strings.Builder{}
expected := "a <code>programmer</code> word"
err := r.renderLine("a `programmer` word", out)
if err != nil {
t.Error(err)
} else if expected != out.String() {
t.Errorf("expected: '%s' got: %s", expected, out.String())
}
})
t.Run("Should check for unclosed '`'", func(t *testing.T) {
out := &strings.Builder{}
err := r.renderLine("a `unclosed programmer", out)
if err == nil {
t.Errorf("expected error")
}
})
t.Run("Should not allow other formatting in code blocks", func(t *testing.T) {
out := &strings.Builder{}
expected := "<code>a := *p</code>"
err := r.renderLine("`a := *p`", out)
if err != nil {
t.Error(err)
} else if expected != out.String() {
t.Errorf("expected: '%s' got: '%s'", expected, out.String())
}
})
t.Run("Should escape basic HTML control characters", func(t *testing.T) {
out := &strings.Builder{}
expected := "<script>alert('xss')</script>"
err := r.renderLine("<script>alert('xss')</script>", out)
if err != nil {
t.Error(err)
} else if expected != out.String() {
t.Errorf("expected: '%s' got: %s", expected, out.String())
}
})
}
var escapetests = []struct {
in string
out string
}{
{`\`, ""},
{`\\`, `\`},
{`\\\`, `\`},
{`\\\\`, `\\`},
{`\*`, `*`},
{`\\**`, `\<strong></strong>`},
}
func TestEscapes(t *testing.T) {
for _, tt := range escapetests {
t.Run(tt.in, func(t *testing.T) {
out := &strings.Builder{}
err := r.renderLine(tt.in, out)
if err != nil {
t.Errorf("error: %s", err.Error())
} else if tt.out != out.String() {
t.Errorf("expected: '%s' got: '%s'", tt.out, out.String())
}
})
}
}
var linktests = []struct {
in string
out string
err bool
}{
{`[1 2]`, `<a href="1">2</a>`, false},
{`[1 2 3]`, `<a href="1">2 3</a>`, false},
{`[1*2* 3]`, `<a href="1*2*">3</a>`, false},
{`[1 *2* 3]`, `<a href="1">*2* 3</a>`, false},
{`[1 *2*3]`, `<a href="1">*2*3</a>`, false},
{`[1 \*2*3]`, `<a href="1">*2*3</a>`, false},
{`[1 \\*2*3]`, `<a href="1">\*2*3</a>`, false},
{`[1 \]]`, `<a href="1">]</a>`, false},
{`[1 <]`, `<a href="1"><</a>`, false},
{`[<a 2]`, `<a href="%3ca">2</a>`, false},
{`[1 2`, "", true},
{`[1]`, "", true},
{`[]`, "", true},
}
func TestLinks(t *testing.T) {
for _, tt := range linktests {
t.Run(tt.in, func(t *testing.T) {
out := &strings.Builder{}
err := r.renderLine(tt.in, out)
if tt.err {
if err == nil {
t.Errorf("expected error")
}
} else if err != nil {
t.Errorf("error: %s", err.Error())
} else if tt.out != out.String() {
t.Errorf("expected: '%s' got: '%s'", tt.out, out.String())
}
})
}
}