Merge pull request #5100 from prometheus/mdl-lexer-subtests

promql: use subtests in TestLexer
This commit is contained in:
Matt Layher 2019-01-16 19:12:11 -05:00 committed by GitHub
commit a68d1f2688
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

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