surrealpatch/util/deep/deep_test.go

266 lines
4.7 KiB
Go
Raw Normal View History

2016-09-12 15:47:07 +00:00
package deep
import (
"time"
2016-09-12 15:47:07 +00:00
"testing"
. "github.com/smartystreets/goconvey/convey"
)
type Mini struct {
embedded bool
}
type Test struct {
PublicNumber int
PublicString string
PublicStruct interface{}
PublicPointr interface{}
PublicSlice []interface{}
PublicMap map[string]interface{}
privateNumber int
privateString string
privateStruct interface{}
privatePointr interface{}
privateSlice []interface{}
privateMap map[string]interface{}
}
func TestMain(t *testing.T) {
Convey("Can copy nil", t, func() {
var item interface{}
item = nil
done := Copy(item)
2019-11-15 13:22:51 +00:00
So(done, ShouldResemble, item)
2016-09-12 15:47:07 +00:00
})
}
func TestSimple(t *testing.T) {
Convey("Can copy bool", t, func() {
item := true
done := Copy(item)
2019-11-15 13:22:51 +00:00
So(done, ShouldResemble, item)
2016-09-12 15:47:07 +00:00
})
Convey("Can copy int64", t, func() {
item := int64(1)
done := Copy(item)
2019-11-15 13:22:51 +00:00
So(done, ShouldResemble, item)
2016-09-12 15:47:07 +00:00
})
Convey("Can copy float64", t, func() {
item := float64(1)
done := Copy(item)
2019-11-15 13:22:51 +00:00
So(done, ShouldResemble, item)
2016-09-12 15:47:07 +00:00
})
Convey("Can copy string", t, func() {
item := "string"
done := Copy(item)
2019-11-15 13:22:51 +00:00
So(done, ShouldResemble, item)
2016-09-12 15:47:07 +00:00
})
Convey("Can copy time.Time", t, func() {
item := time.Now()
done := Copy(item)
2019-11-15 13:22:51 +00:00
So(done, ShouldResemble, item)
})
2016-09-12 15:47:07 +00:00
}
func TestSlices(t *testing.T) {
Convey("Can copy slice of bools", t, func() {
item := []bool{
true,
false,
}
done := Copy(item)
2019-11-15 13:22:51 +00:00
So(done, ShouldResemble, item)
2016-09-12 15:47:07 +00:00
})
Convey("Can copy slice of int64s", t, func() {
item := []int64{
1,
2,
3,
}
done := Copy(item)
2019-11-15 13:22:51 +00:00
So(done, ShouldResemble, item)
2016-09-12 15:47:07 +00:00
})
Convey("Can copy slice of float64s", t, func() {
item := []float64{
1,
2,
3,
}
done := Copy(item)
2019-11-15 13:22:51 +00:00
So(done, ShouldResemble, item)
2016-09-12 15:47:07 +00:00
})
Convey("Can copy slice of strings", t, func() {
item := []string{
"str",
"str",
"str",
}
done := Copy(item)
2019-11-15 13:22:51 +00:00
So(done, ShouldResemble, item)
2016-09-12 15:47:07 +00:00
})
Convey("Can copy slice of time.Times", t, func() {
item := []time.Time{
time.Now(),
time.Now(),
time.Now(),
}
done := Copy(item)
2019-11-15 13:22:51 +00:00
So(done, ShouldResemble, item)
})
2016-09-12 15:47:07 +00:00
Convey("Can copy slice of interfaces", t, func() {
item := []interface{}{
nil,
int64(1),
float64(2),
"str",
time.Now(),
2016-09-12 15:47:07 +00:00
}
done := Copy(item)
2019-11-15 13:22:51 +00:00
So(done, ShouldResemble, item)
2016-09-12 15:47:07 +00:00
})
}
func TestObjects(t *testing.T) {
Convey("Can copy map of bools", t, func() {
item := map[string]bool{
"a": true,
"b": false,
}
done := Copy(item)
2019-11-15 13:22:51 +00:00
So(done, ShouldResemble, item)
2016-09-12 15:47:07 +00:00
})
Convey("Can copy map of int64s", t, func() {
item := map[string]int64{
"a": 1,
"b": 2,
"c": 3,
}
done := Copy(item)
2019-11-15 13:22:51 +00:00
So(done, ShouldResemble, item)
2016-09-12 15:47:07 +00:00
})
Convey("Can copy map of float64s", t, func() {
item := map[string]float64{
"a": 1,
"b": 2,
"c": 3,
}
done := Copy(item)
2019-11-15 13:22:51 +00:00
So(done, ShouldResemble, item)
2016-09-12 15:47:07 +00:00
})
Convey("Can copy map of strings", t, func() {
item := map[string]string{
"a": "str",
"b": "str",
"c": "str",
}
done := Copy(item)
2019-11-15 13:22:51 +00:00
So(done, ShouldResemble, item)
2016-09-12 15:47:07 +00:00
})
Convey("Can copy map of time.Times", t, func() {
item := map[string]time.Time{
"a": time.Now(),
"b": time.Now(),
"c": time.Now(),
}
done := Copy(item)
2019-11-15 13:22:51 +00:00
So(done, ShouldResemble, item)
})
2016-09-12 15:47:07 +00:00
Convey("Can copy map of interfaces", t, func() {
item := map[interface{}]interface{}{
1: nil,
"b": int64(1),
true: float64(2),
"d": "str",
"e": time.Now(),
2016-09-12 15:47:07 +00:00
}
done := Copy(item)
2019-11-15 13:22:51 +00:00
So(done, ShouldResemble, item)
2016-09-12 15:47:07 +00:00
})
}
func TestStructs(t *testing.T) {
full := Test{
PublicNumber: 1,
PublicString: "str",
PublicStruct: Mini{},
PublicPointr: &Mini{},
PublicSlice: []interface{}{nil, 1, "str", true},
PublicMap: map[string]interface{}{
"a": 1,
"b": "str",
"c": true,
"d": time.Now(),
2016-09-12 15:47:07 +00:00
},
privateNumber: 1,
privateString: "str",
privateStruct: Mini{},
privatePointr: &Mini{},
privateSlice: []interface{}{nil, 1, "str", true},
privateMap: map[string]interface{}{
"a": 1,
"b": "str",
"c": true,
"d": time.Now(),
2016-09-12 15:47:07 +00:00
},
}
show := Test{
PublicNumber: full.PublicNumber,
PublicString: full.PublicString,
PublicStruct: full.PublicStruct,
PublicPointr: full.PublicPointr,
PublicSlice: full.PublicSlice,
PublicMap: full.PublicMap,
}
Convey("Can copy struct", t, func() {
item := full
done := Copy(item)
2019-11-15 13:22:51 +00:00
So(show, ShouldResemble, done)
2016-09-12 15:47:07 +00:00
})
Convey("Can copy pointer", t, func() {
item := &full
done := Copy(item)
2019-11-15 13:22:51 +00:00
So(&show, ShouldResemble, done)
2016-09-12 15:47:07 +00:00
})
Convey("Can copy slice of structs", t, func() {
item := []interface{}{full, full}
done := Copy(item)
2019-11-15 13:22:51 +00:00
So([]interface{}{show, show}, ShouldResemble, done)
2016-09-12 15:47:07 +00:00
})
Convey("Can copy slice of pointers", t, func() {
item := []interface{}{&full, &full}
done := Copy(item)
2019-11-15 13:22:51 +00:00
So([]interface{}{&show, &show}, ShouldResemble, done)
2016-09-12 15:47:07 +00:00
})
}