mirror of
https://github.com/gohugoio/hugo.git
synced 2025-04-29 15:10:35 +03:00
parent
2fdc4a24d5
commit
27f5a906a2
4 changed files with 115 additions and 103 deletions
|
@ -16,25 +16,25 @@ package pageparser
|
|||
import "testing"
|
||||
|
||||
var (
|
||||
tstEOF = Item{tEOF, 0, ""}
|
||||
tstLeftNoMD = Item{tLeftDelimScNoMarkup, 0, "{{<"}
|
||||
tstRightNoMD = Item{tRightDelimScNoMarkup, 0, ">}}"}
|
||||
tstLeftMD = Item{tLeftDelimScWithMarkup, 0, "{{%"}
|
||||
tstRightMD = Item{tRightDelimScWithMarkup, 0, "%}}"}
|
||||
tstSCClose = Item{tScClose, 0, "/"}
|
||||
tstSC1 = Item{tScName, 0, "sc1"}
|
||||
tstSC2 = Item{tScName, 0, "sc2"}
|
||||
tstSC3 = Item{tScName, 0, "sc3"}
|
||||
tstSCSlash = Item{tScName, 0, "sc/sub"}
|
||||
tstParam1 = Item{tScParam, 0, "param1"}
|
||||
tstParam2 = Item{tScParam, 0, "param2"}
|
||||
tstVal = Item{tScParamVal, 0, "Hello World"}
|
||||
tstEOF = nti(tEOF, "")
|
||||
tstLeftNoMD = nti(tLeftDelimScNoMarkup, "{{<")
|
||||
tstRightNoMD = nti(tRightDelimScNoMarkup, ">}}")
|
||||
tstLeftMD = nti(tLeftDelimScWithMarkup, "{{%")
|
||||
tstRightMD = nti(tRightDelimScWithMarkup, "%}}")
|
||||
tstSCClose = nti(tScClose, "/")
|
||||
tstSC1 = nti(tScName, "sc1")
|
||||
tstSC2 = nti(tScName, "sc2")
|
||||
tstSC3 = nti(tScName, "sc3")
|
||||
tstSCSlash = nti(tScName, "sc/sub")
|
||||
tstParam1 = nti(tScParam, "param1")
|
||||
tstParam2 = nti(tScParam, "param2")
|
||||
tstVal = nti(tScParamVal, "Hello World")
|
||||
)
|
||||
|
||||
var shortCodeLexerTests = []lexerTest{
|
||||
{"empty", "", []Item{tstEOF}},
|
||||
{"spaces", " \t\n", []Item{{tText, 0, " \t\n"}, tstEOF}},
|
||||
{"text", `to be or not`, []Item{{tText, 0, "to be or not"}, tstEOF}},
|
||||
{"spaces", " \t\n", []Item{nti(tText, " \t\n"), tstEOF}},
|
||||
{"text", `to be or not`, []Item{nti(tText, "to be or not"), tstEOF}},
|
||||
{"no markup", `{{< sc1 >}}`, []Item{tstLeftNoMD, tstSC1, tstRightNoMD, tstEOF}},
|
||||
{"with EOL", "{{< sc1 \n >}}", []Item{tstLeftNoMD, tstSC1, tstRightNoMD, tstEOF}},
|
||||
|
||||
|
@ -43,12 +43,12 @@ var shortCodeLexerTests = []lexerTest{
|
|||
{"simple with markup", `{{% sc1 %}}`, []Item{tstLeftMD, tstSC1, tstRightMD, tstEOF}},
|
||||
{"with spaces", `{{< sc1 >}}`, []Item{tstLeftNoMD, tstSC1, tstRightNoMD, tstEOF}},
|
||||
{"mismatched rightDelim", `{{< sc1 %}}`, []Item{tstLeftNoMD, tstSC1,
|
||||
{tError, 0, "unrecognized character in shortcode action: U+0025 '%'. Note: Parameters with non-alphanumeric args must be quoted"}}},
|
||||
nti(tError, "unrecognized character in shortcode action: U+0025 '%'. Note: Parameters with non-alphanumeric args must be quoted")}},
|
||||
{"inner, markup", `{{% sc1 %}} inner {{% /sc1 %}}`, []Item{
|
||||
tstLeftMD,
|
||||
tstSC1,
|
||||
tstRightMD,
|
||||
{tText, 0, " inner "},
|
||||
nti(tText, " inner "),
|
||||
tstLeftMD,
|
||||
tstSCClose,
|
||||
tstSC1,
|
||||
|
@ -56,20 +56,20 @@ var shortCodeLexerTests = []lexerTest{
|
|||
tstEOF,
|
||||
}},
|
||||
{"close, but no open", `{{< /sc1 >}}`, []Item{
|
||||
tstLeftNoMD, {tError, 0, "got closing shortcode, but none is open"}}},
|
||||
tstLeftNoMD, nti(tError, "got closing shortcode, but none is open")}},
|
||||
{"close wrong", `{{< sc1 >}}{{< /another >}}`, []Item{
|
||||
tstLeftNoMD, tstSC1, tstRightNoMD, tstLeftNoMD, tstSCClose,
|
||||
{tError, 0, "closing tag for shortcode 'another' does not match start tag"}}},
|
||||
nti(tError, "closing tag for shortcode 'another' does not match start tag")}},
|
||||
{"close, but no open, more", `{{< sc1 >}}{{< /sc1 >}}{{< /another >}}`, []Item{
|
||||
tstLeftNoMD, tstSC1, tstRightNoMD, tstLeftNoMD, tstSCClose, tstSC1, tstRightNoMD, tstLeftNoMD, tstSCClose,
|
||||
{tError, 0, "closing tag for shortcode 'another' does not match start tag"}}},
|
||||
nti(tError, "closing tag for shortcode 'another' does not match start tag")}},
|
||||
{"close with extra keyword", `{{< sc1 >}}{{< /sc1 keyword>}}`, []Item{
|
||||
tstLeftNoMD, tstSC1, tstRightNoMD, tstLeftNoMD, tstSCClose, tstSC1,
|
||||
{tError, 0, "unclosed shortcode"}}},
|
||||
nti(tError, "unclosed shortcode")}},
|
||||
{"Youtube id", `{{< sc1 -ziL-Q_456igdO-4 >}}`, []Item{
|
||||
tstLeftNoMD, tstSC1, {tScParam, 0, "-ziL-Q_456igdO-4"}, tstRightNoMD, tstEOF}},
|
||||
tstLeftNoMD, tstSC1, nti(tScParam, "-ziL-Q_456igdO-4"), tstRightNoMD, tstEOF}},
|
||||
{"non-alphanumerics param quoted", `{{< sc1 "-ziL-.%QigdO-4" >}}`, []Item{
|
||||
tstLeftNoMD, tstSC1, {tScParam, 0, "-ziL-.%QigdO-4"}, tstRightNoMD, tstEOF}},
|
||||
tstLeftNoMD, tstSC1, nti(tScParam, "-ziL-.%QigdO-4"), tstRightNoMD, tstEOF}},
|
||||
|
||||
{"two params", `{{< sc1 param1 param2 >}}`, []Item{
|
||||
tstLeftNoMD, tstSC1, tstParam1, tstParam2, tstRightNoMD, tstEOF}},
|
||||
|
@ -94,64 +94,64 @@ var shortCodeLexerTests = []lexerTest{
|
|||
tstLeftNoMD, tstSCClose, tstSC1, tstRightNoMD, tstEOF}},
|
||||
{"nested complex", `{{< sc1 >}}ab{{% sc2 param1 %}}cd{{< sc3 >}}ef{{< /sc3 >}}gh{{% /sc2 %}}ij{{< /sc1 >}}kl`, []Item{
|
||||
tstLeftNoMD, tstSC1, tstRightNoMD,
|
||||
{tText, 0, "ab"},
|
||||
nti(tText, "ab"),
|
||||
tstLeftMD, tstSC2, tstParam1, tstRightMD,
|
||||
{tText, 0, "cd"},
|
||||
nti(tText, "cd"),
|
||||
tstLeftNoMD, tstSC3, tstRightNoMD,
|
||||
{tText, 0, "ef"},
|
||||
nti(tText, "ef"),
|
||||
tstLeftNoMD, tstSCClose, tstSC3, tstRightNoMD,
|
||||
{tText, 0, "gh"},
|
||||
nti(tText, "gh"),
|
||||
tstLeftMD, tstSCClose, tstSC2, tstRightMD,
|
||||
{tText, 0, "ij"},
|
||||
nti(tText, "ij"),
|
||||
tstLeftNoMD, tstSCClose, tstSC1, tstRightNoMD,
|
||||
{tText, 0, "kl"}, tstEOF,
|
||||
nti(tText, "kl"), tstEOF,
|
||||
}},
|
||||
|
||||
{"two quoted params", `{{< sc1 "param nr. 1" "param nr. 2" >}}`, []Item{
|
||||
tstLeftNoMD, tstSC1, {tScParam, 0, "param nr. 1"}, {tScParam, 0, "param nr. 2"}, tstRightNoMD, tstEOF}},
|
||||
tstLeftNoMD, tstSC1, nti(tScParam, "param nr. 1"), nti(tScParam, "param nr. 2"), tstRightNoMD, tstEOF}},
|
||||
{"two named params", `{{< sc1 param1="Hello World" param2="p2Val">}}`, []Item{
|
||||
tstLeftNoMD, tstSC1, tstParam1, tstVal, tstParam2, {tScParamVal, 0, "p2Val"}, tstRightNoMD, tstEOF}},
|
||||
tstLeftNoMD, tstSC1, tstParam1, tstVal, tstParam2, nti(tScParamVal, "p2Val"), tstRightNoMD, tstEOF}},
|
||||
{"escaped quotes", `{{< sc1 param1=\"Hello World\" >}}`, []Item{
|
||||
tstLeftNoMD, tstSC1, tstParam1, tstVal, tstRightNoMD, tstEOF}},
|
||||
{"escaped quotes, positional param", `{{< sc1 \"param1\" >}}`, []Item{
|
||||
tstLeftNoMD, tstSC1, tstParam1, tstRightNoMD, tstEOF}},
|
||||
{"escaped quotes inside escaped quotes", `{{< sc1 param1=\"Hello \"escaped\" World\" >}}`, []Item{
|
||||
tstLeftNoMD, tstSC1, tstParam1,
|
||||
{tScParamVal, 0, `Hello `}, {tError, 0, `got positional parameter 'escaped'. Cannot mix named and positional parameters`}}},
|
||||
nti(tScParamVal, `Hello `), nti(tError, `got positional parameter 'escaped'. Cannot mix named and positional parameters`)}},
|
||||
{"escaped quotes inside nonescaped quotes",
|
||||
`{{< sc1 param1="Hello \"escaped\" World" >}}`, []Item{
|
||||
tstLeftNoMD, tstSC1, tstParam1, {tScParamVal, 0, `Hello "escaped" World`}, tstRightNoMD, tstEOF}},
|
||||
tstLeftNoMD, tstSC1, tstParam1, nti(tScParamVal, `Hello "escaped" World`), tstRightNoMD, tstEOF}},
|
||||
{"escaped quotes inside nonescaped quotes in positional param",
|
||||
`{{< sc1 "Hello \"escaped\" World" >}}`, []Item{
|
||||
tstLeftNoMD, tstSC1, {tScParam, 0, `Hello "escaped" World`}, tstRightNoMD, tstEOF}},
|
||||
tstLeftNoMD, tstSC1, nti(tScParam, `Hello "escaped" World`), tstRightNoMD, tstEOF}},
|
||||
{"unterminated quote", `{{< sc1 param2="Hello World>}}`, []Item{
|
||||
tstLeftNoMD, tstSC1, tstParam2, {tError, 0, "unterminated quoted string in shortcode parameter-argument: 'Hello World>}}'"}}},
|
||||
tstLeftNoMD, tstSC1, tstParam2, nti(tError, "unterminated quoted string in shortcode parameter-argument: 'Hello World>}}'")}},
|
||||
{"one named param, one not", `{{< sc1 param1="Hello World" p2 >}}`, []Item{
|
||||
tstLeftNoMD, tstSC1, tstParam1, tstVal,
|
||||
{tError, 0, "got positional parameter 'p2'. Cannot mix named and positional parameters"}}},
|
||||
nti(tError, "got positional parameter 'p2'. Cannot mix named and positional parameters")}},
|
||||
{"one named param, one quoted positional param", `{{< sc1 param1="Hello World" "And Universe" >}}`, []Item{
|
||||
tstLeftNoMD, tstSC1, tstParam1, tstVal,
|
||||
{tError, 0, "got quoted positional parameter. Cannot mix named and positional parameters"}}},
|
||||
nti(tError, "got quoted positional parameter. Cannot mix named and positional parameters")}},
|
||||
{"one quoted positional param, one named param", `{{< sc1 "param1" param2="And Universe" >}}`, []Item{
|
||||
tstLeftNoMD, tstSC1, tstParam1,
|
||||
{tError, 0, "got named parameter 'param2'. Cannot mix named and positional parameters"}}},
|
||||
nti(tError, "got named parameter 'param2'. Cannot mix named and positional parameters")}},
|
||||
{"ono positional param, one not", `{{< sc1 param1 param2="Hello World">}}`, []Item{
|
||||
tstLeftNoMD, tstSC1, tstParam1,
|
||||
{tError, 0, "got named parameter 'param2'. Cannot mix named and positional parameters"}}},
|
||||
nti(tError, "got named parameter 'param2'. Cannot mix named and positional parameters")}},
|
||||
{"commented out", `{{</* sc1 */>}}`, []Item{
|
||||
{tText, 0, "{{<"}, {tText, 0, " sc1 "}, {tText, 0, ">}}"}, tstEOF}},
|
||||
nti(tText, "{{<"), nti(tText, " sc1 "), nti(tText, ">}}"), tstEOF}},
|
||||
{"commented out, with asterisk inside", `{{</* sc1 "**/*.pdf" */>}}`, []Item{
|
||||
{tText, 0, "{{<"}, {tText, 0, " sc1 \"**/*.pdf\" "}, {tText, 0, ">}}"}, tstEOF}},
|
||||
nti(tText, "{{<"), nti(tText, " sc1 \"**/*.pdf\" "), nti(tText, ">}}"), tstEOF}},
|
||||
{"commented out, missing close", `{{</* sc1 >}}`, []Item{
|
||||
{tError, 0, "comment must be closed"}}},
|
||||
nti(tError, "comment must be closed")}},
|
||||
{"commented out, misplaced close", `{{</* sc1 >}}*/`, []Item{
|
||||
{tError, 0, "comment must be closed"}}},
|
||||
nti(tError, "comment must be closed")}},
|
||||
}
|
||||
|
||||
func TestShortcodeLexer(t *testing.T) {
|
||||
t.Parallel()
|
||||
for i, test := range shortCodeLexerTests {
|
||||
items := collect(test.name, test.input, true, lexMainSection)
|
||||
items := collect([]byte(test.input), true, lexMainSection)
|
||||
if !equal(items, test.items) {
|
||||
t.Errorf("[%d] %s: got\n\t%v\nexpected\n\t%v", i, test.name, items, test.items)
|
||||
}
|
||||
|
@ -159,13 +159,17 @@ func TestShortcodeLexer(t *testing.T) {
|
|||
}
|
||||
|
||||
func BenchmarkShortcodeLexer(b *testing.B) {
|
||||
testInputs := make([][]byte, len(shortCodeLexerTests))
|
||||
for i, input := range shortCodeLexerTests {
|
||||
testInputs[i] = []byte(input.input)
|
||||
}
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
for _, test := range shortCodeLexerTests {
|
||||
items := collect(test.name, test.input, true, lexMainSection)
|
||||
if !equal(items, test.items) {
|
||||
b.Errorf("%s: got\n\t%v\nexpected\n\t%v", test.name, items, test.items)
|
||||
for _, input := range testInputs {
|
||||
items := collect(input, true, lexMainSection)
|
||||
if len(items) == 0 {
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue