@@ -3,14 +3,15 @@ package script
33import (
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 {} })
1415var factory = NewExprFactory (resolver )
1516
1617func 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+
154167func 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+
664745var result interface {}
665746
666747func BenchmarkLit (b * testing.B ) {
0 commit comments