Initial commit of modash
This was taken from github.com/lmika/gopkgs/fp
This commit is contained in:
commit
a20530ddfd
20 changed files with 425 additions and 0 deletions
58
moslice/map_test.go
Normal file
58
moslice/map_test.go
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
package moslice_test
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"lmika.dev/pkg/modash/moslice"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestMap(t *testing.T) {
|
||||
t.Run("should return a mapped slice", func(t *testing.T) {
|
||||
ts := []int{1, 2, 3}
|
||||
us := moslice.Map(ts, func(x int) int { return x + 2 })
|
||||
|
||||
assert.Equal(t, []int{3, 4, 5}, us)
|
||||
})
|
||||
|
||||
t.Run("should return nil if passed in nil", func(t *testing.T) {
|
||||
ts := []int(nil)
|
||||
us := moslice.Map(ts, func(x int) int { return x + 2 })
|
||||
|
||||
assert.Nil(t, us)
|
||||
})
|
||||
}
|
||||
|
||||
func TestMapWithError(t *testing.T) {
|
||||
t.Run("should return a mapped slice with no error", func(t *testing.T) {
|
||||
ts := []int{1, 2, 3}
|
||||
|
||||
us, err := moslice.MapWithError(ts, func(x int) (int, error) { return x + 2, nil })
|
||||
|
||||
assert.Equal(t, []int{3, 4, 5}, us)
|
||||
assert.NoError(t, err)
|
||||
})
|
||||
|
||||
t.Run("should return nil with an error when mapping function returns an error", func(t *testing.T) {
|
||||
ts := []int{1, 2, 3}
|
||||
|
||||
us, err := moslice.MapWithError(ts, func(x int) (int, error) {
|
||||
if x == 2 {
|
||||
return 0, errors.New("bang")
|
||||
}
|
||||
return x + 2, nil
|
||||
})
|
||||
|
||||
assert.Nil(t, us)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
|
||||
t.Run("should return nil if passed in nil", func(t *testing.T) {
|
||||
ts := []int(nil)
|
||||
us, err := moslice.MapWithError(ts, func(x int) (int, error) { return x + 2, nil })
|
||||
|
||||
assert.Nil(t, us)
|
||||
assert.NoError(t, err)
|
||||
})
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue