Skip to content

Commit

Permalink
Unit testing phase2 (#290)
Browse files Browse the repository at this point in the history
* Fixes for unit testing

* Fixes for unit testing

---------

Co-authored-by: Ashutosh Bhide <[email protected]>
  • Loading branch information
abhijitWakchaure and abhide-tibco authored May 7, 2024
1 parent 00658af commit 93d24bc
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 3 deletions.
79 changes: 79 additions & 0 deletions data/expression/script/gocc/ast/comparison.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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 {
Expand All @@ -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:
Expand Down Expand Up @@ -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 {
Expand All @@ -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:
Expand Down Expand Up @@ -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 {
Expand All @@ -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:
Expand Down Expand Up @@ -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 {
Expand All @@ -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:
Expand Down Expand Up @@ -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 {
Expand All @@ -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:
Expand Down Expand Up @@ -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
}
10 changes: 10 additions & 0 deletions data/expression/script/gocc/ast/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
20 changes: 17 additions & 3 deletions data/expression/script/gocc/ast/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ast

import (
"fmt"
"github.com/project-flogo/core/data/coerce"
"strings"

"github.com/project-flogo/core/data"
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}

0 comments on commit 93d24bc

Please sign in to comment.