github.com/MontFerret/ferret@v0.18.0/pkg/compiler/namespace_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/core" 11 "github.com/MontFerret/ferret/pkg/runtime/values" 12 ) 13 14 func TestNamespaceBuilder(t *testing.T) { 15 Convey("Namespaces", t, func() { 16 Convey("Should return an error when a function name contains NS separator", func() { 17 c := compiler.New() 18 err := c.RegisterFunction("FOO::SPY", func(ctx context.Context, args ...core.Value) (value core.Value, e error) { 19 return values.None, nil 20 }) 21 22 So(err, ShouldNotBeNil) 23 }) 24 25 Convey("Should successfully register a name within a namespace", func() { 26 c := compiler.New() 27 err := c.Namespace("FOO").RegisterFunction("SPY", func(ctx context.Context, args ...core.Value) (value core.Value, e error) { 28 return values.None, nil 29 }) 30 31 So(err, ShouldBeNil) 32 33 funcs := c.RegisteredFunctions() 34 35 var exists bool 36 37 for _, name := range funcs { 38 exists = name == "FOO::SPY" 39 40 if exists { 41 break 42 } 43 } 44 45 So(exists, ShouldBeTrue) 46 }) 47 48 Convey("Root namespace should return all registered functions", func() { 49 c := compiler.New() 50 err := c.Namespace("FOO").RegisterFunction("SPY", func(ctx context.Context, args ...core.Value) (value core.Value, e error) { 51 return values.None, nil 52 }) 53 54 So(err, ShouldBeNil) 55 56 funcs := c.RegisteredFunctions() 57 58 So(len(funcs), ShouldBeGreaterThan, 1) 59 }) 60 61 Convey("Namespace should return all registered functions", func() { 62 c := compiler.New() 63 err := c.Namespace("FOO").RegisterFunction("SPY", func(ctx context.Context, args ...core.Value) (value core.Value, e error) { 64 return values.None, nil 65 }) 66 67 So(err, ShouldBeNil) 68 69 err = c.Namespace("FOO").Namespace("UTILS").RegisterFunction("SPY", func(ctx context.Context, args ...core.Value) (value core.Value, e error) { 70 return values.None, nil 71 }) 72 73 So(err, ShouldBeNil) 74 75 funcs := c.Namespace("FOO").RegisteredFunctions() 76 77 So(funcs, ShouldHaveLength, 2) 78 79 funcs2 := c.Namespace("FOO").Namespace("UTILS").RegisteredFunctions() 80 81 So(funcs2, ShouldHaveLength, 1) 82 }) 83 84 Convey("Namespace should return an error if namespace name is incorrect", func() { 85 c := compiler.New() 86 noop := func(ctx context.Context, args ...core.Value) (value core.Value, e error) { 87 return values.None, nil 88 } 89 err := c.Namespace("FOO::").RegisterFunction("SPY", noop) 90 91 So(err, ShouldNotBeNil) 92 93 err = c.Namespace("@F").RegisterFunction("SPY", noop) 94 95 So(err, ShouldNotBeNil) 96 }) 97 }) 98 }