mirror of
https://github.com/MontFerret/ferret.git
synced 2025-11-06 08:39:09 +02:00
Bugfix/#97 array compare (#98)
* added some more unit tests for values.Array * fix values.Array.Compare method * added one more unit test
This commit is contained in:
@@ -3,9 +3,10 @@ package values
|
||||
import (
|
||||
"encoding/binary"
|
||||
"encoding/json"
|
||||
"hash/fnv"
|
||||
|
||||
"github.com/MontFerret/ferret/pkg/runtime/core"
|
||||
"github.com/pkg/errors"
|
||||
"hash/fnv"
|
||||
)
|
||||
|
||||
type (
|
||||
@@ -44,21 +45,27 @@ func (t *Array) String() string {
|
||||
func (t *Array) Compare(other core.Value) int {
|
||||
switch other.Type() {
|
||||
case core.ArrayType:
|
||||
arr := other.(*Array)
|
||||
other := other.(*Array)
|
||||
|
||||
if t.Length() == 0 && arr.Length() == 0 {
|
||||
if t.Length() == 0 && other.Length() == 0 {
|
||||
return 0
|
||||
}
|
||||
if t.Length() < other.Length() {
|
||||
return -1
|
||||
}
|
||||
if t.Length() > other.Length() {
|
||||
return 1
|
||||
}
|
||||
|
||||
var res = 1
|
||||
var res = 0
|
||||
var val core.Value
|
||||
|
||||
for _, val := range t.value {
|
||||
arr.ForEach(func(otherVal core.Value, idx int) bool {
|
||||
other.ForEach(func(otherVal core.Value, idx int) bool {
|
||||
val = t.Get(NewInt(idx))
|
||||
res = val.Compare(otherVal)
|
||||
|
||||
return res != -1
|
||||
return res == 0
|
||||
})
|
||||
}
|
||||
|
||||
return res
|
||||
case core.ObjectType:
|
||||
|
||||
@@ -2,10 +2,11 @@ package values_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/MontFerret/ferret/pkg/runtime/core"
|
||||
"github.com/MontFerret/ferret/pkg/runtime/values"
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestArray(t *testing.T) {
|
||||
@@ -117,6 +118,112 @@ func TestArray(t *testing.T) {
|
||||
|
||||
So(arr1.Compare(arr2), ShouldEqual, 1)
|
||||
})
|
||||
|
||||
Convey("It should return 0 when arrays are equal", func() {
|
||||
Convey("When only simple types are nested", func() {
|
||||
arr1 := values.NewArrayWith(
|
||||
values.NewInt(0), values.NewString("str"),
|
||||
)
|
||||
arr2 := values.NewArrayWith(
|
||||
values.NewInt(0), values.NewString("str"),
|
||||
)
|
||||
|
||||
So(arr1.Compare(arr2), ShouldEqual, 0)
|
||||
})
|
||||
|
||||
Convey("When object and array are nested at the same time", func() {
|
||||
arr1 := values.NewArrayWith(
|
||||
values.NewObjectWith(
|
||||
values.NewObjectProperty("one", values.NewInt(1)),
|
||||
),
|
||||
values.NewArrayWith(
|
||||
values.NewInt(2),
|
||||
),
|
||||
)
|
||||
arr2 := values.NewArrayWith(
|
||||
values.NewObjectWith(
|
||||
values.NewObjectProperty("one", values.NewInt(1)),
|
||||
),
|
||||
values.NewArrayWith(
|
||||
values.NewInt(2),
|
||||
),
|
||||
)
|
||||
|
||||
So(arr1.Compare(arr2), ShouldEqual, 0)
|
||||
})
|
||||
|
||||
Convey("When only objects are nested", func() {
|
||||
arr1 := values.NewArrayWith(
|
||||
values.NewObjectWith(
|
||||
values.NewObjectProperty("one", values.NewInt(1)),
|
||||
),
|
||||
)
|
||||
arr2 := values.NewArrayWith(
|
||||
values.NewObjectWith(
|
||||
values.NewObjectProperty("one", values.NewInt(1)),
|
||||
),
|
||||
)
|
||||
|
||||
So(arr1.Compare(arr2), ShouldEqual, 0)
|
||||
})
|
||||
|
||||
Convey("When only arrays are nested", func() {
|
||||
arr1 := values.NewArrayWith(
|
||||
values.NewArrayWith(
|
||||
values.NewInt(2),
|
||||
),
|
||||
)
|
||||
arr2 := values.NewArrayWith(
|
||||
values.NewArrayWith(
|
||||
values.NewInt(2),
|
||||
),
|
||||
)
|
||||
|
||||
So(arr1.Compare(arr2), ShouldEqual, 0)
|
||||
})
|
||||
|
||||
Convey("When simple and complex types at the same time", func() {
|
||||
arr1 := values.NewArrayWith(
|
||||
values.NewInt(0),
|
||||
values.NewObjectWith(
|
||||
values.NewObjectProperty("one", values.NewInt(1)),
|
||||
),
|
||||
values.NewArrayWith(
|
||||
values.NewInt(2),
|
||||
),
|
||||
)
|
||||
arr2 := values.NewArrayWith(
|
||||
values.NewInt(0),
|
||||
values.NewObjectWith(
|
||||
values.NewObjectProperty("one", values.NewInt(1)),
|
||||
),
|
||||
values.NewArrayWith(
|
||||
values.NewInt(2),
|
||||
),
|
||||
)
|
||||
|
||||
So(arr1.Compare(arr2), ShouldEqual, 0)
|
||||
})
|
||||
|
||||
Convey("When custom complex type", func() {
|
||||
arr1 := values.NewArrayWith(
|
||||
values.NewObjectWith(
|
||||
values.NewObjectProperty(
|
||||
"arr", values.NewArrayWith(values.NewObject()),
|
||||
),
|
||||
),
|
||||
)
|
||||
arr2 := values.NewArrayWith(
|
||||
values.NewObjectWith(
|
||||
values.NewObjectProperty(
|
||||
"arr", values.NewArrayWith(values.NewObject()),
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
So(arr1.Compare(arr2), ShouldEqual, 0)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Convey(".Hash", t, func() {
|
||||
|
||||
Reference in New Issue
Block a user