mirror of https://github.com/prometheus/prometheus
Parser test cleanup (#3977)
* parser test cleanup - Test against the exported package functions instead of the private functions. * Improves readability of TestParseSeries - Moves package function closer to parser functionpull/3967/merge
parent
5b962c5748
commit
d49a3df55b
|
@ -104,14 +104,6 @@ func ParseMetricSelector(input string) (m []*labels.Matcher, err error) {
|
||||||
return vs.LabelMatchers, nil
|
return vs.LabelMatchers, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// parseSeriesDesc parses the description of a time series.
|
|
||||||
func parseSeriesDesc(input string) (labels.Labels, []sequenceValue, error) {
|
|
||||||
p := newParser(input)
|
|
||||||
p.lex.seriesDesc = true
|
|
||||||
|
|
||||||
return p.parseSeriesDesc()
|
|
||||||
}
|
|
||||||
|
|
||||||
// newParser returns a new parser.
|
// newParser returns a new parser.
|
||||||
func newParser(input string) *parser {
|
func newParser(input string) *parser {
|
||||||
p := &parser{
|
p := &parser{
|
||||||
|
@ -167,6 +159,14 @@ func (v sequenceValue) String() string {
|
||||||
return fmt.Sprintf("%f", v.value)
|
return fmt.Sprintf("%f", v.value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// parseSeriesDesc parses the description of a time series.
|
||||||
|
func parseSeriesDesc(input string) (labels.Labels, []sequenceValue, error) {
|
||||||
|
p := newParser(input)
|
||||||
|
p.lex.seriesDesc = true
|
||||||
|
|
||||||
|
return p.parseSeriesDesc()
|
||||||
|
}
|
||||||
|
|
||||||
// parseSeriesDesc parses a description of a time series into its metric and value sequence.
|
// parseSeriesDesc parses a description of a time series into its metric and value sequence.
|
||||||
func (p *parser) parseSeriesDesc() (m labels.Labels, vals []sequenceValue, err error) {
|
func (p *parser) parseSeriesDesc() (m labels.Labels, vals []sequenceValue, err error) {
|
||||||
defer p.recover(&err)
|
defer p.recover(&err)
|
||||||
|
|
|
@ -139,7 +139,8 @@ var testExpr = []struct {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}, {
|
}, {
|
||||||
input: "-some_metric", expected: &UnaryExpr{
|
input: "-some_metric",
|
||||||
|
expected: &UnaryExpr{
|
||||||
Op: itemSUB,
|
Op: itemSUB,
|
||||||
Expr: &VectorSelector{
|
Expr: &VectorSelector{
|
||||||
Name: "some_metric",
|
Name: "some_metric",
|
||||||
|
@ -149,7 +150,8 @@ var testExpr = []struct {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}, {
|
}, {
|
||||||
input: "+some_metric", expected: &UnaryExpr{
|
input: "+some_metric",
|
||||||
|
expected: &UnaryExpr{
|
||||||
Op: itemADD,
|
Op: itemADD,
|
||||||
Expr: &VectorSelector{
|
Expr: &VectorSelector{
|
||||||
Name: "some_metric",
|
Name: "some_metric",
|
||||||
|
@ -1396,9 +1398,7 @@ var testExpr = []struct {
|
||||||
|
|
||||||
func TestParseExpressions(t *testing.T) {
|
func TestParseExpressions(t *testing.T) {
|
||||||
for _, test := range testExpr {
|
for _, test := range testExpr {
|
||||||
parser := newParser(test.input)
|
expr, err := ParseExpr(test.input)
|
||||||
|
|
||||||
expr, err := parser.parseExpr()
|
|
||||||
|
|
||||||
// Unexpected errors are always caused by a bug.
|
// Unexpected errors are always caused by a bug.
|
||||||
if err == errUnexpected {
|
if err == errUnexpected {
|
||||||
|
@ -1409,6 +1409,7 @@ func TestParseExpressions(t *testing.T) {
|
||||||
t.Errorf("error in input '%s'", test.input)
|
t.Errorf("error in input '%s'", test.input)
|
||||||
t.Fatalf("could not parse: %s", err)
|
t.Fatalf("could not parse: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if test.fail && err != nil {
|
if test.fail && err != nil {
|
||||||
if !strings.Contains(err.Error(), test.errMsg) {
|
if !strings.Contains(err.Error(), test.errMsg) {
|
||||||
t.Errorf("unexpected error on input '%s'", test.input)
|
t.Errorf("unexpected error on input '%s'", test.input)
|
||||||
|
@ -1417,24 +1418,6 @@ func TestParseExpressions(t *testing.T) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
err = parser.typecheck(expr)
|
|
||||||
if !test.fail && err != nil {
|
|
||||||
t.Errorf("error on input '%s'", test.input)
|
|
||||||
t.Fatalf("typecheck failed: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if test.fail {
|
|
||||||
if err != nil {
|
|
||||||
if !strings.Contains(err.Error(), test.errMsg) {
|
|
||||||
t.Errorf("unexpected error on input '%s'", test.input)
|
|
||||||
t.Fatalf("expected error to contain %q but got %q", test.errMsg, err)
|
|
||||||
}
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
t.Errorf("error on input '%s'", test.input)
|
|
||||||
t.Fatalf("failure expected, but passed with result: %q", expr)
|
|
||||||
}
|
|
||||||
|
|
||||||
if !reflect.DeepEqual(expr, test.expected) {
|
if !reflect.DeepEqual(expr, test.expected) {
|
||||||
t.Errorf("error on input '%s'", test.input)
|
t.Errorf("error on input '%s'", test.input)
|
||||||
t.Fatalf("no match\n\nexpected:\n%s\ngot: \n%s\n", Tree(test.expected), Tree(expr))
|
t.Fatalf("no match\n\nexpected:\n%s\ngot: \n%s\n", Tree(test.expected), Tree(expr))
|
||||||
|
@ -1444,9 +1427,7 @@ func TestParseExpressions(t *testing.T) {
|
||||||
|
|
||||||
// NaN has no equality. Thus, we need a separate test for it.
|
// NaN has no equality. Thus, we need a separate test for it.
|
||||||
func TestNaNExpression(t *testing.T) {
|
func TestNaNExpression(t *testing.T) {
|
||||||
parser := newParser("NaN")
|
expr, err := ParseExpr("NaN")
|
||||||
|
|
||||||
expr, err := parser.parseExpr()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("error on input 'NaN'")
|
t.Errorf("error on input 'NaN'")
|
||||||
t.Fatalf("coud not parse: %s", err)
|
t.Fatalf("coud not parse: %s", err)
|
||||||
|
@ -1677,9 +1658,7 @@ var testStatement = []struct {
|
||||||
|
|
||||||
func TestParseStatements(t *testing.T) {
|
func TestParseStatements(t *testing.T) {
|
||||||
for _, test := range testStatement {
|
for _, test := range testStatement {
|
||||||
parser := newParser(test.input)
|
stmts, err := ParseStmts(test.input)
|
||||||
|
|
||||||
stmts, err := parser.parseStmts()
|
|
||||||
|
|
||||||
// Unexpected errors are always caused by a bug.
|
// Unexpected errors are always caused by a bug.
|
||||||
if err == errUnexpected {
|
if err == errUnexpected {
|
||||||
|
@ -1694,20 +1673,6 @@ func TestParseStatements(t *testing.T) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
err = parser.typecheck(stmts)
|
|
||||||
if !test.fail && err != nil {
|
|
||||||
t.Errorf("error in input: \n\n%s\n", test.input)
|
|
||||||
t.Fatalf("typecheck failed: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if test.fail {
|
|
||||||
if err != nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
t.Errorf("error in input: \n\n%s\n", test.input)
|
|
||||||
t.Fatalf("failure expected, but passed")
|
|
||||||
}
|
|
||||||
|
|
||||||
if !reflect.DeepEqual(stmts, test.expected) {
|
if !reflect.DeepEqual(stmts, test.expected) {
|
||||||
t.Errorf("error in input: \n\n%s\n", test.input)
|
t.Errorf("error in input: \n\n%s\n", test.input)
|
||||||
t.Fatalf("no match\n\nexpected:\n%s\ngot: \n%s\n", Tree(test.expected), Tree(stmts))
|
t.Fatalf("no match\n\nexpected:\n%s\ngot: \n%s\n", Tree(test.expected), Tree(stmts))
|
||||||
|
@ -1791,30 +1756,24 @@ func newSeq(vals ...float64) (res []sequenceValue) {
|
||||||
|
|
||||||
func TestParseSeries(t *testing.T) {
|
func TestParseSeries(t *testing.T) {
|
||||||
for _, test := range testSeries {
|
for _, test := range testSeries {
|
||||||
parser := newParser(test.input)
|
metric, vals, err := parseSeriesDesc(test.input)
|
||||||
parser.lex.seriesDesc = true
|
|
||||||
|
|
||||||
metric, vals, err := parser.parseSeriesDesc()
|
|
||||||
|
|
||||||
// Unexpected errors are always caused by a bug.
|
// Unexpected errors are always caused by a bug.
|
||||||
if err == errUnexpected {
|
if err == errUnexpected {
|
||||||
t.Fatalf("unexpected error occurred")
|
t.Fatalf("unexpected error occurred")
|
||||||
}
|
}
|
||||||
|
|
||||||
if !test.fail && err != nil {
|
|
||||||
t.Errorf("error in input: \n\n%s\n", test.input)
|
|
||||||
t.Fatalf("could not parse: %s", err)
|
|
||||||
}
|
|
||||||
if test.fail && err != nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if test.fail {
|
if test.fail {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
t.Errorf("error in input: \n\n%s\n", test.input)
|
t.Errorf("error in input: \n\n%s\n", test.input)
|
||||||
t.Fatalf("failure expected, but passed")
|
t.Fatalf("failure expected, but passed")
|
||||||
|
} else {
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("error in input: \n\n%s\n", test.input)
|
||||||
|
t.Fatalf("could not parse: %s", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
require.Equal(t, test.expectedMetric, metric)
|
require.Equal(t, test.expectedMetric, metric)
|
||||||
|
|
Loading…
Reference in New Issue