Skip to content

Commit

Permalink
Merge pull request #4 from project-flogo/master
Browse files Browse the repository at this point in the history
Update core
  • Loading branch information
skothari-tibco committed Feb 27, 2019
2 parents 4682222 + f762374 commit f5fce6c
Show file tree
Hide file tree
Showing 23 changed files with 10,453 additions and 7,112 deletions.
11 changes: 11 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
language: go
go:
- 1.11.x
os:
- linux
- osx
env:
- GO111MODULE=on
script:
- go build
- go test ./...
83 changes: 82 additions & 1 deletion data/expression/script/expr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package script
import (
"encoding/json"
"fmt"
"os"
"testing"

"github.com/project-flogo/core/data"
"github.com/project-flogo/core/data/resolve"
"github.com/stretchr/testify/assert"
)

var resolver = resolve.NewCompositeResolver(map[string]resolve.Resolver{"static": &TestStaticResolver{}, ".": &TestResolver{}})
var resolver = resolve.NewCompositeResolver(map[string]resolve.Resolver{"static": &TestStaticResolver{}, ".": &TestResolver{}, "env": &resolve.EnvResolver{}})
var factory = NewExprFactory(resolver)

func TestLitExprInt(t *testing.T) {
Expand Down Expand Up @@ -151,6 +152,18 @@ func TestLitExprStaticRef(t *testing.T) {
assert.Equal(t, "bar", v)
}

func TestEnvResolve(t *testing.T) {

os.Setenv("FOO","bar")
expr, err := factory.NewExpr(`$env[FOO]`)
assert.Nil(t, err)
assert.NotNil(t, expr)

v, err := expr.Eval(nil)
assert.Nil(t, err)
assert.Equal(t, "bar", v)
}

func TestCmpExprEq(t *testing.T) {
expr, err := factory.NewExpr(`123==123`)
assert.Nil(t, err)
Expand Down Expand Up @@ -661,6 +674,74 @@ func TestTernaryExpr(t *testing.T) {
assert.Equal(t, 40, v)
}

func TestExpression(t *testing.T) {

scope := data.NewSimpleScope(map[string]interface{}{"queryParams": map[string]interface{}{"id": "helloworld"}}, nil)
factory := NewExprFactory(resolve.GetBasicResolver())
os.Setenv("name", "flogo")
os.Setenv("address", "tibco")

testcases := make(map[string]interface{})
testcases[`1>2?tstring.concat("sss","ddddd"):"fff"`] = "fff"
testcases[`1<2?"helloworld":"fff"`] = "helloworld"
testcases["200>100?true:false"] = true
testcases["1 + 2 * 3 + 2 * 6"] = 19
testcases[`tstring.length($.queryParams.id) == 0 ? "Query Id cannot be null" : tstring.length($.queryParams.id)`] = 10
testcases[`tstring.length("helloworld")>11?"helloworld":"fff"`] = "fff"
testcases["123==456"] = false
testcases["123==123"] = true
testcases[`tstring.concat("123","456")=="123456"`] = true
testcases[`tstring.concat("123","456") == tstring.concat("12","3456")`] = true
testcases[`("dddddd" == "dddd3dd") && ("133" == "123")`] = false
testcases[`tstring.length("helloworld") == 10`] = true
testcases[`tstring.length("helloworld") > 10`] = false
testcases[`tstring.length("helloworld") >= 10`] = true
testcases[`tstring.length("helloworld") < 10`] = false
testcases[`tstring.length("helloworld") >= 10`] = true
testcases[`(tstring.length("sea") == 3) == true`] = true

testcases[`(1&&1)==(1&&1)`] = true
testcases[`(true && true) == false`] = false
testcases[`nil==nil`] = true

//Nested Ternary
testcases[`(tstring.length("1234") == 4 ? true : false) ? (2 >1 ? (3>2?"Yes":"nono"):"No") : "false"`] = "Yes"
testcases[`(4 == 4 ? true : false) ? "yes" : "no"`] = "yes"
testcases[`(4 == 4 ? true : false) ? 4 < 3 ? "good" :"false" : "no"`] = "false"
testcases[`4 > 3 ? 6<4 ? "good2" : "false2" : "false"`] = "false2"
testcases[`4 > 5 ? 6<4 ? "good2" : "false2" : 3>2?"ok":"notok"`] = "ok"

//Int vs float
testcases[`1 == 1.23`] = false
testcases[`1 < 1.23`] = true
testcases[`1.23 == 1`] = false
testcases[`1.23 > 1`] = true

//Operator
testcases[`1 + 2 * 3 + 2 * 6 / 2`] = 13
testcases[` 1 + 4 * 5 + -6 `] = 15
testcases[` 2 < 3 && 5 > 4 && 6 < 7 && 56 > 44`] = true
testcases[` 2 < 3 && 5 > 4 || 6 < 7 && 56 < 44`] = true
testcases[`3-2`] = 1
testcases[`3 - 2`] = 1
testcases[`3+-2`] = 1
testcases[`3- -2`] = 5

//testcases[`tstring.length("helloworld")>11?$env[name]:$env[address]`] = "tibco"
//testcases[`$env[name] != nil`] = true
//testcases[`$env[name] == "flogo"`] = true

for k, v := range testcases {
vv, err := factory.NewExpr(k)
assert.Nil(t, err)
result, err := vv.Eval(scope)
assert.Nil(t, err)
if !assert.ObjectsAreEqual(v, result) {
assert.Fail(t, fmt.Sprintf("test expr [%s] failed, expected [%+v] but actual [%+v]", k, v, result))
}
}
}

var result interface{}

func BenchmarkLit(b *testing.B) {
Expand Down
40 changes: 40 additions & 0 deletions data/expression/script/funcexpr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package script
import (
"bytes"
"fmt"
"github.com/project-flogo/core/data/resolve"
"testing"

"github.com/project-flogo/core/data"
Expand Down Expand Up @@ -51,6 +52,25 @@ func TestFuncExprNestedMultiSpace(t *testing.T) {
assert.Equal(t, "This is Flogo", v.(string))
}

func TestFunctionWithRef(t *testing.T) {

scope := data.NewSimpleScope(map[string]interface{}{"queryParams": map[string]interface{}{"id": "flogo"}}, nil)
factory := NewExprFactory(resolve.GetBasicResolver())
testcases := make(map[string]interface{})
testcases[`tstring.concat("This", " is ", $.queryParams.id)`] = "This is flogo"

for k, v := range testcases {
vv, err := factory.NewExpr(k)
assert.Nil(t, err)
result, err := vv.Eval(scope)
assert.Nil(t, err)
if !assert.ObjectsAreEqual(v, result) {
assert.Fail(t, fmt.Sprintf("test function [%s] failed, expected [%+v] but actual [%+v]", k, v, result))
}
}

}

func init() {
function.Register(&fnConcat{})
}
Expand Down Expand Up @@ -86,3 +106,23 @@ func TestFuncExprSingleQuote(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, "abcdef", v)
}

func init() {
function.Register(&tLength{})
}

type tLength struct {
}

func (tLength) Name() string {
return "tstring.length"
}

func (tLength) Sig() (paramTypes []data.Type, isVariadic bool) {
return []data.Type{data.TypeString}, false
}

func (tLength) Eval(params ...interface{}) (interface{}, error) {
p := params[0].(string)
return len(p), nil
}
9 changes: 9 additions & 0 deletions data/expression/script/gocc/ast/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ func NewTernaryExpr(ifNode, thenNode, elseNode interface{}) (Expr, error) {
return &exprTernary{ifExpr: ifExpr, thenExpr: thenExpr, elseExpr: elseExpr}, nil
}

func NewTernaryArgument(first interface{}) (Expr, error) {
switch t := first.(type) {
case Expr:
return t, nil
default:
return nil, fmt.Errorf("unsupported ternary type %+v", first)
}
}

type exprTernary struct {
ifExpr, thenExpr, elseExpr Expr
}
Expand Down
13 changes: 9 additions & 4 deletions data/expression/script/gocc/fs.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ ident : _ident ;

ref : '$' [ _ident ];
selector : '.' _ident ;
index : '[' ( _dq_string | _sq_string | _lit_string | _decimals ) ']' ;
index : '[' ( _dq_string | _sq_string | _lit_string | _decimals | _ident) ']' ;

// -- [ Functions ]

Expand Down Expand Up @@ -104,11 +104,16 @@ PrimaryExpr
;

TernaryExpr
: Expr "?" Expr ":" Expr << ast.NewTernaryExpr($0, $2, $4) >>
: TernaryArgument "?" TernaryArgument ":" TernaryArgument <<ast.NewTernaryExpr($0, $2, $4)>>
;

BoolLit
: "true"
TernaryArgument
: Expr
| TernaryExpr
| "(" TernaryExpr ")" <<ast.NewTernaryArgument($1)>>
;

BoolLit : "true"
| "false"
;

Expand Down
54 changes: 31 additions & 23 deletions data/expression/script/gocc/lexer/acttab.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion data/expression/script/gocc/lexer/lexer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f5fce6c

Please sign in to comment.