2021-09-17 02:50:32 +08:00
|
|
|
|
// Copyright 2015 Matthew Holt and The Caddy Authors
|
2019-08-10 02:05:47 +08:00
|
|
|
|
//
|
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
//
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
//
|
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
|
|
package caddyfile
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"testing"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestLexer(t *testing.T) {
|
2023-02-26 08:34:27 +08:00
|
|
|
|
testCases := []struct {
|
|
|
|
|
input []byte
|
|
|
|
|
expected []Token
|
|
|
|
|
expectErr bool
|
|
|
|
|
errorMessage string
|
|
|
|
|
}{
|
2019-08-10 02:05:47 +08:00
|
|
|
|
{
|
2020-07-21 03:55:51 +08:00
|
|
|
|
input: []byte(`host:123`),
|
2019-08-10 02:05:47 +08:00
|
|
|
|
expected: []Token{
|
|
|
|
|
{Line: 1, Text: "host:123"},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
2020-07-21 03:55:51 +08:00
|
|
|
|
input: []byte(`host:123
|
2019-08-10 02:05:47 +08:00
|
|
|
|
|
2020-07-21 03:55:51 +08:00
|
|
|
|
directive`),
|
2019-08-10 02:05:47 +08:00
|
|
|
|
expected: []Token{
|
|
|
|
|
{Line: 1, Text: "host:123"},
|
|
|
|
|
{Line: 3, Text: "directive"},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
2020-07-21 03:55:51 +08:00
|
|
|
|
input: []byte(`host:123 {
|
2019-08-10 02:05:47 +08:00
|
|
|
|
directive
|
2020-07-21 03:55:51 +08:00
|
|
|
|
}`),
|
2019-08-10 02:05:47 +08:00
|
|
|
|
expected: []Token{
|
|
|
|
|
{Line: 1, Text: "host:123"},
|
|
|
|
|
{Line: 1, Text: "{"},
|
|
|
|
|
{Line: 2, Text: "directive"},
|
|
|
|
|
{Line: 3, Text: "}"},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
2020-07-21 03:55:51 +08:00
|
|
|
|
input: []byte(`host:123 { directive }`),
|
2019-08-10 02:05:47 +08:00
|
|
|
|
expected: []Token{
|
|
|
|
|
{Line: 1, Text: "host:123"},
|
|
|
|
|
{Line: 1, Text: "{"},
|
|
|
|
|
{Line: 1, Text: "directive"},
|
|
|
|
|
{Line: 1, Text: "}"},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
2020-07-21 03:55:51 +08:00
|
|
|
|
input: []byte(`host:123 {
|
2019-08-10 02:05:47 +08:00
|
|
|
|
#comment
|
|
|
|
|
directive
|
|
|
|
|
# comment
|
|
|
|
|
foobar # another comment
|
2020-07-21 03:55:51 +08:00
|
|
|
|
}`),
|
2019-08-10 02:05:47 +08:00
|
|
|
|
expected: []Token{
|
|
|
|
|
{Line: 1, Text: "host:123"},
|
|
|
|
|
{Line: 1, Text: "{"},
|
|
|
|
|
{Line: 3, Text: "directive"},
|
|
|
|
|
{Line: 5, Text: "foobar"},
|
|
|
|
|
{Line: 6, Text: "}"},
|
|
|
|
|
},
|
|
|
|
|
},
|
2020-05-06 02:32:12 +08:00
|
|
|
|
{
|
2020-07-21 03:55:51 +08:00
|
|
|
|
input: []byte(`host:123 {
|
2020-05-06 02:32:12 +08:00
|
|
|
|
# hash inside string is not a comment
|
|
|
|
|
redir / /some/#/path
|
2020-07-21 03:55:51 +08:00
|
|
|
|
}`),
|
2020-05-06 02:32:12 +08:00
|
|
|
|
expected: []Token{
|
|
|
|
|
{Line: 1, Text: "host:123"},
|
|
|
|
|
{Line: 1, Text: "{"},
|
|
|
|
|
{Line: 3, Text: "redir"},
|
|
|
|
|
{Line: 3, Text: "/"},
|
|
|
|
|
{Line: 3, Text: "/some/#/path"},
|
|
|
|
|
{Line: 4, Text: "}"},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
2020-07-21 03:55:51 +08:00
|
|
|
|
input: []byte("# comment at beginning of file\n# comment at beginning of line\nhost:123"),
|
2020-05-06 02:32:12 +08:00
|
|
|
|
expected: []Token{
|
|
|
|
|
{Line: 3, Text: "host:123"},
|
|
|
|
|
},
|
|
|
|
|
},
|
2019-08-10 02:05:47 +08:00
|
|
|
|
{
|
2020-07-21 03:55:51 +08:00
|
|
|
|
input: []byte(`a "quoted value" b
|
|
|
|
|
foobar`),
|
2019-08-10 02:05:47 +08:00
|
|
|
|
expected: []Token{
|
|
|
|
|
{Line: 1, Text: "a"},
|
|
|
|
|
{Line: 1, Text: "quoted value"},
|
|
|
|
|
{Line: 1, Text: "b"},
|
|
|
|
|
{Line: 2, Text: "foobar"},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
2020-07-21 03:55:51 +08:00
|
|
|
|
input: []byte(`A "quoted \"value\" inside" B`),
|
2019-08-10 02:05:47 +08:00
|
|
|
|
expected: []Token{
|
|
|
|
|
{Line: 1, Text: "A"},
|
|
|
|
|
{Line: 1, Text: `quoted "value" inside`},
|
|
|
|
|
{Line: 1, Text: "B"},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
2020-07-21 03:55:51 +08:00
|
|
|
|
input: []byte("An escaped \"newline\\\ninside\" quotes"),
|
2019-08-10 02:05:47 +08:00
|
|
|
|
expected: []Token{
|
2019-09-29 11:18:36 +08:00
|
|
|
|
{Line: 1, Text: "An"},
|
|
|
|
|
{Line: 1, Text: "escaped"},
|
|
|
|
|
{Line: 1, Text: "newline\\\ninside"},
|
2019-08-10 02:05:47 +08:00
|
|
|
|
{Line: 2, Text: "quotes"},
|
|
|
|
|
},
|
|
|
|
|
},
|
2019-09-29 11:18:36 +08:00
|
|
|
|
{
|
2020-07-21 03:55:51 +08:00
|
|
|
|
input: []byte("An escaped newline\\\noutside quotes"),
|
2019-09-29 11:18:36 +08:00
|
|
|
|
expected: []Token{
|
|
|
|
|
{Line: 1, Text: "An"},
|
|
|
|
|
{Line: 1, Text: "escaped"},
|
|
|
|
|
{Line: 1, Text: "newline"},
|
|
|
|
|
{Line: 1, Text: "outside"},
|
|
|
|
|
{Line: 1, Text: "quotes"},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
2020-07-21 03:55:51 +08:00
|
|
|
|
input: []byte("line1\\\nescaped\nline2\nline3"),
|
2019-09-29 11:18:36 +08:00
|
|
|
|
expected: []Token{
|
|
|
|
|
{Line: 1, Text: "line1"},
|
|
|
|
|
{Line: 1, Text: "escaped"},
|
|
|
|
|
{Line: 3, Text: "line2"},
|
|
|
|
|
{Line: 4, Text: "line3"},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
2020-07-21 03:55:51 +08:00
|
|
|
|
input: []byte("line1\\\nescaped1\\\nescaped2\nline4\nline5"),
|
2019-09-29 11:18:36 +08:00
|
|
|
|
expected: []Token{
|
|
|
|
|
{Line: 1, Text: "line1"},
|
|
|
|
|
{Line: 1, Text: "escaped1"},
|
|
|
|
|
{Line: 1, Text: "escaped2"},
|
|
|
|
|
{Line: 4, Text: "line4"},
|
|
|
|
|
{Line: 5, Text: "line5"},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
2020-07-21 03:55:51 +08:00
|
|
|
|
input: []byte(`"unescapable\ in quotes"`),
|
2019-09-29 11:18:36 +08:00
|
|
|
|
expected: []Token{
|
|
|
|
|
{Line: 1, Text: `unescapable\ in quotes`},
|
|
|
|
|
},
|
|
|
|
|
},
|
2019-08-10 02:05:47 +08:00
|
|
|
|
{
|
2020-07-21 03:55:51 +08:00
|
|
|
|
input: []byte(`"don't\escape"`),
|
2019-08-10 02:05:47 +08:00
|
|
|
|
expected: []Token{
|
|
|
|
|
{Line: 1, Text: `don't\escape`},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
2020-07-21 03:55:51 +08:00
|
|
|
|
input: []byte(`"don't\\escape"`),
|
2019-08-10 02:05:47 +08:00
|
|
|
|
expected: []Token{
|
|
|
|
|
{Line: 1, Text: `don't\\escape`},
|
|
|
|
|
},
|
|
|
|
|
},
|
2019-10-16 06:05:53 +08:00
|
|
|
|
{
|
2020-07-21 03:55:51 +08:00
|
|
|
|
input: []byte(`un\escapable`),
|
2019-10-16 06:05:53 +08:00
|
|
|
|
expected: []Token{
|
|
|
|
|
{Line: 1, Text: `un\escapable`},
|
|
|
|
|
},
|
|
|
|
|
},
|
2019-08-10 02:05:47 +08:00
|
|
|
|
{
|
2020-07-21 03:55:51 +08:00
|
|
|
|
input: []byte(`A "quoted value with line
|
2019-08-10 02:05:47 +08:00
|
|
|
|
break inside" {
|
|
|
|
|
foobar
|
2020-07-21 03:55:51 +08:00
|
|
|
|
}`),
|
2019-08-10 02:05:47 +08:00
|
|
|
|
expected: []Token{
|
|
|
|
|
{Line: 1, Text: "A"},
|
|
|
|
|
{Line: 1, Text: "quoted value with line\n\t\t\t\t\tbreak inside"},
|
|
|
|
|
{Line: 2, Text: "{"},
|
|
|
|
|
{Line: 3, Text: "foobar"},
|
|
|
|
|
{Line: 4, Text: "}"},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
2020-07-21 03:55:51 +08:00
|
|
|
|
input: []byte(`"C:\php\php-cgi.exe"`),
|
2019-08-10 02:05:47 +08:00
|
|
|
|
expected: []Token{
|
|
|
|
|
{Line: 1, Text: `C:\php\php-cgi.exe`},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
2020-07-21 03:55:51 +08:00
|
|
|
|
input: []byte(`empty "" string`),
|
2019-08-10 02:05:47 +08:00
|
|
|
|
expected: []Token{
|
|
|
|
|
{Line: 1, Text: `empty`},
|
|
|
|
|
{Line: 1, Text: ``},
|
|
|
|
|
{Line: 1, Text: `string`},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
2020-07-21 03:55:51 +08:00
|
|
|
|
input: []byte("skip those\r\nCR characters"),
|
2019-08-10 02:05:47 +08:00
|
|
|
|
expected: []Token{
|
|
|
|
|
{Line: 1, Text: "skip"},
|
|
|
|
|
{Line: 1, Text: "those"},
|
|
|
|
|
{Line: 2, Text: "CR"},
|
|
|
|
|
{Line: 2, Text: "characters"},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
2020-07-21 03:55:51 +08:00
|
|
|
|
input: []byte("\xEF\xBB\xBF:8080"), // test with leading byte order mark
|
2019-08-10 02:05:47 +08:00
|
|
|
|
expected: []Token{
|
|
|
|
|
{Line: 1, Text: ":8080"},
|
|
|
|
|
},
|
|
|
|
|
},
|
2020-05-06 02:27:49 +08:00
|
|
|
|
{
|
2020-07-21 03:55:51 +08:00
|
|
|
|
input: []byte("simple `backtick quoted` string"),
|
2020-05-06 02:27:49 +08:00
|
|
|
|
expected: []Token{
|
|
|
|
|
{Line: 1, Text: `simple`},
|
|
|
|
|
{Line: 1, Text: `backtick quoted`},
|
|
|
|
|
{Line: 1, Text: `string`},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
2020-07-21 03:55:51 +08:00
|
|
|
|
input: []byte("multiline `backtick\nquoted\n` string"),
|
2020-05-06 02:27:49 +08:00
|
|
|
|
expected: []Token{
|
|
|
|
|
{Line: 1, Text: `multiline`},
|
|
|
|
|
{Line: 1, Text: "backtick\nquoted\n"},
|
|
|
|
|
{Line: 3, Text: `string`},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
2020-07-21 03:55:51 +08:00
|
|
|
|
input: []byte("nested `\"quotes inside\" backticks` string"),
|
2020-05-06 02:27:49 +08:00
|
|
|
|
expected: []Token{
|
|
|
|
|
{Line: 1, Text: `nested`},
|
|
|
|
|
{Line: 1, Text: `"quotes inside" backticks`},
|
|
|
|
|
{Line: 1, Text: `string`},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
2020-07-21 03:55:51 +08:00
|
|
|
|
input: []byte("reverse-nested \"`backticks` inside\" quotes"),
|
2020-05-06 02:27:49 +08:00
|
|
|
|
expected: []Token{
|
|
|
|
|
{Line: 1, Text: `reverse-nested`},
|
|
|
|
|
{Line: 1, Text: "`backticks` inside"},
|
|
|
|
|
{Line: 1, Text: `quotes`},
|
|
|
|
|
},
|
|
|
|
|
},
|
2023-02-26 08:34:27 +08:00
|
|
|
|
{
|
|
|
|
|
input: []byte(`heredoc <<EOF
|
|
|
|
|
content
|
|
|
|
|
EOF same-line-arg
|
|
|
|
|
`),
|
|
|
|
|
expected: []Token{
|
|
|
|
|
{Line: 1, Text: `heredoc`},
|
2023-02-27 05:56:48 +08:00
|
|
|
|
{Line: 1, Text: "content"},
|
2023-02-26 08:34:27 +08:00
|
|
|
|
{Line: 3, Text: `same-line-arg`},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
input: []byte(`heredoc <<VERY-LONG-MARKER
|
|
|
|
|
content
|
|
|
|
|
VERY-LONG-MARKER same-line-arg
|
|
|
|
|
`),
|
|
|
|
|
expected: []Token{
|
|
|
|
|
{Line: 1, Text: `heredoc`},
|
2023-02-27 05:56:48 +08:00
|
|
|
|
{Line: 1, Text: "content"},
|
2023-02-26 08:34:27 +08:00
|
|
|
|
{Line: 3, Text: `same-line-arg`},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
input: []byte(`heredoc <<EOF
|
2023-02-27 05:56:48 +08:00
|
|
|
|
extra-newline
|
|
|
|
|
|
|
|
|
|
EOF same-line-arg
|
|
|
|
|
`),
|
|
|
|
|
expected: []Token{
|
|
|
|
|
{Line: 1, Text: `heredoc`},
|
|
|
|
|
{Line: 1, Text: "extra-newline\n"},
|
|
|
|
|
{Line: 4, Text: `same-line-arg`},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
input: []byte(`heredoc <<EOF
|
|
|
|
|
EOF same-line-arg
|
|
|
|
|
`),
|
|
|
|
|
expected: []Token{
|
|
|
|
|
{Line: 1, Text: `heredoc`},
|
|
|
|
|
{Line: 1, Text: ""},
|
|
|
|
|
{Line: 2, Text: `same-line-arg`},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
input: []byte(`heredoc <<EOF
|
2023-02-26 08:34:27 +08:00
|
|
|
|
content
|
|
|
|
|
EOF same-line-arg
|
|
|
|
|
`),
|
|
|
|
|
expected: []Token{
|
|
|
|
|
{Line: 1, Text: `heredoc`},
|
2023-02-27 05:56:48 +08:00
|
|
|
|
{Line: 1, Text: "content"},
|
2023-02-26 08:34:27 +08:00
|
|
|
|
{Line: 3, Text: `same-line-arg`},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
input: []byte(`prev-line
|
|
|
|
|
heredoc <<EOF
|
|
|
|
|
multi
|
|
|
|
|
line
|
|
|
|
|
content
|
|
|
|
|
EOF same-line-arg
|
|
|
|
|
next-line
|
|
|
|
|
`),
|
|
|
|
|
expected: []Token{
|
|
|
|
|
{Line: 1, Text: `prev-line`},
|
|
|
|
|
{Line: 2, Text: `heredoc`},
|
2023-02-27 05:56:48 +08:00
|
|
|
|
{Line: 2, Text: "\tmulti\n\tline\n\tcontent"},
|
2023-02-26 08:34:27 +08:00
|
|
|
|
{Line: 6, Text: `same-line-arg`},
|
|
|
|
|
{Line: 7, Text: `next-line`},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
2023-08-19 18:32:32 +08:00
|
|
|
|
input: []byte(`escaped-heredoc \<< >>`),
|
|
|
|
|
expected: []Token{
|
|
|
|
|
{Line: 1, Text: `escaped-heredoc`},
|
|
|
|
|
{Line: 1, Text: `<<`},
|
|
|
|
|
{Line: 1, Text: `>>`},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
input: []byte(`not-a-heredoc <EOF
|
2023-02-26 08:34:27 +08:00
|
|
|
|
content
|
|
|
|
|
`),
|
|
|
|
|
expected: []Token{
|
2023-08-19 18:32:32 +08:00
|
|
|
|
{Line: 1, Text: `not-a-heredoc`},
|
2023-02-26 08:34:27 +08:00
|
|
|
|
{Line: 1, Text: `<EOF`},
|
|
|
|
|
{Line: 2, Text: `content`},
|
2023-08-19 18:32:32 +08:00
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
input: []byte(`not-a-heredoc <<<EOF content`),
|
|
|
|
|
expected: []Token{
|
|
|
|
|
{Line: 1, Text: `not-a-heredoc`},
|
|
|
|
|
{Line: 1, Text: `<<<EOF`},
|
|
|
|
|
{Line: 1, Text: `content`},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
input: []byte(`not-a-heredoc "<<" ">>"`),
|
|
|
|
|
expected: []Token{
|
|
|
|
|
{Line: 1, Text: `not-a-heredoc`},
|
|
|
|
|
{Line: 1, Text: `<<`},
|
|
|
|
|
{Line: 1, Text: `>>`},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
input: []byte(`not-a-heredoc << >>`),
|
|
|
|
|
expected: []Token{
|
|
|
|
|
{Line: 1, Text: `not-a-heredoc`},
|
|
|
|
|
{Line: 1, Text: `<<`},
|
|
|
|
|
{Line: 1, Text: `>>`},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
input: []byte(`not-a-heredoc <<HERE SAME LINE
|
|
|
|
|
content
|
|
|
|
|
HERE same-line-arg
|
|
|
|
|
`),
|
|
|
|
|
expected: []Token{
|
|
|
|
|
{Line: 1, Text: `not-a-heredoc`},
|
|
|
|
|
{Line: 1, Text: `<<HERE`},
|
|
|
|
|
{Line: 1, Text: `SAME`},
|
|
|
|
|
{Line: 1, Text: `LINE`},
|
|
|
|
|
{Line: 2, Text: `content`},
|
|
|
|
|
{Line: 3, Text: `HERE`},
|
2023-02-26 08:34:27 +08:00
|
|
|
|
{Line: 3, Text: `same-line-arg`},
|
|
|
|
|
},
|
|
|
|
|
},
|
2023-02-27 05:56:48 +08:00
|
|
|
|
{
|
|
|
|
|
input: []byte(`heredoc <<s
|
|
|
|
|
<EFBFBD>
|
|
|
|
|
s
|
|
|
|
|
`),
|
|
|
|
|
expected: []Token{
|
|
|
|
|
{Line: 1, Text: `heredoc`},
|
|
|
|
|
{Line: 1, Text: "<22>"},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
input: []byte("\u000Aheredoc \u003C\u003C\u0073\u0073\u000A\u00BF\u0057\u0001\u0000\u00FF\u00FF\u00FF\u00FF\u00FF\u00FF\u00FF\u003D\u001F\u000A\u0073\u0073\u000A\u00BF\u0057\u0001\u0000\u00FF\u00FF\u00FF\u00FF\u00FF\u00FF\u00FF\u003D\u001F\u000A\u00BF\u00BF\u0057\u0001\u0000\u00FF\u00FF\u00FF\u00FF\u00FF\u00FF\u00FF\u003D\u001F"),
|
|
|
|
|
expected: []Token{
|
|
|
|
|
{
|
|
|
|
|
Line: 2,
|
|
|
|
|
Text: "heredoc",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Line: 2,
|
|
|
|
|
Text: "\u00BF\u0057\u0001\u0000\u00FF\u00FF\u00FF\u00FF\u00FF\u00FF\u00FF\u003D\u001F",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Line: 5,
|
|
|
|
|
Text: "\u00BF\u0057\u0001\u0000\u00FF\u00FF\u00FF\u00FF\u00FF\u00FF\u00FF\u003D\u001F",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Line: 6,
|
|
|
|
|
Text: "\u00BF\u00BF\u0057\u0001\u0000\u00FF\u00FF\u00FF\u00FF\u00FF\u00FF\u00FF\u003D\u001F",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
2023-08-24 11:27:57 +08:00
|
|
|
|
{
|
|
|
|
|
input: []byte("not-a-heredoc <<\n"),
|
|
|
|
|
expectErr: true,
|
|
|
|
|
errorMessage: "missing opening heredoc marker on line #1; must contain only alpha-numeric characters, dashes and underscores; got empty string",
|
|
|
|
|
},
|
2023-02-26 08:34:27 +08:00
|
|
|
|
{
|
|
|
|
|
input: []byte(`heredoc <<<EOF
|
|
|
|
|
content
|
|
|
|
|
EOF same-line-arg
|
|
|
|
|
`),
|
|
|
|
|
expectErr: true,
|
|
|
|
|
errorMessage: "too many '<' for heredoc on line #1; only use two, for example <<END",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
input: []byte(`heredoc <<EOF
|
|
|
|
|
content
|
|
|
|
|
`),
|
|
|
|
|
expectErr: true,
|
|
|
|
|
errorMessage: "incomplete heredoc <<EOF on line #3, expected ending marker EOF",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
input: []byte(`heredoc <<EOF
|
|
|
|
|
content
|
|
|
|
|
EOF
|
|
|
|
|
`),
|
|
|
|
|
expectErr: true,
|
|
|
|
|
errorMessage: "mismatched leading whitespace in heredoc <<EOF on line #2 [\tcontent], expected whitespace [\t\t] to match the closing marker",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
input: []byte(`heredoc <<EOF
|
|
|
|
|
content
|
|
|
|
|
EOF
|
|
|
|
|
`),
|
|
|
|
|
expectErr: true,
|
|
|
|
|
errorMessage: "mismatched leading whitespace in heredoc <<EOF on line #2 [ content], expected whitespace [\t\t] to match the closing marker",
|
|
|
|
|
},
|
2019-08-10 02:05:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for i, testCase := range testCases {
|
2020-07-21 03:55:51 +08:00
|
|
|
|
actual, err := Tokenize(testCase.input, "")
|
2023-02-26 08:34:27 +08:00
|
|
|
|
if testCase.expectErr {
|
|
|
|
|
if err == nil {
|
2023-02-27 05:56:48 +08:00
|
|
|
|
t.Fatalf("expected error, got actual: %v", actual)
|
2023-02-26 08:34:27 +08:00
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if err.Error() != testCase.errorMessage {
|
2023-02-27 05:56:48 +08:00
|
|
|
|
t.Fatalf("expected error '%v', got: %v", testCase.errorMessage, err)
|
2023-02-26 08:34:27 +08:00
|
|
|
|
}
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-21 03:55:51 +08:00
|
|
|
|
if err != nil {
|
2023-02-27 05:56:48 +08:00
|
|
|
|
t.Fatalf("%v", err)
|
2020-07-21 03:55:51 +08:00
|
|
|
|
}
|
2019-08-10 02:05:47 +08:00
|
|
|
|
lexerCompare(t, i, testCase.expected, actual)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func lexerCompare(t *testing.T, n int, expected, actual []Token) {
|
|
|
|
|
if len(expected) != len(actual) {
|
2023-02-27 05:56:48 +08:00
|
|
|
|
t.Fatalf("Test case %d: expected %d token(s) but got %d", n, len(expected), len(actual))
|
2019-08-10 02:05:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for i := 0; i < len(actual) && i < len(expected); i++ {
|
|
|
|
|
if actual[i].Line != expected[i].Line {
|
2023-02-27 05:56:48 +08:00
|
|
|
|
t.Fatalf("Test case %d token %d ('%s'): expected line %d but was line %d",
|
2019-08-10 02:05:47 +08:00
|
|
|
|
n, i, expected[i].Text, expected[i].Line, actual[i].Line)
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
if actual[i].Text != expected[i].Text {
|
2023-02-27 05:56:48 +08:00
|
|
|
|
t.Fatalf("Test case %d token %d: expected text '%s' but was '%s'",
|
2019-08-10 02:05:47 +08:00
|
|
|
|
n, i, expected[i].Text, actual[i].Text)
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|