2018-10-25 03:30:05 +02:00
|
|
|
package collections_test
|
|
|
|
|
|
|
|
import (
|
2018-10-28 07:45:26 +02:00
|
|
|
"context"
|
2018-10-25 03:30:05 +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 mapIterator(m map[string]core.Value) collections.Iterator {
|
2018-10-28 07:45:26 +02:00
|
|
|
iter, _ := collections.NewDefaultMapIterator(m)
|
|
|
|
|
|
|
|
return iter
|
2018-10-25 03:30:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMapIterator(t *testing.T) {
|
|
|
|
Convey("Should iterate over a map", t, func() {
|
|
|
|
m := map[string]core.Value{
|
|
|
|
"one": values.NewInt(1),
|
|
|
|
"two": values.NewInt(2),
|
|
|
|
"three": values.NewInt(3),
|
|
|
|
"four": values.NewInt(4),
|
|
|
|
"five": values.NewInt(5),
|
|
|
|
}
|
|
|
|
|
|
|
|
iter := mapIterator(m)
|
|
|
|
|
|
|
|
res := make([]core.Value, 0, len(m))
|
2018-10-28 07:45:26 +02:00
|
|
|
ctx := context.Background()
|
|
|
|
scope, _ := core.NewRootScope()
|
2018-10-25 03:30:05 +02:00
|
|
|
|
2018-10-28 07:45:26 +02:00
|
|
|
for {
|
|
|
|
nextScope, err := iter.Next(ctx, scope)
|
2018-10-25 03:30:05 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2018-10-25 03:30:05 +02:00
|
|
|
expected, exists := m[key.String()]
|
|
|
|
|
|
|
|
So(exists, ShouldBeTrue)
|
|
|
|
So(expected, ShouldEqual, item)
|
|
|
|
|
|
|
|
res = append(res, item)
|
|
|
|
}
|
|
|
|
|
|
|
|
So(res, ShouldHaveLength, len(m))
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Should return an error when exhausted", t, func() {
|
|
|
|
m := map[string]core.Value{
|
|
|
|
"one": values.NewInt(1),
|
|
|
|
"two": values.NewInt(2),
|
|
|
|
"three": values.NewInt(3),
|
|
|
|
"four": values.NewInt(4),
|
|
|
|
"five": values.NewInt(5),
|
|
|
|
}
|
|
|
|
|
|
|
|
iter := mapIterator(m)
|
2018-10-28 07:45:26 +02:00
|
|
|
ctx := context.Background()
|
|
|
|
scope, _ := core.NewRootScope()
|
2018-10-25 03:30:05 +02:00
|
|
|
|
2018-10-28 07:45:26 +02:00
|
|
|
_, err := collections.ToSlice(ctx, scope, iter)
|
|
|
|
So(err, ShouldBeNil)
|
2018-10-25 03:30:05 +02:00
|
|
|
|
2018-10-28 07:45:26 +02:00
|
|
|
item, err := iter.Next(ctx, scope)
|
2018-10-25 03:30:05 +02:00
|
|
|
|
|
|
|
So(item, ShouldBeNil)
|
2018-10-28 07:45:26 +02:00
|
|
|
So(err, ShouldBeNil)
|
2018-10-25 03:30:05 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Should NOT iterate over a empty map", t, func() {
|
|
|
|
m := make(map[string]core.Value)
|
|
|
|
|
|
|
|
iter := mapIterator(m)
|
|
|
|
|
2018-10-28 07:45:26 +02:00
|
|
|
ctx := context.Background()
|
|
|
|
scope, _ := core.NewRootScope()
|
2018-10-25 03:30:05 +02:00
|
|
|
|
2018-10-28 07:45:26 +02:00
|
|
|
item, err := iter.Next(ctx, scope)
|
2018-10-25 03:30:05 +02:00
|
|
|
|
2018-10-28 07:45:26 +02:00
|
|
|
So(item, ShouldBeNil)
|
|
|
|
So(err, ShouldBeNil)
|
2018-10-25 03:30:05 +02:00
|
|
|
})
|
|
|
|
}
|