Beispiel #1
0
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")
	}
}
Beispiel #2
0
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")
	}
}
Beispiel #3
0
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)
	}
}
Beispiel #4
0
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")
	}
}
Beispiel #5
0
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))
}
Beispiel #6
0
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)
	}
}
Beispiel #7
0
//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)
}
Beispiel #8
0
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 + "}"
}