mirror of
https://github.com/samber/lo.git
synced 2025-10-04 23:52:41 +08:00
chore: test coverage improvement (#240)
* chore: improves channel functions test coverage * chore: improves errors functions test coverage * chore: improves map functions test coverage * chore: improves parallel slice functions test coverage * chore: improves slice functions test coverage * fix: uses atomic counter on parallel/slice_test * Update channel_test.go Co-authored-by: Samuel Berthe <dev@samuel-berthe.fr>
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package lo
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -111,7 +112,21 @@ func TestDispatchingStrategyRoundRobin(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestDispatchingStrategyRandom(t *testing.T) {
|
||||
// @TODO
|
||||
testWithTimeout(t, 10*time.Millisecond)
|
||||
is := assert.New(t)
|
||||
|
||||
// with this seed, the order of random channels are: 1 - 0
|
||||
rand.Seed(14)
|
||||
|
||||
children := createChannels[int](2, 2)
|
||||
rochildren := channelsToReadOnly(children)
|
||||
defer closeChannels(children)
|
||||
|
||||
for i := 0; i < 2; i++ {
|
||||
children[1] <- i
|
||||
}
|
||||
|
||||
is.Equal(0, DispatchingStrategyRandom[int](42, 0, rochildren))
|
||||
}
|
||||
|
||||
func TestDispatchingStrategyWeightedRandom(t *testing.T) {
|
||||
|
@@ -53,6 +53,13 @@ func TestMust(t *testing.T) {
|
||||
is.PanicsWithValue("operation should fail: assert.AnError general error for testing", func() {
|
||||
Must0(cb(), "operation should fail")
|
||||
})
|
||||
|
||||
is.PanicsWithValue("must: invalid err type 'int', should either be a bool or an error", func() {
|
||||
Must0(0)
|
||||
})
|
||||
is.PanicsWithValue("must: invalid err type 'string', should either be a bool or an error", func() {
|
||||
Must0("error")
|
||||
})
|
||||
}
|
||||
|
||||
func TestMustX(t *testing.T) {
|
||||
@@ -265,6 +272,10 @@ func TestTryX(t *testing.T) {
|
||||
t.Parallel()
|
||||
is := assert.New(t)
|
||||
|
||||
is.True(Try1(func() error {
|
||||
return nil
|
||||
}))
|
||||
|
||||
is.True(Try2(func() (string, error) {
|
||||
return "", nil
|
||||
}))
|
||||
@@ -285,6 +296,10 @@ func TestTryX(t *testing.T) {
|
||||
return "", "", "", "", "", nil
|
||||
}))
|
||||
|
||||
is.False(Try1(func() error {
|
||||
panic("error")
|
||||
}))
|
||||
|
||||
is.False(Try2(func() (string, error) {
|
||||
panic("error")
|
||||
}))
|
||||
@@ -305,6 +320,10 @@ func TestTryX(t *testing.T) {
|
||||
panic("error")
|
||||
}))
|
||||
|
||||
is.False(Try1(func() error {
|
||||
return errors.New("foo")
|
||||
}))
|
||||
|
||||
is.False(Try2(func() (string, error) {
|
||||
return "", errors.New("foo")
|
||||
}))
|
||||
@@ -489,11 +508,18 @@ func TestTryWithErrorValue(t *testing.T) {
|
||||
is := assert.New(t)
|
||||
|
||||
err, ok := TryWithErrorValue(func() error {
|
||||
// getting error in case of panic, using recover function
|
||||
panic("error")
|
||||
})
|
||||
is.False(ok)
|
||||
is.Equal("error", err)
|
||||
|
||||
err, ok = TryWithErrorValue(func() error {
|
||||
return errors.New("foo")
|
||||
})
|
||||
is.False(ok)
|
||||
is.EqualError(err.(error), "foo")
|
||||
|
||||
err, ok = TryWithErrorValue(func() error {
|
||||
return nil
|
||||
})
|
||||
|
41
map_test.go
41
map_test.go
@@ -108,6 +108,27 @@ func TestEntries(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestToPairs(t *testing.T) {
|
||||
t.Parallel()
|
||||
is := assert.New(t)
|
||||
|
||||
r1 := ToPairs(map[string]int{"baz": 3, "qux": 4})
|
||||
|
||||
sort.Slice(r1, func(i, j int) bool {
|
||||
return r1[i].Value < r1[j].Value
|
||||
})
|
||||
is.EqualValues(r1, []Entry[string, int]{
|
||||
{
|
||||
Key: "baz",
|
||||
Value: 3,
|
||||
},
|
||||
{
|
||||
Key: "qux",
|
||||
Value: 4,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func TestFromEntries(t *testing.T) {
|
||||
t.Parallel()
|
||||
is := assert.New(t)
|
||||
@@ -128,6 +149,26 @@ func TestFromEntries(t *testing.T) {
|
||||
is.Equal(r1["bar"], 2)
|
||||
}
|
||||
|
||||
func TestFromPairs(t *testing.T) {
|
||||
t.Parallel()
|
||||
is := assert.New(t)
|
||||
|
||||
r1 := FromPairs([]Entry[string, int]{
|
||||
{
|
||||
Key: "baz",
|
||||
Value: 3,
|
||||
},
|
||||
{
|
||||
Key: "qux",
|
||||
Value: 4,
|
||||
},
|
||||
})
|
||||
|
||||
is.Len(r1, 2)
|
||||
is.Equal(r1["baz"], 3)
|
||||
is.Equal(r1["qux"], 4)
|
||||
}
|
||||
|
||||
func TestInvert(t *testing.T) {
|
||||
t.Parallel()
|
||||
is := assert.New(t)
|
||||
|
@@ -3,6 +3,7 @@ package parallel
|
||||
import (
|
||||
"sort"
|
||||
"strconv"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
@@ -24,6 +25,18 @@ func TestMap(t *testing.T) {
|
||||
is.Equal(result2, []string{"1", "2", "3", "4"})
|
||||
}
|
||||
|
||||
func TestForEach(t *testing.T) {
|
||||
is := assert.New(t)
|
||||
|
||||
var counter uint64
|
||||
collection := []int{1, 2, 3, 4}
|
||||
ForEach(collection, func(x int, i int) {
|
||||
atomic.AddUint64(&counter, 1)
|
||||
})
|
||||
|
||||
is.Equal(uint64(4), atomic.LoadUint64(&counter))
|
||||
}
|
||||
|
||||
func TestTimes(t *testing.T) {
|
||||
is := assert.New(t)
|
||||
|
||||
|
@@ -197,6 +197,9 @@ func TestChunk(t *testing.T) {
|
||||
is.Equal(result2, [][]int{{0, 1}, {2, 3}, {4, 5}, {6}})
|
||||
is.Equal(result3, [][]int{})
|
||||
is.Equal(result4, [][]int{{0}})
|
||||
is.PanicsWithValue("Second parameter must be greater than 0", func() {
|
||||
Chunk([]int{0}, 0)
|
||||
})
|
||||
}
|
||||
|
||||
func TestPartitionBy(t *testing.T) {
|
||||
@@ -383,6 +386,41 @@ func TestAssociate(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestSliceToMap(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
type foo struct {
|
||||
baz string
|
||||
bar int
|
||||
}
|
||||
transform := func(f *foo) (string, int) {
|
||||
return f.baz, f.bar
|
||||
}
|
||||
testCases := []struct {
|
||||
in []*foo
|
||||
expect map[string]int
|
||||
}{
|
||||
{
|
||||
in: []*foo{{baz: "apple", bar: 1}},
|
||||
expect: map[string]int{"apple": 1},
|
||||
},
|
||||
{
|
||||
in: []*foo{{baz: "apple", bar: 1}, {baz: "banana", bar: 2}},
|
||||
expect: map[string]int{"apple": 1, "banana": 2},
|
||||
},
|
||||
{
|
||||
in: []*foo{{baz: "apple", bar: 1}, {baz: "apple", bar: 2}},
|
||||
expect: map[string]int{"apple": 2},
|
||||
},
|
||||
}
|
||||
for i, testCase := range testCases {
|
||||
t.Run(fmt.Sprintf("test_%d", i), func(t *testing.T) {
|
||||
is := assert.New(t)
|
||||
is.Equal(SliceToMap(testCase.in, transform), testCase.expect)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDrop(t *testing.T) {
|
||||
t.Parallel()
|
||||
is := assert.New(t)
|
||||
@@ -584,6 +622,7 @@ func TestSlice(t *testing.T) {
|
||||
out12 := Slice(in, 1, 0)
|
||||
out13 := Slice(in, 5, 0)
|
||||
out14 := Slice(in, 6, 4)
|
||||
out15 := Slice(in, 6, 7)
|
||||
|
||||
is.Equal([]int{}, out1)
|
||||
is.Equal([]int{0}, out2)
|
||||
@@ -599,6 +638,7 @@ func TestSlice(t *testing.T) {
|
||||
is.Equal([]int{}, out12)
|
||||
is.Equal([]int{}, out13)
|
||||
is.Equal([]int{}, out14)
|
||||
is.Equal([]int{}, out15)
|
||||
}
|
||||
|
||||
func TestReplace(t *testing.T) {
|
||||
|
Reference in New Issue
Block a user