2015-03-30 09:12:51 -07:00
|
|
|
// Copyright 2015 The Prometheus Authors
|
|
|
|
// 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.
|
|
|
|
|
2020-02-03 08:23:44 -08:00
|
|
|
package parser
|
2015-03-30 09:12:51 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2019-10-09 17:06:53 -07:00
|
|
|
|
2020-10-29 02:43:23 -07:00
|
|
|
"github.com/stretchr/testify/require"
|
2023-09-14 09:57:31 -07:00
|
|
|
|
|
|
|
"github.com/prometheus/prometheus/promql/parser/posrange"
|
2015-03-30 09:12:51 -07:00
|
|
|
)
|
|
|
|
|
2019-01-16 13:35:45 -08:00
|
|
|
type testCase struct {
|
2015-05-11 05:04:53 -07:00
|
|
|
input string
|
2019-12-09 11:03:31 -08:00
|
|
|
expected []Item
|
2015-05-11 05:04:53 -07:00
|
|
|
fail bool
|
|
|
|
seriesDesc bool // Whether to lex a series description.
|
2019-01-16 13:35:45 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
var tests = []struct {
|
|
|
|
name string
|
|
|
|
tests []testCase
|
2015-03-30 09:12:51 -07:00
|
|
|
}{
|
|
|
|
{
|
2019-01-16 13:35:45 -08:00
|
|
|
name: "common",
|
|
|
|
tests: []testCase{
|
|
|
|
{
|
|
|
|
input: ",",
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{{COMMA, 0, ","}},
|
2019-01-16 13:35:45 -08:00
|
|
|
}, {
|
|
|
|
input: "()",
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{{LEFT_PAREN, 0, `(`}, {RIGHT_PAREN, 1, `)`}},
|
2019-01-16 13:35:45 -08:00
|
|
|
}, {
|
|
|
|
input: "{}",
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{{LEFT_BRACE, 0, `{`}, {RIGHT_BRACE, 1, `}`}},
|
2019-01-16 13:35:45 -08:00
|
|
|
}, {
|
|
|
|
input: "[5m]",
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{
|
2019-11-26 05:29:42 -08:00
|
|
|
{LEFT_BRACKET, 0, `[`},
|
|
|
|
{DURATION, 1, `5m`},
|
|
|
|
{RIGHT_BRACKET, 3, `]`},
|
2019-01-16 13:35:45 -08:00
|
|
|
},
|
2019-11-11 00:56:24 -08:00
|
|
|
}, {
|
|
|
|
input: "[ 5m]",
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{
|
2019-11-26 05:29:42 -08:00
|
|
|
{LEFT_BRACKET, 0, `[`},
|
|
|
|
{DURATION, 2, `5m`},
|
|
|
|
{RIGHT_BRACKET, 4, `]`},
|
2019-11-11 00:56:24 -08:00
|
|
|
},
|
|
|
|
}, {
|
|
|
|
input: "[ 5m]",
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{
|
2019-11-26 05:29:42 -08:00
|
|
|
{LEFT_BRACKET, 0, `[`},
|
|
|
|
{DURATION, 3, `5m`},
|
|
|
|
{RIGHT_BRACKET, 5, `]`},
|
2019-11-11 00:56:24 -08:00
|
|
|
},
|
|
|
|
}, {
|
|
|
|
input: "[ 5m ]",
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{
|
2019-11-26 05:29:42 -08:00
|
|
|
{LEFT_BRACKET, 0, `[`},
|
|
|
|
{DURATION, 3, `5m`},
|
|
|
|
{RIGHT_BRACKET, 6, `]`},
|
2019-11-11 00:56:24 -08:00
|
|
|
},
|
2019-01-16 13:35:45 -08:00
|
|
|
}, {
|
|
|
|
input: "\r\n\r",
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{},
|
2019-01-16 13:35:45 -08:00
|
|
|
},
|
2015-03-30 09:12:51 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-01-16 13:35:45 -08:00
|
|
|
name: "numbers",
|
|
|
|
tests: []testCase{
|
|
|
|
{
|
|
|
|
input: "1",
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{{NUMBER, 0, "1"}},
|
2019-01-16 13:35:45 -08:00
|
|
|
}, {
|
|
|
|
input: "4.23",
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{{NUMBER, 0, "4.23"}},
|
2019-01-16 13:35:45 -08:00
|
|
|
}, {
|
|
|
|
input: ".3",
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{{NUMBER, 0, ".3"}},
|
2019-01-16 13:35:45 -08:00
|
|
|
}, {
|
|
|
|
input: "5.",
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{{NUMBER, 0, "5."}},
|
2019-01-16 13:35:45 -08:00
|
|
|
}, {
|
|
|
|
input: "NaN",
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{{NUMBER, 0, "NaN"}},
|
2019-01-16 13:35:45 -08:00
|
|
|
}, {
|
|
|
|
input: "nAN",
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{{NUMBER, 0, "nAN"}},
|
2019-01-16 13:35:45 -08:00
|
|
|
}, {
|
|
|
|
input: "NaN 123",
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{{NUMBER, 0, "NaN"}, {NUMBER, 4, "123"}},
|
2019-01-16 13:35:45 -08:00
|
|
|
}, {
|
|
|
|
input: "NaN123",
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{{IDENTIFIER, 0, "NaN123"}},
|
2019-01-16 13:35:45 -08:00
|
|
|
}, {
|
|
|
|
input: "iNf",
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{{NUMBER, 0, "iNf"}},
|
2019-01-16 13:35:45 -08:00
|
|
|
}, {
|
|
|
|
input: "Inf",
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{{NUMBER, 0, "Inf"}},
|
2019-01-16 13:35:45 -08:00
|
|
|
}, {
|
|
|
|
input: "+Inf",
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{{ADD, 0, "+"}, {NUMBER, 1, "Inf"}},
|
2019-01-16 13:35:45 -08:00
|
|
|
}, {
|
|
|
|
input: "+Inf 123",
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{{ADD, 0, "+"}, {NUMBER, 1, "Inf"}, {NUMBER, 5, "123"}},
|
2019-01-16 13:35:45 -08:00
|
|
|
}, {
|
|
|
|
input: "-Inf",
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{{SUB, 0, "-"}, {NUMBER, 1, "Inf"}},
|
2019-01-16 13:35:45 -08:00
|
|
|
}, {
|
|
|
|
input: "Infoo",
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{{IDENTIFIER, 0, "Infoo"}},
|
2019-01-16 13:35:45 -08:00
|
|
|
}, {
|
|
|
|
input: "-Infoo",
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{{SUB, 0, "-"}, {IDENTIFIER, 1, "Infoo"}},
|
2019-01-16 13:35:45 -08:00
|
|
|
}, {
|
|
|
|
input: "-Inf 123",
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{{SUB, 0, "-"}, {NUMBER, 1, "Inf"}, {NUMBER, 5, "123"}},
|
2019-01-16 13:35:45 -08:00
|
|
|
}, {
|
|
|
|
input: "0x123",
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{{NUMBER, 0, "0x123"}},
|
2019-01-16 13:35:45 -08:00
|
|
|
},
|
2016-03-01 14:23:18 -08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-01-16 13:35:45 -08:00
|
|
|
name: "strings",
|
|
|
|
tests: []testCase{
|
|
|
|
{
|
|
|
|
input: "\"test\\tsequence\"",
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{{STRING, 0, `"test\tsequence"`}},
|
2019-01-16 13:35:45 -08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "\"test\\\\.expression\"",
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{{STRING, 0, `"test\\.expression"`}},
|
2019-01-16 13:35:45 -08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "\"test\\.expression\"",
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{
|
2019-11-26 05:29:42 -08:00
|
|
|
{ERROR, 0, "unknown escape sequence U+002E '.'"},
|
|
|
|
{STRING, 0, `"test\.expression"`},
|
2019-01-16 13:35:45 -08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "`test\\.expression`",
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{{STRING, 0, "`test\\.expression`"}},
|
2019-01-16 13:35:45 -08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
// See https://github.com/prometheus/prometheus/issues/939.
|
|
|
|
input: ".٩",
|
|
|
|
fail: true,
|
|
|
|
},
|
2015-03-30 09:12:51 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-01-16 13:35:45 -08:00
|
|
|
name: "durations",
|
|
|
|
tests: []testCase{
|
|
|
|
{
|
|
|
|
input: "5s",
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{{DURATION, 0, "5s"}},
|
2019-01-16 13:35:45 -08:00
|
|
|
}, {
|
|
|
|
input: "123m",
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{{DURATION, 0, "123m"}},
|
2019-01-16 13:35:45 -08:00
|
|
|
}, {
|
|
|
|
input: "1h",
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{{DURATION, 0, "1h"}},
|
2019-01-16 13:35:45 -08:00
|
|
|
}, {
|
|
|
|
input: "3w",
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{{DURATION, 0, "3w"}},
|
2019-01-16 13:35:45 -08:00
|
|
|
}, {
|
|
|
|
input: "1y",
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{{DURATION, 0, "1y"}},
|
2019-01-16 13:35:45 -08:00
|
|
|
},
|
|
|
|
},
|
2015-03-30 09:12:51 -07:00
|
|
|
},
|
|
|
|
{
|
2019-01-16 13:35:45 -08:00
|
|
|
name: "identifiers",
|
|
|
|
tests: []testCase{
|
|
|
|
{
|
|
|
|
input: "abc",
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{{IDENTIFIER, 0, "abc"}},
|
2019-01-16 13:35:45 -08:00
|
|
|
}, {
|
|
|
|
input: "a:bc",
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{{METRIC_IDENTIFIER, 0, "a:bc"}},
|
2019-01-16 13:35:45 -08:00
|
|
|
}, {
|
|
|
|
input: "abc d",
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{{IDENTIFIER, 0, "abc"}, {IDENTIFIER, 4, "d"}},
|
2019-01-16 13:35:45 -08:00
|
|
|
}, {
|
|
|
|
input: ":bc",
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{{METRIC_IDENTIFIER, 0, ":bc"}},
|
2019-01-16 13:35:45 -08:00
|
|
|
}, {
|
|
|
|
input: "0a:bc",
|
|
|
|
fail: true,
|
|
|
|
},
|
2015-05-08 07:43:02 -07:00
|
|
|
},
|
2015-03-30 09:12:51 -07:00
|
|
|
},
|
|
|
|
{
|
2019-01-16 13:35:45 -08:00
|
|
|
name: "comments",
|
|
|
|
tests: []testCase{
|
|
|
|
{
|
|
|
|
input: "# some comment",
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{{COMMENT, 0, "# some comment"}},
|
2019-01-16 13:35:45 -08:00
|
|
|
}, {
|
|
|
|
input: "5 # 1+1\n5",
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{
|
2019-11-26 05:29:42 -08:00
|
|
|
{NUMBER, 0, "5"},
|
|
|
|
{COMMENT, 2, "# 1+1"},
|
|
|
|
{NUMBER, 8, "5"},
|
2019-01-16 13:35:45 -08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-03-30 09:12:51 -07:00
|
|
|
},
|
|
|
|
{
|
2019-01-16 13:35:45 -08:00
|
|
|
name: "operators",
|
|
|
|
tests: []testCase{
|
|
|
|
{
|
|
|
|
input: `=`,
|
2020-09-09 03:10:02 -07:00
|
|
|
expected: []Item{{EQL, 0, `=`}},
|
2019-01-16 13:35:45 -08:00
|
|
|
}, {
|
2020-09-09 03:10:02 -07:00
|
|
|
// Inside braces equality is a single '=' character but in terms of a token
|
|
|
|
// it should be treated as ASSIGN.
|
2019-01-16 13:35:45 -08:00
|
|
|
input: `{=}`,
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{{LEFT_BRACE, 0, `{`}, {EQL, 1, `=`}, {RIGHT_BRACE, 2, `}`}},
|
2019-01-16 13:35:45 -08:00
|
|
|
}, {
|
|
|
|
input: `==`,
|
2020-09-09 03:10:02 -07:00
|
|
|
expected: []Item{{EQLC, 0, `==`}},
|
2019-01-16 13:35:45 -08:00
|
|
|
}, {
|
|
|
|
input: `!=`,
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{{NEQ, 0, `!=`}},
|
2019-01-16 13:35:45 -08:00
|
|
|
}, {
|
|
|
|
input: `<`,
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{{LSS, 0, `<`}},
|
2019-01-16 13:35:45 -08:00
|
|
|
}, {
|
|
|
|
input: `>`,
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{{GTR, 0, `>`}},
|
2019-01-16 13:35:45 -08:00
|
|
|
}, {
|
|
|
|
input: `>=`,
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{{GTE, 0, `>=`}},
|
2019-01-16 13:35:45 -08:00
|
|
|
}, {
|
|
|
|
input: `<=`,
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{{LTE, 0, `<=`}},
|
2019-01-16 13:35:45 -08:00
|
|
|
}, {
|
|
|
|
input: `+`,
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{{ADD, 0, `+`}},
|
2019-01-16 13:35:45 -08:00
|
|
|
}, {
|
|
|
|
input: `-`,
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{{SUB, 0, `-`}},
|
2019-01-16 13:35:45 -08:00
|
|
|
}, {
|
|
|
|
input: `*`,
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{{MUL, 0, `*`}},
|
2019-01-16 13:35:45 -08:00
|
|
|
}, {
|
|
|
|
input: `/`,
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{{DIV, 0, `/`}},
|
2019-01-16 13:35:45 -08:00
|
|
|
}, {
|
|
|
|
input: `^`,
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{{POW, 0, `^`}},
|
2019-01-16 13:35:45 -08:00
|
|
|
}, {
|
|
|
|
input: `%`,
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{{MOD, 0, `%`}},
|
2019-01-16 13:35:45 -08:00
|
|
|
}, {
|
|
|
|
input: `AND`,
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{{LAND, 0, `AND`}},
|
2019-01-16 13:35:45 -08:00
|
|
|
}, {
|
|
|
|
input: `or`,
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{{LOR, 0, `or`}},
|
2019-01-16 13:35:45 -08:00
|
|
|
}, {
|
|
|
|
input: `unless`,
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{{LUNLESS, 0, `unless`}},
|
2021-02-09 08:03:16 -08:00
|
|
|
}, {
|
|
|
|
input: `@`,
|
|
|
|
expected: []Item{{AT, 0, `@`}},
|
2019-01-16 13:35:45 -08:00
|
|
|
},
|
|
|
|
},
|
2015-03-30 09:12:51 -07:00
|
|
|
},
|
2017-06-16 07:19:24 -07:00
|
|
|
{
|
2019-01-16 13:35:45 -08:00
|
|
|
name: "aggregators",
|
|
|
|
tests: []testCase{
|
|
|
|
{
|
|
|
|
input: `sum`,
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{{SUM, 0, `sum`}},
|
2019-01-16 13:35:45 -08:00
|
|
|
}, {
|
|
|
|
input: `AVG`,
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{{AVG, 0, `AVG`}},
|
2020-06-30 07:51:18 -07:00
|
|
|
}, {
|
|
|
|
input: `GROUP`,
|
|
|
|
expected: []Item{{GROUP, 0, `GROUP`}},
|
2019-01-16 13:35:45 -08:00
|
|
|
}, {
|
|
|
|
input: `MAX`,
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{{MAX, 0, `MAX`}},
|
2019-01-16 13:35:45 -08:00
|
|
|
}, {
|
|
|
|
input: `min`,
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{{MIN, 0, `min`}},
|
2019-01-16 13:35:45 -08:00
|
|
|
}, {
|
|
|
|
input: `count`,
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{{COUNT, 0, `count`}},
|
2019-01-16 13:35:45 -08:00
|
|
|
}, {
|
|
|
|
input: `stdvar`,
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{{STDVAR, 0, `stdvar`}},
|
2019-01-16 13:35:45 -08:00
|
|
|
}, {
|
|
|
|
input: `stddev`,
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{{STDDEV, 0, `stddev`}},
|
2019-01-16 13:35:45 -08:00
|
|
|
},
|
|
|
|
},
|
2017-06-16 07:19:24 -07:00
|
|
|
},
|
|
|
|
{
|
2019-01-16 13:35:45 -08:00
|
|
|
name: "keywords",
|
|
|
|
tests: []testCase{
|
|
|
|
{
|
|
|
|
input: "offset",
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{{OFFSET, 0, "offset"}},
|
2021-10-22 01:06:44 -07:00
|
|
|
},
|
|
|
|
{
|
2019-01-16 13:35:45 -08:00
|
|
|
input: "by",
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{{BY, 0, "by"}},
|
2021-10-22 01:06:44 -07:00
|
|
|
},
|
|
|
|
{
|
2019-01-16 13:35:45 -08:00
|
|
|
input: "without",
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{{WITHOUT, 0, "without"}},
|
2021-10-22 01:06:44 -07:00
|
|
|
},
|
|
|
|
{
|
2019-01-16 13:35:45 -08:00
|
|
|
input: "on",
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{{ON, 0, "on"}},
|
2021-10-22 01:06:44 -07:00
|
|
|
},
|
|
|
|
{
|
2019-01-16 13:35:45 -08:00
|
|
|
input: "ignoring",
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{{IGNORING, 0, "ignoring"}},
|
2021-10-22 01:06:44 -07:00
|
|
|
},
|
|
|
|
{
|
2019-01-16 13:35:45 -08:00
|
|
|
input: "group_left",
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{{GROUP_LEFT, 0, "group_left"}},
|
2021-10-22 01:06:44 -07:00
|
|
|
},
|
|
|
|
{
|
2019-01-16 13:35:45 -08:00
|
|
|
input: "group_right",
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{{GROUP_RIGHT, 0, "group_right"}},
|
2021-10-22 01:06:44 -07:00
|
|
|
},
|
|
|
|
{
|
2019-01-16 13:35:45 -08:00
|
|
|
input: "bool",
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{{BOOL, 0, "bool"}},
|
2019-01-16 13:35:45 -08:00
|
|
|
},
|
2021-08-25 04:57:49 -07:00
|
|
|
{
|
|
|
|
input: "atan2",
|
|
|
|
expected: []Item{{ATAN2, 0, "atan2"}},
|
|
|
|
},
|
2019-01-16 13:35:45 -08:00
|
|
|
},
|
2017-06-16 07:19:24 -07:00
|
|
|
},
|
2021-02-09 08:03:16 -08:00
|
|
|
{
|
|
|
|
name: "preprocessors",
|
|
|
|
tests: []testCase{
|
|
|
|
{
|
|
|
|
input: `start`,
|
|
|
|
expected: []Item{{START, 0, `start`}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `end`,
|
|
|
|
expected: []Item{{END, 0, `end`}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-05-12 01:39:10 -07:00
|
|
|
{
|
2019-01-16 13:35:45 -08:00
|
|
|
name: "selectors",
|
|
|
|
tests: []testCase{
|
|
|
|
{
|
|
|
|
input: `台北`,
|
|
|
|
fail: true,
|
|
|
|
}, {
|
|
|
|
input: `{台北='a'}`,
|
|
|
|
fail: true,
|
|
|
|
}, {
|
|
|
|
input: `{0a='a'}`,
|
|
|
|
fail: true,
|
|
|
|
}, {
|
|
|
|
input: `{foo='bar'}`,
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{
|
2019-11-26 05:29:42 -08:00
|
|
|
{LEFT_BRACE, 0, `{`},
|
|
|
|
{IDENTIFIER, 1, `foo`},
|
|
|
|
{EQL, 4, `=`},
|
|
|
|
{STRING, 5, `'bar'`},
|
|
|
|
{RIGHT_BRACE, 10, `}`},
|
2019-01-16 13:35:45 -08:00
|
|
|
},
|
|
|
|
}, {
|
|
|
|
input: `{foo="bar"}`,
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{
|
2019-11-26 05:29:42 -08:00
|
|
|
{LEFT_BRACE, 0, `{`},
|
|
|
|
{IDENTIFIER, 1, `foo`},
|
|
|
|
{EQL, 4, `=`},
|
|
|
|
{STRING, 5, `"bar"`},
|
|
|
|
{RIGHT_BRACE, 10, `}`},
|
2019-01-16 13:35:45 -08:00
|
|
|
},
|
|
|
|
}, {
|
|
|
|
input: `{foo="bar\"bar"}`,
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{
|
2019-11-26 05:29:42 -08:00
|
|
|
{LEFT_BRACE, 0, `{`},
|
|
|
|
{IDENTIFIER, 1, `foo`},
|
|
|
|
{EQL, 4, `=`},
|
|
|
|
{STRING, 5, `"bar\"bar"`},
|
|
|
|
{RIGHT_BRACE, 15, `}`},
|
2019-01-16 13:35:45 -08:00
|
|
|
},
|
|
|
|
}, {
|
|
|
|
input: `{NaN != "bar" }`,
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{
|
2019-11-26 05:29:42 -08:00
|
|
|
{LEFT_BRACE, 0, `{`},
|
|
|
|
{IDENTIFIER, 1, `NaN`},
|
|
|
|
{NEQ, 5, `!=`},
|
|
|
|
{STRING, 8, `"bar"`},
|
|
|
|
{RIGHT_BRACE, 14, `}`},
|
2019-01-16 13:35:45 -08:00
|
|
|
},
|
|
|
|
}, {
|
|
|
|
input: `{alert=~"bar" }`,
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{
|
2019-11-26 05:29:42 -08:00
|
|
|
{LEFT_BRACE, 0, `{`},
|
|
|
|
{IDENTIFIER, 1, `alert`},
|
|
|
|
{EQL_REGEX, 6, `=~`},
|
|
|
|
{STRING, 8, `"bar"`},
|
|
|
|
{RIGHT_BRACE, 14, `}`},
|
2019-01-16 13:35:45 -08:00
|
|
|
},
|
|
|
|
}, {
|
|
|
|
input: `{on!~"bar"}`,
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{
|
2019-11-26 05:29:42 -08:00
|
|
|
{LEFT_BRACE, 0, `{`},
|
|
|
|
{IDENTIFIER, 1, `on`},
|
|
|
|
{NEQ_REGEX, 3, `!~`},
|
|
|
|
{STRING, 5, `"bar"`},
|
|
|
|
{RIGHT_BRACE, 10, `}`},
|
2019-01-16 13:35:45 -08:00
|
|
|
},
|
|
|
|
}, {
|
|
|
|
input: `{alert!#"bar"}`, fail: true,
|
|
|
|
}, {
|
|
|
|
input: `{foo:a="bar"}`, fail: true,
|
|
|
|
},
|
2015-05-12 01:39:10 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-01-16 13:35:45 -08:00
|
|
|
name: "common errors",
|
|
|
|
tests: []testCase{
|
|
|
|
{
|
|
|
|
input: `=~`, fail: true,
|
|
|
|
}, {
|
|
|
|
input: `!~`, fail: true,
|
|
|
|
}, {
|
|
|
|
input: `!(`, fail: true,
|
|
|
|
}, {
|
|
|
|
input: "1a", fail: true,
|
|
|
|
},
|
2015-05-12 01:39:10 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-01-16 13:35:45 -08:00
|
|
|
name: "mismatched parentheses",
|
|
|
|
tests: []testCase{
|
|
|
|
{
|
|
|
|
input: `(`, fail: true,
|
|
|
|
}, {
|
|
|
|
input: `())`, fail: true,
|
|
|
|
}, {
|
|
|
|
input: `(()`, fail: true,
|
|
|
|
}, {
|
|
|
|
input: `{`, fail: true,
|
|
|
|
}, {
|
|
|
|
input: `}`, fail: true,
|
|
|
|
}, {
|
|
|
|
input: "{{", fail: true,
|
|
|
|
}, {
|
|
|
|
input: "{{}}", fail: true,
|
|
|
|
}, {
|
|
|
|
input: `[`, fail: true,
|
|
|
|
}, {
|
|
|
|
input: `[[`, fail: true,
|
|
|
|
}, {
|
|
|
|
input: `[]]`, fail: true,
|
|
|
|
}, {
|
|
|
|
input: `[[]]`, fail: true,
|
|
|
|
}, {
|
|
|
|
input: `]`, fail: true,
|
|
|
|
},
|
2015-05-12 01:39:10 -07:00
|
|
|
},
|
|
|
|
},
|
2018-12-22 05:47:13 -08:00
|
|
|
{
|
2019-01-16 13:35:45 -08:00
|
|
|
name: "encoding issues",
|
|
|
|
tests: []testCase{
|
|
|
|
{
|
|
|
|
input: "\"\xff\"", fail: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "`\xff`", fail: true,
|
|
|
|
},
|
2018-12-22 05:47:13 -08:00
|
|
|
},
|
|
|
|
},
|
2023-08-25 14:35:42 -07:00
|
|
|
{
|
|
|
|
name: "histogram series descriptions",
|
|
|
|
tests: []testCase{
|
|
|
|
{
|
|
|
|
input: `{} {{buckets:[5]}}`,
|
|
|
|
expected: []Item{
|
|
|
|
{LEFT_BRACE, 0, `{`},
|
|
|
|
{RIGHT_BRACE, 1, `}`},
|
|
|
|
{SPACE, 2, ` `},
|
|
|
|
{OPEN_HIST, 3, `{{`},
|
|
|
|
{BUCKETS_DESC, 5, `buckets`},
|
|
|
|
{COLON, 12, `:`},
|
|
|
|
{LEFT_BRACKET, 13, `[`},
|
|
|
|
{NUMBER, 14, `5`},
|
|
|
|
{RIGHT_BRACKET, 15, `]`},
|
|
|
|
{CLOSE_HIST, 16, `}}`},
|
|
|
|
},
|
|
|
|
seriesDesc: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `{} {{buckets: [5 10 7]}}`,
|
|
|
|
expected: []Item{
|
|
|
|
{LEFT_BRACE, 0, `{`},
|
|
|
|
{RIGHT_BRACE, 1, `}`},
|
|
|
|
{SPACE, 2, ` `},
|
|
|
|
{OPEN_HIST, 3, `{{`},
|
|
|
|
{BUCKETS_DESC, 5, `buckets`},
|
|
|
|
{COLON, 12, `:`},
|
|
|
|
{SPACE, 13, ` `},
|
|
|
|
{LEFT_BRACKET, 14, `[`},
|
|
|
|
{NUMBER, 15, `5`},
|
|
|
|
{SPACE, 16, ` `},
|
|
|
|
{NUMBER, 17, `10`},
|
|
|
|
{SPACE, 19, ` `},
|
|
|
|
{NUMBER, 20, `7`},
|
|
|
|
{RIGHT_BRACKET, 21, `]`},
|
|
|
|
{CLOSE_HIST, 22, `}}`},
|
|
|
|
},
|
|
|
|
seriesDesc: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `{} {{buckets: [5 10 7] schema:1}}`,
|
|
|
|
expected: []Item{
|
|
|
|
{LEFT_BRACE, 0, `{`},
|
|
|
|
{RIGHT_BRACE, 1, `}`},
|
|
|
|
{SPACE, 2, ` `},
|
|
|
|
{OPEN_HIST, 3, `{{`},
|
|
|
|
{BUCKETS_DESC, 5, `buckets`},
|
|
|
|
{COLON, 12, `:`},
|
|
|
|
{SPACE, 13, ` `},
|
|
|
|
{LEFT_BRACKET, 14, `[`},
|
|
|
|
{NUMBER, 15, `5`},
|
|
|
|
{SPACE, 16, ` `},
|
|
|
|
{NUMBER, 17, `10`},
|
|
|
|
{SPACE, 19, ` `},
|
|
|
|
{NUMBER, 20, `7`},
|
|
|
|
{RIGHT_BRACKET, 21, `]`},
|
|
|
|
{SPACE, 22, ` `},
|
|
|
|
{SCHEMA_DESC, 23, `schema`},
|
|
|
|
{COLON, 29, `:`},
|
|
|
|
{NUMBER, 30, `1`},
|
|
|
|
{CLOSE_HIST, 31, `}}`},
|
|
|
|
},
|
|
|
|
seriesDesc: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-12-22 05:47:13 -08:00
|
|
|
{
|
2019-01-16 13:35:45 -08:00
|
|
|
name: "series descriptions",
|
|
|
|
tests: []testCase{
|
|
|
|
{
|
|
|
|
input: `{} _ 1 x .3`,
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{
|
2019-11-26 05:29:42 -08:00
|
|
|
{LEFT_BRACE, 0, `{`},
|
|
|
|
{RIGHT_BRACE, 1, `}`},
|
|
|
|
{SPACE, 2, ` `},
|
|
|
|
{BLANK, 3, `_`},
|
|
|
|
{SPACE, 4, ` `},
|
|
|
|
{NUMBER, 5, `1`},
|
|
|
|
{SPACE, 6, ` `},
|
|
|
|
{TIMES, 7, `x`},
|
|
|
|
{SPACE, 8, ` `},
|
|
|
|
{NUMBER, 9, `.3`},
|
2019-01-16 13:35:45 -08:00
|
|
|
},
|
|
|
|
seriesDesc: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `metric +Inf Inf NaN`,
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{
|
2019-11-26 05:29:42 -08:00
|
|
|
{IDENTIFIER, 0, `metric`},
|
|
|
|
{SPACE, 6, ` `},
|
|
|
|
{ADD, 7, `+`},
|
|
|
|
{NUMBER, 8, `Inf`},
|
|
|
|
{SPACE, 11, ` `},
|
|
|
|
{NUMBER, 12, `Inf`},
|
|
|
|
{SPACE, 15, ` `},
|
|
|
|
{NUMBER, 16, `NaN`},
|
2019-01-16 13:35:45 -08:00
|
|
|
},
|
|
|
|
seriesDesc: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `metric 1+1x4`,
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{
|
2019-11-26 05:29:42 -08:00
|
|
|
{IDENTIFIER, 0, `metric`},
|
|
|
|
{SPACE, 6, ` `},
|
|
|
|
{NUMBER, 7, `1`},
|
|
|
|
{ADD, 8, `+`},
|
|
|
|
{NUMBER, 9, `1`},
|
|
|
|
{TIMES, 10, `x`},
|
|
|
|
{NUMBER, 11, `4`},
|
2019-01-16 13:35:45 -08:00
|
|
|
},
|
|
|
|
seriesDesc: true,
|
|
|
|
},
|
2018-12-22 05:47:13 -08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-01-16 13:35:45 -08:00
|
|
|
name: "subqueries",
|
|
|
|
tests: []testCase{
|
|
|
|
{
|
|
|
|
input: `test_name{on!~"bar"}[4m:4s]`,
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{
|
2019-11-26 05:29:42 -08:00
|
|
|
{IDENTIFIER, 0, `test_name`},
|
|
|
|
{LEFT_BRACE, 9, `{`},
|
|
|
|
{IDENTIFIER, 10, `on`},
|
|
|
|
{NEQ_REGEX, 12, `!~`},
|
|
|
|
{STRING, 14, `"bar"`},
|
|
|
|
{RIGHT_BRACE, 19, `}`},
|
|
|
|
{LEFT_BRACKET, 20, `[`},
|
|
|
|
{DURATION, 21, `4m`},
|
|
|
|
{COLON, 23, `:`},
|
|
|
|
{DURATION, 24, `4s`},
|
|
|
|
{RIGHT_BRACKET, 26, `]`},
|
2019-01-16 13:35:45 -08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `test:name{on!~"bar"}[4m:4s]`,
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{
|
2019-11-26 05:29:42 -08:00
|
|
|
{METRIC_IDENTIFIER, 0, `test:name`},
|
|
|
|
{LEFT_BRACE, 9, `{`},
|
|
|
|
{IDENTIFIER, 10, `on`},
|
|
|
|
{NEQ_REGEX, 12, `!~`},
|
|
|
|
{STRING, 14, `"bar"`},
|
|
|
|
{RIGHT_BRACE, 19, `}`},
|
|
|
|
{LEFT_BRACKET, 20, `[`},
|
|
|
|
{DURATION, 21, `4m`},
|
|
|
|
{COLON, 23, `:`},
|
|
|
|
{DURATION, 24, `4s`},
|
|
|
|
{RIGHT_BRACKET, 26, `]`},
|
2019-01-16 13:35:45 -08:00
|
|
|
},
|
2021-10-22 01:06:44 -07:00
|
|
|
},
|
|
|
|
{
|
2019-01-16 13:35:45 -08:00
|
|
|
input: `test:name{on!~"b:ar"}[4m:4s]`,
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{
|
2019-11-26 05:29:42 -08:00
|
|
|
{METRIC_IDENTIFIER, 0, `test:name`},
|
|
|
|
{LEFT_BRACE, 9, `{`},
|
|
|
|
{IDENTIFIER, 10, `on`},
|
|
|
|
{NEQ_REGEX, 12, `!~`},
|
|
|
|
{STRING, 14, `"b:ar"`},
|
|
|
|
{RIGHT_BRACE, 20, `}`},
|
|
|
|
{LEFT_BRACKET, 21, `[`},
|
|
|
|
{DURATION, 22, `4m`},
|
|
|
|
{COLON, 24, `:`},
|
|
|
|
{DURATION, 25, `4s`},
|
|
|
|
{RIGHT_BRACKET, 27, `]`},
|
2019-01-16 13:35:45 -08:00
|
|
|
},
|
2021-10-22 01:06:44 -07:00
|
|
|
},
|
|
|
|
{
|
2019-01-16 13:35:45 -08:00
|
|
|
input: `test:name{on!~"b:ar"}[4m:]`,
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{
|
2019-11-26 05:29:42 -08:00
|
|
|
{METRIC_IDENTIFIER, 0, `test:name`},
|
|
|
|
{LEFT_BRACE, 9, `{`},
|
|
|
|
{IDENTIFIER, 10, `on`},
|
|
|
|
{NEQ_REGEX, 12, `!~`},
|
|
|
|
{STRING, 14, `"b:ar"`},
|
|
|
|
{RIGHT_BRACE, 20, `}`},
|
|
|
|
{LEFT_BRACKET, 21, `[`},
|
|
|
|
{DURATION, 22, `4m`},
|
|
|
|
{COLON, 24, `:`},
|
|
|
|
{RIGHT_BRACKET, 25, `]`},
|
2019-01-16 13:35:45 -08:00
|
|
|
},
|
2021-10-22 01:06:44 -07:00
|
|
|
},
|
|
|
|
{ // Nested Subquery.
|
2019-01-16 13:35:45 -08:00
|
|
|
input: `min_over_time(rate(foo{bar="baz"}[2s])[5m:])[4m:3s]`,
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{
|
2019-11-26 05:29:42 -08:00
|
|
|
{IDENTIFIER, 0, `min_over_time`},
|
|
|
|
{LEFT_PAREN, 13, `(`},
|
|
|
|
{IDENTIFIER, 14, `rate`},
|
|
|
|
{LEFT_PAREN, 18, `(`},
|
|
|
|
{IDENTIFIER, 19, `foo`},
|
|
|
|
{LEFT_BRACE, 22, `{`},
|
|
|
|
{IDENTIFIER, 23, `bar`},
|
|
|
|
{EQL, 26, `=`},
|
|
|
|
{STRING, 27, `"baz"`},
|
|
|
|
{RIGHT_BRACE, 32, `}`},
|
|
|
|
{LEFT_BRACKET, 33, `[`},
|
|
|
|
{DURATION, 34, `2s`},
|
|
|
|
{RIGHT_BRACKET, 36, `]`},
|
|
|
|
{RIGHT_PAREN, 37, `)`},
|
|
|
|
{LEFT_BRACKET, 38, `[`},
|
|
|
|
{DURATION, 39, `5m`},
|
|
|
|
{COLON, 41, `:`},
|
|
|
|
{RIGHT_BRACKET, 42, `]`},
|
|
|
|
{RIGHT_PAREN, 43, `)`},
|
|
|
|
{LEFT_BRACKET, 44, `[`},
|
|
|
|
{DURATION, 45, `4m`},
|
|
|
|
{COLON, 47, `:`},
|
|
|
|
{DURATION, 48, `3s`},
|
|
|
|
{RIGHT_BRACKET, 50, `]`},
|
2019-01-16 13:35:45 -08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
// Subquery with offset.
|
|
|
|
{
|
|
|
|
input: `test:name{on!~"b:ar"}[4m:4s] offset 10m`,
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{
|
2019-11-26 05:29:42 -08:00
|
|
|
{METRIC_IDENTIFIER, 0, `test:name`},
|
|
|
|
{LEFT_BRACE, 9, `{`},
|
|
|
|
{IDENTIFIER, 10, `on`},
|
|
|
|
{NEQ_REGEX, 12, `!~`},
|
|
|
|
{STRING, 14, `"b:ar"`},
|
|
|
|
{RIGHT_BRACE, 20, `}`},
|
|
|
|
{LEFT_BRACKET, 21, `[`},
|
|
|
|
{DURATION, 22, `4m`},
|
|
|
|
{COLON, 24, `:`},
|
|
|
|
{DURATION, 25, `4s`},
|
|
|
|
{RIGHT_BRACKET, 27, `]`},
|
|
|
|
{OFFSET, 29, "offset"},
|
|
|
|
{DURATION, 36, "10m"},
|
2019-01-16 13:35:45 -08:00
|
|
|
},
|
2021-10-22 01:06:44 -07:00
|
|
|
},
|
|
|
|
{
|
2019-01-16 13:35:45 -08:00
|
|
|
input: `min_over_time(rate(foo{bar="baz"}[2s])[5m:] offset 6m)[4m:3s]`,
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{
|
2019-11-26 05:29:42 -08:00
|
|
|
{IDENTIFIER, 0, `min_over_time`},
|
|
|
|
{LEFT_PAREN, 13, `(`},
|
|
|
|
{IDENTIFIER, 14, `rate`},
|
|
|
|
{LEFT_PAREN, 18, `(`},
|
|
|
|
{IDENTIFIER, 19, `foo`},
|
|
|
|
{LEFT_BRACE, 22, `{`},
|
|
|
|
{IDENTIFIER, 23, `bar`},
|
|
|
|
{EQL, 26, `=`},
|
|
|
|
{STRING, 27, `"baz"`},
|
|
|
|
{RIGHT_BRACE, 32, `}`},
|
|
|
|
{LEFT_BRACKET, 33, `[`},
|
|
|
|
{DURATION, 34, `2s`},
|
|
|
|
{RIGHT_BRACKET, 36, `]`},
|
|
|
|
{RIGHT_PAREN, 37, `)`},
|
|
|
|
{LEFT_BRACKET, 38, `[`},
|
|
|
|
{DURATION, 39, `5m`},
|
|
|
|
{COLON, 41, `:`},
|
|
|
|
{RIGHT_BRACKET, 42, `]`},
|
|
|
|
{OFFSET, 44, `offset`},
|
|
|
|
{DURATION, 51, `6m`},
|
|
|
|
{RIGHT_PAREN, 53, `)`},
|
|
|
|
{LEFT_BRACKET, 54, `[`},
|
|
|
|
{DURATION, 55, `4m`},
|
|
|
|
{COLON, 57, `:`},
|
|
|
|
{DURATION, 58, `3s`},
|
|
|
|
{RIGHT_BRACKET, 60, `]`},
|
2019-01-16 13:35:45 -08:00
|
|
|
},
|
|
|
|
},
|
2019-11-11 00:56:24 -08:00
|
|
|
{
|
|
|
|
input: `test:name[ 5m]`,
|
2019-12-09 11:03:31 -08:00
|
|
|
expected: []Item{
|
2019-11-26 05:29:42 -08:00
|
|
|
{METRIC_IDENTIFIER, 0, `test:name`},
|
|
|
|
{LEFT_BRACKET, 9, `[`},
|
|
|
|
{DURATION, 11, `5m`},
|
|
|
|
{RIGHT_BRACKET, 13, `]`},
|
2019-11-11 00:56:24 -08:00
|
|
|
},
|
|
|
|
},
|
2019-01-16 13:35:45 -08:00
|
|
|
{
|
|
|
|
input: `test:name{o:n!~"bar"}[4m:4s]`,
|
|
|
|
fail: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `test:name{on!~"bar"}[4m:4s:4h]`,
|
|
|
|
fail: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `test:name{on!~"bar"}[4m:4s:]`,
|
|
|
|
fail: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `test:name{on!~"bar"}[4m::]`,
|
|
|
|
fail: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `test:name{on!~"bar"}[:4s]`,
|
|
|
|
fail: true,
|
|
|
|
},
|
2018-12-22 05:47:13 -08:00
|
|
|
},
|
|
|
|
},
|
2015-03-30 09:12:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestLexer tests basic functionality of the lexer. More elaborate tests are implemented
|
|
|
|
// for the parser to avoid duplicated effort.
|
|
|
|
func TestLexer(t *testing.T) {
|
2019-01-16 13:35:45 -08:00
|
|
|
for _, typ := range tests {
|
|
|
|
t.Run(typ.name, func(t *testing.T) {
|
|
|
|
for i, test := range typ.tests {
|
2019-12-09 11:03:31 -08:00
|
|
|
l := &Lexer{
|
2019-01-16 13:35:45 -08:00
|
|
|
input: test.input,
|
|
|
|
seriesDesc: test.seriesDesc,
|
|
|
|
}
|
2016-08-29 00:20:43 -07:00
|
|
|
|
2020-01-09 03:26:58 -08:00
|
|
|
var out []Item
|
|
|
|
|
|
|
|
for l.state = lexStatements; l.state != nil; {
|
|
|
|
out = append(out, Item{})
|
|
|
|
l.NextItem(&out[len(out)-1])
|
|
|
|
}
|
2015-03-30 09:12:51 -07:00
|
|
|
|
2019-01-16 13:35:45 -08:00
|
|
|
lastItem := out[len(out)-1]
|
|
|
|
if test.fail {
|
2020-01-09 03:26:58 -08:00
|
|
|
hasError := false
|
|
|
|
for _, item := range out {
|
|
|
|
if item.Typ == ERROR {
|
|
|
|
hasError = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !hasError {
|
2019-01-16 13:35:45 -08:00
|
|
|
t.Logf("%d: input %q", i, test.input)
|
2021-09-13 12:19:20 -07:00
|
|
|
require.Fail(t, "expected lexing error but did not fail")
|
2019-01-16 13:35:45 -08:00
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2019-12-09 11:03:31 -08:00
|
|
|
if lastItem.Typ == ERROR {
|
2019-01-16 13:35:45 -08:00
|
|
|
t.Logf("%d: input %q", i, test.input)
|
2021-09-13 12:19:20 -07:00
|
|
|
require.Fail(t, "unexpected lexing error at position %d: %s", lastItem.Pos, lastItem)
|
2019-01-16 13:35:45 -08:00
|
|
|
}
|
2015-03-30 09:12:51 -07:00
|
|
|
|
2023-09-14 09:57:31 -07:00
|
|
|
eofItem := Item{EOF, posrange.Pos(len(test.input)), ""}
|
2020-10-29 02:43:23 -07:00
|
|
|
require.Equal(t, lastItem, eofItem, "%d: input %q", i, test.input)
|
2019-10-09 17:06:53 -07:00
|
|
|
|
2019-01-16 13:35:45 -08:00
|
|
|
out = out[:len(out)-1]
|
2020-10-29 02:43:23 -07:00
|
|
|
require.Equal(t, test.expected, out, "%d: input %q", i, test.input)
|
2019-01-16 13:35:45 -08:00
|
|
|
}
|
|
|
|
})
|
2015-03-30 09:12:51 -07:00
|
|
|
}
|
|
|
|
}
|