Skip to content

Commit f5fce6c

Browse files
Merge pull request #4 from project-flogo/master
Update core
2 parents 4682222 + f762374 commit f5fce6c

23 files changed

Lines changed: 10453 additions & 7112 deletions

.travis.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
language: go
2+
go:
3+
- 1.11.x
4+
os:
5+
- linux
6+
- osx
7+
env:
8+
- GO111MODULE=on
9+
script:
10+
- go build
11+
- go test ./...

data/expression/script/expr_test.go

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ package script
33
import (
44
"encoding/json"
55
"fmt"
6+
"os"
67
"testing"
78

89
"github.com/project-flogo/core/data"
910
"github.com/project-flogo/core/data/resolve"
1011
"github.com/stretchr/testify/assert"
1112
)
1213

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

1617
func TestLitExprInt(t *testing.T) {
@@ -151,6 +152,18 @@ func TestLitExprStaticRef(t *testing.T) {
151152
assert.Equal(t, "bar", v)
152153
}
153154

155+
func TestEnvResolve(t *testing.T) {
156+
157+
os.Setenv("FOO","bar")
158+
expr, err := factory.NewExpr(`$env[FOO]`)
159+
assert.Nil(t, err)
160+
assert.NotNil(t, expr)
161+
162+
v, err := expr.Eval(nil)
163+
assert.Nil(t, err)
164+
assert.Equal(t, "bar", v)
165+
}
166+
154167
func TestCmpExprEq(t *testing.T) {
155168
expr, err := factory.NewExpr(`123==123`)
156169
assert.Nil(t, err)
@@ -661,6 +674,74 @@ func TestTernaryExpr(t *testing.T) {
661674
assert.Equal(t, 40, v)
662675
}
663676

677+
func TestExpression(t *testing.T) {
678+
679+
scope := data.NewSimpleScope(map[string]interface{}{"queryParams": map[string]interface{}{"id": "helloworld"}}, nil)
680+
factory := NewExprFactory(resolve.GetBasicResolver())
681+
os.Setenv("name", "flogo")
682+
os.Setenv("address", "tibco")
683+
684+
testcases := make(map[string]interface{})
685+
testcases[`1>2?tstring.concat("sss","ddddd"):"fff"`] = "fff"
686+
testcases[`1<2?"helloworld":"fff"`] = "helloworld"
687+
testcases["200>100?true:false"] = true
688+
testcases["1 + 2 * 3 + 2 * 6"] = 19
689+
testcases[`tstring.length($.queryParams.id) == 0 ? "Query Id cannot be null" : tstring.length($.queryParams.id)`] = 10
690+
testcases[`tstring.length("helloworld")>11?"helloworld":"fff"`] = "fff"
691+
testcases["123==456"] = false
692+
testcases["123==123"] = true
693+
testcases[`tstring.concat("123","456")=="123456"`] = true
694+
testcases[`tstring.concat("123","456") == tstring.concat("12","3456")`] = true
695+
testcases[`("dddddd" == "dddd3dd") && ("133" == "123")`] = false
696+
testcases[`tstring.length("helloworld") == 10`] = true
697+
testcases[`tstring.length("helloworld") > 10`] = false
698+
testcases[`tstring.length("helloworld") >= 10`] = true
699+
testcases[`tstring.length("helloworld") < 10`] = false
700+
testcases[`tstring.length("helloworld") >= 10`] = true
701+
testcases[`(tstring.length("sea") == 3) == true`] = true
702+
703+
testcases[`(1&&1)==(1&&1)`] = true
704+
testcases[`(true && true) == false`] = false
705+
testcases[`nil==nil`] = true
706+
707+
//Nested Ternary
708+
testcases[`(tstring.length("1234") == 4 ? true : false) ? (2 >1 ? (3>2?"Yes":"nono"):"No") : "false"`] = "Yes"
709+
testcases[`(4 == 4 ? true : false) ? "yes" : "no"`] = "yes"
710+
testcases[`(4 == 4 ? true : false) ? 4 < 3 ? "good" :"false" : "no"`] = "false"
711+
testcases[`4 > 3 ? 6<4 ? "good2" : "false2" : "false"`] = "false2"
712+
testcases[`4 > 5 ? 6<4 ? "good2" : "false2" : 3>2?"ok":"notok"`] = "ok"
713+
714+
//Int vs float
715+
testcases[`1 == 1.23`] = false
716+
testcases[`1 < 1.23`] = true
717+
testcases[`1.23 == 1`] = false
718+
testcases[`1.23 > 1`] = true
719+
720+
//Operator
721+
testcases[`1 + 2 * 3 + 2 * 6 / 2`] = 13
722+
testcases[` 1 + 4 * 5 + -6 `] = 15
723+
testcases[` 2 < 3 && 5 > 4 && 6 < 7 && 56 > 44`] = true
724+
testcases[` 2 < 3 && 5 > 4 || 6 < 7 && 56 < 44`] = true
725+
testcases[`3-2`] = 1
726+
testcases[`3 - 2`] = 1
727+
testcases[`3+-2`] = 1
728+
testcases[`3- -2`] = 5
729+
730+
//testcases[`tstring.length("helloworld")>11?$env[name]:$env[address]`] = "tibco"
731+
//testcases[`$env[name] != nil`] = true
732+
//testcases[`$env[name] == "flogo"`] = true
733+
734+
for k, v := range testcases {
735+
vv, err := factory.NewExpr(k)
736+
assert.Nil(t, err)
737+
result, err := vv.Eval(scope)
738+
assert.Nil(t, err)
739+
if !assert.ObjectsAreEqual(v, result) {
740+
assert.Fail(t, fmt.Sprintf("test expr [%s] failed, expected [%+v] but actual [%+v]", k, v, result))
741+
}
742+
}
743+
}
744+
664745
var result interface{}
665746

666747
func BenchmarkLit(b *testing.B) {

data/expression/script/funcexpr_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package script
33
import (
44
"bytes"
55
"fmt"
6+
"github.com/project-flogo/core/data/resolve"
67
"testing"
78

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

55+
func TestFunctionWithRef(t *testing.T) {
56+
57+
scope := data.NewSimpleScope(map[string]interface{}{"queryParams": map[string]interface{}{"id": "flogo"}}, nil)
58+
factory := NewExprFactory(resolve.GetBasicResolver())
59+
testcases := make(map[string]interface{})
60+
testcases[`tstring.concat("This", " is ", $.queryParams.id)`] = "This is flogo"
61+
62+
for k, v := range testcases {
63+
vv, err := factory.NewExpr(k)
64+
assert.Nil(t, err)
65+
result, err := vv.Eval(scope)
66+
assert.Nil(t, err)
67+
if !assert.ObjectsAreEqual(v, result) {
68+
assert.Fail(t, fmt.Sprintf("test function [%s] failed, expected [%+v] but actual [%+v]", k, v, result))
69+
}
70+
}
71+
72+
}
73+
5474
func init() {
5575
function.Register(&fnConcat{})
5676
}
@@ -86,3 +106,23 @@ func TestFuncExprSingleQuote(t *testing.T) {
86106
assert.Nil(t, err)
87107
assert.Equal(t, "abcdef", v)
88108
}
109+
110+
func init() {
111+
function.Register(&tLength{})
112+
}
113+
114+
type tLength struct {
115+
}
116+
117+
func (tLength) Name() string {
118+
return "tstring.length"
119+
}
120+
121+
func (tLength) Sig() (paramTypes []data.Type, isVariadic bool) {
122+
return []data.Type{data.TypeString}, false
123+
}
124+
125+
func (tLength) Eval(params ...interface{}) (interface{}, error) {
126+
p := params[0].(string)
127+
return len(p), nil
128+
}

data/expression/script/gocc/ast/expr.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ func NewTernaryExpr(ifNode, thenNode, elseNode interface{}) (Expr, error) {
5151
return &exprTernary{ifExpr: ifExpr, thenExpr: thenExpr, elseExpr: elseExpr}, nil
5252
}
5353

54+
func NewTernaryArgument(first interface{}) (Expr, error) {
55+
switch t := first.(type) {
56+
case Expr:
57+
return t, nil
58+
default:
59+
return nil, fmt.Errorf("unsupported ternary type %+v", first)
60+
}
61+
}
62+
5463
type exprTernary struct {
5564
ifExpr, thenExpr, elseExpr Expr
5665
}

data/expression/script/gocc/fs.bnf

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ ident : _ident ;
3636

3737
ref : '$' [ _ident ];
3838
selector : '.' _ident ;
39-
index : '[' ( _dq_string | _sq_string | _lit_string | _decimals ) ']' ;
39+
index : '[' ( _dq_string | _sq_string | _lit_string | _decimals | _ident) ']' ;
4040

4141
// -- [ Functions ]
4242

@@ -104,11 +104,16 @@ PrimaryExpr
104104
;
105105

106106
TernaryExpr
107-
: Expr "?" Expr ":" Expr << ast.NewTernaryExpr($0, $2, $4) >>
107+
: TernaryArgument "?" TernaryArgument ":" TernaryArgument <<ast.NewTernaryExpr($0, $2, $4)>>
108108
;
109109

110-
BoolLit
111-
: "true"
110+
TernaryArgument
111+
: Expr
112+
| TernaryExpr
113+
| "(" TernaryExpr ")" <<ast.NewTernaryArgument($1)>>
114+
;
115+
116+
BoolLit : "true"
112117
| "false"
113118
;
114119

data/expression/script/gocc/lexer/acttab.go

Lines changed: 31 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

data/expression/script/gocc/lexer/lexer.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)