Feature/cover unit test (#81)

This commit is contained in:
libin
2024-07-31 19:51:01 +08:00
committed by GitHub
parent 0990bbce4d
commit a59af60d9a
60 changed files with 2571 additions and 2606 deletions

View File

@@ -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)
})
}
}