func TestComposeNot(t *testing.T) { expr := &ast.Expr{ Function: &ast.Function{ Name: "not", Params: []*ast.Expr{ { Terminal: &ast.Terminal{ BoolValue: proto.Bool(false), }, }, }, }, } b, err := NewBool(expr) if err != nil { panic(err) } r, err := b.Eval(nil) if err != nil { panic(err) } if r != true { t.Fatalf("expected true") } str := funcs.Sprint(b.Func) if str != "true" { t.Fatalf("trimming did not work: %s", str) } }
func TestComposeContains(t *testing.T) { expr := &ast.Expr{ Function: &ast.Function{ Name: "contains", Params: []*ast.Expr{ { Function: &ast.Function{ Name: "nfkc", Params: []*ast.Expr{ { Terminal: &ast.Terminal{ Variable: &ast.Variable{ Name: "a.a.a", Type: types.SINGLE_STRING, }, }, }, }, }, }, { Function: &ast.Function{ Name: "nfkc", Params: []*ast.Expr{ { Terminal: &ast.Terminal{ StringValue: proto.String("TheStreet"), }, }, }, }, }, }, }, } b, err := NewBool(expr) if err != nil { panic(err) } r, err := b.Eval(serialize.NewStringValue("TheStreet")) if err != nil { panic(err) } if r != true { t.Fatalf("expected true") } r, err = b.Eval(serialize.NewStringValue("ThatStreet")) if err != nil { panic(err) } if r != false { t.Fatalf("expected false") } if strings.Contains(funcs.Sprint(b.Func), "nfkc(`TheStreet`)") { t.Fatalf("trimming did not work") } }
func TestList(t *testing.T) { expr := &ast.Expr{ Function: &ast.Function{ Name: "eq", Params: []*ast.Expr{ { Function: &ast.Function{ Name: "elem", Params: []*ast.Expr{ { List: &ast.List{ Type: types.LIST_STRING, Elems: []*ast.Expr{ { Terminal: &ast.Terminal{ StringValue: proto.String("abc"), }, }, }, }, }, { Terminal: &ast.Terminal{ Int64Value: proto.Int64(0), }, }, }, }, }, { Terminal: &ast.Terminal{ StringValue: proto.String("abc"), }, }, }, }, } b, err := NewBool(expr) if err != nil { panic(err) } str := funcs.Sprint(b.Func) if str != "true" { t.Fatalf("not enough trimming on %s", str) } r, err := b.Eval(nil) if err != nil { panic(err) } if r != true { t.Fatalf("expected true") } }
func TestComposeListBool(t *testing.T) { expr := &ast.Expr{ Function: &ast.Function{ Name: "eq", Params: []*ast.Expr{ { Function: &ast.Function{ Name: "length", Params: []*ast.Expr{ { List: &ast.List{ Type: types.LIST_BOOL, Elems: []*ast.Expr{ { Terminal: &ast.Terminal{ BoolValue: proto.Bool(true), }, }, { Terminal: &ast.Terminal{ BoolValue: proto.Bool(false), }, }, }, }, }, }, }, }, { Terminal: &ast.Terminal{ Int64Value: proto.Int64(2), }, }, }, }, } b, err := NewBool(expr) if err != nil { panic(err) } r, err := b.Eval(nil) if err != nil { panic(err) } if r != true { t.Fatalf("expected true") } str := funcs.Sprint(b.Func) if str != "true" { t.Fatalf("trimming did not work: %s", str) } }
func TestNoTrim(t *testing.T) { expr := &ast.Expr{ Function: &ast.Function{ Name: "eq", Params: []*ast.Expr{ { Function: &ast.Function{ Name: "elem", Params: []*ast.Expr{ { List: &ast.List{ Type: types.LIST_STRING, Elems: []*ast.Expr{ { Function: &ast.Function{ Name: "print", Params: []*ast.Expr{ { Terminal: &ast.Terminal{ Variable: &ast.Variable{ Name: "a.a.a", Type: types.SINGLE_STRING, }, }, }, }, }, }, }, }, }, { Terminal: &ast.Terminal{ Int64Value: proto.Int64(0), }, }, }, }, }, { Terminal: &ast.Terminal{ StringValue: proto.String("abc"), }, }, }, }, } b, err := NewBool(expr) if err != nil { panic(err) } str := funcs.Sprint(b.Func) if str == "false" { t.Fatalf("too much trimming") } t.Logf("trimmed = %s", str) r, err := b.Eval(serialize.NewStringValue("abc")) if err != nil { panic(err) } if r != true { t.Fatalf("expected true") } }
func TestComposeListInt64(t *testing.T) { expr := &ast.Expr{ Function: &ast.Function{ Name: "eq", Params: []*ast.Expr{ { Function: &ast.Function{ Name: "elem", Params: []*ast.Expr{ { Function: &ast.Function{ Name: "print", Params: []*ast.Expr{ { List: &ast.List{ Type: types.LIST_INT64, Elems: []*ast.Expr{ { Terminal: &ast.Terminal{ Int64Value: proto.Int64(1), }, }, { Terminal: &ast.Terminal{ Int64Value: proto.Int64(2), }, }, }, }, }, }, }, }, { Terminal: &ast.Terminal{ Int64Value: proto.Int64(1), }, }, }, }, }, { Terminal: &ast.Terminal{ Int64Value: proto.Int64(2), }, }, }, }, } b, err := NewBool(expr) if err != nil { panic(err) } r, err := b.Eval(nil) if err != nil { panic(err) } if r != true { t.Fatalf("expected true") } t.Logf("%s", funcs.Sprint(b.Func)) }