mirror of
https://github.com/jefferyjob/go-easy-utils.git
synced 2025-10-07 16:00:51 +08:00
Feature/cover unit test (#81)
This commit is contained in:
@@ -1,26 +1,10 @@
|
||||
package sliceUtil
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"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
|
||||
@@ -28,31 +12,29 @@ func TestColumnSlice(t *testing.T) {
|
||||
}
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
input []Person
|
||||
column string
|
||||
output []any
|
||||
name string
|
||||
input []Person
|
||||
inputColumn string
|
||||
want []any
|
||||
}{
|
||||
{
|
||||
name: "success",
|
||||
input: []Person{{"Alice", 18}, {"Bob", 20}, {"Charlie", 22}},
|
||||
column: "Age",
|
||||
output: []any{18, 20, 22},
|
||||
name: "success",
|
||||
input: []Person{{"Alice", 18}, {"Bob", 20}, {"Charlie", 22}},
|
||||
inputColumn: "Age",
|
||||
want: []any{18, 20, 22},
|
||||
},
|
||||
{
|
||||
name: "column not found",
|
||||
input: []Person{{"Alice", 18}, {"Bob", 20}, {"Charlie", 22}},
|
||||
column: "Gender",
|
||||
output: []any{},
|
||||
name: "column not found",
|
||||
input: []Person{{"Alice", 18}, {"Bob", 20}, {"Charlie", 22}},
|
||||
inputColumn: "Gender",
|
||||
want: []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)
|
||||
}
|
||||
res := ColumnSlice(tc.input, tc.inputColumn)
|
||||
assert.Equal(t, tc.want, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user