mirror of
https://github.com/MontFerret/ferret.git
synced 2025-07-07 00:56:53 +02:00
Feature/#95 deepclone (#101)
* rename method Clone to Copy * added Cloneable interface * added Value to Cloneable interface * implemented Cloneable intefrace by array * implemented Cloneable interface by Object * unit tests for Object.Clone * move core.IsCloneable to value.go * change Clone function * move IsClonable to package values
This commit is contained in:
@ -502,4 +502,62 @@ func TestArray(t *testing.T) {
|
||||
So(arr.Get(0), ShouldEqual, 1)
|
||||
})
|
||||
})
|
||||
|
||||
Convey(".Clone", t, func() {
|
||||
Convey("Cloned array should be equal to source array", func() {
|
||||
arr := values.NewArrayWith(
|
||||
values.NewInt(0),
|
||||
values.NewObjectWith(
|
||||
values.NewObjectProperty("one", values.NewInt(1)),
|
||||
),
|
||||
values.NewArrayWith(
|
||||
values.NewInt(2),
|
||||
),
|
||||
)
|
||||
|
||||
clone := arr.Clone().(*values.Array)
|
||||
|
||||
So(arr.Length(), ShouldEqual, clone.Length())
|
||||
So(arr.Compare(clone), ShouldEqual, 0)
|
||||
})
|
||||
|
||||
Convey("Cloned array should be independent of the source array", func() {
|
||||
arr := values.NewArrayWith(
|
||||
values.NewInt(0),
|
||||
values.NewInt(1),
|
||||
values.NewInt(2),
|
||||
values.NewInt(3),
|
||||
values.NewInt(4),
|
||||
values.NewInt(5),
|
||||
)
|
||||
|
||||
clone := arr.Clone().(*values.Array)
|
||||
|
||||
arr.Push(values.NewInt(6))
|
||||
|
||||
So(arr.Length(), ShouldNotEqual, clone.Length())
|
||||
So(arr.Compare(clone), ShouldNotEqual, 0)
|
||||
})
|
||||
|
||||
Convey("Cloned array must contain copies of the nested objects", func() {
|
||||
arr := values.NewArrayWith(
|
||||
values.NewArrayWith(
|
||||
values.NewInt(0),
|
||||
values.NewInt(1),
|
||||
values.NewInt(2),
|
||||
values.NewInt(3),
|
||||
values.NewInt(4),
|
||||
),
|
||||
)
|
||||
|
||||
clone := arr.Clone().(*values.Array)
|
||||
|
||||
nestedInArr := arr.Get(values.NewInt(0)).(*values.Array)
|
||||
nestedInArr.Push(values.NewInt(5))
|
||||
|
||||
nestedInClone := clone.Get(values.NewInt(0)).(*values.Array)
|
||||
|
||||
So(nestedInArr.Compare(nestedInClone), ShouldNotEqual, 0)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
Reference in New Issue
Block a user