mirror of
https://github.com/jefferyjob/go-easy-utils.git
synced 2025-10-18 21:04:30 +08:00
@@ -1,7 +1,6 @@
|
||||
package sliceUtil
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
@@ -18,11 +17,7 @@ import (
|
||||
// }
|
||||
//
|
||||
// // 获取年龄列
|
||||
// ages, err := ColumnSlice(people, "Age")
|
||||
// if err != nil {
|
||||
// fmt.Println(err)
|
||||
// return
|
||||
// }
|
||||
// ages := ColumnSlice(people, "Age")
|
||||
// fmt.Println(ages) // 输出:[18 20 22]
|
||||
//}
|
||||
|
||||
@@ -34,43 +29,30 @@ func TestColumnSlice(t *testing.T) {
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
input interface{}
|
||||
input []Person
|
||||
column string
|
||||
output []interface{}
|
||||
err error
|
||||
output []any
|
||||
}{
|
||||
{
|
||||
name: "success",
|
||||
input: []Person{{"Alice", 18}, {"Bob", 20}, {"Charlie", 22}},
|
||||
column: "Age",
|
||||
output: []interface{}{18, 20, 22},
|
||||
err: nil,
|
||||
},
|
||||
{
|
||||
name: "not a slice",
|
||||
input: Person{"Alice", 18},
|
||||
column: "Age",
|
||||
output: nil,
|
||||
err: errors.New("not a slice"),
|
||||
output: []any{18, 20, 22},
|
||||
},
|
||||
{
|
||||
name: "column not found",
|
||||
input: []Person{{"Alice", 18}, {"Bob", 20}, {"Charlie", 22}},
|
||||
column: "Gender",
|
||||
output: nil,
|
||||
err: errors.New("column not found"),
|
||||
output: []any{},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
output, err := ColumnSlice(tc.input, tc.column)
|
||||
output := ColumnSlice(tc.input, tc.column)
|
||||
if !reflect.DeepEqual(output, tc.output) {
|
||||
t.Errorf("expected %v, but got %v", tc.output, output)
|
||||
}
|
||||
if !reflect.DeepEqual(err, tc.err) {
|
||||
t.Errorf("expected %v, but got %v", tc.err, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user