1
0
mirror of https://github.com/MontFerret/ferret.git synced 2025-07-17 01:32:22 +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

40
pkg/stdlib/objects/has.go Normal file
View File

@ -0,0 +1,40 @@
package objects
import (
"context"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
)
/*
* Returns the value stored by the given key.
* @params (String) - The key name string.
* @returns (Boolean) - True if the key exists else false.
*/
func Has(_ context.Context, args ...core.Value) (core.Value, error) {
err := core.ValidateArgs(args, 2, 2)
if err != nil {
return values.None, err
}
err = core.ValidateType(args[0], core.ObjectType)
if err != nil {
return values.None, err
}
err = core.ValidateType(args[1], core.StringType)
if err != nil {
return values.None, err
}
obj := args[0].(*values.Object)
keyName := args[1].(values.String)
_, has := obj.Get(keyName)
return has, nil
}

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)
})
}

View File

@ -0,0 +1,46 @@
package objects
import (
"context"
"sort"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
)
func Keys(_ context.Context, args ...core.Value) (core.Value, error) {
err := core.ValidateArgs(args, 1, 2)
if err != nil {
return values.None, err
}
err = core.ValidateType(args[0], core.ObjectType)
if err != nil {
return values.None, err
}
obj := args[0].(*values.Object)
needSort := false
if len(args) == 2 {
err = core.ValidateType(args[1], core.BooleanType)
if err != nil {
return values.None, err
}
needSort = bool(args[1].(values.Boolean))
}
keys := sort.StringSlice(obj.Keys())
keysArray := values.NewArray(len(keys))
if needSort {
keys.Sort()
}
for _, key := range keys {
keysArray.Push(values.NewString(key))
}
return keysArray, nil
}

View File

@ -0,0 +1,93 @@
package objects_test
import (
"context"
"testing"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
"github.com/MontFerret/ferret/pkg/stdlib/objects"
. "github.com/smartystreets/goconvey/convey"
)
func TestKeys(t *testing.T) {
Convey("Keys(obj, false) should return 'a', 'c', 'b' in any order", t, func() {
obj := values.NewObjectWith(
values.NewObjectProperty("a", values.NewInt(0)),
values.NewObjectProperty("b", values.NewInt(1)),
values.NewObjectProperty("c", values.NewInt(2)),
)
keys, err := objects.Keys(context.Background(), obj)
keysArray := keys.(*values.Array)
So(err, ShouldEqual, nil)
So(keysArray.Type(), ShouldEqual, core.ArrayType)
So(keysArray.Length(), ShouldEqual, 3)
for _, k := range []string{"b", "a", "c"} {
iof := keysArray.IndexOf(values.NewString(k))
So(iof, ShouldNotEqual, -1)
}
})
Convey("Keys(obj, false) should return ['a', 'b', 'c']", t, func() {
obj := values.NewObjectWith(
values.NewObjectProperty("b", values.NewInt(0)),
values.NewObjectProperty("a", values.NewInt(1)),
values.NewObjectProperty("c", values.NewInt(3)),
)
keys, err := objects.Keys(context.Background(), obj, values.NewBoolean(true))
keysArray := keys.(*values.Array)
So(err, ShouldEqual, nil)
for idx, key := range []string{"a", "b", "c"} {
So(keysArray.Get(values.NewInt(idx)), ShouldEqual, values.NewString(key))
}
})
Convey("When there are no keys", t, func() {
obj := values.NewObject()
keys, err := objects.Keys(context.Background(), obj, values.NewBoolean(true))
keysArray := keys.(*values.Array)
So(err, ShouldEqual, nil)
So(keysArray.Length(), ShouldEqual, values.NewInt(0))
So(int(keysArray.Length()), ShouldEqual, 0)
keys, err = objects.Keys(context.Background(), obj, values.NewBoolean(false))
keysArray = keys.(*values.Array)
So(err, ShouldEqual, nil)
So(keysArray.Length(), ShouldEqual, values.NewInt(0))
So(int(keysArray.Length()), ShouldEqual, 0)
})
Convey("When not enought arguments", t, func() {
_, err := objects.Keys(context.Background())
So(err, ShouldBeError)
})
Convey("When first argument isn't object", t, func() {
notObj := values.NewInt(0)
_, err := objects.Keys(context.Background(), notObj)
So(err, ShouldBeError)
})
Convey("When second argument isn't boolean", t, func() {
obj := values.NewObject()
_, err := objects.Keys(context.Background(), obj, obj)
So(err, ShouldBeError)
})
}