170 lines
3.5 KiB
Go
170 lines
3.5 KiB
Go
package copier
|
|
|
|
import (
|
|
"fmt"
|
|
"reflect"
|
|
"slices"
|
|
"testing"
|
|
)
|
|
|
|
func TestSliceCopy(t *testing.T) {
|
|
type person struct {
|
|
Name string
|
|
Age int
|
|
}
|
|
|
|
src := []person{
|
|
{Name: "Alice", Age: 20},
|
|
{Name: "Bob", Age: 30},
|
|
}
|
|
|
|
dst := []person{}
|
|
|
|
if err := Copy(&dst, src); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// Check if the copy was successful
|
|
if len(dst) != len(src) {
|
|
t.Errorf("Expected %d elements, got %d", len(src), len(dst))
|
|
}
|
|
|
|
fmt.Println("dst:", dst)
|
|
}
|
|
|
|
func TestPtrSliceCopy(t *testing.T) {
|
|
type person struct {
|
|
Name string
|
|
Age int
|
|
}
|
|
|
|
src := []*person{
|
|
{Name: "Alice", Age: 20},
|
|
{Name: "Bob", Age: 30},
|
|
}
|
|
|
|
dst := []*person{}
|
|
|
|
if err := Copy(&dst, src); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// Check if the copy was successful
|
|
if len(dst) != len(src) {
|
|
t.Errorf("Expected %d elements, got %d", len(src), len(dst))
|
|
}
|
|
|
|
for i := range src {
|
|
if src[i].Name != dst[i].Name {
|
|
t.Errorf("Expected %s, got %s", src[i].Name, dst[i].Name)
|
|
}
|
|
if src[i].Age != dst[i].Age {
|
|
t.Errorf("Expected %d, got %d", src[i].Age, dst[i].Age)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestAndSlice(t *testing.T) {
|
|
src := []any{"developer", "golang", "backend"}
|
|
dst := []string{}
|
|
|
|
if err := Copy(&dst, src); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if !slices.Equal([]string{"developer", "golang", "backend"}, dst) {
|
|
t.Errorf("Expected %v, got %v", []string{"developer", "golang", "backend"}, dst)
|
|
}
|
|
}
|
|
|
|
func TestStructAppendSlice(t *testing.T) {
|
|
type person struct {
|
|
Name string
|
|
Age int
|
|
}
|
|
|
|
src := person{Name: "Alice", Age: 20}
|
|
|
|
dst := []person{
|
|
{Name: "Charlie", Age: 40},
|
|
}
|
|
|
|
if err := Copy(&dst, src); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if len(dst) != 2 {
|
|
t.Errorf("Expected %d elements, got %d", 2, len(dst))
|
|
}
|
|
|
|
t.Log(dst)
|
|
}
|
|
|
|
func TestAppendSlice(t *testing.T) {
|
|
src := []string{"developer", "golang", "backend"}
|
|
dst := []any{"do"}
|
|
|
|
if err := Copy(&dst, src); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
t.Log(dst)
|
|
|
|
if !slices.Equal([]any{"do", "developer", "golang", "backend"}, dst) {
|
|
t.Errorf("Expected %v, got %v", []string{"do", "developer", "golang", "backend"}, dst)
|
|
}
|
|
|
|
}
|
|
|
|
func TestCopySliceOfDifferentTypes(t *testing.T) {
|
|
var ss []string
|
|
var is []int
|
|
if err := Copy(&ss, is); err != nil {
|
|
t.Error(err)
|
|
}
|
|
var anotherSs []string
|
|
if !reflect.DeepEqual(ss, anotherSs) {
|
|
t.Errorf("Copy nil slice to nil slice should get nil slice")
|
|
}
|
|
|
|
t.Log(ss)
|
|
}
|
|
|
|
func TestCopyFromStructToSlice(t *testing.T) {
|
|
user := User{Name: "Jinzhu", Age: 18, Role: "Admin", Notes: []string{"hello world"}}
|
|
employees := []Employee{}
|
|
|
|
if err := Copy(employees, &user); err != nil && len(employees) != 0 {
|
|
t.Errorf("Copy to unaddressable value should get error")
|
|
}
|
|
|
|
if Copy(&employees, &user); len(employees) != 1 {
|
|
t.Errorf("Should only have one elem when copy struct to slice")
|
|
} else {
|
|
checkEmployee(employees[0], user, t, "Copy From Struct To Slice Ptr")
|
|
}
|
|
|
|
t.Log("ssss", employees)
|
|
|
|
employees2 := &[]Employee{}
|
|
if Copy(&employees2, user); len(*employees2) != 1 {
|
|
t.Errorf("Should only have one elem when copy struct to slice")
|
|
} else {
|
|
checkEmployee((*employees2)[0], user, t, "Copy From Struct To Double Slice Ptr")
|
|
}
|
|
|
|
employees3 := []*Employee{}
|
|
if Copy(&employees3, user); len(employees3) != 1 {
|
|
t.Errorf("Should only have one elem when copy struct to slice")
|
|
} else {
|
|
checkEmployee(*(employees3[0]), user, t, "Copy From Struct To Ptr Slice Ptr")
|
|
}
|
|
|
|
employees4 := &[]*Employee{}
|
|
if Copy(&employees4, user); len(*employees4) != 1 {
|
|
t.Errorf("Should only have one elem when copy struct to slice")
|
|
} else {
|
|
checkEmployee(*((*employees4)[0]), user, t, "Copy From Struct To Double Ptr Slice Ptr")
|
|
}
|
|
}
|