1
0
mirror of https://github.com/MontFerret/ferret.git synced 2024-12-16 11:37:36 +02:00
ferret/pkg/stdlib/objects/has_test.go
3timeslazy 809a51b217 Added objects 'Has' and 'Keys' function (#69)
* add pkg/stdlib/objects Length function

* rename lenght.go -> length.go

* fix tests according to other tests

* add new tests to length tests

* delete objects method Length

* add objects method Has

* add objects function Keys

* small fixes in Keys and Has functions

* change Has function

* unit tests for Keys function
2018-10-07 17:53:41 -04:00

82 lines
2.0 KiB
Go

package objects_test
import (
"context"
"testing"
"github.com/MontFerret/ferret/pkg/runtime/values"
"github.com/MontFerret/ferret/pkg/stdlib/objects"
. "github.com/smartystreets/goconvey/convey"
)
func TestHas(t *testing.T) {
Convey("When key exists", t, func() {
obj := values.NewObjectWith(
values.NewObjectProperty("key", values.NewString("val")),
)
val, err := objects.Has(context.Background(), obj, values.NewString("key"))
valBool := val.(values.Boolean)
So(err, ShouldEqual, nil)
So(valBool, ShouldEqual, values.NewBoolean(true))
So(bool(valBool), ShouldEqual, true)
})
Convey("When key doesn't exists", t, func() {
obj := values.NewObjectWith(
values.NewObjectProperty("anyOtherKey", values.NewString("val")),
)
val, err := objects.Has(context.Background(), obj, values.NewString("key"))
valBool := val.(values.Boolean)
So(err, ShouldEqual, nil)
So(valBool, ShouldEqual, values.NewBoolean(false))
So(bool(valBool), ShouldEqual, false)
})
Convey("When there are no keys", t, func() {
obj := values.NewObject()
val, err := objects.Has(context.Background(), obj, values.NewString("key"))
valBool := val.(values.Boolean)
So(err, ShouldEqual, nil)
So(valBool, ShouldEqual, values.NewBoolean(false))
So(bool(valBool), ShouldEqual, false)
})
Convey("Not enought arguments", t, func() {
val, err := objects.Has(context.Background())
So(err, ShouldBeError)
So(val, ShouldEqual, values.None)
val, err = objects.Has(context.Background(), values.NewObject())
So(err, ShouldBeError)
So(val, ShouldEqual, values.None)
})
Convey("When keyName isn't string", t, func() {
obj := values.NewObject()
key := values.NewInt(1)
val, err := objects.Has(context.Background(), obj, key)
So(err, ShouldBeError)
So(val, ShouldEqual, values.None)
})
Convey("When first argument isn't object", t, func() {
notObj := values.NewInt(1)
val, err := objects.Has(context.Background(), notObj, values.NewString("key"))
So(err, ShouldBeError)
So(val, ShouldEqual, values.None)
})
}