Files
go-easy-utils/sliceUtil/column_slice_test.go
jeffery 9a8c60b30a Feature/generic type (#19)
Supports generics and any
2023-04-07 19:21:05 +08:00

59 lines
1.0 KiB
Go

package sliceUtil
import (
"reflect"
"testing"
)
//func TestColumnSliceDemo(t *testing.T) {
// type Person struct {
// Name string
// Age int
// }
// people := []Person{
// {"Alice", 18},
// {"Bob", 20},
// {"Charlie", 22},
// }
//
// // 获取年龄列
// ages := ColumnSlice(people, "Age")
// fmt.Println(ages) // 输出:[18 20 22]
//}
func TestColumnSlice(t *testing.T) {
type Person struct {
Name string
Age int
}
testCases := []struct {
name string
input []Person
column string
output []any
}{
{
name: "success",
input: []Person{{"Alice", 18}, {"Bob", 20}, {"Charlie", 22}},
column: "Age",
output: []any{18, 20, 22},
},
{
name: "column not found",
input: []Person{{"Alice", 18}, {"Bob", 20}, {"Charlie", 22}},
column: "Gender",
output: []any{},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
output := ColumnSlice(tc.input, tc.column)
if !reflect.DeepEqual(output, tc.output) {
t.Errorf("expected %v, but got %v", tc.output, output)
}
})
}
}