package expr
import "testing"
func TestParseAtoms(t *testing.T) {
cases := map[string]string{
"method=GET": "cmp",
"status>=400": "cmp",
"host=x and method=GET": "and",
"a=1 or b=2": "or",
"not method=GET": "not",
"(a=1 or b=2) and c=3": "and",
}
for src, wantKind := range cases {
t.Run(src, func(t *testing.T) {
n, err := Parse(src)
if err != nil {
t.Fatalf("Parse: %v", err)
}
if n.Kind != wantKind {
t.Errorf("kind = %q want %q", n.Kind, wantKind)
}
})
}
}
func TestParseString(t *testing.T) {
n, err := Parse(`path="/api/v1/things"`)
if err != nil {
t.Fatal(err)
}
if n.Kind != "cmp" || n.Children[1].Kind != "string" {
t.Fatalf("unexpected: %+v", n)
}
if n.Children[1].Str != "/api/v1/things" {
t.Errorf("str = %q", n.Children[1].Str)
}
}
func TestParseErrors(t *testing.T) {
for _, bad := range []string{
"",
"method==GET",
"a= ",
"a= b)",
"(a=1",
} {
if _, err := Parse(bad); err == nil {
t.Errorf("expected error for %q", bad)
}
}
}
func TestTrimQuotes(t *testing.T) {
if trimQuotes(`"abc"`) != "abc" {
t.Fatal("double quote")
}
if trimQuotes(`'abc'`) != "abc" {
t.Fatal("single quote")
}
}