github.com/MontFerret/ferret@v0.18.0/pkg/compiler/compiler_for_ternary_test.go (about) 1 package compiler_test 2 3 import ( 4 "context" 5 "testing" 6 7 . "github.com/smartystreets/goconvey/convey" 8 9 "github.com/MontFerret/ferret/pkg/compiler" 10 ) 11 12 func TestForTernaryExpression(t *testing.T) { 13 Convey("RETURN foo ? TRUE : (FOR i IN 1..5 RETURN i*2)", t, func() { 14 c := compiler.New() 15 16 out1, err := c.MustCompile(` 17 LET foo = FALSE 18 RETURN foo ? TRUE : (FOR i IN 1..5 RETURN i*2) 19 `).Run(context.Background()) 20 21 So(err, ShouldBeNil) 22 So(string(out1), ShouldEqual, `[2,4,6,8,10]`) 23 24 out2, err := c.MustCompile(` 25 LET foo = TRUE 26 RETURN foo ? TRUE : (FOR i IN 1..5 RETURN i*2) 27 `).Run(context.Background()) 28 29 So(err, ShouldBeNil) 30 So(string(out2), ShouldEqual, `true`) 31 }) 32 33 Convey("RETURN foo ? TRUE : (FOR i IN 1..5 T::FAIL() RETURN i*2)?", t, func() { 34 c := compiler.New() 35 36 out1, err := c.MustCompile(` 37 LET foo = FALSE 38 RETURN foo ? TRUE : (FOR i IN 1..5 T::FAIL() RETURN i*2)? 39 `).Run(context.Background()) 40 41 So(err, ShouldBeNil) 42 So(string(out1), ShouldEqual, `null`) 43 }) 44 45 Convey("RETURN foo ? (FOR i IN 1..5 RETURN i) : (FOR i IN 1..5 RETURN i*2)", t, func() { 46 c := compiler.New() 47 48 out1, err := c.MustCompile(` 49 LET foo = FALSE 50 RETURN foo ? (FOR i IN 1..5 RETURN i) : (FOR i IN 1..5 RETURN i*2) 51 `).Run(context.Background()) 52 53 So(err, ShouldBeNil) 54 So(string(out1), ShouldEqual, `[2,4,6,8,10]`) 55 56 out2, err := c.MustCompile(` 57 LET foo = TRUE 58 RETURN foo ? (FOR i IN 1..5 RETURN i) : (FOR i IN 1..5 RETURN i*2) 59 `).Run(context.Background()) 60 61 So(err, ShouldBeNil) 62 So(string(out2), ShouldEqual, `[1,2,3,4,5]`) 63 }) 64 65 Convey("RETURN foo ? (FOR i IN 1..5 RETURN T::FAIL())? : (FOR i IN 1..5 RETURN T::FAIL())?", t, func() { 66 c := compiler.New() 67 68 out1, err := c.MustCompile(` 69 LET foo = FALSE 70 RETURN foo ? (FOR i IN 1..5 RETURN T::FAIL()) : (FOR i IN 1..5 RETURN T::FAIL())? 71 `).Run(context.Background()) 72 73 So(err, ShouldBeNil) 74 So(string(out1), ShouldEqual, `null`) 75 76 out2, err := c.MustCompile(` 77 LET foo = TRUE 78 RETURN foo ? (FOR i IN 1..5 RETURN T::FAIL())? : (FOR i IN 1..5 RETURN T::FAIL()) 79 `).Run(context.Background()) 80 81 So(err, ShouldBeNil) 82 So(string(out2), ShouldEqual, `null`) 83 }) 84 85 Convey("LET res = foo ? TRUE : (FOR i IN 1..5 RETURN i*2)", t, func() { 86 c := compiler.New() 87 88 out1, err := c.MustCompile(` 89 LET foo = FALSE 90 LET res = foo ? TRUE : (FOR i IN 1..5 RETURN i*2) 91 RETURN res 92 `).Run(context.Background()) 93 94 So(err, ShouldBeNil) 95 So(string(out1), ShouldEqual, `[2,4,6,8,10]`) 96 97 out2, err := c.MustCompile(` 98 LET foo = TRUE 99 LET res = foo ? TRUE : (FOR i IN 1..5 RETURN i*2) 100 RETURN res 101 `).Run(context.Background()) 102 103 So(err, ShouldBeNil) 104 So(string(out2), ShouldEqual, `true`) 105 }) 106 107 Convey("LET res = foo ? (FOR i IN 1..5 RETURN i) : (FOR i IN 1..5 RETURN i*2)", t, func() { 108 c := compiler.New() 109 110 out1, err := c.MustCompile(` 111 LET foo = FALSE 112 LET res = foo ? (FOR i IN 1..5 RETURN i) : (FOR i IN 1..5 RETURN i*2) 113 RETURN res 114 `).Run(context.Background()) 115 116 So(err, ShouldBeNil) 117 So(string(out1), ShouldEqual, `[2,4,6,8,10]`) 118 119 out2, err := c.MustCompile(` 120 LET foo = TRUE 121 LET res = foo ? (FOR i IN 1..5 RETURN i) : (FOR i IN 1..5 RETURN i*2) 122 RETURN res 123 `).Run(context.Background()) 124 125 So(err, ShouldBeNil) 126 So(string(out2), ShouldEqual, `[1,2,3,4,5]`) 127 }) 128 129 Convey("LET res = (FOR i IN 1..5 RETURN T::FAIL())? ? TRUE : FALSE", t, func() { 130 c := compiler.New() 131 132 out1, err := c.MustCompile(` 133 LET res = (FOR i IN 1..5 RETURN T::FAIL())? ? TRUE : FALSE 134 RETURN res 135 `).Run(context.Background()) 136 137 So(err, ShouldBeNil) 138 So(string(out1), ShouldEqual, `false`) 139 140 out2, err := c.MustCompile(` 141 LET res = (FOR i IN 1..5 RETURN i)? ? TRUE : FALSE 142 RETURN res 143 `).Run(context.Background()) 144 145 So(err, ShouldBeNil) 146 So(string(out2), ShouldEqual, `true`) 147 }) 148 } 149 150 func BenchmarkForTernary(b *testing.B) { 151 p := compiler.New().MustCompile(` 152 LET foo = FALSE 153 RETURN foo ? TRUE : (FOR i IN 1..5 RETURN i*2) 154 `) 155 156 for n := 0; n < b.N; n++ { 157 p.Run(context.Background()) 158 } 159 }