1
0
mirror of https://github.com/MontFerret/ferret.git synced 2024-12-16 11:37:36 +02:00
ferret/pkg/runtime/collections/keyed_test.go

97 lines
2.3 KiB
Go
Raw Normal View History

package collections_test
import (
2018-10-28 07:45:26 +02:00
"context"
"github.com/MontFerret/ferret/pkg/runtime/collections"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
. "github.com/smartystreets/goconvey/convey"
"testing"
)
func objectIterator(obj *values.Object) collections.Iterator {
2018-10-28 07:45:26 +02:00
iter, _ := collections.NewDefaultKeyedIterator(obj)
return iter
}
func TestObjectIterator(t *testing.T) {
Convey("Should iterate over a map", t, func() {
m := values.NewObjectWith(
values.NewObjectProperty("one", values.NewInt(1)),
values.NewObjectProperty("two", values.NewInt(2)),
values.NewObjectProperty("three", values.NewInt(3)),
values.NewObjectProperty("four", values.NewInt(4)),
values.NewObjectProperty("five", values.NewInt(5)),
)
iter := objectIterator(m)
res := make([]core.Value, 0, m.Length())
2018-10-28 07:45:26 +02:00
ctx := context.Background()
scope, _ := core.NewRootScope()
for {
nextScope, err := iter.Next(ctx, scope)
So(err, ShouldBeNil)
2018-10-28 07:45:26 +02:00
if nextScope == nil {
break
}
key := nextScope.MustGetVariable(collections.DefaultKeyVar)
item := nextScope.MustGetVariable(collections.DefaultValueVar)
expected, exists := m.Get(values.NewString(key.String()))
So(bool(exists), ShouldBeTrue)
So(expected, ShouldEqual, item)
res = append(res, item)
}
So(res, ShouldHaveLength, m.Length())
})
Convey("Should return an error when exhausted", t, func() {
m := values.NewObjectWith(
values.NewObjectProperty("one", values.NewInt(1)),
values.NewObjectProperty("two", values.NewInt(2)),
values.NewObjectProperty("three", values.NewInt(3)),
values.NewObjectProperty("four", values.NewInt(4)),
values.NewObjectProperty("five", values.NewInt(5)),
)
iter := objectIterator(m)
2018-10-28 07:45:26 +02:00
ctx := context.Background()
scope, _ := core.NewRootScope()
2018-10-28 07:45:26 +02:00
res, err := collections.ToSlice(ctx, scope, iter)
2018-10-28 07:45:26 +02:00
So(err, ShouldBeNil)
So(res, ShouldNotBeNil)
2018-10-28 07:45:26 +02:00
nextScope, err := iter.Next(ctx, scope)
2018-10-28 07:45:26 +02:00
So(nextScope, ShouldBeNil)
So(err, ShouldBeNil)
})
Convey("Should NOT iterate over a empty map", t, func() {
m := values.NewObject()
iter := objectIterator(m)
2018-10-28 07:45:26 +02:00
ctx := context.Background()
scope, _ := core.NewRootScope()
2018-10-28 07:45:26 +02:00
nextScope, err := iter.Next(ctx, scope)
2018-10-28 07:45:26 +02:00
So(nextScope, ShouldBeNil)
So(err, ShouldBeNil)
})
}