Esempio n. 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")
	}
}
Esempio n. 2
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")
	}
}
Esempio n. 3
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")
	}
}
Esempio n. 4
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))
}
Esempio n. 5
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)
	}
}
Esempio n. 6
0
func TestComposeStringEq(t *testing.T) {
	expr := ast.NewNestedFunction("eq",
		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")
	}
}
Esempio n. 7
0
//ConvertBuiltInIntoFunction converts a BuiltIn Expr into a Function Expr.
func ConvertBuiltInIntoFunction(e *ast.Expr) (*ast.Expr, error) {
	if e.BuiltIn == nil {
		return e, nil
	}
	s := e.GetBuiltIn().GetSymbol().GetValue()
	right := e.GetBuiltIn().GetExpr().Clone()
	typ, err := Which(right)
	if err != nil {
		return nil, err
	}
	if types.IsList(typ) {
		typ = types.ListToSingle(typ)
	}
	left := ast.NewVar(typ)
	funcName := ast.BuiltInFunctionName(s)
	e2 := ast.NewNestedFunction(funcName, left, right)
	if funcName == "regex" {
		e2 = ast.NewNestedFunction(funcName, right, ast.NewVar(types.SINGLE_STRING))
	} else if funcName == "type" {
		e2 = ast.NewNestedFunction(funcName, right)
	}
	return e2, nil
}
Esempio n. 8
0
func TestConst(t *testing.T) {
	expr := ast.NewNestedFunction("regex",
		ast.NewStringVar(),
		ast.NewStringConst("ab"),
	)
	b, err := NewBool(expr)
	if err != nil {
		panic(err)
	}
	_, err = NewBoolFunc(b)
	if err == nil {
		t.Fatalf("expected error")
	}
	if !strings.Contains(err.Error(), "regex has constant") || !strings.Contains(err.Error(), "has a variable parameter") {
		t.Fatalf("expected more specific error %s", err.Error())
	}
}
Esempio n. 9
0
func TestTrimInit(t *testing.T) {
	expr := ast.NewNestedFunction("regex",
		ast.NewStringConst(".*"),
		ast.NewStringConst("ab"),
	)
	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")
	}
}
Esempio n. 10
0
func TestComposeRegex(t *testing.T) {
	expr := ast.NewNestedFunction("regex",
		ast.NewStringConst("ab"),
		ast.NewStringVar(),
	)
	b, err := NewBool(expr)
	if err != nil {
		panic(err)
	}
	f, err := NewBoolFunc(b)
	if err != nil {
		panic(err)
	}
	r, err := f.Eval(debug.NewStringValue("a"))
	if err != nil {
		panic(err)
	}
	if r != false {
		t.Fatalf("expected false")
	}
}