From 93d24bca28804b8207af21b07e0695ad1073890b Mon Sep 17 00:00:00 2001 From: Abhijit Wakchaure <18426297+abhijitWakchaure@users.noreply.github.com> Date: Tue, 7 May 2024 14:57:45 +0530 Subject: [PATCH] Unit testing phase2 (#290) * Fixes for unit testing * Fixes for unit testing --------- Co-authored-by: Ashutosh Bhide --- data/expression/script/gocc/ast/comparison.go | 79 +++++++++++++++++++ data/expression/script/gocc/ast/expr.go | 10 +++ data/expression/script/gocc/ast/function.go | 20 ++++- 3 files changed, 106 insertions(+), 3 deletions(-) diff --git a/data/expression/script/gocc/ast/comparison.go b/data/expression/script/gocc/ast/comparison.go index fc483953..5df4a6d2 100644 --- a/data/expression/script/gocc/ast/comparison.go +++ b/data/expression/script/gocc/ast/comparison.go @@ -37,6 +37,7 @@ func NewCmpExpr(left, operand, right interface{}) (Expr, error) { type cmpEqExpr struct { left, right Expr + evalResult ExprEvalData } func (e *cmpEqExpr) Init(resolver resolve.CompositeResolver, root bool) error { @@ -49,6 +50,7 @@ func (e *cmpEqExpr) Init(resolver resolve.CompositeResolver, root bool) error { } func (e *cmpEqExpr) Eval(scope data.Scope) (interface{}, error) { + lv, rv, err := evalLR(e.left, e.right, scope) if err != nil { return nil, err @@ -58,6 +60,14 @@ func (e *cmpEqExpr) Eval(scope data.Scope) (interface{}, error) { return rv == nil && lv == nil, nil } + leftValue, _ := coerce.ToString(lv) + rightValue, _ := coerce.ToString(rv) + e.evalResult = ExprEvalData{ + ExpressionType: "operator", + ExpressionName: "equal to", + ExpressionEvaluation: leftValue + " == " + rightValue, + } + rt := reflect.TypeOf(rv).Kind() switch le := lv.(type) { case int, int32, int64: @@ -120,8 +130,13 @@ func (e *cmpEqExpr) Eval(scope data.Scope) (interface{}, error) { return false, nil } +func (e *cmpEqExpr) Detail() ExprEvalData { + return e.evalResult +} + type cmpNotEqExpr struct { left, right Expr + evalResult ExprEvalData } func (e *cmpNotEqExpr) Init(resolver resolve.CompositeResolver, root bool) error { @@ -144,6 +159,14 @@ func (e *cmpNotEqExpr) Eval(scope data.Scope) (interface{}, error) { return !(rv == nil && lv == nil), nil } + leftValue, _ := coerce.ToString(lv) + rightValue, _ := coerce.ToString(rv) + e.evalResult = ExprEvalData{ + ExpressionType: "operator", + ExpressionName: "not equal to", + ExpressionEvaluation: leftValue + " != " + rightValue, + } + rt := reflect.TypeOf(rv).Kind() switch le := lv.(type) { case int, int32, int64: @@ -205,8 +228,13 @@ func (e *cmpNotEqExpr) Eval(scope data.Scope) (interface{}, error) { return true, nil } +func (e *cmpNotEqExpr) Detail() ExprEvalData { + return e.evalResult +} + type cmpGtExpr struct { left, right Expr + evalResult ExprEvalData } func (e *cmpGtExpr) Init(resolver resolve.CompositeResolver, root bool) error { @@ -229,6 +257,14 @@ func (e *cmpGtExpr) Eval(scope data.Scope) (interface{}, error) { return false, nil } + leftValue, _ := coerce.ToString(lv) + rightValue, _ := coerce.ToString(rv) + e.evalResult = ExprEvalData{ + ExpressionType: "operator", + ExpressionName: "greater than", + ExpressionEvaluation: leftValue + " > " + rightValue, + } + rt := reflect.TypeOf(rv).Kind() switch le := lv.(type) { case int, int32, int64: @@ -285,8 +321,13 @@ func (e *cmpGtExpr) Eval(scope data.Scope) (interface{}, error) { return false, fmt.Errorf("cannot compare %s with %s", reflect.TypeOf(lv).String(), reflect.TypeOf(rv).String()) } +func (e *cmpGtExpr) Detail() ExprEvalData { + return e.evalResult +} + type cmpGtEqExpr struct { left, right Expr + evalResult ExprEvalData } func (e *cmpGtEqExpr) Init(resolver resolve.CompositeResolver, root bool) error { @@ -308,6 +349,14 @@ func (e *cmpGtEqExpr) Eval(scope data.Scope) (interface{}, error) { return lv == nil && rv == nil, nil } + leftValue, _ := coerce.ToString(lv) + rightValue, _ := coerce.ToString(rv) + e.evalResult = ExprEvalData{ + ExpressionType: "operator", + ExpressionName: "greater than equal to", + ExpressionEvaluation: leftValue + " >= " + rightValue, + } + rt := reflect.TypeOf(rv).Kind() switch le := lv.(type) { case int, int32, int64: @@ -368,8 +417,13 @@ func (e *cmpGtEqExpr) Eval(scope data.Scope) (interface{}, error) { return false, fmt.Errorf("cannot compare %s with %s", reflect.TypeOf(lv).String(), reflect.TypeOf(rv).String()) } +func (e *cmpGtEqExpr) Detail() ExprEvalData { + return e.evalResult +} + type cmpLtExpr struct { left, right Expr + evalResult ExprEvalData } func (e *cmpLtExpr) Init(resolver resolve.CompositeResolver, root bool) error { @@ -392,6 +446,14 @@ func (e *cmpLtExpr) Eval(scope data.Scope) (interface{}, error) { return false, nil } + leftValue, _ := coerce.ToString(lv) + rightValue, _ := coerce.ToString(rv) + e.evalResult = ExprEvalData{ + ExpressionType: "operator", + ExpressionName: "less than", + ExpressionEvaluation: leftValue + " < " + rightValue, + } + rt := reflect.TypeOf(rv).Kind() switch le := lv.(type) { case int, int32, int64: @@ -450,8 +512,13 @@ func (e *cmpLtExpr) Eval(scope data.Scope) (interface{}, error) { return false, fmt.Errorf("cannot compare %s with %s", reflect.TypeOf(lv).String(), reflect.TypeOf(rv).String()) } +func (e *cmpLtExpr) Detail() ExprEvalData { + return e.evalResult +} + type cmpLtEqExpr struct { left, right Expr + evalResult ExprEvalData } func (e *cmpLtEqExpr) Init(resolver resolve.CompositeResolver, root bool) error { @@ -473,6 +540,14 @@ func (e *cmpLtEqExpr) Eval(scope data.Scope) (interface{}, error) { return lv == nil && rv == nil, nil } + leftValue, _ := coerce.ToString(lv) + rightValue, _ := coerce.ToString(rv) + e.evalResult = ExprEvalData{ + ExpressionType: "operator", + ExpressionName: "less than equal to", + ExpressionEvaluation: leftValue + " <= " + rightValue, + } + rt := reflect.TypeOf(rv).Kind() switch le := lv.(type) { case int, int32, int64: @@ -531,3 +606,7 @@ func (e *cmpLtEqExpr) Eval(scope data.Scope) (interface{}, error) { return false, fmt.Errorf("cannot compare %s with %s", reflect.TypeOf(lv).String(), reflect.TypeOf(rv).String()) } + +func (e *cmpLtEqExpr) Detail() ExprEvalData { + return e.evalResult +} diff --git a/data/expression/script/gocc/ast/expr.go b/data/expression/script/gocc/ast/expr.go index ec2cad74..61199daa 100644 --- a/data/expression/script/gocc/ast/expr.go +++ b/data/expression/script/gocc/ast/expr.go @@ -203,3 +203,13 @@ func (e *keyIndexExpr) Eval(scope data.Scope) (interface{}, error) { } return "[" + index + "]", nil } + +type ExprEvalResult interface { + Detail() ExprEvalData +} + +type ExprEvalData struct { + ExpressionType string `json:"expressionType"` + ExpressionName string `json:"expressionName"` + ExpressionEvaluation string `json:"expressionEvaluation"` +} diff --git a/data/expression/script/gocc/ast/function.go b/data/expression/script/gocc/ast/function.go index c9e2767c..18c0f6b8 100644 --- a/data/expression/script/gocc/ast/function.go +++ b/data/expression/script/gocc/ast/function.go @@ -2,6 +2,7 @@ package ast import ( "fmt" + "github.com/project-flogo/core/data/coerce" "strings" "github.com/project-flogo/core/data" @@ -39,8 +40,9 @@ func NewFuncExpr(name interface{}, args interface{}) (Expr, error) { } type funcExpr struct { - f function.Function - params []Expr + f function.Function + params []Expr + evalResult ExprEvalData } func (e *funcExpr) Init(resolver resolve.CompositeResolver, root bool) error { @@ -64,5 +66,17 @@ func (e *funcExpr) Eval(scope data.Scope) (interface{}, error) { vals[idx] = v } - return function.Eval(e.f, vals...) + evalResult := ExprEvalData{ + ExpressionType: "function", + ExpressionName: e.f.Name(), + } + + eval, err := function.Eval(e.f, vals...) + evalResult.ExpressionEvaluation, _ = coerce.ToString(eval) + e.evalResult = evalResult + return eval, err +} + +func (e *funcExpr) Detail() ExprEvalData { + return e.evalResult }