Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit testing phase2 #290

Merged
merged 2 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}