1
0
mirror of https://github.com/MontFerret/ferret.git synced 2024-12-12 11:15:14 +02:00

Bugfix/#89 compare (#93)

* change object method Compare

* added unit tests for Compare method

* changed Compare method

* fix Compare method
This commit is contained in:
3timeslazy 2018-10-10 20:04:15 +03:00 committed by Tim Voronov
parent 0bc4280d3f
commit b74e490624
2 changed files with 40 additions and 12 deletions

View File

@ -59,21 +59,32 @@ func (t *Object) String() string {
func (t *Object) Compare(other core.Value) int {
switch other.Type() {
case core.ObjectType:
arr := other.(*Object)
other := other.(*Object)
if t.Length() == 0 && arr.Length() == 0 {
if t.Length() == 0 && other.Length() == 0 {
return 0
}
var res = 1
for _, val := range t.value {
arr.ForEach(func(otherVal core.Value, key string) bool {
res = val.Compare(otherVal)
return res != -1
})
if t.Length() < other.Length() {
return -1
}
if t.Length() > other.Length() {
return 1
}
var res = 0
var val core.Value
var exists bool
other.ForEach(func(otherVal core.Value, key string) bool {
res = -1
if val, exists = t.value[key]; exists {
res = val.Compare(otherVal)
}
return res == 0
})
return res
default:

View File

@ -1,10 +1,11 @@
package values_test
import (
"testing"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
. "github.com/smartystreets/goconvey/convey"
"testing"
)
func TestObject(t *testing.T) {
@ -120,6 +121,22 @@ func TestObject(t *testing.T) {
So(obj1.Compare(obj2), ShouldEqual, 0)
})
Convey("It should return 0 when both objects are equal (independent of key order)", func() {
obj1 := values.NewObjectWith(
values.NewObjectProperty("foo", values.NewString("foo")),
values.NewObjectProperty("bar", values.NewString("bar")),
)
obj2 := values.NewObjectWith(
values.NewObjectProperty("foo", values.NewString("foo")),
values.NewObjectProperty("bar", values.NewString("bar")),
)
So(obj1.Compare(obj1), ShouldEqual, 0)
So(obj2.Compare(obj2), ShouldEqual, 0)
So(obj1.Compare(obj2), ShouldEqual, 0)
So(obj2.Compare(obj1), ShouldEqual, 0)
})
Convey("It should return 1 when other array is empty", func() {
obj1 := values.NewObjectWith(values.NewObjectProperty("foo", values.NewString("bar")))
obj2 := values.NewObject()