30 lines
881 B
Go
30 lines
881 B
Go
|
package moslice_test
|
||
|
|
||
|
import (
|
||
|
"lmika.dev/pkg/modash/moslice"
|
||
|
"strings"
|
||
|
"testing"
|
||
|
|
||
|
"github.com/stretchr/testify/assert"
|
||
|
)
|
||
|
|
||
|
func TestFilter(t *testing.T) {
|
||
|
var (
|
||
|
ints = []int{1, 2, 3, 4, 5}
|
||
|
strs = []string{"foo", "bar", "baz"}
|
||
|
)
|
||
|
|
||
|
t.Run("should filter items matching the predicate", func(t *testing.T) {
|
||
|
assert.Equal(t, []int{2, 4}, moslice.Filter(ints, func(x int) bool { return x%2 == 0 }))
|
||
|
assert.Equal(t, []string{"bar", "baz"}, moslice.Filter(strs, func(x string) bool { return strings.Contains(x, "b") }))
|
||
|
})
|
||
|
|
||
|
t.Run("should moslice nil if the passed in slice is nil", func(t *testing.T) {
|
||
|
assert.Nil(t, moslice.Filter(nil, func(x int) bool { return x%2 == 0 }))
|
||
|
})
|
||
|
|
||
|
t.Run("should return empty slice if the passed in slice is empty slice", func(t *testing.T) {
|
||
|
assert.Equal(t, []int{}, moslice.Filter([]int{}, func(x int) bool { return x%2 == 0 }))
|
||
|
})
|
||
|
}
|