promql: use subtests in TestLexer

Signed-off-by: Matt Layher <mdlayher@gmail.com>
pull/5100/head
Matt Layher 6 years ago
parent 45c8b084c6
commit f62fd2bfc9

@ -19,13 +19,20 @@ import (
"testing"
)
var tests = []struct {
type testCase struct {
input string
expected []item
fail bool
seriesDesc bool // Whether to lex a series description.
}
var tests = []struct {
name string
tests []testCase
}{
// Test common stuff.
{
name: "common",
tests: []testCase{
{
input: ",",
expected: []item{{itemComma, 0, ","}},
@ -46,7 +53,11 @@ var tests = []struct {
input: "\r\n\r",
expected: []item{},
},
// Test numbers.
},
},
{
name: "numbers",
tests: []testCase{
{
input: "1",
expected: []item{{itemNumber, 0, "1"}},
@ -99,7 +110,11 @@ var tests = []struct {
input: "0x123",
expected: []item{{itemNumber, 0, "0x123"}},
},
// Test strings.
},
},
{
name: "strings",
tests: []testCase{
{
input: "\"test\\tsequence\"",
expected: []item{{itemString, 0, `"test\tsequence"`}},
@ -124,7 +139,11 @@ var tests = []struct {
input: ".٩",
fail: true,
},
// Test duration.
},
},
{
name: "durations",
tests: []testCase{
{
input: "5s",
expected: []item{{itemDuration, 0, "5s"}},
@ -141,7 +160,11 @@ var tests = []struct {
input: "1y",
expected: []item{{itemDuration, 0, "1y"}},
},
// Test identifiers.
},
},
{
name: "identifiers",
tests: []testCase{
{
input: "abc",
expected: []item{{itemIdentifier, 0, "abc"}},
@ -158,7 +181,11 @@ var tests = []struct {
input: "0a:bc",
fail: true,
},
// Test comments.
},
},
{
name: "comments",
tests: []testCase{
{
input: "# some comment",
expected: []item{{itemComment, 0, "# some comment"}},
@ -170,7 +197,11 @@ var tests = []struct {
{itemNumber, 8, "5"},
},
},
// Test operators.
},
},
{
name: "operators",
tests: []testCase{
{
input: `=`,
expected: []item{{itemAssign, 0, `=`}},
@ -224,7 +255,11 @@ var tests = []struct {
input: `unless`,
expected: []item{{itemLUnless, 0, `unless`}},
},
// Test aggregators.
},
},
{
name: "aggregators",
tests: []testCase{
{
input: `sum`,
expected: []item{{itemSum, 0, `sum`}},
@ -247,7 +282,11 @@ var tests = []struct {
input: `stddev`,
expected: []item{{itemStddev, 0, `stddev`}},
},
// Test keywords.
},
},
{
name: "keywords",
tests: []testCase{
{
input: "offset",
expected: []item{{itemOffset, 0, "offset"}},
@ -273,7 +312,11 @@ var tests = []struct {
input: "bool",
expected: []item{{itemBool, 0, "bool"}},
},
// Test Selector.
},
},
{
name: "selectors",
tests: []testCase{
{
input: `台北`,
fail: true,
@ -342,7 +385,11 @@ var tests = []struct {
}, {
input: `{foo:a="bar"}`, fail: true,
},
// Test common errors.
},
},
{
name: "common errors",
tests: []testCase{
{
input: `=~`, fail: true,
}, {
@ -352,7 +399,11 @@ var tests = []struct {
}, {
input: "1a", fail: true,
},
// Test mismatched parens.
},
},
{
name: "mismatched parentheses",
tests: []testCase{
{
input: `(`, fail: true,
}, {
@ -378,14 +429,22 @@ var tests = []struct {
}, {
input: `]`, fail: true,
},
// Test encoding issues.
},
},
{
name: "encoding issues",
tests: []testCase{
{
input: "\"\xff\"", fail: true,
},
{
input: "`\xff`", fail: true,
},
// Test series description.
},
},
{
name: "series descriptions",
tests: []testCase{
{
input: `{} _ 1 x .3`,
expected: []item{
@ -429,7 +488,11 @@ var tests = []struct {
},
seriesDesc: true,
},
// Test subquery.
},
},
{
name: "subqueries",
tests: []testCase{
{
input: `test_name{on!~"bar"}[4m:4s]`,
expected: []item{
@ -590,12 +653,16 @@ var tests = []struct {
input: `test:name{on!~"bar"}[:4s]`,
fail: true,
},
},
},
}
// TestLexer tests basic functionality of the lexer. More elaborate tests are implemented
// for the parser to avoid duplicated effort.
func TestLexer(t *testing.T) {
for i, test := range tests {
for _, typ := range tests {
t.Run(typ.name, func(t *testing.T) {
for i, test := range typ.tests {
l := &lexer{
input: test.input,
items: make(chan item),
@ -631,6 +698,8 @@ func TestLexer(t *testing.T) {
t.Fatalf("lexing mismatch:\nexpected:\n%s\ngot:\n%s", expectedList(test.expected), expectedList(out))
}
}
})
}
}
func expectedList(exp []item) string {

Loading…
Cancel
Save