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