44 lines
933 B
Go
44 lines
933 B
Go
package langext
|
|
|
|
import (
|
|
"git.blackforestbytes.com/BlackForestBytes/goext/tst"
|
|
"testing"
|
|
)
|
|
|
|
func TestDeepCopyByJsonStruct(t *testing.T) {
|
|
type item struct {
|
|
Name string `json:"name"`
|
|
Age int `json:"age"`
|
|
}
|
|
src := item{Name: "alice", Age: 30}
|
|
dst, err := DeepCopyByJson(src)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
tst.AssertEqual(t, dst.Name, "alice")
|
|
tst.AssertEqual(t, dst.Age, 30)
|
|
}
|
|
|
|
func TestDeepCopyByJsonSlice(t *testing.T) {
|
|
src := []int{1, 2, 3}
|
|
dst, err := DeepCopyByJson(src)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
tst.AssertArrayEqual(t, dst, []int{1, 2, 3})
|
|
|
|
// Mutating the copy must not affect the source
|
|
dst[0] = 99
|
|
tst.AssertEqual(t, src[0], 1)
|
|
}
|
|
|
|
func TestDeepCopyByJsonError(t *testing.T) {
|
|
type bad struct {
|
|
C chan int
|
|
}
|
|
_, err := DeepCopyByJson(bad{C: make(chan int)})
|
|
if err == nil {
|
|
t.Errorf("expected error for un-marshalable type")
|
|
}
|
|
}
|