mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-26 22:04:32 +02:00
@@ -362,8 +362,7 @@ var lexTests = []lexTest{
|
||||
{"extra right paren", "{{3)}}", []item{
|
||||
tLeft,
|
||||
mkItem(itemNumber, "3"),
|
||||
tRpar,
|
||||
mkItem(itemError, `unexpected right paren U+0029 ')'`),
|
||||
mkItem(itemError, "unexpected right paren"),
|
||||
}},
|
||||
|
||||
// Fixed bugs
|
||||
@@ -397,7 +396,12 @@ var lexTests = []lexTest{
|
||||
|
||||
// collect gathers the emitted items into a slice.
|
||||
func collect(t *lexTest, left, right string) (items []item) {
|
||||
l := lex(t.name, t.input, left, right, true)
|
||||
l := lex(t.name, t.input, left, right)
|
||||
l.options = lexOptions{
|
||||
emitComment: true,
|
||||
breakOK: true,
|
||||
continueOK: true,
|
||||
}
|
||||
for {
|
||||
item := l.nextItem()
|
||||
items = append(items, item)
|
||||
@@ -434,7 +438,9 @@ func TestLex(t *testing.T) {
|
||||
items := collect(&test, "", "")
|
||||
if !equal(items, test.items, false) {
|
||||
t.Errorf("%s: got\n\t%+v\nexpected\n\t%v", test.name, items, test.items)
|
||||
return // TODO
|
||||
}
|
||||
t.Log(test.name, "OK")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -472,6 +478,39 @@ func TestDelims(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestDelimsAlphaNumeric(t *testing.T) {
|
||||
test := lexTest{"right delimiter with alphanumeric start", "{{hub .host hub}}", []item{
|
||||
mkItem(itemLeftDelim, "{{hub"),
|
||||
mkItem(itemSpace, " "),
|
||||
mkItem(itemField, ".host"),
|
||||
mkItem(itemSpace, " "),
|
||||
mkItem(itemRightDelim, "hub}}"),
|
||||
tEOF,
|
||||
}}
|
||||
items := collect(&test, "{{hub", "hub}}")
|
||||
|
||||
if !equal(items, test.items, false) {
|
||||
t.Errorf("%s: got\n\t%v\nexpected\n\t%v", test.name, items, test.items)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDelimsAndMarkers(t *testing.T) {
|
||||
test := lexTest{"delims that look like markers", "{{- .x -}} {{- - .x - -}}", []item{
|
||||
mkItem(itemLeftDelim, "{{- "),
|
||||
mkItem(itemField, ".x"),
|
||||
mkItem(itemRightDelim, " -}}"),
|
||||
mkItem(itemLeftDelim, "{{- "),
|
||||
mkItem(itemField, ".x"),
|
||||
mkItem(itemRightDelim, " -}}"),
|
||||
tEOF,
|
||||
}}
|
||||
items := collect(&test, "{{- ", " -}}")
|
||||
|
||||
if !equal(items, test.items, false) {
|
||||
t.Errorf("%s: got\n\t%v\nexpected\n\t%v", test.name, items, test.items)
|
||||
}
|
||||
}
|
||||
|
||||
var lexPosTests = []lexTest{
|
||||
{"empty", "", []item{{itemEOF, 0, "", 1}}},
|
||||
{"punctuation", "{{,@%#}}", []item{
|
||||
@@ -533,22 +572,6 @@ func TestPos(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// Test that an error shuts down the lexing goroutine.
|
||||
func TestShutdown(t *testing.T) {
|
||||
// We need to duplicate template.Parse here to hold on to the lexer.
|
||||
const text = "erroneous{{define}}{{else}}1234"
|
||||
lexer := lex("foo", text, "{{", "}}", false)
|
||||
_, err := New("root").parseLexer(lexer)
|
||||
if err == nil {
|
||||
t.Fatalf("expected error")
|
||||
}
|
||||
// The error should have drained the input. Therefore, the lexer should be shut down.
|
||||
token, ok := <-lexer.items
|
||||
if ok {
|
||||
t.Fatalf("input was not drained; got %v", token)
|
||||
}
|
||||
}
|
||||
|
||||
// parseLexer is a local version of parse that lets us pass in the lexer instead of building it.
|
||||
// We expect an error, so the tree set and funcs list are explicitly nil.
|
||||
func (t *Tree) parseLexer(lex *lexer) (tree *Tree, err error) {
|
||||
|
Reference in New Issue
Block a user