Skip to content

Commit

Permalink
Bugfix/MontFerret#97 array compare (MontFerret#98)
Browse files Browse the repository at this point in the history
* added some more unit tests for values.Array

* fix values.Array.Compare method

* added one more unit test
  • Loading branch information
3timeslazy authored and ziflex committed Oct 11, 2018
1 parent b74e490 commit 54c9857
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 11 deletions.
27 changes: 17 additions & 10 deletions pkg/runtime/values/array.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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 {
res = val.Compare(otherVal)
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:
Expand Down
109 changes: 108 additions & 1 deletion pkg/runtime/values/array_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 54c9857

Please sign in to comment.