1
0
mirror of https://github.com/MontFerret/ferret.git synced 2024-12-14 11:23:02 +02:00
ferret/pkg/compiler/namespace_test.go
Tim Voronov 22382a0f61
Added namespace builder (#336)
* Added namespace builder

* Fixed linting issues

* Added extra check

* Updated e2e lib

* Renamed NamespaceBuilder to NamespaceContainer and changed func receivers

* Renamed NewLib to RegisterLib
2019-07-22 17:21:20 -04:00

97 lines
2.5 KiB
Go

package compiler_test
import (
"context"
"github.com/MontFerret/ferret/pkg/compiler"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
. "github.com/smartystreets/goconvey/convey"
"testing"
)
func TestNamespaceBuilder(t *testing.T) {
Convey("Namespaces", t, func() {
Convey("Should return an error when a function name contains NS separator", func() {
c := compiler.New()
err := c.RegisterFunction("T::SPY", func(ctx context.Context, args ...core.Value) (value core.Value, e error) {
return values.None, nil
})
So(err, ShouldNotBeNil)
})
Convey("Should successfully register a name within a namespace", func() {
c := compiler.New()
err := c.Namespace("T").RegisterFunction("SPY", func(ctx context.Context, args ...core.Value) (value core.Value, e error) {
return values.None, nil
})
So(err, ShouldBeNil)
funcs := c.RegisteredFunctions()
var exists bool
for _, name := range funcs {
exists = name == "T::SPY"
if exists {
break
}
}
So(exists, ShouldBeTrue)
})
Convey("Root namespace should return all registered functions", func() {
c := compiler.New()
err := c.Namespace("T").RegisterFunction("SPY", func(ctx context.Context, args ...core.Value) (value core.Value, e error) {
return values.None, nil
})
So(err, ShouldBeNil)
funcs := c.RegisteredFunctions()
So(len(funcs), ShouldBeGreaterThan, 1)
})
Convey("Namespace should return all registered functions", func() {
c := compiler.New()
err := c.Namespace("T").RegisterFunction("SPY", func(ctx context.Context, args ...core.Value) (value core.Value, e error) {
return values.None, nil
})
So(err, ShouldBeNil)
err = c.Namespace("T").Namespace("UTILS").RegisterFunction("SPY", func(ctx context.Context, args ...core.Value) (value core.Value, e error) {
return values.None, nil
})
So(err, ShouldBeNil)
funcs := c.Namespace("T").RegisteredFunctions()
So(funcs, ShouldHaveLength, 2)
funcs2 := c.Namespace("T").Namespace("UTILS").RegisteredFunctions()
So(funcs2, ShouldHaveLength, 1)
})
Convey("Namespace should return an error if namespace name is incorrect", func() {
c := compiler.New()
noop := func(ctx context.Context, args ...core.Value) (value core.Value, e error) {
return values.None, nil
}
err := c.Namespace("T::").RegisterFunction("SPY", noop)
So(err, ShouldNotBeNil)
err = c.Namespace("@F").RegisterFunction("SPY", noop)
So(err, ShouldNotBeNil)
})
})
}