-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
244 lines (217 loc) · 9.97 KB
/
main_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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package main
import (
"testing"
)
type example struct {
name string
wiki string
html string
newWiki string
}
var examples = []example{
// <em> and <strong>
{"f101", "foo ''bar'' baz", "foo <em>bar</em> baz", ""},
{"f102", "foo ''bar'' ''baz'' qux", "foo <em>bar</em> <em>baz</em> qux", ""},
{"f103", "foo '''bar''' baz", "foo <strong>bar</strong> baz", ""},
{"f104", "foo '''bar''' '''baz''' qux", "foo <strong>bar</strong> <strong>baz</strong> qux", ""},
{"f105", "foo '''''bar''''' baz", "foo <strong><em>bar</em></strong> baz", ""},
{"f106", "foo ''bar baz", "foo <em>bar baz</em>", "foo ''bar baz''"},
{"f107", "foo '''bar baz", "foo <strong>bar baz</strong>", "foo '''bar baz'''"},
// Links
{"l101", "foo [[Bar]] baz", `foo <a href="Bar">Bar</a> baz`, ""},
{"l102", "foo [[Bar|some label]] baz", `foo <a href="Bar">some label</a> baz`, ""},
{"l103", "foo [[Bar|some label|foo]] baz", `foo <a href="Bar">some label|foo</a> baz`, ""},
// External links
{"l201",
"foo [[https://foo.bar/nowhere?query=string#fragment]] baz",
`foo <a href="https://foo.bar/nowhere?query=string#fragment"></a> baz`,
"foo [https://foo.bar/nowhere?query=string#fragment] baz"},
{"l202",
"foo [[https://foo.bar/nowhere?query=string#fragment|some label]] baz",
`foo <a href="https://foo.bar/nowhere?query=string#fragment">some label</a> baz`,
"foo [https://foo.bar/nowhere?query=string#fragment some label] baz"},
{"l203",
"foo [[https://foo.bar/nowhere?query=string#fragment|some label|foo]] baz",
`foo <a href="https://foo.bar/nowhere?query=string#fragment">some label|foo</a> baz`,
"foo [https://foo.bar/nowhere?query=string#fragment some label|foo] baz"},
{"l301",
"foo [https://foo.bar/nowhere?query=string#fragment] baz",
`foo <a href="https://foo.bar/nowhere?query=string#fragment"></a> baz`,
""},
{"l302",
"foo [https://foo.bar/nowhere?query=string#fragment some label] baz",
`foo <a href="https://foo.bar/nowhere?query=string#fragment">some label</a> baz`,
""},
// Images
{"i101", "foo [[File:filename.extension]] baz", `foo <img src="filename.extension" options="" link=""></img> baz`, ""},
{"i102", "foo [[File:filename.extension|options]] baz", `foo <img src="filename.extension" options="options" link=""></img> baz`, ""},
{"i103", "foo [[File:filename.extension|options|caption words]] baz", `foo <img src="filename.extension" options="options" link="">caption words</img> baz`, ""},
{"i104", "foo [[File:filename.extension|options|link=Internal]] baz", `foo <img src="filename.extension" options="options" link="Internal"></img> baz`, ""},
{"i105", "foo [[File:filename.extension|options|link=http://External]] baz", `foo <img src="filename.extension" options="options" link="http://External"></img> baz`, ""},
// References
{"r101",
"foo <ref>[[ABC]]</ref> baz", `foo <ref data="W1tBQkNdXQ=="></ref> baz`,
""},
{"r102",
`foo <ref name="qux">[[ABC]]</ref> baz`,
`foo <ref data="W1tBQkNdXQ==" name="qux"></ref> baz`,
""},
{"r103",
`foo <ref name="qux" /> baz`,
`foo <ref data="" name="qux" ></ref> baz`,
""},
{"r104",
`foo <ref name=qux/> baz`,
`foo <ref data="" name=qux></ref> baz`,
""},
{"r105",
`foo <ref name= qux /> baz`,
`foo <ref data="" name= qux ></ref> baz`,
""},
{"r106",
`foo <ref name=Chandler/> <ref name="Hartnagle-Taylor and Ty Taylor"/> baz`,
`foo <ref data="" name=Chandler></ref> <ref data="" name="Hartnagle-Taylor and Ty Taylor"></ref> baz`,
""},
// {"r201",
// `The Smithfield was first introduced to Australia during colonial times.<ref name=Chandler/> It was a handy dog used to work the meat markets in [[Smithfield Meat Market|Smithfield]], London. It is a dog standing from {{Convert|18|to|21|in|cm}}<ref name="Hartnagle-Taylor and Ty Taylor"/> and has a shaggy appearance.`,
// "",
// ""},
// <nowiki>
{"w101", "foo <nowiki>''qux''</nowiki> baz", `foo <nowiki data="JydxdXgnJw=="></nowiki> baz`, ""},
{"w102", "foo <nowiki abc>''qux''</nowiki> baz", `foo <nowiki data="JydxdXgnJw==" abc></nowiki> baz`, ""},
// Templates
{"t101", "foo {{bar}} baz", `foo <template name="bar"></template> baz`, ""},
{"t102",
"foo {{bar|qux}} baz",
`foo <template name="bar"><arg name="">qux</arg></template> baz`,
""},
{"t103",
"foo {{bar|qux|abc}} baz",
`foo <template name="bar"><arg name="">qux</arg><arg name="">abc</arg></template> baz`,
""},
{"t104",
"foo {{bar|qux=abc}} baz",
`foo <template name="bar"><arg name="qux">abc</arg></template> baz`,
""},
{"t105",
"foo {{bar|\nqux=abc}} baz",
`foo <template name="bar"><arg name="qux">abc</arg></template> baz`,
"foo {{bar|qux=abc}} baz"},
{"t106",
"foo {{bar| qux =abc}} baz",
`foo <template name="bar"><arg name="qux">abc</arg></template> baz`,
"foo {{bar|qux=abc}} baz"},
{"t107",
"foo {{bar\n|qux=abc}} baz",
`foo <template name="bar"><arg name="qux">abc</arg></template> baz`,
"foo {{bar|qux=abc}} baz"},
{"t108",
"foo {{bar\n|qux=[[abc|foo]]}} baz",
`foo <template name="bar"><arg name="qux"><a href="abc">foo</a></arg></template> baz`,
"foo {{bar|qux=[[abc|foo]]}} baz"},
// Nested templates
{"t201",
"foo {{bar|{{qux|xyz}}|a=c}} baz",
`foo <template name="bar"><arg name=""><template name="qux"><arg name="">xyz</arg></template></arg><arg name="a">c</arg></template> baz`,
""},
// Headings
{"h101", "====== The Heading ======\nbar", "<h6> The Heading </h6>\nbar", ""},
{"h102", "===== The Heading =====\nbar", "<h5> The Heading </h5>\nbar", ""},
{"h103", "==== The Heading ====\nbar", "<h4> The Heading </h4>\nbar", ""},
{"h104", "=== The Heading ===\nbar", "<h3> The Heading </h3>\nbar", ""},
{"h105", "== The Heading ==\nbar", "<h2> The Heading </h2>\nbar", ""},
{"h106", "= The Heading =\nbar", "<h1> The Heading </h1>\nbar", ""},
{"h201", " ====== The Heading ======\nbar", " <h6> The Heading </h6>\nbar", ""},
{"h202", " ===== The Heading =====\nbar", " <h5> The Heading </h5>\nbar", ""},
{"h203", " ==== The Heading ====\nbar", " <h4> The Heading </h4>\nbar", ""},
{"h204", " === The Heading ===\nbar", " <h3> The Heading </h3>\nbar", ""},
{"h205", " == The Heading ==\nbar", " <h2> The Heading </h2>\nbar", ""},
{"h206", " = The Heading =\nbar", " <h1> The Heading </h1>\nbar", ""},
{"h301", "foo\n====== The Heading ======\nbar", "foo\n<h6> The Heading </h6>\nbar", ""},
{"h302", "foo\n===== The Heading =====\nbar", "foo\n<h5> The Heading </h5>\nbar", ""},
{"h303", "foo\n==== The Heading ====\nbar", "foo\n<h4> The Heading </h4>\nbar", ""},
{"h304", "foo\n=== The Heading ===\nbar", "foo\n<h3> The Heading </h3>\nbar", ""},
{"h305", "foo\n== The Heading ==\nbar", "foo\n<h2> The Heading </h2>\nbar", ""},
{"h306", "foo\n= The Heading =\nbar", "foo\n<h1> The Heading </h1>\nbar", ""},
// Lists
{"o101", "Foo\n* Bar\n* Baz\nQux", "Foo\n<li> Bar</li>\n<li> Baz</li>\nQux", ""},
{"o102", "Foo\n# Bar\n# Baz\nQux", "Foo\n<oli> Bar</oli>\n<oli> Baz</oli>\nQux", ""},
{"o103", "Foo\n*Bar\n*Baz\nQux", "Foo\n<li>Bar</li>\n<li>Baz</li>\nQux", ""},
{"o104", "Foo\n#Bar\n#Baz\nQux", "Foo\n<oli>Bar</oli>\n<oli>Baz</oli>\nQux", ""},
// Tables
{"g101", "Foo\n{|\n|-\n|Bar\n|}\nQux",
"Foo\n<table >\n<tr >\n<td >Bar</td>\n</tr>\n</table>\nQux",
""},
{"g102", "Foo\n{|\n|-\n|Bar\n|Baz\n|}\nQux",
"Foo\n<table >\n<tr >\n<td >Bar</td>\n<td >Baz</td>\n</tr>\n</table>\nQux",
""},
{"g103", "Foo\n{|\n|-\n|Bar\n|-\n|Baz\n|}\nQux",
"Foo\n<table >\n<tr >\n<td >Bar</td>\n</tr>\n<tr >\n<td >Baz</td>\n</tr>\n</table>\nQux",
""},
{"g201", "Foo\n{|\n|Bar\n|}\nQux",
"Foo\n<table >\n<tr>\n<td >Bar</td>\n</tr>\n</table>\nQux",
"Foo\n{|\n|-\n|Bar\n|}\nQux"},
{"g202", "Foo\n{|\n|Bar\n|Baz\n|}\nQux",
"Foo\n<table >\n<tr>\n<td >Bar</td>\n<td >Baz</td>\n</tr>\n</table>\nQux",
"Foo\n{|\n|-\n|Bar\n|Baz\n|}\nQux"},
{"g203", "Foo\n{|\n|Bar\n|-\n|Baz\n|}\nQux",
"Foo\n<table >\n<tr>\n<td >Bar</td>\n</tr>\n<tr >\n<td >Baz</td>\n</tr>\n</table>\nQux",
"Foo\n{|\n|-\n|Bar\n|-\n|Baz\n|}\nQux"},
{"g301", "Foo\n{|\n!Bar\n|}\nQux",
"Foo\n<table >\n<tr>\n<th >Bar</th>\n</tr>\n</table>\nQux",
"Foo\n{|\n|-\n!Bar\n|}\nQux"},
{"g302", "Foo\n{|\n!Bar\n!Baz\n|}\nQux",
"Foo\n<table >\n<tr>\n<th >Bar</th>\n<th >Baz</th>\n</tr>\n</table>\nQux",
"Foo\n{|\n|-\n!Bar\n!Baz\n|}\nQux"},
{"g303", "Foo\n{|\n!Bar\n|-\n!Baz\n|}\nQux",
"Foo\n<table >\n<tr>\n<th >Bar</th>\n</tr>\n<tr >\n<th >Baz</th>\n</tr>\n</table>\nQux",
"Foo\n{|\n|-\n!Bar\n|-\n!Baz\n|}\nQux"},
}
func TestExamples(t *testing.T) {
for _, test := range examples {
if test.newWiki == "" {
test.newWiki = test.wiki
}
html := WikiToHtml(test.wiki)
if html != test.html {
t.Errorf("%v:\n expected HTML: '%v'\n from wiki: '%v'\n got: '%v'\n\n",
test.name, test.html, test.wiki, html)
}
wiki := HtmlToWiki(test.html)
if wiki != test.newWiki {
t.Errorf("%v:\n expected wiki: '%v'\n from HTML: '%v'\n got: '%v'\n\n",
test.name, test.newWiki, test.html, wiki)
}
}
}
type balanceHtmlTagsExample struct {
before string
expected string
}
var balanceHtmlTagsExamples = []balanceHtmlTagsExample{
// Already balanced
{"foo bar", "foo bar"},
{"foo <bar>bar</bar> baz", "foo <bar>bar</bar> baz"},
{"foo <bar a=1>bar</bar> baz", "foo <bar a=1>bar</bar> baz"},
{"foo <qux><bar>bar</bar><bar>quxx</bar></qux> baz", "foo <qux><bar>bar</bar><bar>quxx</bar></qux> baz"},
// Too many opens
{"foo <bar>bar baz", "foo <bar>bar baz</bar>"},
{"foo <bar a=1>bar baz", "foo <bar a=1>bar baz</bar>"},
{"foo <bar>bar <qux>baz", "foo <bar>bar <qux>baz</bar></qux>"},
{"foo <bar><qux>bar baz", "foo <bar><qux>bar baz</bar></qux>"},
{"foo <bar><qux>bar</bar> baz", "foo <bar><qux>bar</qux></bar> baz"},
{"foo <bar><qux>bar<abc></bar> baz", "foo <bar><qux>bar<abc></abc></qux></bar> baz"},
// To many closes
{"foo bar</bar> baz", "foo bar baz"},
{"foo bar</bar> baz</foo>", "foo bar baz"},
{"foo bar</bar> <abc>baz</foo>", "foo bar <abc>baz</abc>"},
// "<>"
}
func TestBalanceHtmlTags(t *testing.T) {
for _, test := range balanceHtmlTagsExamples {
result := BalanceHtmlTags(test.before)
if test.expected != result {
t.Errorf("Expected '%v', got '%v'", test.expected, result)
}
}
}