mirror of
https://github.com/samber/lo.git
synced 2026-04-22 15:37:14 +08:00
💄
This commit is contained in:
+79
-54
@@ -1,7 +1,7 @@
|
||||
package lo
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"errors"
|
||||
"math/rand"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -374,9 +374,12 @@ func TestMinByErr(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result, err := MinByErr(tt.input, func(item, min string) (bool, error) {
|
||||
return len(item) < len(min), nil
|
||||
t.Parallel()
|
||||
|
||||
result, err := MinByErr(tt.input, func(item, mIn string) (bool, error) {
|
||||
return len(item) < len(mIn), nil
|
||||
})
|
||||
is.NoError(err)
|
||||
is.Equal(tt.expected, result)
|
||||
@@ -405,23 +408,21 @@ func TestMinByErr(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, tt := range errorTests {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
callbackCount := 0
|
||||
result, err := MinByErr(tt.input, func(item, min string) (bool, error) {
|
||||
result, err := MinByErr(tt.input, func(item, mIn string) (bool, error) {
|
||||
callbackCount++
|
||||
if item == tt.errorAt {
|
||||
return false, testErr
|
||||
}
|
||||
return len(item) < len(min), nil
|
||||
return len(item) < len(mIn), nil
|
||||
})
|
||||
is.ErrorIs(err, testErr)
|
||||
is.Equal(tt.expectedCalls, callbackCount)
|
||||
// Result should be the current min at the time of error
|
||||
if tt.expectedCalls == 1 {
|
||||
is.Equal("a", result) // Still the first element
|
||||
} else {
|
||||
is.Equal("a", result) // "a" is still min
|
||||
}
|
||||
is.Equal("a", result) // Still the first element
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -454,43 +455,43 @@ func TestMinIndexByErr(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
input []string
|
||||
less func(a, b string) (bool, error)
|
||||
wantValue string
|
||||
wantIndex int
|
||||
wantErr bool
|
||||
errMsg string
|
||||
name string
|
||||
input []string
|
||||
less func(a, b string) (bool, error)
|
||||
wantValue string
|
||||
wantIndex int
|
||||
wantErr bool
|
||||
errMsg string
|
||||
expectedCallbackCount int
|
||||
}{
|
||||
{
|
||||
name: "empty slice",
|
||||
input: []string{},
|
||||
less: func(a, b string) (bool, error) { return len(a) < len(b), nil },
|
||||
wantValue: "",
|
||||
wantIndex: -1,
|
||||
wantErr: false,
|
||||
name: "empty slice",
|
||||
input: []string{},
|
||||
less: func(a, b string) (bool, error) { return len(a) < len(b), nil },
|
||||
wantValue: "",
|
||||
wantIndex: -1,
|
||||
wantErr: false,
|
||||
expectedCallbackCount: 0,
|
||||
},
|
||||
{
|
||||
name: "success case",
|
||||
input: []string{"s1", "string2", "s3"},
|
||||
less: func(a, b string) (bool, error) { return len(a) < len(b), nil },
|
||||
wantValue: "s1",
|
||||
wantIndex: 0,
|
||||
wantErr: false,
|
||||
name: "success case",
|
||||
input: []string{"s1", "string2", "s3"},
|
||||
less: func(a, b string) (bool, error) { return len(a) < len(b), nil },
|
||||
wantValue: "s1",
|
||||
wantIndex: 0,
|
||||
wantErr: false,
|
||||
expectedCallbackCount: 2,
|
||||
},
|
||||
{
|
||||
name: "error on first comparison",
|
||||
input: []string{"s1", "string2", "s3"},
|
||||
less: func(a, b string) (bool, error) {
|
||||
return false, fmt.Errorf("comparison error")
|
||||
return false, errors.New("comparison error")
|
||||
},
|
||||
wantValue: "",
|
||||
wantIndex: -1,
|
||||
wantErr: true,
|
||||
errMsg: "comparison error",
|
||||
wantValue: "",
|
||||
wantIndex: -1,
|
||||
wantErr: true,
|
||||
errMsg: "comparison error",
|
||||
expectedCallbackCount: 1,
|
||||
},
|
||||
{
|
||||
@@ -498,23 +499,23 @@ func TestMinIndexByErr(t *testing.T) {
|
||||
input: []string{"a", "bb", "ccc", "error", "e"},
|
||||
less: func(a, b string) (bool, error) {
|
||||
if a == "error" || b == "error" {
|
||||
return false, fmt.Errorf("error value encountered")
|
||||
return false, errors.New("error value encountered")
|
||||
}
|
||||
return len(a) < len(b), nil
|
||||
},
|
||||
wantValue: "",
|
||||
wantIndex: -1,
|
||||
wantErr: true,
|
||||
errMsg: "error value encountered",
|
||||
wantValue: "",
|
||||
wantIndex: -1,
|
||||
wantErr: true,
|
||||
errMsg: "error value encountered",
|
||||
expectedCallbackCount: 3,
|
||||
},
|
||||
{
|
||||
name: "single element",
|
||||
input: []string{"single"},
|
||||
less: func(a, b string) (bool, error) { return len(a) < len(b), nil },
|
||||
wantValue: "single",
|
||||
wantIndex: 0,
|
||||
wantErr: false,
|
||||
name: "single element",
|
||||
input: []string{"single"},
|
||||
less: func(a, b string) (bool, error) { return len(a) < len(b), nil },
|
||||
wantValue: "single",
|
||||
wantIndex: 0,
|
||||
wantErr: false,
|
||||
expectedCallbackCount: 0,
|
||||
},
|
||||
}
|
||||
@@ -627,7 +628,10 @@ func TestEarliestByErr(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
result, err := EarliestByErr(tt.input, func(i foo) (time.Time, error) {
|
||||
return i.bar, nil
|
||||
})
|
||||
@@ -664,7 +668,10 @@ func TestEarliestByErr(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, tt := range errorTests {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
callbackCount := 0
|
||||
_, err := EarliestByErr(tt.input, func(i foo) (time.Time, error) {
|
||||
callbackCount++
|
||||
@@ -775,9 +782,12 @@ func TestMaxByErr(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result, err := MaxByErr(tt.input, func(item, max string) (bool, error) {
|
||||
return len(item) > len(max), nil
|
||||
t.Parallel()
|
||||
|
||||
result, err := MaxByErr(tt.input, func(item, mAx string) (bool, error) {
|
||||
return len(item) > len(mAx), nil
|
||||
})
|
||||
is.NoError(err)
|
||||
is.Equal(tt.expected, result)
|
||||
@@ -806,14 +816,17 @@ func TestMaxByErr(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, tt := range errorTests {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
callbackCount := 0
|
||||
result, err := MaxByErr(tt.input, func(item, max string) (bool, error) {
|
||||
result, err := MaxByErr(tt.input, func(item, mAx string) (bool, error) {
|
||||
callbackCount++
|
||||
if item == tt.errorAt {
|
||||
return false, testErr
|
||||
}
|
||||
return len(item) > len(max), nil
|
||||
return len(item) > len(mAx), nil
|
||||
})
|
||||
is.ErrorIs(err, testErr)
|
||||
is.Equal(tt.expectedCalls, callbackCount)
|
||||
@@ -897,9 +910,12 @@ func TestMaxIndexByErr(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result, index, err := MaxIndexByErr(tt.input, func(item, max string) (bool, error) {
|
||||
return len(item) > len(max), nil
|
||||
t.Parallel()
|
||||
|
||||
result, index, err := MaxIndexByErr(tt.input, func(item, mAx string) (bool, error) {
|
||||
return len(item) > len(mAx), nil
|
||||
})
|
||||
is.NoError(err)
|
||||
is.Equal(tt.expectedResult, result)
|
||||
@@ -935,14 +951,17 @@ func TestMaxIndexByErr(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, tt := range errorTests {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
callbackCount := 0
|
||||
result, index, err := MaxIndexByErr(tt.input, func(item, max string) (bool, error) {
|
||||
result, index, err := MaxIndexByErr(tt.input, func(item, mAx string) (bool, error) {
|
||||
callbackCount++
|
||||
if item == tt.errorAt {
|
||||
return false, testErr
|
||||
}
|
||||
return len(item) > len(max), nil
|
||||
return len(item) > len(mAx), nil
|
||||
})
|
||||
is.ErrorIs(err, testErr)
|
||||
is.Equal(tt.expectedCalls, callbackCount)
|
||||
@@ -1029,7 +1048,10 @@ func TestLatestByErr(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
result, err := LatestByErr(tt.input, func(i foo) (time.Time, error) {
|
||||
return i.bar, nil
|
||||
})
|
||||
@@ -1066,7 +1088,10 @@ func TestLatestByErr(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, tt := range errorTests {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
callbackCount := 0
|
||||
_, err := LatestByErr(tt.input, func(i foo) (time.Time, error) {
|
||||
callbackCount++
|
||||
|
||||
+3
-3
@@ -1,7 +1,7 @@
|
||||
package lo
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"errors"
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
@@ -400,7 +400,7 @@ func TestWithoutByErr(t *testing.T) {
|
||||
input: []User{{Name: "nick"}, {Name: "peter"}, {Name: "lily"}},
|
||||
iteratee: func(item User) (string, error) {
|
||||
if item.Name == "peter" {
|
||||
return "", fmt.Errorf("peter not allowed")
|
||||
return "", errors.New("peter not allowed")
|
||||
}
|
||||
return item.Name, nil
|
||||
},
|
||||
@@ -413,7 +413,7 @@ func TestWithoutByErr(t *testing.T) {
|
||||
name: "error on first element",
|
||||
input: []User{{Name: "nick"}, {Name: "peter"}},
|
||||
iteratee: func(item User) (string, error) {
|
||||
return "", fmt.Errorf("first element error")
|
||||
return "", errors.New("first element error")
|
||||
},
|
||||
exclude: []string{"nick"},
|
||||
want: nil,
|
||||
|
||||
+69
-68
@@ -1,6 +1,7 @@
|
||||
package lo
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"testing"
|
||||
@@ -173,7 +174,7 @@ func TestPickByErr(t *testing.T) {
|
||||
input: map[string]int{"foo": 1, "bar": 2, "baz": 3},
|
||||
predicate: func(key string, value int) (bool, error) {
|
||||
if key == "bar" {
|
||||
return false, fmt.Errorf("bar not allowed")
|
||||
return false, errors.New("bar not allowed")
|
||||
}
|
||||
return true, nil
|
||||
},
|
||||
@@ -317,7 +318,7 @@ func TestOmitByErr(t *testing.T) {
|
||||
input: map[string]int{"foo": 1, "bar": 2, "baz": 3},
|
||||
predicate: func(key string, value int) (bool, error) {
|
||||
if key == "bar" {
|
||||
return false, fmt.Errorf("bar not allowed")
|
||||
return false, errors.New("bar not allowed")
|
||||
}
|
||||
return true, nil
|
||||
},
|
||||
@@ -664,7 +665,7 @@ func TestMapValuesErr(t *testing.T) {
|
||||
input: map[int]int{1: 1, 2: 2},
|
||||
iteratee: func(x, _ int) (string, error) {
|
||||
if x == 1 {
|
||||
return "", fmt.Errorf("cannot process 1")
|
||||
return "", errors.New("cannot process 1")
|
||||
}
|
||||
return strconv.FormatInt(int64(x), 10), nil
|
||||
},
|
||||
@@ -849,7 +850,7 @@ func TestMapEntriesErr(t *testing.T) {
|
||||
input: map[string]int{"foo": 1, "bar": 2, "baz": 3},
|
||||
iteratee: func(k string, v int) (string, int, error) {
|
||||
if k == "bar" {
|
||||
return "", 0, fmt.Errorf("bar not allowed")
|
||||
return "", 0, errors.New("bar not allowed")
|
||||
}
|
||||
return k, v, nil
|
||||
},
|
||||
@@ -983,7 +984,7 @@ func TestMapEntriesErr(t *testing.T) {
|
||||
input: map[string]int{"a": 1, "b": 2},
|
||||
iteratee: func(k string, v int) (int, string, error) {
|
||||
if k == "b" {
|
||||
return 0, "", fmt.Errorf("cannot invert b")
|
||||
return 0, "", errors.New("cannot invert b")
|
||||
}
|
||||
return v, k, nil
|
||||
},
|
||||
@@ -1031,75 +1032,75 @@ func TestMapToSliceErr(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
input map[int]int64
|
||||
iteratee func(int, int64) (string, error)
|
||||
want []string
|
||||
wantErr string
|
||||
wantCallCount int // 0 means don't check (maps have no order)
|
||||
name string
|
||||
input map[int]int64
|
||||
iteratee func(int, int64) (string, error)
|
||||
want []string
|
||||
wantErr string
|
||||
wantCallCount int // 0 means don't check (maps have no order)
|
||||
}{
|
||||
{
|
||||
name: "successful transformation",
|
||||
input: map[int]int64{1: 5, 2: 6, 3: 7},
|
||||
iteratee: func(k int, v int64) (string, error) {
|
||||
return fmt.Sprintf("%d_%d", k, v), nil
|
||||
},
|
||||
want: []string{"1_5", "2_6", "3_7"},
|
||||
wantErr: "",
|
||||
wantCallCount: 0, // map iteration order is not deterministic
|
||||
},
|
||||
{
|
||||
name: "empty map",
|
||||
input: map[int]int64{},
|
||||
iteratee: func(k int, v int64) (string, error) {
|
||||
return fmt.Sprintf("%d_%d", k, v), nil
|
||||
},
|
||||
want: []string{},
|
||||
wantErr: "",
|
||||
wantCallCount: 0,
|
||||
},
|
||||
{
|
||||
name: "error on specific key",
|
||||
input: map[int]int64{1: 5, 2: 6, 3: 7},
|
||||
iteratee: func(k int, v int64) (string, error) {
|
||||
if k == 2 {
|
||||
return "", fmt.Errorf("key 2 not allowed")
|
||||
}
|
||||
return fmt.Sprintf("%d_%d", k, v), nil
|
||||
},
|
||||
want: nil,
|
||||
wantErr: "key 2 not allowed",
|
||||
wantCallCount: 0, // map iteration order is not deterministic
|
||||
},
|
||||
{
|
||||
name: "constant value",
|
||||
input: map[int]int64{1: 5, 2: 6, 3: 7},
|
||||
iteratee: func(k int, v int64) (string, error) {
|
||||
return "constant", nil
|
||||
},
|
||||
want: []string{"constant", "constant", "constant"},
|
||||
wantErr: "",
|
||||
wantCallCount: 0,
|
||||
},
|
||||
{
|
||||
name: "successful transformation",
|
||||
input: map[int]int64{1: 5, 2: 6, 3: 7},
|
||||
iteratee: func(k int, v int64) (string, error) {
|
||||
return fmt.Sprintf("%d_%d", k, v), nil
|
||||
},
|
||||
want: []string{"1_5", "2_6", "3_7"},
|
||||
wantErr: "",
|
||||
wantCallCount: 0, // map iteration order is not deterministic
|
||||
},
|
||||
{
|
||||
name: "empty map",
|
||||
input: map[int]int64{},
|
||||
iteratee: func(k int, v int64) (string, error) {
|
||||
return fmt.Sprintf("%d_%d", k, v), nil
|
||||
},
|
||||
want: []string{},
|
||||
wantErr: "",
|
||||
wantCallCount: 0,
|
||||
},
|
||||
{
|
||||
name: "error on specific key",
|
||||
input: map[int]int64{1: 5, 2: 6, 3: 7},
|
||||
iteratee: func(k int, v int64) (string, error) {
|
||||
if k == 2 {
|
||||
return "", errors.New("key 2 not allowed")
|
||||
}
|
||||
return fmt.Sprintf("%d_%d", k, v), nil
|
||||
},
|
||||
want: nil,
|
||||
wantErr: "key 2 not allowed",
|
||||
wantCallCount: 0, // map iteration order is not deterministic
|
||||
},
|
||||
{
|
||||
name: "constant value",
|
||||
input: map[int]int64{1: 5, 2: 6, 3: 7},
|
||||
iteratee: func(k int, v int64) (string, error) {
|
||||
return "constant", nil
|
||||
},
|
||||
want: []string{"constant", "constant", "constant"},
|
||||
wantErr: "",
|
||||
wantCallCount: 0,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
tt := tt // capture range variable
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
is := assert.New(t)
|
||||
tt := tt // capture range variable
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
is := assert.New(t)
|
||||
|
||||
got, err := MapToSliceErr(tt.input, tt.iteratee)
|
||||
got, err := MapToSliceErr(tt.input, tt.iteratee)
|
||||
|
||||
if tt.wantErr != "" {
|
||||
is.Error(err)
|
||||
is.Equal(tt.wantErr, err.Error())
|
||||
is.Nil(got)
|
||||
} else {
|
||||
is.NoError(err)
|
||||
is.ElementsMatch(tt.want, got)
|
||||
}
|
||||
})
|
||||
if tt.wantErr != "" {
|
||||
is.Error(err)
|
||||
is.Equal(tt.wantErr, err.Error())
|
||||
is.Nil(got)
|
||||
} else {
|
||||
is.NoError(err)
|
||||
is.ElementsMatch(tt.want, got)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -231,6 +231,7 @@ func TestProductBy(t *testing.T) {
|
||||
is.Equal(uint32(1), result8)
|
||||
}
|
||||
|
||||
//nolint:errcheck,forcetypeassert
|
||||
func TestProductByErr(t *testing.T) {
|
||||
t.Parallel()
|
||||
is := assert.New(t)
|
||||
@@ -286,7 +287,10 @@ func TestProductByErr(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
switch input := tt.input.(type) {
|
||||
case []float32:
|
||||
result, err := ProductByErr(input, func(n float32) (float32, error) { return n, nil })
|
||||
@@ -340,7 +344,10 @@ func TestProductByErr(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, tt := range errorTests {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
callbackCount := 0
|
||||
result, err := ProductByErr(tt.input, func(n int32) (int32, error) {
|
||||
callbackCount++
|
||||
@@ -386,6 +393,7 @@ func TestMeanBy(t *testing.T) {
|
||||
is.Equal(uint32(0), result4)
|
||||
}
|
||||
|
||||
//nolint:errcheck,forcetypeassert
|
||||
func TestMeanByErr(t *testing.T) {
|
||||
t.Parallel()
|
||||
is := assert.New(t)
|
||||
@@ -426,7 +434,10 @@ func TestMeanByErr(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
switch input := tt.input.(type) {
|
||||
case []float32:
|
||||
result, err := MeanByErr(input, func(n float32) (float32, error) { return n, nil })
|
||||
@@ -472,7 +483,10 @@ func TestMeanByErr(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, tt := range errorTests {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
callbackCount := 0
|
||||
_, err := MeanByErr(tt.input, func(n int32) (int32, error) {
|
||||
callbackCount++
|
||||
|
||||
+259
-258
@@ -1,6 +1,7 @@
|
||||
package lo
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"math"
|
||||
"sort"
|
||||
@@ -53,74 +54,74 @@ func TestMapErr(t *testing.T) {
|
||||
is := assert.New(t)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
input []int
|
||||
transform func(item int, index int) (string, error)
|
||||
wantResult []string
|
||||
wantErr bool
|
||||
errMsg string
|
||||
name string
|
||||
input []int
|
||||
transform func(item, index int) (string, error)
|
||||
wantResult []string
|
||||
wantErr bool
|
||||
errMsg string
|
||||
expectedCallbackCount int
|
||||
}{
|
||||
{
|
||||
name: "successful transformation",
|
||||
input: []int{1, 2, 3, 4},
|
||||
transform: func(x int, _ int) (string, error) {
|
||||
transform: func(x, _ int) (string, error) {
|
||||
return strconv.Itoa(x), nil
|
||||
},
|
||||
wantResult: []string{"1", "2", "3", "4"},
|
||||
wantErr: false,
|
||||
wantResult: []string{"1", "2", "3", "4"},
|
||||
wantErr: false,
|
||||
expectedCallbackCount: 4,
|
||||
},
|
||||
{
|
||||
name: "error at third element stops iteration",
|
||||
input: []int{1, 2, 3, 4},
|
||||
transform: func(x int, _ int) (string, error) {
|
||||
transform: func(x, _ int) (string, error) {
|
||||
if x == 3 {
|
||||
return "", fmt.Errorf("number 3 is not allowed")
|
||||
return "", errors.New("number 3 is not allowed")
|
||||
}
|
||||
return strconv.Itoa(x), nil
|
||||
},
|
||||
wantResult: nil,
|
||||
wantErr: true,
|
||||
errMsg: "number 3 is not allowed",
|
||||
wantResult: nil,
|
||||
wantErr: true,
|
||||
errMsg: "number 3 is not allowed",
|
||||
expectedCallbackCount: 3,
|
||||
},
|
||||
{
|
||||
name: "error at first element stops iteration immediately",
|
||||
input: []int{1, 2, 3, 4},
|
||||
transform: func(x int, _ int) (string, error) {
|
||||
transform: func(x, _ int) (string, error) {
|
||||
if x == 1 {
|
||||
return "", fmt.Errorf("number 1 is not allowed")
|
||||
return "", errors.New("number 1 is not allowed")
|
||||
}
|
||||
return strconv.Itoa(x), nil
|
||||
},
|
||||
wantResult: nil,
|
||||
wantErr: true,
|
||||
errMsg: "number 1 is not allowed",
|
||||
wantResult: nil,
|
||||
wantErr: true,
|
||||
errMsg: "number 1 is not allowed",
|
||||
expectedCallbackCount: 1,
|
||||
},
|
||||
{
|
||||
name: "error at last element",
|
||||
input: []int{1, 2, 3, 4},
|
||||
transform: func(x int, _ int) (string, error) {
|
||||
transform: func(x, _ int) (string, error) {
|
||||
if x == 4 {
|
||||
return "", fmt.Errorf("number 4 is not allowed")
|
||||
return "", errors.New("number 4 is not allowed")
|
||||
}
|
||||
return strconv.Itoa(x), nil
|
||||
},
|
||||
wantResult: nil,
|
||||
wantErr: true,
|
||||
errMsg: "number 4 is not allowed",
|
||||
wantResult: nil,
|
||||
wantErr: true,
|
||||
errMsg: "number 4 is not allowed",
|
||||
expectedCallbackCount: 4,
|
||||
},
|
||||
{
|
||||
name: "empty input slice",
|
||||
input: []int{},
|
||||
transform: func(x int, _ int) (string, error) {
|
||||
transform: func(x, _ int) (string, error) {
|
||||
return strconv.Itoa(x), nil
|
||||
},
|
||||
wantResult: []string{},
|
||||
wantErr: false,
|
||||
wantResult: []string{},
|
||||
wantErr: false,
|
||||
expectedCallbackCount: 0,
|
||||
},
|
||||
}
|
||||
@@ -132,7 +133,7 @@ func TestMapErr(t *testing.T) {
|
||||
|
||||
// Track callback count to test early return
|
||||
callbackCount := 0
|
||||
wrappedTransform := func(item int, index int) (string, error) {
|
||||
wrappedTransform := func(item, index int) (string, error) {
|
||||
callbackCount++
|
||||
return tt.transform(item, index)
|
||||
}
|
||||
@@ -218,12 +219,12 @@ func TestFlatMapErr(t *testing.T) {
|
||||
is := assert.New(t)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
input []int64
|
||||
transform func(item int64, index int) ([]string, error)
|
||||
wantResult []string
|
||||
wantErr bool
|
||||
errMsg string
|
||||
name string
|
||||
input []int64
|
||||
transform func(item int64, index int) ([]string, error)
|
||||
wantResult []string
|
||||
wantErr bool
|
||||
errMsg string
|
||||
expectedCallbackCount int
|
||||
}{
|
||||
{
|
||||
@@ -232,8 +233,8 @@ func TestFlatMapErr(t *testing.T) {
|
||||
transform: func(x int64, _ int) ([]string, error) {
|
||||
return []string{strconv.FormatInt(x, 10), strconv.FormatInt(x, 10)}, nil
|
||||
},
|
||||
wantResult: []string{"0", "0", "1", "1", "2", "2"},
|
||||
wantErr: false,
|
||||
wantResult: []string{"0", "0", "1", "1", "2", "2"},
|
||||
wantErr: false,
|
||||
expectedCallbackCount: 3,
|
||||
},
|
||||
{
|
||||
@@ -241,13 +242,13 @@ func TestFlatMapErr(t *testing.T) {
|
||||
input: []int64{0, 1, 2, 3},
|
||||
transform: func(x int64, _ int) ([]string, error) {
|
||||
if x == 1 {
|
||||
return nil, fmt.Errorf("number 1 is not allowed")
|
||||
return nil, errors.New("number 1 is not allowed")
|
||||
}
|
||||
return []string{strconv.FormatInt(x, 10)}, nil
|
||||
},
|
||||
wantResult: nil,
|
||||
wantErr: true,
|
||||
errMsg: "number 1 is not allowed",
|
||||
wantResult: nil,
|
||||
wantErr: true,
|
||||
errMsg: "number 1 is not allowed",
|
||||
expectedCallbackCount: 2,
|
||||
},
|
||||
{
|
||||
@@ -255,13 +256,13 @@ func TestFlatMapErr(t *testing.T) {
|
||||
input: []int64{0, 1, 2, 3},
|
||||
transform: func(x int64, _ int) ([]string, error) {
|
||||
if x == 0 {
|
||||
return nil, fmt.Errorf("number 0 is not allowed")
|
||||
return nil, errors.New("number 0 is not allowed")
|
||||
}
|
||||
return []string{strconv.FormatInt(x, 10)}, nil
|
||||
},
|
||||
wantResult: nil,
|
||||
wantErr: true,
|
||||
errMsg: "number 0 is not allowed",
|
||||
wantResult: nil,
|
||||
wantErr: true,
|
||||
errMsg: "number 0 is not allowed",
|
||||
expectedCallbackCount: 1,
|
||||
},
|
||||
{
|
||||
@@ -269,13 +270,13 @@ func TestFlatMapErr(t *testing.T) {
|
||||
input: []int64{0, 1, 2},
|
||||
transform: func(x int64, _ int) ([]string, error) {
|
||||
if x == 2 {
|
||||
return nil, fmt.Errorf("number 2 is not allowed")
|
||||
return nil, errors.New("number 2 is not allowed")
|
||||
}
|
||||
return []string{strconv.FormatInt(x, 10)}, nil
|
||||
},
|
||||
wantResult: nil,
|
||||
wantErr: true,
|
||||
errMsg: "number 2 is not allowed",
|
||||
wantResult: nil,
|
||||
wantErr: true,
|
||||
errMsg: "number 2 is not allowed",
|
||||
expectedCallbackCount: 3,
|
||||
},
|
||||
{
|
||||
@@ -284,8 +285,8 @@ func TestFlatMapErr(t *testing.T) {
|
||||
transform: func(x int64, _ int) ([]string, error) {
|
||||
return []string{strconv.FormatInt(x, 10)}, nil
|
||||
},
|
||||
wantResult: []string{},
|
||||
wantErr: false,
|
||||
wantResult: []string{},
|
||||
wantErr: false,
|
||||
expectedCallbackCount: 0,
|
||||
},
|
||||
{
|
||||
@@ -294,8 +295,8 @@ func TestFlatMapErr(t *testing.T) {
|
||||
transform: func(x int64, _ int) ([]string, error) {
|
||||
return []string{}, nil
|
||||
},
|
||||
wantResult: []string{},
|
||||
wantErr: false,
|
||||
wantResult: []string{},
|
||||
wantErr: false,
|
||||
expectedCallbackCount: 3,
|
||||
},
|
||||
{
|
||||
@@ -307,8 +308,8 @@ func TestFlatMapErr(t *testing.T) {
|
||||
}
|
||||
return []string{strconv.FormatInt(x, 10)}, nil
|
||||
},
|
||||
wantResult: []string{"1", "3"},
|
||||
wantErr: false,
|
||||
wantResult: []string{"1", "3"},
|
||||
wantErr: false,
|
||||
expectedCallbackCount: 3,
|
||||
},
|
||||
}
|
||||
@@ -372,13 +373,13 @@ func TestReduceErr(t *testing.T) {
|
||||
is := assert.New(t)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
input []int
|
||||
accumulator func(agg int, item int, index int) (int, error)
|
||||
initial int
|
||||
wantResult int
|
||||
wantErr bool
|
||||
errMsg string
|
||||
name string
|
||||
input []int
|
||||
accumulator func(agg, item, index int) (int, error)
|
||||
initial int
|
||||
wantResult int
|
||||
wantErr bool
|
||||
errMsg string
|
||||
expectedCallbackCount int
|
||||
}{
|
||||
{
|
||||
@@ -387,9 +388,9 @@ func TestReduceErr(t *testing.T) {
|
||||
accumulator: func(agg, item, _ int) (int, error) {
|
||||
return agg + item, nil
|
||||
},
|
||||
initial: 0,
|
||||
wantResult: 10,
|
||||
wantErr: false,
|
||||
initial: 0,
|
||||
wantResult: 10,
|
||||
wantErr: false,
|
||||
expectedCallbackCount: 4,
|
||||
},
|
||||
{
|
||||
@@ -397,14 +398,14 @@ func TestReduceErr(t *testing.T) {
|
||||
input: []int{1, 2, 3, 4},
|
||||
accumulator: func(agg, item, _ int) (int, error) {
|
||||
if item == 3 {
|
||||
return 0, fmt.Errorf("number 3 is not allowed")
|
||||
return 0, errors.New("number 3 is not allowed")
|
||||
}
|
||||
return agg + item, nil
|
||||
},
|
||||
initial: 0,
|
||||
wantResult: 0,
|
||||
wantErr: true,
|
||||
errMsg: "number 3 is not allowed",
|
||||
initial: 0,
|
||||
wantResult: 0,
|
||||
wantErr: true,
|
||||
errMsg: "number 3 is not allowed",
|
||||
expectedCallbackCount: 3,
|
||||
},
|
||||
{
|
||||
@@ -412,14 +413,14 @@ func TestReduceErr(t *testing.T) {
|
||||
input: []int{1, 2, 3, 4},
|
||||
accumulator: func(agg, item, _ int) (int, error) {
|
||||
if item == 1 {
|
||||
return 0, fmt.Errorf("number 1 is not allowed")
|
||||
return 0, errors.New("number 1 is not allowed")
|
||||
}
|
||||
return agg + item, nil
|
||||
},
|
||||
initial: 0,
|
||||
wantResult: 0,
|
||||
wantErr: true,
|
||||
errMsg: "number 1 is not allowed",
|
||||
initial: 0,
|
||||
wantResult: 0,
|
||||
wantErr: true,
|
||||
errMsg: "number 1 is not allowed",
|
||||
expectedCallbackCount: 1,
|
||||
},
|
||||
{
|
||||
@@ -427,14 +428,14 @@ func TestReduceErr(t *testing.T) {
|
||||
input: []int{1, 2, 3, 4},
|
||||
accumulator: func(agg, item, _ int) (int, error) {
|
||||
if item == 4 {
|
||||
return 0, fmt.Errorf("number 4 is not allowed")
|
||||
return 0, errors.New("number 4 is not allowed")
|
||||
}
|
||||
return agg + item, nil
|
||||
},
|
||||
initial: 0,
|
||||
wantResult: 0,
|
||||
wantErr: true,
|
||||
errMsg: "number 4 is not allowed",
|
||||
initial: 0,
|
||||
wantResult: 0,
|
||||
wantErr: true,
|
||||
errMsg: "number 4 is not allowed",
|
||||
expectedCallbackCount: 4,
|
||||
},
|
||||
{
|
||||
@@ -443,9 +444,9 @@ func TestReduceErr(t *testing.T) {
|
||||
accumulator: func(agg, item, _ int) (int, error) {
|
||||
return agg + item, nil
|
||||
},
|
||||
initial: 10,
|
||||
wantResult: 10,
|
||||
wantErr: false,
|
||||
initial: 10,
|
||||
wantResult: 10,
|
||||
wantErr: false,
|
||||
expectedCallbackCount: 0,
|
||||
},
|
||||
{
|
||||
@@ -454,9 +455,9 @@ func TestReduceErr(t *testing.T) {
|
||||
accumulator: func(agg, item, _ int) (int, error) {
|
||||
return agg + item, nil
|
||||
},
|
||||
initial: 10,
|
||||
wantResult: 20,
|
||||
wantErr: false,
|
||||
initial: 10,
|
||||
wantResult: 20,
|
||||
wantErr: false,
|
||||
expectedCallbackCount: 4,
|
||||
},
|
||||
}
|
||||
@@ -468,7 +469,7 @@ func TestReduceErr(t *testing.T) {
|
||||
|
||||
// Track callback count to test early return
|
||||
callbackCount := 0
|
||||
wrappedAccumulator := func(agg int, item int, index int) (int, error) {
|
||||
wrappedAccumulator := func(agg, item, index int) (int, error) {
|
||||
callbackCount++
|
||||
return tt.accumulator(agg, item, index)
|
||||
}
|
||||
@@ -511,13 +512,13 @@ func TestReduceRightErr(t *testing.T) {
|
||||
is := assert.New(t)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
input []int
|
||||
accumulator func(agg int, item int, index int) (int, error)
|
||||
initial int
|
||||
wantResult int
|
||||
wantErr bool
|
||||
errMsg string
|
||||
name string
|
||||
input []int
|
||||
accumulator func(agg, item, index int) (int, error)
|
||||
initial int
|
||||
wantResult int
|
||||
wantErr bool
|
||||
errMsg string
|
||||
expectedCallbackCount int
|
||||
}{
|
||||
{
|
||||
@@ -526,9 +527,9 @@ func TestReduceRightErr(t *testing.T) {
|
||||
accumulator: func(agg, item, _ int) (int, error) {
|
||||
return agg + item, nil
|
||||
},
|
||||
initial: 0,
|
||||
wantResult: 10,
|
||||
wantErr: false,
|
||||
initial: 0,
|
||||
wantResult: 10,
|
||||
wantErr: false,
|
||||
expectedCallbackCount: 4,
|
||||
},
|
||||
{
|
||||
@@ -536,14 +537,14 @@ func TestReduceRightErr(t *testing.T) {
|
||||
input: []int{1, 2, 3, 4},
|
||||
accumulator: func(agg, item, _ int) (int, error) {
|
||||
if item == 3 {
|
||||
return 0, fmt.Errorf("number 3 is not allowed")
|
||||
return 0, errors.New("number 3 is not allowed")
|
||||
}
|
||||
return agg + item, nil
|
||||
},
|
||||
initial: 0,
|
||||
wantResult: 0,
|
||||
wantErr: true,
|
||||
errMsg: "number 3 is not allowed",
|
||||
initial: 0,
|
||||
wantResult: 0,
|
||||
wantErr: true,
|
||||
errMsg: "number 3 is not allowed",
|
||||
expectedCallbackCount: 2,
|
||||
},
|
||||
{
|
||||
@@ -551,14 +552,14 @@ func TestReduceRightErr(t *testing.T) {
|
||||
input: []int{1, 2, 3, 4},
|
||||
accumulator: func(agg, item, _ int) (int, error) {
|
||||
if item == 4 {
|
||||
return 0, fmt.Errorf("number 4 is not allowed")
|
||||
return 0, errors.New("number 4 is not allowed")
|
||||
}
|
||||
return agg + item, nil
|
||||
},
|
||||
initial: 0,
|
||||
wantResult: 0,
|
||||
wantErr: true,
|
||||
errMsg: "number 4 is not allowed",
|
||||
initial: 0,
|
||||
wantResult: 0,
|
||||
wantErr: true,
|
||||
errMsg: "number 4 is not allowed",
|
||||
expectedCallbackCount: 1,
|
||||
},
|
||||
{
|
||||
@@ -566,14 +567,14 @@ func TestReduceRightErr(t *testing.T) {
|
||||
input: []int{1, 2, 3, 4},
|
||||
accumulator: func(agg, item, _ int) (int, error) {
|
||||
if item == 1 {
|
||||
return 0, fmt.Errorf("number 1 is not allowed")
|
||||
return 0, errors.New("number 1 is not allowed")
|
||||
}
|
||||
return agg + item, nil
|
||||
},
|
||||
initial: 0,
|
||||
wantResult: 0,
|
||||
wantErr: true,
|
||||
errMsg: "number 1 is not allowed",
|
||||
initial: 0,
|
||||
wantResult: 0,
|
||||
wantErr: true,
|
||||
errMsg: "number 1 is not allowed",
|
||||
expectedCallbackCount: 4,
|
||||
},
|
||||
{
|
||||
@@ -582,9 +583,9 @@ func TestReduceRightErr(t *testing.T) {
|
||||
accumulator: func(agg, item, _ int) (int, error) {
|
||||
return agg + item, nil
|
||||
},
|
||||
initial: 10,
|
||||
wantResult: 10,
|
||||
wantErr: false,
|
||||
initial: 10,
|
||||
wantResult: 10,
|
||||
wantErr: false,
|
||||
expectedCallbackCount: 0,
|
||||
},
|
||||
{
|
||||
@@ -593,9 +594,9 @@ func TestReduceRightErr(t *testing.T) {
|
||||
accumulator: func(agg, item, _ int) (int, error) {
|
||||
return agg + item, nil
|
||||
},
|
||||
initial: 10,
|
||||
wantResult: 20,
|
||||
wantErr: false,
|
||||
initial: 10,
|
||||
wantResult: 20,
|
||||
wantErr: false,
|
||||
expectedCallbackCount: 4,
|
||||
},
|
||||
}
|
||||
@@ -607,7 +608,7 @@ func TestReduceRightErr(t *testing.T) {
|
||||
|
||||
// Track callback count to test early return
|
||||
callbackCount := 0
|
||||
wrappedAccumulator := func(agg int, item int, index int) (int, error) {
|
||||
wrappedAccumulator := func(agg, item, index int) (int, error) {
|
||||
callbackCount++
|
||||
return tt.accumulator(agg, item, index)
|
||||
}
|
||||
@@ -705,12 +706,12 @@ func TestUniqByErr(t *testing.T) {
|
||||
is := assert.New(t)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
input []int
|
||||
iteratee func(item int) (int, error)
|
||||
wantResult []int
|
||||
wantErr bool
|
||||
errMsg string
|
||||
name string
|
||||
input []int
|
||||
iteratee func(item int) (int, error)
|
||||
wantResult []int
|
||||
wantErr bool
|
||||
errMsg string
|
||||
expectedCallbackCount int
|
||||
}{
|
||||
{
|
||||
@@ -719,8 +720,8 @@ func TestUniqByErr(t *testing.T) {
|
||||
iteratee: func(i int) (int, error) {
|
||||
return i % 3, nil
|
||||
},
|
||||
wantResult: []int{0, 1, 2},
|
||||
wantErr: false,
|
||||
wantResult: []int{0, 1, 2},
|
||||
wantErr: false,
|
||||
expectedCallbackCount: 6,
|
||||
},
|
||||
{
|
||||
@@ -728,13 +729,13 @@ func TestUniqByErr(t *testing.T) {
|
||||
input: []int{0, 1, 2, 3, 4, 5},
|
||||
iteratee: func(i int) (int, error) {
|
||||
if i == 3 {
|
||||
return 0, fmt.Errorf("number 3 is not allowed")
|
||||
return 0, errors.New("number 3 is not allowed")
|
||||
}
|
||||
return i % 3, nil
|
||||
},
|
||||
wantResult: nil,
|
||||
wantErr: true,
|
||||
errMsg: "number 3 is not allowed",
|
||||
wantResult: nil,
|
||||
wantErr: true,
|
||||
errMsg: "number 3 is not allowed",
|
||||
expectedCallbackCount: 4,
|
||||
},
|
||||
{
|
||||
@@ -742,13 +743,13 @@ func TestUniqByErr(t *testing.T) {
|
||||
input: []int{0, 1, 2, 3, 4, 5},
|
||||
iteratee: func(i int) (int, error) {
|
||||
if i == 0 {
|
||||
return 0, fmt.Errorf("number 0 is not allowed")
|
||||
return 0, errors.New("number 0 is not allowed")
|
||||
}
|
||||
return i % 3, nil
|
||||
},
|
||||
wantResult: nil,
|
||||
wantErr: true,
|
||||
errMsg: "number 0 is not allowed",
|
||||
wantResult: nil,
|
||||
wantErr: true,
|
||||
errMsg: "number 0 is not allowed",
|
||||
expectedCallbackCount: 1,
|
||||
},
|
||||
{
|
||||
@@ -756,13 +757,13 @@ func TestUniqByErr(t *testing.T) {
|
||||
input: []int{0, 1, 2, 3, 4, 5},
|
||||
iteratee: func(i int) (int, error) {
|
||||
if i == 5 {
|
||||
return 0, fmt.Errorf("number 5 is not allowed")
|
||||
return 0, errors.New("number 5 is not allowed")
|
||||
}
|
||||
return i % 3, nil
|
||||
},
|
||||
wantResult: nil,
|
||||
wantErr: true,
|
||||
errMsg: "number 5 is not allowed",
|
||||
wantResult: nil,
|
||||
wantErr: true,
|
||||
errMsg: "number 5 is not allowed",
|
||||
expectedCallbackCount: 6,
|
||||
},
|
||||
{
|
||||
@@ -771,8 +772,8 @@ func TestUniqByErr(t *testing.T) {
|
||||
iteratee: func(i int) (int, error) {
|
||||
return i % 3, nil
|
||||
},
|
||||
wantResult: []int{},
|
||||
wantErr: false,
|
||||
wantResult: []int{},
|
||||
wantErr: false,
|
||||
expectedCallbackCount: 0,
|
||||
},
|
||||
{
|
||||
@@ -781,8 +782,8 @@ func TestUniqByErr(t *testing.T) {
|
||||
iteratee: func(i int) (int, error) {
|
||||
return i % 3, nil
|
||||
},
|
||||
wantResult: []int{1},
|
||||
wantErr: false,
|
||||
wantResult: []int{1},
|
||||
wantErr: false,
|
||||
expectedCallbackCount: 4,
|
||||
},
|
||||
{
|
||||
@@ -791,8 +792,8 @@ func TestUniqByErr(t *testing.T) {
|
||||
iteratee: func(i int) (int, error) {
|
||||
return i, nil
|
||||
},
|
||||
wantResult: []int{0, 1, 2, 3},
|
||||
wantErr: false,
|
||||
wantResult: []int{0, 1, 2, 3},
|
||||
wantErr: false,
|
||||
expectedCallbackCount: 4,
|
||||
},
|
||||
}
|
||||
@@ -852,12 +853,12 @@ func TestGroupByErr(t *testing.T) {
|
||||
is := assert.New(t)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
input []int
|
||||
iteratee func(item int) (int, error)
|
||||
wantResult map[int][]int
|
||||
wantErr bool
|
||||
errMsg string
|
||||
name string
|
||||
input []int
|
||||
iteratee func(item int) (int, error)
|
||||
wantResult map[int][]int
|
||||
wantErr bool
|
||||
errMsg string
|
||||
expectedCallbackCount int
|
||||
}{
|
||||
{
|
||||
@@ -871,7 +872,7 @@ func TestGroupByErr(t *testing.T) {
|
||||
1: {1, 4},
|
||||
2: {2, 5},
|
||||
},
|
||||
wantErr: false,
|
||||
wantErr: false,
|
||||
expectedCallbackCount: 6,
|
||||
},
|
||||
{
|
||||
@@ -879,13 +880,13 @@ func TestGroupByErr(t *testing.T) {
|
||||
input: []int{0, 1, 2, 3, 4, 5},
|
||||
iteratee: func(i int) (int, error) {
|
||||
if i == 3 {
|
||||
return 0, fmt.Errorf("number 3 is not allowed")
|
||||
return 0, errors.New("number 3 is not allowed")
|
||||
}
|
||||
return i % 3, nil
|
||||
},
|
||||
wantResult: nil,
|
||||
wantErr: true,
|
||||
errMsg: "number 3 is not allowed",
|
||||
wantResult: nil,
|
||||
wantErr: true,
|
||||
errMsg: "number 3 is not allowed",
|
||||
expectedCallbackCount: 4,
|
||||
},
|
||||
{
|
||||
@@ -893,13 +894,13 @@ func TestGroupByErr(t *testing.T) {
|
||||
input: []int{0, 1, 2, 3, 4, 5},
|
||||
iteratee: func(i int) (int, error) {
|
||||
if i == 0 {
|
||||
return 0, fmt.Errorf("number 0 is not allowed")
|
||||
return 0, errors.New("number 0 is not allowed")
|
||||
}
|
||||
return i % 3, nil
|
||||
},
|
||||
wantResult: nil,
|
||||
wantErr: true,
|
||||
errMsg: "number 0 is not allowed",
|
||||
wantResult: nil,
|
||||
wantErr: true,
|
||||
errMsg: "number 0 is not allowed",
|
||||
expectedCallbackCount: 1,
|
||||
},
|
||||
{
|
||||
@@ -907,13 +908,13 @@ func TestGroupByErr(t *testing.T) {
|
||||
input: []int{0, 1, 2, 3, 4, 5},
|
||||
iteratee: func(i int) (int, error) {
|
||||
if i == 5 {
|
||||
return 0, fmt.Errorf("number 5 is not allowed")
|
||||
return 0, errors.New("number 5 is not allowed")
|
||||
}
|
||||
return i % 3, nil
|
||||
},
|
||||
wantResult: nil,
|
||||
wantErr: true,
|
||||
errMsg: "number 5 is not allowed",
|
||||
wantResult: nil,
|
||||
wantErr: true,
|
||||
errMsg: "number 5 is not allowed",
|
||||
expectedCallbackCount: 6,
|
||||
},
|
||||
{
|
||||
@@ -922,8 +923,8 @@ func TestGroupByErr(t *testing.T) {
|
||||
iteratee: func(i int) (int, error) {
|
||||
return i % 3, nil
|
||||
},
|
||||
wantResult: map[int][]int{},
|
||||
wantErr: false,
|
||||
wantResult: map[int][]int{},
|
||||
wantErr: false,
|
||||
expectedCallbackCount: 0,
|
||||
},
|
||||
{
|
||||
@@ -935,7 +936,7 @@ func TestGroupByErr(t *testing.T) {
|
||||
wantResult: map[int][]int{
|
||||
0: {3, 6, 9, 12},
|
||||
},
|
||||
wantErr: false,
|
||||
wantErr: false,
|
||||
expectedCallbackCount: 4,
|
||||
},
|
||||
}
|
||||
@@ -1019,12 +1020,12 @@ func TestGroupByMapErr(t *testing.T) {
|
||||
is := assert.New(t)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
input []int
|
||||
transform func(item int) (int, int, error)
|
||||
wantResult map[int][]int
|
||||
wantErr bool
|
||||
errMsg string
|
||||
name string
|
||||
input []int
|
||||
transform func(item int) (int, int, error)
|
||||
wantResult map[int][]int
|
||||
wantErr bool
|
||||
errMsg string
|
||||
expectedCallbackCount int
|
||||
}{
|
||||
{
|
||||
@@ -1038,7 +1039,7 @@ func TestGroupByMapErr(t *testing.T) {
|
||||
1: {2, 8},
|
||||
2: {4, 10},
|
||||
},
|
||||
wantErr: false,
|
||||
wantErr: false,
|
||||
expectedCallbackCount: 6,
|
||||
},
|
||||
{
|
||||
@@ -1046,13 +1047,13 @@ func TestGroupByMapErr(t *testing.T) {
|
||||
input: []int{0, 1, 2, 3, 4, 5},
|
||||
transform: func(i int) (int, int, error) {
|
||||
if i == 3 {
|
||||
return 0, 0, fmt.Errorf("number 3 is not allowed")
|
||||
return 0, 0, errors.New("number 3 is not allowed")
|
||||
}
|
||||
return i % 3, i * 2, nil
|
||||
},
|
||||
wantResult: nil,
|
||||
wantErr: true,
|
||||
errMsg: "number 3 is not allowed",
|
||||
wantResult: nil,
|
||||
wantErr: true,
|
||||
errMsg: "number 3 is not allowed",
|
||||
expectedCallbackCount: 4,
|
||||
},
|
||||
{
|
||||
@@ -1060,13 +1061,13 @@ func TestGroupByMapErr(t *testing.T) {
|
||||
input: []int{0, 1, 2, 3, 4, 5},
|
||||
transform: func(i int) (int, int, error) {
|
||||
if i == 0 {
|
||||
return 0, 0, fmt.Errorf("number 0 is not allowed")
|
||||
return 0, 0, errors.New("number 0 is not allowed")
|
||||
}
|
||||
return i % 3, i * 2, nil
|
||||
},
|
||||
wantResult: nil,
|
||||
wantErr: true,
|
||||
errMsg: "number 0 is not allowed",
|
||||
wantResult: nil,
|
||||
wantErr: true,
|
||||
errMsg: "number 0 is not allowed",
|
||||
expectedCallbackCount: 1,
|
||||
},
|
||||
{
|
||||
@@ -1074,13 +1075,13 @@ func TestGroupByMapErr(t *testing.T) {
|
||||
input: []int{0, 1, 2, 3, 4, 5},
|
||||
transform: func(i int) (int, int, error) {
|
||||
if i == 5 {
|
||||
return 0, 0, fmt.Errorf("number 5 is not allowed")
|
||||
return 0, 0, errors.New("number 5 is not allowed")
|
||||
}
|
||||
return i % 3, i * 2, nil
|
||||
},
|
||||
wantResult: nil,
|
||||
wantErr: true,
|
||||
errMsg: "number 5 is not allowed",
|
||||
wantResult: nil,
|
||||
wantErr: true,
|
||||
errMsg: "number 5 is not allowed",
|
||||
expectedCallbackCount: 6,
|
||||
},
|
||||
{
|
||||
@@ -1089,8 +1090,8 @@ func TestGroupByMapErr(t *testing.T) {
|
||||
transform: func(i int) (int, int, error) {
|
||||
return i % 3, i * 2, nil
|
||||
},
|
||||
wantResult: map[int][]int{},
|
||||
wantErr: false,
|
||||
wantResult: map[int][]int{},
|
||||
wantErr: false,
|
||||
expectedCallbackCount: 0,
|
||||
},
|
||||
{
|
||||
@@ -1102,7 +1103,7 @@ func TestGroupByMapErr(t *testing.T) {
|
||||
wantResult: map[int][]int{
|
||||
0: {3, 6, 9, 12},
|
||||
},
|
||||
wantErr: false,
|
||||
wantErr: false,
|
||||
expectedCallbackCount: 4,
|
||||
},
|
||||
}
|
||||
@@ -1296,80 +1297,80 @@ func TestPartitionByErr(t *testing.T) {
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
input []int
|
||||
iteratee func(item int) (string, error)
|
||||
wantResult [][]int
|
||||
wantErr bool
|
||||
errMsg string
|
||||
name string
|
||||
input []int
|
||||
iteratee func(item int) (string, error)
|
||||
wantResult [][]int
|
||||
wantErr bool
|
||||
errMsg string
|
||||
expectedCallbackCount int
|
||||
}{
|
||||
{
|
||||
name: "successful partition",
|
||||
input: []int{-2, -1, 0, 1, 2, 3, 4, 5},
|
||||
iteratee: oddEven,
|
||||
wantResult: [][]int{{-2, -1}, {0, 2, 4}, {1, 3, 5}},
|
||||
wantErr: false,
|
||||
name: "successful partition",
|
||||
input: []int{-2, -1, 0, 1, 2, 3, 4, 5},
|
||||
iteratee: oddEven,
|
||||
wantResult: [][]int{{-2, -1}, {0, 2, 4}, {1, 3, 5}},
|
||||
wantErr: false,
|
||||
expectedCallbackCount: 8,
|
||||
},
|
||||
{
|
||||
name: "error at fifth element stops iteration",
|
||||
name: "error at fifth element stops iteration",
|
||||
input: []int{-2, -1, 0, 1, 2, 3},
|
||||
iteratee: func(x int) (string, error) {
|
||||
if x == 2 {
|
||||
return "", fmt.Errorf("number 2 is not allowed")
|
||||
return "", errors.New("number 2 is not allowed")
|
||||
}
|
||||
return oddEven(x)
|
||||
},
|
||||
wantResult: nil,
|
||||
wantErr: true,
|
||||
errMsg: "number 2 is not allowed",
|
||||
wantResult: nil,
|
||||
wantErr: true,
|
||||
errMsg: "number 2 is not allowed",
|
||||
expectedCallbackCount: 5,
|
||||
},
|
||||
{
|
||||
name: "error at first element stops iteration immediately",
|
||||
name: "error at first element stops iteration immediately",
|
||||
input: []int{-2, -1, 0, 1},
|
||||
iteratee: func(x int) (string, error) {
|
||||
if x == -2 {
|
||||
return "", fmt.Errorf("number -2 is not allowed")
|
||||
return "", errors.New("number -2 is not allowed")
|
||||
}
|
||||
return oddEven(x)
|
||||
},
|
||||
wantResult: nil,
|
||||
wantErr: true,
|
||||
errMsg: "number -2 is not allowed",
|
||||
wantResult: nil,
|
||||
wantErr: true,
|
||||
errMsg: "number -2 is not allowed",
|
||||
expectedCallbackCount: 1,
|
||||
},
|
||||
{
|
||||
name: "error at last element",
|
||||
name: "error at last element",
|
||||
input: []int{-2, -1, 0, 1, 2},
|
||||
iteratee: func(x int) (string, error) {
|
||||
if x == 2 {
|
||||
return "", fmt.Errorf("number 2 is not allowed")
|
||||
return "", errors.New("number 2 is not allowed")
|
||||
}
|
||||
return oddEven(x)
|
||||
},
|
||||
wantResult: nil,
|
||||
wantErr: true,
|
||||
errMsg: "number 2 is not allowed",
|
||||
wantResult: nil,
|
||||
wantErr: true,
|
||||
errMsg: "number 2 is not allowed",
|
||||
expectedCallbackCount: 5,
|
||||
},
|
||||
{
|
||||
name: "empty input slice",
|
||||
input: []int{},
|
||||
iteratee: oddEven,
|
||||
wantResult: [][]int{},
|
||||
wantErr: false,
|
||||
name: "empty input slice",
|
||||
input: []int{},
|
||||
iteratee: oddEven,
|
||||
wantResult: [][]int{},
|
||||
wantErr: false,
|
||||
expectedCallbackCount: 0,
|
||||
},
|
||||
{
|
||||
name: "all elements in same partition",
|
||||
name: "all elements in same partition",
|
||||
input: []int{1, 3, 5},
|
||||
iteratee: func(x int) (string, error) {
|
||||
return "odd", nil
|
||||
},
|
||||
wantResult: [][]int{{1, 3, 5}},
|
||||
wantErr: false,
|
||||
wantResult: [][]int{{1, 3, 5}},
|
||||
wantErr: false,
|
||||
expectedCallbackCount: 3,
|
||||
},
|
||||
}
|
||||
@@ -1574,75 +1575,75 @@ func TestKeyByErr(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
input []string
|
||||
iteratee func(item string) (int, error)
|
||||
wantResult map[int]string
|
||||
wantErr bool
|
||||
errMsg string
|
||||
name string
|
||||
input []string
|
||||
iteratee func(item string) (int, error)
|
||||
wantResult map[int]string
|
||||
wantErr bool
|
||||
errMsg string
|
||||
expectedCallbackCount int
|
||||
}{
|
||||
{
|
||||
name: "empty slice",
|
||||
input: []string{},
|
||||
iteratee: func(s string) (int, error) { return len(s), nil },
|
||||
wantResult: map[int]string{},
|
||||
wantErr: false,
|
||||
name: "empty slice",
|
||||
input: []string{},
|
||||
iteratee: func(s string) (int, error) { return len(s), nil },
|
||||
wantResult: map[int]string{},
|
||||
wantErr: false,
|
||||
expectedCallbackCount: 0,
|
||||
},
|
||||
{
|
||||
name: "success case",
|
||||
input: []string{"a", "aa", "aaa"},
|
||||
iteratee: func(s string) (int, error) { return len(s), nil },
|
||||
wantResult: map[int]string{1: "a", 2: "aa", 3: "aaa"},
|
||||
wantErr: false,
|
||||
name: "success case",
|
||||
input: []string{"a", "aa", "aaa"},
|
||||
iteratee: func(s string) (int, error) { return len(s), nil },
|
||||
wantResult: map[int]string{1: "a", 2: "aa", 3: "aaa"},
|
||||
wantErr: false,
|
||||
expectedCallbackCount: 3,
|
||||
},
|
||||
{
|
||||
name: "error stops iteration - first item",
|
||||
name: "error stops iteration - first item",
|
||||
input: []string{"a", "aa", "aaa"},
|
||||
iteratee: func(s string) (int, error) {
|
||||
return 0, fmt.Errorf("error on %s", s)
|
||||
},
|
||||
wantResult: nil,
|
||||
wantErr: true,
|
||||
errMsg: "error on a",
|
||||
wantResult: nil,
|
||||
wantErr: true,
|
||||
errMsg: "error on a",
|
||||
expectedCallbackCount: 1,
|
||||
},
|
||||
{
|
||||
name: "error stops iteration - middle item",
|
||||
name: "error stops iteration - middle item",
|
||||
input: []string{"a", "aa", "aaa"},
|
||||
iteratee: func(s string) (int, error) {
|
||||
if s == "aa" {
|
||||
return 0, fmt.Errorf("middle error")
|
||||
return 0, errors.New("middle error")
|
||||
}
|
||||
return len(s), nil
|
||||
},
|
||||
wantResult: nil,
|
||||
wantErr: true,
|
||||
errMsg: "middle error",
|
||||
wantResult: nil,
|
||||
wantErr: true,
|
||||
errMsg: "middle error",
|
||||
expectedCallbackCount: 2,
|
||||
},
|
||||
{
|
||||
name: "error stops iteration - last item",
|
||||
name: "error stops iteration - last item",
|
||||
input: []string{"a", "aa", "aaa"},
|
||||
iteratee: func(s string) (int, error) {
|
||||
if s == "aaa" {
|
||||
return 0, fmt.Errorf("last error")
|
||||
return 0, errors.New("last error")
|
||||
}
|
||||
return len(s), nil
|
||||
},
|
||||
wantResult: nil,
|
||||
wantErr: true,
|
||||
errMsg: "last error",
|
||||
wantResult: nil,
|
||||
wantErr: true,
|
||||
errMsg: "last error",
|
||||
expectedCallbackCount: 3,
|
||||
},
|
||||
{
|
||||
name: "duplicate keys",
|
||||
input: []string{"a", "b", "c"},
|
||||
iteratee: func(s string) (int, error) { return 1, nil },
|
||||
wantResult: map[int]string{1: "c"},
|
||||
wantErr: false,
|
||||
name: "duplicate keys",
|
||||
input: []string{"a", "b", "c"},
|
||||
iteratee: func(s string) (int, error) { return 1, nil },
|
||||
wantResult: map[int]string{1: "c"},
|
||||
wantErr: false,
|
||||
expectedCallbackCount: 3,
|
||||
},
|
||||
}
|
||||
@@ -2272,7 +2273,7 @@ func TestCountByErr(t *testing.T) {
|
||||
name: "error on first element",
|
||||
input: []int{1, 2, 3},
|
||||
predicate: func(i int) (bool, error) {
|
||||
return false, fmt.Errorf("first element error")
|
||||
return false, errors.New("first element error")
|
||||
},
|
||||
want: 0,
|
||||
wantErr: "first element error",
|
||||
|
||||
Reference in New Issue
Block a user