From 576c65384ceb0412e025a3c5c3f52d40e1cc9dc0 Mon Sep 17 00:00:00 2001 From: VictorAssunc Date: Tue, 11 Oct 2022 11:06:36 -0300 Subject: [PATCH 1/7] chore: improves channel functions test coverage --- channel.go | 2 +- channel_test.go | 18 +++++++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/channel.go b/channel.go index f136ac95..0b9b8044 100644 --- a/channel.go +++ b/channel.go @@ -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{} diff --git a/channel_test.go b/channel_test.go index b3446338..ce203594 100644 --- a/channel_test.go +++ b/channel_test.go @@ -1,6 +1,7 @@ package lo import ( + "math/rand" "testing" "time" @@ -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) { From 98eebd193b1c4386a4b9f663b1ec92d29b645995 Mon Sep 17 00:00:00 2001 From: VictorAssunc Date: Tue, 11 Oct 2022 11:09:04 -0300 Subject: [PATCH 2/7] chore: improves errors functions test coverage --- errors_test.go | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/errors_test.go b/errors_test.go index 522298ce..08cace9d 100644 --- a/errors_test.go +++ b/errors_test.go @@ -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) { @@ -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 })) @@ -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") })) @@ -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") })) @@ -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 }) From ea2e41f70368b5b2c3588686c5f71169fd05ce73 Mon Sep 17 00:00:00 2001 From: VictorAssunc Date: Tue, 11 Oct 2022 11:11:01 -0300 Subject: [PATCH 3/7] chore: improves map functions test coverage --- map_test.go | 45 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/map_test.go b/map_test.go index ed1ded60..88ddf75c 100644 --- a/map_test.go +++ b/map_test.go @@ -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", @@ -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}) From 19012e7119fee58a2692887fc39707c8edff843d Mon Sep 17 00:00:00 2001 From: VictorAssunc Date: Tue, 11 Oct 2022 11:12:23 -0300 Subject: [PATCH 4/7] chore: improves parallel slice functions test coverage --- parallel/slice_test.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/parallel/slice_test.go b/parallel/slice_test.go index caaefcf5..96c554fe 100644 --- a/parallel/slice_test.go +++ b/parallel/slice_test.go @@ -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 + 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) }) From 21845f8a76ac9e4d31c900139e52fbcc994929a3 Mon Sep 17 00:00:00 2001 From: VictorAssunc Date: Tue, 11 Oct 2022 11:15:03 -0300 Subject: [PATCH 5/7] chore: improves slice functions test coverage --- slice_test.go | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/slice_test.go b/slice_test.go index a3f36533..8f3bc368 100644 --- a/slice_test.go +++ b/slice_test.go @@ -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,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) @@ -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) { From 343276237a5eaa97b2415244a329d18b4b7da92e Mon Sep 17 00:00:00 2001 From: VictorAssunc Date: Fri, 21 Oct 2022 09:10:39 -0300 Subject: [PATCH 6/7] fix: uses atomic counter on parallel/slice_test --- parallel/slice_test.go | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/parallel/slice_test.go b/parallel/slice_test.go index 96c554fe..248b45da 100644 --- a/parallel/slice_test.go +++ b/parallel/slice_test.go @@ -3,21 +3,22 @@ package parallel import ( "sort" "strconv" + "sync/atomic" "testing" - + "github.com/stretchr/testify/assert" ) func TestMap(t *testing.T) { is := assert.New(t) - + result1 := Map([]int{1, 2, 3, 4}, func(x int, _ int) string { return "Hello" }) result2 := Map([]int64{1, 2, 3, 4}, func(x int64, _ int) string { return strconv.FormatInt(x, 10) }) - + is.Equal(len(result1), 4) is.Equal(len(result2), 4) is.Equal(result1, []string{"Hello", "Hello", "Hello", "Hello"}) @@ -27,13 +28,13 @@ func TestMap(t *testing.T) { func TestForEach(t *testing.T) { is := assert.New(t) - count := 0 + var counter uint64 collection := []int{1, 2, 3, 4} ForEach(collection, func(x int, i int) { - count++ + atomic.AddUint64(&counter, 1) }) - is.Equal(4, count) + is.Equal(uint64(4), atomic.LoadUint64(&counter)) } func TestTimes(t *testing.T) { @@ -42,25 +43,25 @@ func TestTimes(t *testing.T) { result1 := Times(3, func(i int) string { return strconv.FormatInt(int64(i), 10) }) - + is.Equal(len(result1), 3) is.Equal(result1, []string{"0", "1", "2"}) } func TestGroupBy(t *testing.T) { is := assert.New(t) - + result1 := GroupBy([]int{0, 1, 2, 3, 4, 5}, func(i int) int { return i % 3 }) - + // order for x := range result1 { sort.Slice(result1[x], func(i, j int) bool { return result1[x][i] < result1[x][j] }) } - + is.EqualValues(len(result1), 3) is.EqualValues(result1, map[int][]int{ 0: {0, 3}, @@ -71,7 +72,7 @@ func TestGroupBy(t *testing.T) { func TestPartitionBy(t *testing.T) { is := assert.New(t) - + oddEven := func(x int) string { if x < 0 { return "negative" @@ -80,10 +81,10 @@ func TestPartitionBy(t *testing.T) { } return "odd" } - + result1 := PartitionBy([]int{-2, -1, 0, 1, 2, 3, 4, 5}, oddEven) result2 := PartitionBy([]int{}, oddEven) - + // order sort.Slice(result1, func(i, j int) bool { return result1[i][0] < result1[j][0] @@ -93,7 +94,7 @@ func TestPartitionBy(t *testing.T) { return result1[x][i] < result1[x][j] }) } - + is.ElementsMatch(result1, [][]int{{-2, -1}, {0, 2, 4}, {1, 3, 5}}) is.Equal(result2, [][]int{}) } From 040da80b1ff712580becfac20caee52089c79aba Mon Sep 17 00:00:00 2001 From: Samuel Berthe Date: Sat, 22 Oct 2022 18:57:09 +0200 Subject: [PATCH 7/7] Update channel_test.go --- channel_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/channel_test.go b/channel_test.go index 7e87d4a1..7a1f4c77 100644 --- a/channel_test.go +++ b/channel_test.go @@ -112,7 +112,6 @@ func TestDispatchingStrategyRoundRobin(t *testing.T) { } func TestDispatchingStrategyRandom(t *testing.T) { - t.Parallel() testWithTimeout(t, 10*time.Millisecond) is := assert.New(t)