1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-12 08:42:29 +08:00

test: we should write clean unit test code

This commit is contained in:
dudaodong
2024-08-28 16:06:08 +08:00
parent ca40b5d6c6
commit ec092a009a

View File

@@ -16,12 +16,20 @@ func TestContain(t *testing.T) {
assert := internal.NewAssert(t, "TestContain") assert := internal.NewAssert(t, "TestContain")
assert.Equal(true, Contain([]string{"a", "b", "c"}, "a")) tests := []struct {
assert.Equal(false, Contain([]string{"a", "b", "c"}, "d")) slice []string
assert.Equal(true, Contain([]string{""}, "")) give string
assert.Equal(false, Contain([]string{}, "")) want bool
}{
{[]string{"a", "b", "c"}, "a", true},
{[]string{"a", "b", "c"}, "d", false},
{[]string{""}, "", true},
{[]string{}, "", false},
}
assert.Equal(true, Contain([]int{1, 2, 3}, 1)) for _, tt := range tests {
assert.Equal(tt.want, Contain(tt.slice, tt.give))
}
} }
func TestContainBy(t *testing.T) { func TestContainBy(t *testing.T) {
@@ -30,32 +38,53 @@ func TestContainBy(t *testing.T) {
assert := internal.NewAssert(t, "TestContainBy") assert := internal.NewAssert(t, "TestContainBy")
type foo struct { type foo struct {
A string a string
B int b int
} }
array1 := []foo{{A: "1", B: 1}, {A: "2", B: 2}} tests := []struct {
result1 := ContainBy(array1, func(f foo) bool { return f.A == "1" && f.B == 1 }) slice []foo
result2 := ContainBy(array1, func(f foo) bool { return f.A == "2" && f.B == 1 }) predicateFn func(f foo) bool
want bool
}{
{
[]foo{{a: "1", b: 1}, {a: "2", b: 2}},
func(f foo) bool { return f.a == "1" && f.b == 1 },
true,
},
{
[]foo{{a: "1", b: 1}, {a: "2", b: 2}},
func(f foo) bool { return f.a == "2" && f.b == 1 },
false,
},
}
array2 := []string{"a", "b", "c"} for _, tt := range tests {
result3 := ContainBy(array2, func(t string) bool { return t == "a" }) assert.Equal(tt.want, ContainBy(tt.slice, tt.predicateFn))
result4 := ContainBy(array2, func(t string) bool { return t == "d" }) }
assert.Equal(true, result1)
assert.Equal(false, result2)
assert.Equal(true, result3)
assert.Equal(false, result4)
} }
func TestContainSubSlice(t *testing.T) { func TestContainSubSlice(t *testing.T) {
t.Parallel() t.Parallel()
assert := internal.NewAssert(t, "TestContainSubSlice") assert := internal.NewAssert(t, "TestContainSubSlice")
assert.Equal(true, ContainSubSlice([]string{"a", "a", "b", "c"}, []string{"a", "a"}))
assert.Equal(false, ContainSubSlice([]string{"a", "a", "b", "c"}, []string{"a", "d"})) tests := []struct {
assert.Equal(true, ContainSubSlice([]int{1, 2, 3}, []int{1, 2})) slice []string
assert.Equal(false, ContainSubSlice([]int{1, 2, 3}, []int{0, 1})) subSlice []string
want bool
}{
{[]string{"a", "b", "c"}, []string{"a", "b"}, true},
{[]string{"a", "b", "c"}, []string{"a", "d"}, false},
{[]string{"a", "b", "c"}, []string{"a", "b", "c"}, true},
{[]string{"a", "b", "c"}, []string{"a", "b", "c", "d"}, false},
{[]string{"a", "b", ""}, []string{"a", ""}, true},
{[]string{""}, []string{""}, true},
}
for _, tt := range tests {
assert.Equal(tt.want, ContainSubSlice(tt.slice, tt.subSlice))
}
} }
func TestChunk(t *testing.T) { func TestChunk(t *testing.T) {
@@ -63,29 +92,24 @@ func TestChunk(t *testing.T) {
assert := internal.NewAssert(t, "TestChunk") assert := internal.NewAssert(t, "TestChunk")
arr := []string{"a", "b", "c", "d", "e"} tests := []struct {
slice []string
chuanSize int
want [][]string
}{
{[]string{"a", "b", "c", "d", "e"}, -1, [][]string{}},
{[]string{"a", "b", "c", "d", "e"}, 0, [][]string{}},
{[]string{"a", "b", "c", "d", "e"}, 1, [][]string{{"a"}, {"b"}, {"c"}, {"d"}, {"e"}}},
{[]string{"a", "b", "c", "d", "e"}, 2, [][]string{{"a", "b"}, {"c", "d"}, {"e"}}},
{[]string{"a", "b", "c", "d", "e"}, 3, [][]string{{"a", "b", "c"}, {"d", "e"}}},
{[]string{"a", "b", "c", "d", "e"}, 4, [][]string{{"a", "b", "c", "d"}, {"e"}}},
{[]string{"a", "b", "c", "d", "e"}, 5, [][]string{{"a", "b", "c", "d", "e"}}},
{[]string{"a", "b", "c", "d", "e"}, 6, [][]string{{"a", "b", "c", "d", "e"}}},
}
assert.Equal([][]string{}, Chunk(arr, -1)) for _, tt := range tests {
assert.Equal(tt.want, Chunk(tt.slice, tt.chuanSize))
assert.Equal([][]string{}, Chunk(arr, 0)) }
r1 := [][]string{{"a"}, {"b"}, {"c"}, {"d"}, {"e"}}
assert.Equal(r1, Chunk(arr, 1))
r2 := [][]string{{"a", "b"}, {"c", "d"}, {"e"}}
assert.Equal(r2, Chunk(arr, 2))
r3 := [][]string{{"a", "b", "c"}, {"d", "e"}}
assert.Equal(r3, Chunk(arr, 3))
r4 := [][]string{{"a", "b", "c", "d"}, {"e"}}
assert.Equal(r4, Chunk(arr, 4))
r5 := [][]string{{"a", "b", "c", "d", "e"}}
assert.Equal(r5, Chunk(arr, 5))
r6 := [][]string{{"a", "b", "c", "d", "e"}}
assert.Equal(r6, Chunk(arr, 6))
} }
func TestCompact(t *testing.T) { func TestCompact(t *testing.T) {
@@ -134,6 +158,7 @@ func TestEqual(t *testing.T) {
assert.Equal(true, Equal(slice1, slice2)) assert.Equal(true, Equal(slice1, slice2))
assert.Equal(false, Equal(slice1, slice3)) assert.Equal(false, Equal(slice1, slice3))
assert.Equal(false, Equal(slice2, slice3))
} }
// go test -fuzz=Fuzz -fuzztime=10s . // go test -fuzz=Fuzz -fuzztime=10s .
@@ -163,12 +188,27 @@ func TestEvery(t *testing.T) {
assert := internal.NewAssert(t, "TestEvery") assert := internal.NewAssert(t, "TestEvery")
nums := []int{1, 2, 3, 5}
isEven := func(i, num int) bool { isEven := func(i, num int) bool {
return num%2 == 0 return num%2 == 0
} }
isOdd := func(i, num int) bool {
return num%2 == 1
}
assert.Equal(false, Every(nums, isEven)) tests := []struct {
slice []int
predicateFn func(i, num int) bool
want bool
}{
{[]int{1, 3, 5, 7}, isOdd, true},
{[]int{2, 4, 6, 8}, isEven, true},
{[]int{1, 2, 3, 4}, isOdd, false},
{[]int{1, 2, 3, 4}, isEven, false},
}
for _, tt := range tests {
assert.Equal(tt.want, Every(tt.slice, tt.predicateFn))
}
} }
func TestNone(t *testing.T) { func TestNone(t *testing.T) {
@@ -176,12 +216,27 @@ func TestNone(t *testing.T) {
assert := internal.NewAssert(t, "TestNone") assert := internal.NewAssert(t, "TestNone")
nums := []int{1, 2, 3, 5} isEven := func(i, num int) bool {
check := func(i, num int) bool { return num%2 == 0
}
isOdd := func(i, num int) bool {
return num%2 == 1 return num%2 == 1
} }
assert.Equal(false, None(nums, check)) tests := []struct {
slice []int
predicateFn func(i, num int) bool
want bool
}{
{[]int{1, 3, 5, 7}, isEven, true},
{[]int{2, 4, 6, 8}, isOdd, true},
{[]int{1, 2, 3, 4}, isOdd, false},
{[]int{1, 2, 3, 4}, isEven, false},
}
for _, tt := range tests {
assert.Equal(tt.want, None(tt.slice, tt.predicateFn))
}
} }
func TestSome(t *testing.T) { func TestSome(t *testing.T) {
@@ -189,12 +244,27 @@ func TestSome(t *testing.T) {
assert := internal.NewAssert(t, "TestSome") assert := internal.NewAssert(t, "TestSome")
nums := []int{1, 2, 3, 5} isEven := func(i, num int) bool {
hasEven := func(i, num int) bool {
return num%2 == 0 return num%2 == 0
} }
isOdd := func(i, num int) bool {
return num%2 == 1
}
assert.Equal(true, Some(nums, hasEven)) tests := []struct {
slice []int
predicateFn func(i, num int) bool
want bool
}{
{[]int{1, 3, 5, 7}, isEven, false},
{[]int{2, 4, 6, 8}, isOdd, false},
{[]int{1, 2, 3, 4}, isOdd, true},
{[]int{1, 2, 3, 4}, isEven, true},
}
for _, tt := range tests {
assert.Equal(tt.want, Some(tt.slice, tt.predicateFn))
}
} }
func TestFilter(t *testing.T) { func TestFilter(t *testing.T) {
@@ -202,33 +272,37 @@ func TestFilter(t *testing.T) {
assert := internal.NewAssert(t, "TestFilter") assert := internal.NewAssert(t, "TestFilter")
nums := []int{1, 2, 3, 4, 5} t.Run("filter int slice", func(t *testing.T) {
isEven := func(i, num int) bool { nums := []int{1, 2, 3, 4, 5}
return num%2 == 0 isEven := func(i, num int) bool {
} return num%2 == 0
}
assert.Equal([]int{2, 4}, Filter(nums, isEven)) assert.Equal([]int{2, 4}, Filter(nums, isEven))
})
type student struct { t.Run("filter struct slice", func(t *testing.T) {
name string type student struct {
age int name string
} age int
students := []student{ }
{"a", 10}, students := []student{
{"b", 11}, {"a", 10},
{"c", 12}, {"b", 11},
{"d", 13}, {"c", 12},
{"e", 14}, {"d", 13},
} {"e", 14},
studentsOfAageGreat12 := []student{ }
{"d", 13}, studentsOfAgeGreat12 := []student{
{"e", 14}, {"d", 13},
} {"e", 14},
filterFunc := func(i int, s student) bool { }
return s.age > 12 filterFunc := func(i int, s student) bool {
} return s.age > 12
}
assert.Equal(studentsOfAageGreat12, Filter(students, filterFunc)) assert.Equal(studentsOfAgeGreat12, Filter(students, filterFunc))
})
} }
func TestGroupBy(t *testing.T) { func TestGroupBy(t *testing.T) {
@@ -249,6 +323,8 @@ func TestGroupBy(t *testing.T) {
func TestGroupWith(t *testing.T) { func TestGroupWith(t *testing.T) {
t.Parallel() t.Parallel()
assert := internal.NewAssert(t, "TestGroupWith")
nums := []float64{6.1, 4.2, 6.3} nums := []float64{6.1, 4.2, 6.3}
floor := func(num float64) float64 { floor := func(num float64) float64 {
return math.Floor(num) return math.Floor(num)
@@ -257,9 +333,8 @@ func TestGroupWith(t *testing.T) {
4: {4.2}, 4: {4.2},
6: {6.1, 6.3}, 6: {6.1, 6.3},
} }
actual := GroupWith(nums, floor)
assert := internal.NewAssert(t, "TestGroupWith") assert.Equal(expected, GroupWith(nums, floor))
assert.Equal(expected, actual)
} }
func TestCount(t *testing.T) { func TestCount(t *testing.T) {
@@ -295,8 +370,15 @@ func TestFind(t *testing.T) {
result, ok := Find(nums, even) result, ok := Find(nums, even)
assert := internal.NewAssert(t, "TestFind") assert := internal.NewAssert(t, "TestFind")
assert.Equal(true, ok) assert.Equal(true, ok)
assert.Equal(2, *result) assert.Equal(2, *result)
_, ok = Find(nums, func(_ int, v int) bool {
return v == 6
})
assert.Equal(false, ok)
} }
func TestFindBy(t *testing.T) { func TestFindBy(t *testing.T) {
@@ -358,19 +440,6 @@ func TestFindLast(t *testing.T) {
assert.Equal(4, *result) assert.Equal(4, *result)
} }
func TestFindFoundNothing(t *testing.T) {
t.Parallel()
nums := []int{1, 1, 1, 1, 1, 1}
findFunc := func(i, num int) bool {
return num > 1
}
_, ok := Find(nums, findFunc)
assert := internal.NewAssert(t, "TestFindFoundNothing")
assert.Equal(false, ok)
}
func TestFlatten(t *testing.T) { func TestFlatten(t *testing.T) {
t.Parallel() t.Parallel()
@@ -633,14 +702,12 @@ func TestReduceBy(t *testing.T) {
result1 := ReduceBy([]int{1, 2, 3, 4}, 0, func(_ int, item int, agg int) int { result1 := ReduceBy([]int{1, 2, 3, 4}, 0, func(_ int, item int, agg int) int {
return agg + item return agg + item
}) })
assert.Equal(10, result1)
result2 := ReduceBy([]int{1, 2, 3, 4}, "", func(_ int, item int, agg string) string { result2 := ReduceBy([]int{1, 2, 3, 4}, "", func(_ int, item int, agg string) string {
return agg + fmt.Sprintf("%v", item) return agg + fmt.Sprintf("%v", item)
}) })
assert.Equal(10, result1)
assert.Equal("1234", result2) assert.Equal("1234", result2)
} }
func TestReduceRight(t *testing.T) { func TestReduceRight(t *testing.T) {
@@ -692,15 +759,23 @@ func TestDeleteAt(t *testing.T) {
t.Parallel() t.Parallel()
assert := internal.NewAssert(t, "TestDeleteAt") assert := internal.NewAssert(t, "TestDeleteAt")
arr := []int{1, 2, 3, 4, 5}
assert.Equal([]int{2, 3, 4, 5}, DeleteAt(arr, 0)) tests := []struct {
assert.Equal([]int{1, 2, 3, 4}, DeleteAt(arr, 4)) slice []int
deletePos int
wang []int
}{
{[]int{1, 2, 3, 4, 5}, 0, []int{2, 3, 4, 5}},
{[]int{1, 2, 3, 4, 5}, 1, []int{1, 3, 4, 5}},
{[]int{1, 2, 3, 4, 5}, 2, []int{1, 2, 4, 5}},
{[]int{1, 2, 3, 4, 5}, 3, []int{1, 2, 3, 5}},
{[]int{1, 2, 3, 4, 5}, 4, []int{1, 2, 3, 4}},
{[]int{1, 2, 3, 4, 5}, 5, []int{1, 2, 3, 4}},
}
assert.Equal([]int{1, 2, 3, 4}, DeleteAt(arr, 5)) for _, tt := range tests {
assert.Equal([]int{1, 2, 3, 4}, DeleteAt(arr, 6)) assert.Equal(tt.wang, DeleteAt(tt.slice, tt.deletePos))
}
assert.Equal([]int{1, 2, 3, 4, 5}, arr)
} }
func TestDeleteRange(t *testing.T) { func TestDeleteRange(t *testing.T) {
@@ -720,16 +795,24 @@ func TestDrop(t *testing.T) {
assert := internal.NewAssert(t, "TestDrop") assert := internal.NewAssert(t, "TestDrop")
assert.Equal([]int{}, Drop([]int{}, 0)) tests := []struct {
assert.Equal([]int{}, Drop([]int{}, 1)) slice []int
assert.Equal([]int{}, Drop([]int{}, -1)) dropNum int
want []int
}{
{[]int{}, 0, []int{}},
{[]int{}, 1, []int{}},
{[]int{}, -1, []int{}},
{[]int{1, 2, 3, 4, 5}, -1, []int{1, 2, 3, 4, 5}},
{[]int{1, 2, 3, 4, 5}, 0, []int{1, 2, 3, 4, 5}},
{[]int{1, 2, 3, 4, 5}, 1, []int{2, 3, 4, 5}},
{[]int{1, 2, 3, 4, 5}, 5, []int{}},
{[]int{1, 2, 3, 4, 5}, 6, []int{}},
}
assert.Equal([]int{1, 2, 3, 4, 5}, Drop([]int{1, 2, 3, 4, 5}, 0)) for _, tt := range tests {
assert.Equal([]int{2, 3, 4, 5}, Drop([]int{1, 2, 3, 4, 5}, 1)) assert.Equal(tt.want, Drop(tt.slice, tt.dropNum))
assert.Equal([]int{}, Drop([]int{1, 2, 3, 4, 5}, 5)) }
assert.Equal([]int{}, Drop([]int{1, 2, 3, 4, 5}, 6))
assert.Equal([]int{1, 2, 3, 4, 5}, Drop([]int{1, 2, 3, 4, 5}, -1))
} }
func TestDropRight(t *testing.T) { func TestDropRight(t *testing.T) {
@@ -737,16 +820,23 @@ func TestDropRight(t *testing.T) {
assert := internal.NewAssert(t, "TestDropRight") assert := internal.NewAssert(t, "TestDropRight")
assert.Equal([]int{}, DropRight([]int{}, 0)) tests := []struct {
assert.Equal([]int{}, DropRight([]int{}, 1)) slice []int
assert.Equal([]int{}, DropRight([]int{}, -1)) dropNum int
want []int
assert.Equal([]int{1, 2, 3, 4, 5}, DropRight([]int{1, 2, 3, 4, 5}, 0)) }{
assert.Equal([]int{1, 2, 3, 4}, DropRight([]int{1, 2, 3, 4, 5}, 1)) {[]int{}, 0, []int{}},
assert.Equal([]int{}, DropRight([]int{1, 2, 3, 4, 5}, 5)) {[]int{}, 1, []int{}},
assert.Equal([]int{}, DropRight([]int{1, 2, 3, 4, 5}, 6)) {[]int{}, -1, []int{}},
{[]int{1, 2, 3, 4, 5}, -1, []int{1, 2, 3, 4, 5}},
assert.Equal([]int{1, 2, 3, 4, 5}, DropRight([]int{1, 2, 3, 4, 5}, -1)) {[]int{1, 2, 3, 4, 5}, 0, []int{1, 2, 3, 4, 5}},
{[]int{1, 2, 3, 4, 5}, 1, []int{1, 2, 3, 4}},
{[]int{}, 5, []int{}},
{[]int{}, 6, []int{}},
}
for _, tt := range tests {
assert.Equal(tt.want, DropRight(tt.slice, tt.dropNum))
}
} }
func TestDropWhile(t *testing.T) { func TestDropWhile(t *testing.T) {
@@ -754,22 +844,19 @@ func TestDropWhile(t *testing.T) {
assert := internal.NewAssert(t, "TestDropWhile") assert := internal.NewAssert(t, "TestDropWhile")
numbers := []int{1, 2, 3, 4, 5} tests := []struct {
slice []int
fn func(int) bool
want []int
}{
{[]int{1, 2, 3, 4, 5}, func(n int) bool { return n != 2 }, []int{2, 3, 4, 5}},
{[]int{1, 2, 3, 4, 5}, func(n int) bool { return true }, []int{}},
{[]int{1, 2, 3, 4, 5}, func(n int) bool { return n == 0 }, []int{1, 2, 3, 4, 5}},
}
r1 := DropWhile(numbers, func(n int) bool { for _, tt := range tests {
return n != 2 assert.Equal(tt.want, DropWhile(tt.slice, tt.fn))
}) }
assert.Equal([]int{2, 3, 4, 5}, r1)
r2 := DropWhile(numbers, func(n int) bool {
return true
})
assert.Equal([]int{}, r2)
r3 := DropWhile(numbers, func(n int) bool {
return n == 0
})
assert.Equal([]int{1, 2, 3, 4, 5}, r3)
} }
func TestDropRightWhile(t *testing.T) { func TestDropRightWhile(t *testing.T) {
@@ -777,22 +864,19 @@ func TestDropRightWhile(t *testing.T) {
assert := internal.NewAssert(t, "TestDropRightWhile") assert := internal.NewAssert(t, "TestDropRightWhile")
numbers := []int{1, 2, 3, 4, 5} tests := []struct {
slice []int
fn func(int) bool
want []int
}{
{[]int{1, 2, 3, 4, 5}, func(n int) bool { return n != 2 }, []int{1, 2}},
{[]int{1, 2, 3, 4, 5}, func(n int) bool { return true }, []int{}},
{[]int{1, 2, 3, 4, 5}, func(n int) bool { return n == 0 }, []int{1, 2, 3, 4, 5}},
}
r1 := DropRightWhile(numbers, func(n int) bool { for _, tt := range tests {
return n != 2 assert.Equal(tt.want, DropRightWhile(tt.slice, tt.fn))
}) }
assert.Equal([]int{1, 2}, r1)
r2 := DropRightWhile(numbers, func(n int) bool {
return true
})
assert.Equal([]int{}, r2)
r3 := DropRightWhile(numbers, func(n int) bool {
return n == 0
})
assert.Equal([]int{1, 2, 3, 4, 5}, r3)
} }
func TestInsertAt(t *testing.T) { func TestInsertAt(t *testing.T) {
@@ -800,15 +884,25 @@ func TestInsertAt(t *testing.T) {
assert := internal.NewAssert(t, "TestInsertAt") assert := internal.NewAssert(t, "TestInsertAt")
strs := []string{"a", "b", "c"} tests := []struct {
assert.Equal([]string{"a", "b", "c"}, InsertAt(strs, -1, "1")) slice []string
assert.Equal([]string{"a", "b", "c"}, InsertAt(strs, 4, "1")) insertPos int
assert.Equal([]string{"1", "a", "b", "c"}, InsertAt(strs, 0, "1")) insertValue any
assert.Equal([]string{"a", "1", "b", "c"}, InsertAt(strs, 1, "1")) want []string
assert.Equal([]string{"a", "b", "1", "c"}, InsertAt(strs, 2, "1")) }{
assert.Equal([]string{"a", "b", "c", "1"}, InsertAt(strs, 3, "1")) {[]string{"a", "b", "c"}, -1, "1", []string{"a", "b", "c"}},
assert.Equal([]string{"1", "2", "3", "a", "b", "c"}, InsertAt(strs, 0, []string{"1", "2", "3"})) {[]string{"a", "b", "c"}, 4, "1", []string{"a", "b", "c"}},
assert.Equal([]string{"a", "b", "c", "1", "2", "3"}, InsertAt(strs, 3, []string{"1", "2", "3"})) {[]string{"a", "b", "c"}, 0, "1", []string{"1", "a", "b", "c"}},
{[]string{"a", "b", "c"}, 1, "1", []string{"a", "1", "b", "c"}},
{[]string{"a", "b", "c"}, 2, "1", []string{"a", "b", "1", "c"}},
{[]string{"a", "b", "c"}, 3, "1", []string{"a", "b", "c", "1"}},
{[]string{"a", "b", "c"}, 0, []string{"1", "2", "3"}, []string{"1", "2", "3", "a", "b", "c"}},
{[]string{"a", "b", "c"}, 3, []string{"1", "2", "3"}, []string{"a", "b", "c", "1", "2", "3"}},
}
for _, tt := range tests {
assert.Equal(tt.want, InsertAt(tt.slice, tt.insertPos, tt.insertValue))
}
} }
func TestUpdateAt(t *testing.T) { func TestUpdateAt(t *testing.T) {