1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
package metricsql
import (
"testing"
)
func TestExpandWithExprsSuccess(t *testing.T) {
f := func(q, qExpected string) {
t.Helper()
for i := 0; i < 3; i++ {
qExpanded, err := ExpandWithExprs(q)
if err != nil {
t.Fatalf("unexpected error when expanding %q: %s", q, err)
}
if qExpanded != qExpected {
t.Fatalf("unexpected expanded expression for %q;\ngot\n%q\nwant\n%q", q, qExpanded, qExpected)
}
}
}
f(`1`, `1`)
f(`foobar`, `foobar`)
f(`with (x = 1) x+x`, `2`)
f(`with (f(x) = x*x) 3+f(2)+2`, `9`)
}
func TestExpandWithExprsError(t *testing.T) {
f := func(q string) {
t.Helper()
for i := 0; i < 3; i++ {
qExpanded, err := ExpandWithExprs(q)
if err == nil {
t.Fatalf("expecting non-nil error when expanding %q", q)
}
if qExpanded != "" {
t.Fatalf("unexpected non-empty qExpanded=%q", qExpanded)
}
}
}
f(``)
f(` with (`)
}
func TestVisitAll(t *testing.T) {
f := func(q, sExpected string) {
t.Helper()
expr, err := Parse(q)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
var buf []byte
VisitAll(expr, func(e Expr) {
buf = e.AppendString(buf)
buf = append(buf, ',')
})
if string(buf) != sExpected {
t.Fatalf("unexpected result; got\n%q\nwant\n%q", buf, sExpected)
}
}
f("123", "123,")
f("1+2", "3,")
f("1+a", "1,a, (), (),1 + a,")
f("f(a<b+1, sum(x) by (y))", "a,b,1, (), (),b + 1, (), (),a < (b + 1),x,by (y),sum(x) by (y),f(a < (b + 1), sum(x) by (y)),")
f("x[1s]", "x,x[1s],")
}
|