github.com/MontFerret/ferret@v0.18.0/pkg/compiler/compiler_param_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 "github.com/MontFerret/ferret/pkg/runtime" 11 ) 12 13 func TestParam(t *testing.T) { 14 Convey("Should be possible to use as a return value", t, func() { 15 out := compiler.New(). 16 MustCompile(` 17 RETURN @param 18 `). 19 MustRun(context.Background(), runtime.WithParam("param", "foobar")) 20 21 So(string(out), ShouldEqual, `"foobar"`) 22 }) 23 24 Convey("Should be possible to use as a FOR source", t, func() { 25 out := compiler.New(). 26 MustCompile(` 27 FOR i IN @values 28 SORT i 29 RETURN i 30 `). 31 MustRun(context.Background(), runtime.WithParam("values", []int{1, 2, 3, 4})) 32 33 So(string(out), ShouldEqual, `[1,2,3,4]`) 34 35 out2 := compiler.New(). 36 MustCompile(` 37 FOR i IN @values 38 SORT i 39 RETURN i 40 `). 41 MustRun(context.Background(), runtime.WithParam("values", map[string]int{ 42 "foo": 1, 43 "bar": 2, 44 "faz": 3, 45 "qaz": 4, 46 })) 47 48 So(string(out2), ShouldEqual, `[1,2,3,4]`) 49 }) 50 51 Convey("Should be possible to use in range", t, func() { 52 prog := compiler.New(). 53 MustCompile(` 54 FOR i IN @start..@end 55 SORT i 56 RETURN i 57 `) 58 59 out := prog.MustRun( 60 context.Background(), 61 runtime.WithParam("start", 1), 62 runtime.WithParam("end", 4), 63 ) 64 65 So(string(out), ShouldEqual, `[1,2,3,4]`) 66 67 }) 68 69 Convey("Should be possible to use in member expression", t, func() { 70 prog := compiler.New(). 71 MustCompile(` 72 RETURN @param.value 73 `) 74 75 out := prog.MustRun( 76 context.Background(), 77 runtime.WithParam("param", map[string]interface{}{ 78 "value": "foobar", 79 }), 80 ) 81 82 So(string(out), ShouldEqual, `"foobar"`) 83 84 }) 85 86 Convey("Should be possible to use in member expression as a computed property", t, func() { 87 prog := compiler.New(). 88 MustCompile(` 89 LET obj = { foo: "bar" } 90 RETURN obj[@param] 91 `) 92 93 out := prog.MustRun( 94 context.Background(), 95 runtime.WithParam("param", "foo"), 96 ) 97 98 So(string(out), ShouldEqual, `"bar"`) 99 }) 100 101 Convey("Should be possible to use in member expression as segments", t, func() { 102 prog := compiler.New(). 103 MustCompile(` 104 LET doc = { foo: { bar: "baz" } } 105 106 RETURN doc.@attr.@subattr 107 `) 108 109 out := prog.MustRun( 110 context.Background(), 111 runtime.WithParam("attr", "foo"), 112 runtime.WithParam("subattr", "bar"), 113 ) 114 115 So(string(out), ShouldEqual, `"baz"`) 116 }) 117 118 Convey("Should return an error if param values are not passed", t, func() { 119 prog := compiler.New(). 120 MustCompile(` 121 LET doc = { foo: { bar: "baz" } } 122 123 RETURN doc.@attr.@subattr 124 `) 125 126 _, err := prog.Run( 127 context.Background(), 128 ) 129 130 So(err, ShouldNotBeNil) 131 So(err.Error(), ShouldContainSubstring, runtime.ErrMissedParam.Error()) 132 }) 133 134 Convey("Should be possible to use in member expression as segments", t, func() { 135 prog := compiler.New(). 136 MustCompile(` 137 LET doc = { foo: { bar: "baz" } } 138 139 RETURN doc.@attr.@subattr 140 `) 141 142 _, err := prog.Run( 143 context.Background(), 144 runtime.WithParam("attr", "foo"), 145 ) 146 147 So(err, ShouldNotBeNil) 148 So(err.Error(), ShouldContainSubstring, "subattr") 149 }) 150 151 Convey("Should be possible to use in struct with nested struct which nil", t, func() { 152 type Some2 struct { 153 } 154 type Some struct { 155 Some2 *Some2 156 } 157 158 someObj := &Some{} 159 prog := compiler.New(). 160 MustCompile(` 161 162 RETURN null 163 `) 164 165 panics := func() { 166 _, _ = prog.Run( 167 context.Background(), 168 runtime.WithParam("struct", someObj), 169 ) 170 } 171 172 So(panics, ShouldNotPanic) 173 }) 174 }