mirror of
https://github.com/jefferyjob/go-easy-utils.git
synced 2025-11-03 03:03:19 +08:00
52
sliceUtil/extract_keys_test.go
Normal file
52
sliceUtil/extract_keys_test.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package sliceUtil
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestExtractKeysInline(t *testing.T) {
|
||||
// 使用匿名结构体定义测试数据
|
||||
persons := []struct {
|
||||
ID int
|
||||
Name string
|
||||
}{
|
||||
{1, "Alice"},
|
||||
{2, "Bob"},
|
||||
{3, "Charlie"},
|
||||
}
|
||||
|
||||
// 使用函数字面量定义 keyFunc 函数
|
||||
keys := ExtractKeys(persons, func(p struct {
|
||||
ID int
|
||||
Name string
|
||||
}) int {
|
||||
return p.ID
|
||||
})
|
||||
|
||||
expectedKeys := []int{1, 2, 3}
|
||||
if !reflect.DeepEqual(keys, expectedKeys) {
|
||||
t.Errorf("Expected keys %v, but got %v", expectedKeys, keys)
|
||||
}
|
||||
|
||||
// 另一个例子:使用不同的结构体和 keyFunc 函数
|
||||
animals := []struct {
|
||||
ID string
|
||||
Name string
|
||||
}{
|
||||
{"cat001", "Cat"},
|
||||
{"dog002", "Dog"},
|
||||
}
|
||||
|
||||
animalKeys := ExtractKeys(animals, func(a struct {
|
||||
ID string
|
||||
Name string
|
||||
}) string {
|
||||
return a.Name
|
||||
})
|
||||
|
||||
expectedAnimalKeys := []string{"Cat", "Dog"}
|
||||
if !reflect.DeepEqual(animalKeys, expectedAnimalKeys) {
|
||||
t.Errorf("Expected animal keys %v, but got %v", expectedAnimalKeys, animalKeys)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user