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

162 lines
3.0 KiB
Go
Raw Normal View History

2018-09-18 22:42:38 +02:00
package collections_test
import (
2018-10-28 07:45:26 +02:00
"context"
2018-09-18 22:42:38 +02:00
"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 TestLimit(t *testing.T) {
Convey("Should limit iteration", t, func() {
arr := []core.Value{
values.NewInt(1),
values.NewInt(2),
values.NewInt(3),
values.NewInt(4),
values.NewInt(5),
}
2018-10-28 07:45:26 +02:00
iter, err := collections.NewLimitIterator(
sliceIterator(arr),
2018-09-18 22:42:38 +02:00
1,
0,
)
So(err, ShouldBeNil)
2018-10-28 07:45:26 +02:00
ctx := context.Background()
scope, _ := core.NewRootScope()
2018-09-18 22:42:38 +02:00
2018-10-28 07:45:26 +02:00
res, err := collections.ToSlice(ctx, scope, iter)
2018-09-18 22:42:38 +02:00
2018-10-28 07:45:26 +02:00
So(err, ShouldBeNil)
2018-09-18 22:42:38 +02:00
So(len(res), ShouldEqual, 1)
})
Convey("Should limit iteration (2)", t, func() {
arr := []core.Value{
values.NewInt(1),
values.NewInt(2),
values.NewInt(3),
values.NewInt(4),
values.NewInt(5),
}
2018-10-28 07:45:26 +02:00
iter, err := collections.NewLimitIterator(
sliceIterator(arr),
2018-09-18 22:42:38 +02:00
2,
0,
)
So(err, ShouldBeNil)
2018-10-28 07:45:26 +02:00
ctx := context.Background()
scope, _ := core.NewRootScope()
2018-09-18 22:42:38 +02:00
2018-10-28 07:45:26 +02:00
res, err := collections.ToSlice(ctx, scope, iter)
2018-09-18 22:42:38 +02:00
2018-10-28 07:45:26 +02:00
So(err, ShouldBeNil)
2018-09-18 22:42:38 +02:00
So(len(res), ShouldEqual, 2)
})
Convey("Should limit iteration with offset", t, func() {
arr := []core.Value{
values.NewInt(1),
values.NewInt(2),
values.NewInt(3),
values.NewInt(4),
values.NewInt(5),
}
offset := 2
2018-10-28 07:45:26 +02:00
iter, err := collections.NewLimitIterator(
sliceIterator(arr),
2018-09-18 22:42:38 +02:00
2,
offset,
)
So(err, ShouldBeNil)
2018-10-28 07:45:26 +02:00
ctx := context.Background()
scope, _ := core.NewRootScope()
2018-09-18 22:42:38 +02:00
2018-10-28 07:45:26 +02:00
res, err := collections.ToSlice(ctx, scope, iter)
2018-09-18 22:42:38 +02:00
2018-10-28 07:45:26 +02:00
So(err, ShouldBeNil)
2018-09-18 22:42:38 +02:00
So(len(res), ShouldEqual, 2)
2018-10-28 07:45:26 +02:00
for idx, nextScope := range res {
2018-09-18 22:42:38 +02:00
expected := arr[idx+offset]
2018-10-28 07:45:26 +02:00
current := nextScope.MustGetVariable(collections.DefaultValueVar)
2018-09-18 22:42:38 +02:00
So(expected, ShouldEqual, current)
}
})
Convey("Should limit iteration with offset at the end", t, func() {
arr := []core.Value{
values.NewInt(1),
values.NewInt(2),
values.NewInt(3),
values.NewInt(4),
values.NewInt(5),
}
offset := 3
2018-10-28 07:45:26 +02:00
iter, err := collections.NewLimitIterator(
sliceIterator(arr),
2018-09-18 22:42:38 +02:00
2,
offset,
)
So(err, ShouldBeNil)
2018-10-28 07:45:26 +02:00
ctx := context.Background()
scope, _ := core.NewRootScope()
2018-09-18 22:42:38 +02:00
2018-10-28 07:45:26 +02:00
res, err := collections.ToSlice(ctx, scope, iter)
2018-09-18 22:42:38 +02:00
2018-10-28 07:45:26 +02:00
So(err, ShouldBeNil)
2018-09-18 22:42:38 +02:00
So(len(res), ShouldEqual, 2)
2018-10-28 07:45:26 +02:00
for idx, nextScope := range res {
2018-09-18 22:42:38 +02:00
expected := arr[idx+offset]
2018-10-28 07:45:26 +02:00
current := nextScope.MustGetVariable(collections.DefaultValueVar)
2018-09-18 22:42:38 +02:00
So(expected, ShouldEqual, current)
}
})
Convey("Should limit iteration with offset with going out of bounds", t, func() {
arr := []core.Value{
values.NewInt(1),
values.NewInt(2),
values.NewInt(3),
values.NewInt(4),
values.NewInt(5),
}
offset := 4
2018-10-28 07:45:26 +02:00
iter, err := collections.NewLimitIterator(
sliceIterator(arr),
2018-09-18 22:42:38 +02:00
2,
offset,
)
So(err, ShouldBeNil)
2018-10-28 07:45:26 +02:00
ctx := context.Background()
scope, _ := core.NewRootScope()
2018-09-18 22:42:38 +02:00
2018-10-28 07:45:26 +02:00
res, err := collections.ToSlice(ctx, scope, iter)
2018-09-18 22:42:38 +02:00
2018-10-28 07:45:26 +02:00
So(err, ShouldBeNil)
2018-09-18 22:42:38 +02:00
So(len(res), ShouldEqual, 1)
})
}