mirror of
https://github.com/jefferyjob/go-easy-utils.git
synced 2025-10-05 15:06:50 +08:00
Feature/cover unit test (#81)
This commit is contained in:
@@ -6,57 +6,41 @@ import (
|
||||
)
|
||||
|
||||
func TestSliceToMap(t *testing.T) {
|
||||
// Test case 1: Test with a slice of Person structs
|
||||
type Person struct {
|
||||
type person struct {
|
||||
ID int
|
||||
Name string
|
||||
}
|
||||
|
||||
people := []Person{
|
||||
{ID: 1, Name: "Alice"},
|
||||
{ID: 2, Name: "Bob"},
|
||||
{ID: 3, Name: "Charlie"},
|
||||
testCases := []struct {
|
||||
name string
|
||||
inputSlices []person
|
||||
inputFunc func(p person) int
|
||||
want map[int]person
|
||||
}{
|
||||
{
|
||||
name: "以id为map的key",
|
||||
inputSlices: []person{
|
||||
{ID: 1, Name: "Alice"},
|
||||
{ID: 2, Name: "Bob"},
|
||||
{ID: 3, Name: "Charlie"},
|
||||
},
|
||||
inputFunc: func(p person) int {
|
||||
return p.ID
|
||||
},
|
||||
want: map[int]person{
|
||||
1: {ID: 1, Name: "Alice"},
|
||||
2: {ID: 2, Name: "Bob"},
|
||||
3: {ID: 3, Name: "Charlie"},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
keyFunc := func(p Person) int {
|
||||
return p.ID
|
||||
}
|
||||
|
||||
expected := map[int]Person{
|
||||
1: {ID: 1, Name: "Alice"},
|
||||
2: {ID: 2, Name: "Bob"},
|
||||
3: {ID: 3, Name: "Charlie"},
|
||||
}
|
||||
|
||||
result := SliceToMap(people, keyFunc)
|
||||
|
||||
if !reflect.DeepEqual(result, expected) {
|
||||
t.Errorf("Test case 1 failed. Expected %v, got %v", expected, result)
|
||||
}
|
||||
|
||||
// Test case 2: Test with an empty slice
|
||||
emptySlice := []Person{}
|
||||
expectedEmpty := map[int]Person{}
|
||||
|
||||
resultEmpty := SliceToMap(emptySlice, keyFunc)
|
||||
|
||||
if !reflect.DeepEqual(resultEmpty, expectedEmpty) {
|
||||
t.Errorf("Test case 2 failed. Expected %v, got %v", expectedEmpty, resultEmpty)
|
||||
}
|
||||
|
||||
// Test case 3: Test with a slice containing elements with the same key
|
||||
duplicatePeople := []Person{
|
||||
{ID: 1, Name: "Alice"},
|
||||
{ID: 1, Name: "Bob"},
|
||||
}
|
||||
|
||||
expectedDuplicate := map[int]Person{
|
||||
1: {ID: 1, Name: "Bob"}, // The last occurrence of the duplicate key will overwrite the previous one
|
||||
}
|
||||
|
||||
resultDuplicate := SliceToMap(duplicatePeople, keyFunc)
|
||||
|
||||
if !reflect.DeepEqual(resultDuplicate, expectedDuplicate) {
|
||||
t.Errorf("Test case 3 failed. Expected %v, got %v", expectedDuplicate, resultDuplicate)
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res := SliceToMap(tc.inputSlices, tc.inputFunc)
|
||||
if !reflect.DeepEqual(tc.want, res) {
|
||||
t.Errorf("failed. Expected %v, got %v", tc.want, res)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user