func TestComposeContains(t *testing.T) { expr := ast.NewNestedFunction("contains", ast.NewNestedFunction("toLower", ast.NewStringVar()), ast.NewNestedFunction("toLower", ast.NewStringConst("TheStreet")), ) b, err := NewBool(expr) if err != nil { panic(err) } f, err := NewBoolFunc(b) if err != nil { panic(err) } r, err := f.Eval(debug.NewStringValue("TheStreet")) if err != nil { panic(err) } if r != true { t.Fatalf("expected true") } r, err = f.Eval(debug.NewStringValue("ThatStreet")) if err != nil { panic(err) } if r != false { t.Fatalf("expected false") } if strings.Contains(funcs.Sprint(f.(*composedBool).Func), "toLower(`TheStreet`)") { t.Fatalf("trimming did not work") } }
func TestList(t *testing.T) { expr := ast.NewNestedFunction("eq", ast.NewNestedFunction("elem", ast.NewStringList( ast.NewStringConst("abc"), ), ast.NewIntConst(0), ), ast.NewStringConst("abc"), ) b, err := NewBool(expr) if err != nil { panic(err) } f, err := NewBoolFunc(b) if err != nil { panic(err) } str := funcs.Sprint(f.(*composedBool).Func) if str != "->true" { t.Fatalf("not enough trimming on %s", str) } r, err := f.Eval(nil) if err != nil { panic(err) } if r != true { t.Fatalf("expected true") } }
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) } f, err := NewBoolFunc(b) if err != nil { panic(err) } r, err := f.Eval(nil) if err != nil { panic(err) } if r != true { t.Fatalf("expected true") } str := funcs.Sprint(f.(*composedBool).Func) if str != "->true" { t.Fatalf("trimming did not work: %s", str) } }
func TestNoTrim(t *testing.T) { expr := ast.NewNestedFunction("eq", ast.NewNestedFunction("elem", ast.NewStringList( ast.NewNestedFunction("print", ast.NewStringVar(), ), ), ast.NewIntConst(0), ), ast.NewStringConst("abc"), ) b, err := NewBool(expr) if err != nil { panic(err) } f, err := NewBoolFunc(b) if err != nil { panic(err) } str := funcs.Sprint(f.(*composedBool).Func) if str == "false" { t.Fatalf("too much trimming") } t.Logf("trimmed = %s", str) r, err := f.Eval(debug.NewStringValue("abc")) if err != nil { panic(err) } if r != true { t.Fatalf("expected true") } }
func TestComposeListInt64(t *testing.T) { expr := ast.NewNestedFunction("eq", ast.NewNestedFunction("elem", ast.NewNestedFunction("print", ast.NewIntList( ast.NewIntConst(1), ast.NewIntConst(2), ), ), ast.NewIntConst(1), ), ast.NewIntConst(2), ) b, err := NewBool(expr) if err != nil { panic(err) } f, err := NewBoolFunc(b) if err != nil { panic(err) } r, err := f.Eval(nil) if err != nil { panic(err) } if r != true { t.Fatalf("expected true") } t.Logf("%s", funcs.Sprint(f.(*composedBool).Func)) }
func TestComposeListBool(t *testing.T) { expr := ast.NewNestedFunction("eq", ast.NewNestedFunction("length", ast.NewBoolList( ast.NewTrue(), ast.NewFalse(), ), ), ast.NewIntConst(2), ) b, err := NewBool(expr) if err != nil { panic(err) } f, err := NewBoolFunc(b) if err != nil { panic(err) } r, err := f.Eval(nil) if err != nil { panic(err) } if r != true { t.Fatalf("expected true") } str := funcs.Sprint(f.(*composedBool).Func) if str != "->true" { t.Fatalf("trimming did not work: %s", str) } }
//FuncToName decompiles a function back into a name expression, if possible. func FuncToName(f funcs.Bool) *ast.NameExpr { exprStr := funcs.Sprint(f) expr, err := relapseparser.NewParser().ParseExpr(exprStr) if err != nil { panic(err) } return exprToName(expr) }
func (this *ifExprs) String() string { if this.ret != nil { ss := make([]string, len(this.ret)) for i := range this.ret { ss[i] = this.ret[i].String() } return strings.Join(ss, ", ") } sthen := addtab(this.then.String()) sels := addtab(this.els.String()) sfunc := funcs.Sprint(this.cond) return "{\n" + sfunc + "\nThen:\n" + sthen + "\nElse:\n" + sels + "}" }