コード例 #1
0
func TestEvalLambdaNode_EvalRegex_Sanity(t *testing.T) {
	evaluator, err := stateful.NewEvalLambdaNode(&ast.LambdaNode{
		Expression: &ast.RegexNode{
			Regex: regexp.MustCompile("^abc.*"),
		},
	})

	if err != nil {
		t.Fatalf("Failed to compile lambda node: %v", err)
	}

	result, err := evaluator.EvalRegex(stateful.NewScope(), stateful.CreateExecutionState())
	if err != nil {
		t.Errorf("Got unexpected error: %v", err)
	}

	if got, exp := result.String(), "^abc.*"; exp != got {
		t.Errorf("unexpected result: got: %T(%v), expected: %s", got, got, exp)
	}
}
コード例 #2
0
func TestEvalLambdaNode_EvalString_Sanity(t *testing.T) {
	evaluator, err := stateful.NewEvalLambdaNode(&ast.LambdaNode{
		Expression: &ast.StringNode{
			Literal: "string",
		},
	})

	if err != nil {
		t.Fatalf("Failed to compile lambda node: %v", err)
	}

	result, err := evaluator.EvalString(stateful.NewScope(), stateful.CreateExecutionState())
	if err != nil {
		t.Errorf("Got unexpected error: %v", err)
	}

	if got, exp := result, "string"; got != exp {
		t.Errorf("unexpected result: got: %T(%v), expected: %s", got, got, exp)
	}
}
コード例 #3
0
func TestEvalLambdaNode_EvalDuration_Sanity(t *testing.T) {
	evaluator, err := stateful.NewEvalLambdaNode(&ast.LambdaNode{
		Expression: &ast.UnaryNode{
			Operator: ast.TokenMinus,
			Node: &ast.DurationNode{
				Dur: time.Minute,
			},
		},
	})

	if err != nil {
		t.Fatalf("Failed to compile lambda node: %v", err)
	}

	result, err := evaluator.EvalDuration(stateful.NewScope(), stateful.CreateExecutionState())
	if err != nil {
		t.Errorf("Got unexpected error: %v", err)
	}

	if got, exp := result, -time.Minute; got != exp {
		t.Errorf("unexpected result: got: %T(%v), expected: %s", got, got, exp)
	}
}
コード例 #4
0
func TestEvalLambdaNode_EvalBool_Sanity(t *testing.T) {
	evaluator, err := stateful.NewEvalLambdaNode(&ast.LambdaNode{
		Expression: &ast.UnaryNode{
			Operator: ast.TokenNot,
			Node: &ast.BoolNode{
				Bool: false,
			},
		},
	})

	if err != nil {
		t.Fatalf("Failed to compile lambda node: %v", err)
	}

	result, err := evaluator.EvalBool(stateful.NewScope(), stateful.CreateExecutionState())
	if err != nil {
		t.Errorf("Got unexpected error: %v", err)
	}

	if !result {
		t.Errorf("unexpected result: got: %t, expected: true", result)
	}
}
コード例 #5
0
func TestEvalLambdaNode_EvalInt64_Sanity(t *testing.T) {
	evaluator, err := stateful.NewEvalLambdaNode(&ast.LambdaNode{
		Expression: &ast.UnaryNode{
			Operator: ast.TokenMinus,
			Node: &ast.NumberNode{
				IsInt: true,
				Int64: int64(12),
			},
		},
	})

	if err != nil {
		t.Fatalf("Failed to compile lambda node: %v", err)
	}

	result, err := evaluator.EvalInt(stateful.NewScope(), stateful.CreateExecutionState())
	if err != nil {
		t.Errorf("Got unexpected error: %v", err)
	}

	if result != int64(-12) {
		t.Errorf("unexpected result: got: %T(%v), expected: int64(-12)", result, result)
	}
}