github.com/MontFerret/ferret@v0.18.0/pkg/compiler/compiler_func_ns_test.go (about) 1 package compiler_test 2 3 import ( 4 "context" 5 "fmt" 6 "regexp" 7 "testing" 8 9 . "github.com/smartystreets/goconvey/convey" 10 11 "github.com/MontFerret/ferret/pkg/compiler" 12 "github.com/MontFerret/ferret/pkg/parser" 13 "github.com/MontFerret/ferret/pkg/runtime/core" 14 "github.com/MontFerret/ferret/pkg/runtime/values" 15 ) 16 17 func TestFunctionNSCall(t *testing.T) { 18 Convey("Should compile RETURN T::SPY", t, func() { 19 c := compiler.New() 20 21 var counter int 22 err := c.Namespace("T").RegisterFunction("SPY", func(_ context.Context, _ ...core.Value) (core.Value, error) { 23 counter++ 24 25 return values.None, nil 26 }) 27 28 So(err, ShouldBeNil) 29 30 p, err := c.Compile(` 31 RETURN T::SPY() 32 `) 33 34 So(err, ShouldBeNil) 35 36 _, err = p.Run(context.Background()) 37 38 So(err, ShouldBeNil) 39 40 So(counter, ShouldEqual, 1) 41 }) 42 43 Convey("Should compile RETURN T::UTILS::SPY", t, func() { 44 c := compiler.New() 45 46 var counter int 47 err := c.Namespace("T").Namespace("UTILS").RegisterFunction("SPY", func(_ context.Context, _ ...core.Value) (core.Value, error) { 48 counter++ 49 50 return values.None, nil 51 }) 52 53 So(err, ShouldBeNil) 54 55 p, err := c.Compile(` 56 RETURN T::UTILS::SPY() 57 `) 58 59 So(err, ShouldBeNil) 60 61 _, err = p.Run(context.Background()) 62 63 So(err, ShouldBeNil) 64 65 So(counter, ShouldEqual, 1) 66 }) 67 68 Convey("Should NOT compile RETURN T:UTILS::SPY", t, func() { 69 c := compiler.New() 70 71 var counter int 72 err := c.Namespace("T").Namespace("UTILS").RegisterFunction("SPY", func(_ context.Context, _ ...core.Value) (core.Value, error) { 73 counter++ 74 75 return values.None, nil 76 }) 77 78 So(err, ShouldBeNil) 79 80 _, err = c.Compile(` 81 RETURN T:UTILS::SPY() 82 `) 83 84 So(err, ShouldNotBeNil) 85 }) 86 87 Convey("T::FAIL()? should return NONE", t, func() { 88 c := compiler.New() 89 90 p, err := c.Compile(` 91 RETURN T::FAIL()? 92 `) 93 94 So(err, ShouldBeNil) 95 96 out, err := p.Run(context.Background()) 97 98 So(err, ShouldBeNil) 99 So(string(out), ShouldEqual, `null`) 100 }) 101 102 Convey("Should use keywords", t, func() { 103 p := parser.New("RETURN TRUE") 104 c := compiler.New() 105 106 r := regexp.MustCompile(`\w+`) 107 108 for _, l := range p.GetLiteralNames() { 109 if r.MatchString(l) { 110 kw := l[1 : len(l)-1] 111 112 segment := kw 113 err := c.Namespace("T").Namespace(segment).RegisterFunction("TEST", func(ctx context.Context, args ...core.Value) (core.Value, error) { 114 return values.True, nil 115 }) 116 117 So(err, ShouldBeNil) 118 119 err = c.Namespace("T").Namespace(segment).RegisterFunction(segment, func(ctx context.Context, args ...core.Value) (core.Value, error) { 120 return values.True, nil 121 }) 122 123 So(err, ShouldBeNil) 124 125 p, err := c.Compile(fmt.Sprintf(` 126 RETURN T::%s::TEST() 127 `, segment)) 128 129 So(err, ShouldBeNil) 130 131 out := p.MustRun(context.Background()) 132 133 So(string(out), ShouldEqual, "true") 134 135 p, err = c.Compile(fmt.Sprintf(` 136 RETURN T::%s::%s() 137 `, segment, segment)) 138 139 So(err, ShouldBeNil) 140 141 out = p.MustRun(context.Background()) 142 143 So(string(out), ShouldEqual, "true") 144 } 145 } 146 }) 147 148 }