mirror of
https://github.com/jefferyjob/go-easy-utils.git
synced 2025-10-05 15:06:50 +08:00
59 lines
1.0 KiB
Go
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)
|
|
}
|
|
})
|
|
}
|
|
}
|