1
0
mirror of https://github.com/MontFerret/ferret.git synced 2025-01-16 03:21:03 +02:00

Added unit tests for Setter and Getter interfaces

This commit is contained in:
Tim Voronov 2018-12-24 10:48:34 -05:00
parent a99eb219af
commit e535f602d5
2 changed files with 153 additions and 1 deletions

View File

@ -160,7 +160,7 @@ func SetIn(to core.Value, byPath []core.Value, value core.Value) error {
setter, ok := parent.(core.Setter)
if ok {
return setter.SetIn(byPath[idx:0], value)
return setter.SetIn(byPath[idx:], value)
}
// redefine parent

View File

@ -0,0 +1,152 @@
package values_test
import (
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
type CustomType struct {
properties map[core.Value]core.Value
}
func (t *CustomType) MarshalJSON() ([]byte, error) {
return nil, core.ErrNotImplemented
}
func (t *CustomType) Type() core.Type {
return core.CustomType
}
func (t *CustomType) String() string {
return ""
}
func (t *CustomType) Compare(other core.Value) int {
return other.Compare(t) * -1
}
func (t *CustomType) Unwrap() interface{} {
return t
}
func (t *CustomType) Hash() uint64 {
return 0
}
func (t *CustomType) Copy() core.Value {
return values.None
}
func (t *CustomType) GetIn(path []core.Value) (core.Value, error) {
if path == nil || len(path) == 0 {
return values.None, nil
}
propKey := path[0]
propValue, ok := t.properties[propKey]
if !ok {
return values.None, nil
}
if len(path) == 1 {
return propValue, nil
}
return values.GetIn(propValue, path[1:])
}
func (t *CustomType) SetIn(path []core.Value, value core.Value) error {
if path == nil || len(path) == 0 {
return nil
}
propKey := path[0]
propValue, ok := t.properties[propKey]
if !ok {
return nil
}
if len(path) == 1 {
t.properties[propKey] = value
return nil
}
return values.SetIn(propValue, path[1:], value)
}
func TestHelpers(t *testing.T) {
Convey("Helpers", t, func() {
Convey("Getter", func() {
Convey("It should get a value by a given path", func() {
ct := &CustomType{
properties: map[core.Value]core.Value{
values.NewString("foo"): values.NewInt(1),
values.NewString("bar"): &CustomType{
properties: map[core.Value]core.Value{
values.NewString("qaz"): values.NewInt(2),
},
},
},
}
foo, err := values.GetIn(ct, []core.Value{
values.NewString("foo"),
})
So(err, ShouldBeNil)
So(foo, ShouldEqual, values.NewInt(1))
qaz, err := values.GetIn(ct, []core.Value{
values.NewString("bar"),
values.NewString("qaz"),
})
So(err, ShouldBeNil)
So(qaz, ShouldEqual, values.NewInt(2))
})
})
Convey("Setter", func() {
Convey("It should get a value by a given path", func() {
ct := &CustomType{
properties: map[core.Value]core.Value{
values.NewString("foo"): values.NewInt(1),
values.NewString("bar"): &CustomType{
properties: map[core.Value]core.Value{
values.NewString("qaz"): values.NewInt(2),
},
},
},
}
err := values.SetIn(ct, []core.Value{
values.NewString("foo"),
}, values.NewInt(2))
So(err, ShouldBeNil)
So(ct.properties[values.NewString("foo")], ShouldEqual, values.NewInt(2))
err = values.SetIn(ct, []core.Value{
values.NewString("bar"),
values.NewString("qaz"),
}, values.NewString("foobar"))
So(err, ShouldBeNil)
qaz, err := values.GetIn(ct, []core.Value{
values.NewString("bar"),
values.NewString("qaz"),
})
So(err, ShouldBeNil)
So(qaz, ShouldEqual, values.NewString("foobar"))
})
})
})
}