Debian Codegraph @

parser/pageparser/pageparser_shortcode_test.go

raw
1 // Copyright 2018 The Hugo Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package pageparser
15
16 import (
17 "testing"
18
19 qt "github.com/frankban/quicktest"
20 )
21
22 var (
23 tstEOF = nti(tEOF, "")
24 tstLeftNoMD = nti(tLeftDelimScNoMarkup, "{{<")
25 tstRightNoMD = nti(tRightDelimScNoMarkup, ">}}")
26 tstLeftMD = nti(tLeftDelimScWithMarkup, "{{%")
27 tstRightMD = nti(tRightDelimScWithMarkup, "%}}")
28 tstSCClose = nti(tScClose, "/")
29 tstSC1 = nti(tScName, "sc1")
30 tstSC1Inline = nti(tScNameInline, "sc1.inline")
31 tstSC2Inline = nti(tScNameInline, "sc2.inline")
32 tstSC2 = nti(tScName, "sc2")
33 tstSC3 = nti(tScName, "sc3")
34 tstSCSlash = nti(tScName, "sc/sub")
35 tstParam1 = nti(tScParam, "param1")
36 tstParam2 = nti(tScParam, "param2")
37 tstVal = nti(tScParamVal, "Hello World")
38 tstText = nti(tText, "Hello World")
39 )
40
41 var shortCodeLexerTests = []lexerTest{
42 {"empty", "", []typeText{tstEOF}, nil},
43 {"spaces", " \t\n", []typeText{nti(tText, " \t\n"), tstEOF}, nil},
44 {"text", `to be or not`, []typeText{nti(tText, "to be or not"), tstEOF}, nil},
45 {"no markup", `{{< sc1 >}}`, []typeText{tstLeftNoMD, tstSC1, tstRightNoMD, tstEOF}, nil},
46 {"with EOL", "{{< sc1 \n >}}", []typeText{tstLeftNoMD, tstSC1, tstRightNoMD, tstEOF}, nil},
47
48 {"forward slash inside name", `{{< sc/sub >}}`, []typeText{tstLeftNoMD, tstSCSlash, tstRightNoMD, tstEOF}, nil},
49
50 {"simple with markup", `{{% sc1 %}}`, []typeText{tstLeftMD, tstSC1, tstRightMD, tstEOF}, nil},
51 {"with spaces", `{{< sc1 >}}`, []typeText{tstLeftNoMD, tstSC1, tstRightNoMD, tstEOF}, nil},
52 {"indented on new line", "Hello\n {{% sc1 %}}", []typeText{nti(tText, "Hello\n"), nti(tIndentation, " "), tstLeftMD, tstSC1, tstRightMD, tstEOF}, nil},
53 {"indented on new line tab", "Hello\n\t{{% sc1 %}}", []typeText{nti(tText, "Hello\n"), nti(tIndentation, "\t"), tstLeftMD, tstSC1, tstRightMD, tstEOF}, nil},
54 {"indented on first line", " {{% sc1 %}}", []typeText{nti(tIndentation, " "), tstLeftMD, tstSC1, tstRightMD, tstEOF}, nil},
55 {"mismatched rightDelim", `{{< sc1 %}}`, []typeText{
56 tstLeftNoMD, tstSC1,
57 nti(tError, "unrecognized character in shortcode action: U+0025 '%'. Note: Parameters with non-alphanumeric args must be quoted"),
58 }, nil},
59 {"inner, markup", `{{% sc1 %}} inner {{% /sc1 %}}`, []typeText{
60 tstLeftMD,
61 tstSC1,
62 tstRightMD,
63 nti(tText, " inner "),
64 tstLeftMD,
65 tstSCClose,
66 tstSC1,
67 tstRightMD,
68 tstEOF,
69 }, nil},
70 {"close, but no open", `{{< /sc1 >}}`, []typeText{
71 tstLeftNoMD, nti(tError, "got closing shortcode, but none is open"),
72 }, nil},
73 {"close wrong", `{{< sc1 >}}{{< /another >}}`, []typeText{
74 tstLeftNoMD, tstSC1, tstRightNoMD, tstLeftNoMD, tstSCClose,
75 nti(tError, "closing tag for shortcode 'another' does not match start tag"),
76 }, nil},
77 {"close, but no open, more", `{{< sc1 >}}{{< /sc1 >}}{{< /another >}}`, []typeText{
78 tstLeftNoMD, tstSC1, tstRightNoMD, tstLeftNoMD, tstSCClose, tstSC1, tstRightNoMD, tstLeftNoMD, tstSCClose,
79 nti(tError, "closing tag for shortcode 'another' does not match start tag"),
80 }, nil},
81 {"close with extra keyword", `{{< sc1 >}}{{< /sc1 keyword>}}`, []typeText{
82 tstLeftNoMD, tstSC1, tstRightNoMD, tstLeftNoMD, tstSCClose, tstSC1,
83 nti(tError, "unclosed shortcode"),
84 }, nil},
85 {"float param, positional", `{{< sc1 3.14 >}}`, []typeText{
86 tstLeftNoMD, tstSC1, nti(tScParam, "3.14"), tstRightNoMD, tstEOF,
87 }, nil},
88 {"float param, named", `{{< sc1 param1=3.14 >}}`, []typeText{
89 tstLeftNoMD, tstSC1, tstParam1, nti(tScParamVal, "3.14"), tstRightNoMD, tstEOF,
90 }, nil},
91 {"named param, raw string", `{{< sc1 param1=` + "`" + "Hello World" + "`" + " >}}", []typeText{
92 tstLeftNoMD, tstSC1, tstParam1, nti(tScParamVal, "Hello World"), tstRightNoMD, tstEOF,
93 }, nil},
94 {"float param, named, space before", `{{< sc1 param1= 3.14 >}}`, []typeText{
95 tstLeftNoMD, tstSC1, tstParam1, nti(tScParamVal, "3.14"), tstRightNoMD, tstEOF,
96 }, nil},
97 {"Youtube id", `{{< sc1 -ziL-Q_456igdO-4 >}}`, []typeText{
98 tstLeftNoMD, tstSC1, nti(tScParam, "-ziL-Q_456igdO-4"), tstRightNoMD, tstEOF,
99 }, nil},
100 {"non-alphanumerics param quoted", `{{< sc1 "-ziL-.%QigdO-4" >}}`, []typeText{
101 tstLeftNoMD, tstSC1, nti(tScParam, "-ziL-.%QigdO-4"), tstRightNoMD, tstEOF,
102 }, nil},
103 {"raw string", `{{< sc1` + "`" + "Hello World" + "`" + ` >}}`, []typeText{
104 tstLeftNoMD, tstSC1, nti(tScParam, "Hello World"), tstRightNoMD, tstEOF,
105 }, nil},
106 {"raw string with newline", `{{< sc1` + "`" + `Hello
107 World` + "`" + ` >}}`, []typeText{
108 tstLeftNoMD, tstSC1, nti(tScParam, `Hello
109 World`), tstRightNoMD, tstEOF,
110 }, nil},
111 {"raw string with escape character", `{{< sc1` + "`" + `Hello \b World` + "`" + ` >}}`, []typeText{
112 tstLeftNoMD, tstSC1, nti(tScParam, `Hello \b World`), tstRightNoMD, tstEOF,
113 }, nil},
114 {"two params", `{{< sc1 param1 param2 >}}`, []typeText{
115 tstLeftNoMD, tstSC1, tstParam1, tstParam2, tstRightNoMD, tstEOF,
116 }, nil},
117 // issue #934
118 {"self-closing", `{{< sc1 />}}`, []typeText{
119 tstLeftNoMD, tstSC1, tstSCClose, tstRightNoMD, tstEOF,
120 }, nil},
121 // Issue 2498
122 {"multiple self-closing", `{{< sc1 />}}{{< sc1 />}}`, []typeText{
123 tstLeftNoMD, tstSC1, tstSCClose, tstRightNoMD,
124 tstLeftNoMD, tstSC1, tstSCClose, tstRightNoMD, tstEOF,
125 }, nil},
126 {"self-closing with param", `{{< sc1 param1 />}}`, []typeText{
127 tstLeftNoMD, tstSC1, tstParam1, tstSCClose, tstRightNoMD, tstEOF,
128 }, nil},
129 {"self-closing with extra keyword", `{{< sc1 / keyword>}}`, []typeText{
130 tstLeftNoMD, tstSC1, tstSCClose, nti(tError, "closing tag for shortcode 'keyword' does not match start tag"),
131 }, nil},
132 {"multiple self-closing with param", `{{< sc1 param1 />}}{{< sc1 param1 />}}`, []typeText{
133 tstLeftNoMD, tstSC1, tstParam1, tstSCClose, tstRightNoMD,
134 tstLeftNoMD, tstSC1, tstParam1, tstSCClose, tstRightNoMD, tstEOF,
135 }, nil},
136 {"multiple different self-closing with param", `{{< sc1 param1 />}}{{< sc2 param1 />}}`, []typeText{
137 tstLeftNoMD, tstSC1, tstParam1, tstSCClose, tstRightNoMD,
138 tstLeftNoMD, tstSC2, tstParam1, tstSCClose, tstRightNoMD, tstEOF,
139 }, nil},
140 {"nested simple", `{{< sc1 >}}{{< sc2 >}}{{< /sc1 >}}`, []typeText{
141 tstLeftNoMD, tstSC1, tstRightNoMD,
142 tstLeftNoMD, tstSC2, tstRightNoMD,
143 tstLeftNoMD, tstSCClose, tstSC1, tstRightNoMD, tstEOF,
144 }, nil},
145 {"nested same", `{{< sc1 >}}{{< sc1 >}}{{< /sc1 >}}{{< /sc1 >}}`, []typeText{
146 tstLeftNoMD, tstSC1, tstRightNoMD,
147 tstLeftNoMD, tstSC1, tstRightNoMD,
148 tstLeftNoMD, tstSCClose, tstSC1, tstRightNoMD,
149 tstLeftNoMD, tstSCClose, tstSC1, tstRightNoMD, tstEOF,
150 }, nil},
151 {"nested complex", `{{< sc1 >}}ab{{% sc2 param1 %}}cd{{< sc3 >}}ef{{< /sc3 >}}gh{{% /sc2 %}}ij{{< /sc1 >}}kl`, []typeText{
152 tstLeftNoMD, tstSC1, tstRightNoMD,
153 nti(tText, "ab"),
154 tstLeftMD, tstSC2, tstParam1, tstRightMD,
155 nti(tText, "cd"),
156 tstLeftNoMD, tstSC3, tstRightNoMD,
157 nti(tText, "ef"),
158 tstLeftNoMD, tstSCClose, tstSC3, tstRightNoMD,
159 nti(tText, "gh"),
160 tstLeftMD, tstSCClose, tstSC2, tstRightMD,
161 nti(tText, "ij"),
162 tstLeftNoMD, tstSCClose, tstSC1, tstRightNoMD,
163 nti(tText, "kl"), tstEOF,
164 }, nil},
165
166 {"two quoted params", `{{< sc1 "param nr. 1" "param nr. 2" >}}`, []typeText{
167 tstLeftNoMD, tstSC1, nti(tScParam, "param nr. 1"), nti(tScParam, "param nr. 2"), tstRightNoMD, tstEOF,
168 }, nil},
169 {"two named params", `{{< sc1 param1="Hello World" param2="p2Val">}}`, []typeText{
170 tstLeftNoMD, tstSC1, tstParam1, tstVal, tstParam2, nti(tScParamVal, "p2Val"), tstRightNoMD, tstEOF,
171 }, nil},
172 {"escaped quotes", `{{< sc1 param1=\"Hello World\" >}}`, []typeText{
173 tstLeftNoMD, tstSC1, tstParam1, tstVal, tstRightNoMD, tstEOF,
174 }, nil},
175 {"escaped quotes, positional param", `{{< sc1 \"param1\" >}}`, []typeText{
176 tstLeftNoMD, tstSC1, tstParam1, tstRightNoMD, tstEOF,
177 }, nil},
178 {"escaped quotes inside escaped quotes", `{{< sc1 param1=\"Hello \"escaped\" World\" >}}`, []typeText{
179 tstLeftNoMD, tstSC1, tstParam1,
180 nti(tScParamVal, `Hello `), nti(tError, `got positional parameter 'escaped'. Cannot mix named and positional parameters`),
181 }, nil},
182 {
183 "escaped quotes inside nonescaped quotes",
184 `{{< sc1 param1="Hello \"escaped\" World" >}}`,
185 []typeText{
186 tstLeftNoMD, tstSC1, tstParam1, nti(tScParamVal, `Hello "escaped" World`), tstRightNoMD, tstEOF,
187 },
188 nil,
189 },
190 {
191 "escaped quotes inside nonescaped quotes in positional param",
192 `{{< sc1 "Hello \"escaped\" World" >}}`,
193 []typeText{
194 tstLeftNoMD, tstSC1, nti(tScParam, `Hello "escaped" World`), tstRightNoMD, tstEOF,
195 },
196 nil,
197 },
198 {"escaped raw string, named param", `{{< sc1 param1=` + `\` + "`" + "Hello World" + `\` + "`" + ` >}}`, []typeText{
199 tstLeftNoMD, tstSC1, tstParam1, nti(tError, "unrecognized escape character"),
200 }, nil},
201 {"escaped raw string, positional param", `{{< sc1 param1 ` + `\` + "`" + "Hello World" + `\` + "`" + ` >}}`, []typeText{
202 tstLeftNoMD, tstSC1, tstParam1, nti(tError, "unrecognized escape character"),
203 }, nil},
204 {"two raw string params", `{{< sc1` + "`" + "Hello World" + "`" + "`" + "Second Param" + "`" + ` >}}`, []typeText{
205 tstLeftNoMD, tstSC1, nti(tScParam, "Hello World"), nti(tScParam, "Second Param"), tstRightNoMD, tstEOF,
206 }, nil},
207 {"unterminated quote", `{{< sc1 param2="Hello World>}}`, []typeText{
208 tstLeftNoMD, tstSC1, tstParam2, nti(tError, "unterminated quoted string in shortcode parameter-argument: 'Hello World>}}'"),
209 }, nil},
210 {"unterminated raw string", `{{< sc1` + "`" + "Hello World" + ` >}}`, []typeText{
211 tstLeftNoMD, tstSC1, nti(tError, "unterminated raw string in shortcode parameter-argument: 'Hello World >}}'"),
212 }, nil},
213 {"unterminated raw string in second argument", `{{< sc1` + "`" + "Hello World" + "`" + "`" + "Second Param" + ` >}}`, []typeText{
214 tstLeftNoMD, tstSC1, nti(tScParam, "Hello World"), nti(tError, "unterminated raw string in shortcode parameter-argument: 'Second Param >}}'"),
215 }, nil},
216 {"one named param, one not", `{{< sc1 param1="Hello World" p2 >}}`, []typeText{
217 tstLeftNoMD, tstSC1, tstParam1, tstVal,
218 nti(tError, "got positional parameter 'p2'. Cannot mix named and positional parameters"),
219 }, nil},
220 {"one named param, one quoted positional param, both raw strings", `{{< sc1 param1=` + "`" + "Hello World" + "`" + "`" + "Second Param" + "`" + ` >}}`, []typeText{
221 tstLeftNoMD, tstSC1, tstParam1, tstVal,
222 nti(tError, "got quoted positional parameter. Cannot mix named and positional parameters"),
223 }, nil},
224 {"one named param, one quoted positional param", `{{< sc1 param1="Hello World" "And Universe" >}}`, []typeText{
225 tstLeftNoMD, tstSC1, tstParam1, tstVal,
226 nti(tError, "got quoted positional parameter. Cannot mix named and positional parameters"),
227 }, nil},
228 {"one quoted positional param, one named param", `{{< sc1 "param1" param2="And Universe" >}}`, []typeText{
229 tstLeftNoMD, tstSC1, tstParam1,
230 nti(tError, "got named parameter 'param2'. Cannot mix named and positional parameters"),
231 }, nil},
232 {"ono positional param, one not", `{{< sc1 param1 param2="Hello World">}}`, []typeText{
233 tstLeftNoMD, tstSC1, tstParam1,
234 nti(tError, "got named parameter 'param2'. Cannot mix named and positional parameters"),
235 }, nil},
236 {"commented out", `{{</* sc1 */>}}`, []typeText{
237 nti(tText, "{{<"), nti(tText, " sc1 "), nti(tText, ">}}"), tstEOF,
238 }, nil},
239 {"commented out, with asterisk inside", `{{</* sc1 "**/*.pdf" */>}}`, []typeText{
240 nti(tText, "{{<"), nti(tText, " sc1 \"**/*.pdf\" "), nti(tText, ">}}"), tstEOF,
241 }, nil},
242 {"commented out, missing close", `{{</* sc1 >}}`, []typeText{
243 nti(tError, "comment must be closed"),
244 }, nil},
245 {"commented out, misplaced close", `{{</* sc1 >}}*/`, []typeText{
246 nti(tError, "comment must be closed"),
247 }, nil},
248 // Inline shortcodes
249 {"basic inline", `{{< sc1.inline >}}Hello World{{< /sc1.inline >}}`, []typeText{tstLeftNoMD, tstSC1Inline, tstRightNoMD, tstText, tstLeftNoMD, tstSCClose, tstSC1Inline, tstRightNoMD, tstEOF}, nil},
250 {"basic inline with space", `{{< sc1.inline >}}Hello World{{< / sc1.inline >}}`, []typeText{tstLeftNoMD, tstSC1Inline, tstRightNoMD, tstText, tstLeftNoMD, tstSCClose, tstSC1Inline, tstRightNoMD, tstEOF}, nil},
251 {"inline self closing", `{{< sc1.inline >}}Hello World{{< /sc1.inline >}}Hello World{{< sc1.inline />}}`, []typeText{tstLeftNoMD, tstSC1Inline, tstRightNoMD, tstText, tstLeftNoMD, tstSCClose, tstSC1Inline, tstRightNoMD, tstText, tstLeftNoMD, tstSC1Inline, tstSCClose, tstRightNoMD, tstEOF}, nil},
252 {"inline self closing, then a new inline", `{{< sc1.inline >}}Hello World{{< /sc1.inline >}}Hello World{{< sc1.inline />}}{{< sc2.inline >}}Hello World{{< /sc2.inline >}}`, []typeText{
253 tstLeftNoMD, tstSC1Inline, tstRightNoMD, tstText, tstLeftNoMD, tstSCClose, tstSC1Inline, tstRightNoMD, tstText, tstLeftNoMD, tstSC1Inline, tstSCClose, tstRightNoMD,
254 tstLeftNoMD, tstSC2Inline, tstRightNoMD, tstText, tstLeftNoMD, tstSCClose, tstSC2Inline, tstRightNoMD, tstEOF,
255 }, nil},
256 {"inline with template syntax", `{{< sc1.inline >}}{{ .Get 0 }}{{ .Get 1 }}{{< /sc1.inline >}}`, []typeText{tstLeftNoMD, tstSC1Inline, tstRightNoMD, nti(tText, "{{ .Get 0 }}"), nti(tText, "{{ .Get 1 }}"), tstLeftNoMD, tstSCClose, tstSC1Inline, tstRightNoMD, tstEOF}, nil},
257 {"inline with nested shortcode (not supported)", `{{< sc1.inline >}}Hello World{{< sc1 >}}{{< /sc1.inline >}}`, []typeText{tstLeftNoMD, tstSC1Inline, tstRightNoMD, tstText, nti(tError, "inline shortcodes do not support nesting")}, nil},
258 {"inline case mismatch", `{{< sc1.Inline >}}Hello World{{< /sc1.Inline >}}`, []typeText{tstLeftNoMD, nti(tError, "period in shortcode name only allowed for inline identifiers")}, nil},
259 }
260
261 func TestShortcodeLexer(t *testing.T) {
262 t.Parallel()
263 c := qt.New(t)
264 for i, test := range shortCodeLexerTests {
265 t.Run(test.name, func(t *testing.T) {
266 items, err := collect([]byte(test.input), true, lexMainSection)
267 c.Assert(err, qt.IsNil)
268 if !equal(test.input, items, test.items) {
269 got := itemsToString(items, []byte(test.input))
270 expected := testItemsToString(test.items)
271 c.Assert(got, qt.Equals, expected, qt.Commentf("Test %d: %s", i, test.name))
272 }
273 })
274 }
275 }
276
277 func BenchmarkShortcodeLexer(b *testing.B) {
278 testInputs := make([][]byte, len(shortCodeLexerTests))
279 for i, input := range shortCodeLexerTests {
280 testInputs[i] = []byte(input.input)
281 }
282 var cfg Config
283
284 for b.Loop() {
285 for _, input := range testInputs {
286 _, err := collectWithConfig(input, true, lexMainSection, cfg)
287 if err != nil {
288 b.Fatal(err)
289 }
290
291 }
292 }
293 }