1
0
mirror of https://github.com/MontFerret/ferret.git synced 2025-07-15 01:25:00 +02:00

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
This commit is contained in:
3timeslazy
2018-10-08 00:53:41 +03:00
committed by Tim Voronov
parent 3a834d9a0e
commit 809a51b217
4 changed files with 260 additions and 0 deletions

View File

@ -0,0 +1,81 @@
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)
})
}