Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: test coverage improvement #240

Merged
merged 8 commits into from Oct 22, 2022
2 changes: 1 addition & 1 deletion channel.go
Expand Up @@ -94,7 +94,7 @@ func DispatchingStrategyRandom[T any](msg T, index uint64, channels []<-chan T)
}
}

// DispatchingStrategyRandom distributes messages in a weighted manner.
// DispatchingStrategyWeightedRandom distributes messages in a weighted manner.
// If the channel capacity is exceeded, another random channel will be selected and so on.
func DispatchingStrategyWeightedRandom[T any](weights []int) DispatchingStrategy[T] {
seq := []int{}
Expand Down
18 changes: 17 additions & 1 deletion channel_test.go
@@ -1,6 +1,7 @@
package lo

import (
"math/rand"
"testing"
"time"

Expand Down Expand Up @@ -111,7 +112,22 @@ func TestDispatchingStrategyRoundRobin(t *testing.T) {
}

func TestDispatchingStrategyRandom(t *testing.T) {
// @TODO
t.Parallel()
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) {
Expand Down
34 changes: 30 additions & 4 deletions errors_test.go
Expand Up @@ -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) {
Expand Down Expand Up @@ -264,7 +271,11 @@ func TestTry(t *testing.T) {
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
}))
Expand All @@ -284,7 +295,11 @@ func TestTryX(t *testing.T) {
is.True(Try6(func() (string, string, string, string, string, error) {
return "", "", "", "", "", nil
}))


is.False(Try1(func() error {
panic("error")
}))

is.False(Try2(func() (string, error) {
panic("error")
}))
Expand All @@ -304,7 +319,11 @@ func TestTryX(t *testing.T) {
is.False(Try6(func() (string, string, string, string, string, error) {
panic("error")
}))


is.False(Try1(func() error {
return errors.New("foo")
}))

is.False(Try2(func() (string, error) {
return "", errors.New("foo")
}))
Expand Down Expand Up @@ -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
})
Expand Down
45 changes: 43 additions & 2 deletions map_test.go
Expand Up @@ -108,10 +108,31 @@ func TestEntries(t *testing.T) {
})
}

func TestFromEntries(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)

r1 := FromEntries([]Entry[string, int]{
{
Key: "foo",
Expand All @@ -128,10 +149,30 @@ func TestFromEntries(t *testing.T) {
is.Equal(r1["bar"], 2)
}

func TestInvert(t *testing.T) {
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)

r1 := Invert(map[string]int{"a": 1, "b": 2})
r2 := Invert(map[string]int{"a": 1, "b": 2, "c": 1})

Expand Down
14 changes: 13 additions & 1 deletion parallel/slice_test.go
Expand Up @@ -24,9 +24,21 @@ func TestMap(t *testing.T) {
is.Equal(result2, []string{"1", "2", "3", "4"})
}

func TestTimes(t *testing.T) {
func TestForEach(t *testing.T) {
is := assert.New(t)

count := 0
samber marked this conversation as resolved.
Show resolved Hide resolved
collection := []int{1, 2, 3, 4}
ForEach(collection, func(x int, i int) {
count++
})

is.Equal(4, count)
}

func TestTimes(t *testing.T) {
is := assert.New(t)

result1 := Times(3, func(i int) string {
return strconv.FormatInt(int64(i), 10)
})
Expand Down
42 changes: 41 additions & 1 deletion slice_test.go
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -584,7 +622,8 @@ 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)
is.Equal([]int{0, 1, 2, 3, 4}, out3)
Expand All @@ -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) {
Expand Down