|
| 1 | +package godash_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/stretchr/testify/assert" |
| 5 | + "github.com/thecasualcoder/godash" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +func TestFilter(t *testing.T) { |
| 10 | + t.Run("should filter elements that fail predicate", func(t *testing.T) { |
| 11 | + input := []int{1, 2, 3, 4, 5, 6, 7, 8} |
| 12 | + var output []int |
| 13 | + |
| 14 | + err := godash.Filter(input, &output, func(a int) bool { |
| 15 | + return a%2 == 0 |
| 16 | + }) |
| 17 | + expected := []int{2, 4, 6, 8} |
| 18 | + |
| 19 | + assert.NoError(t, err) |
| 20 | + assert.Equal(t, expected, output) |
| 21 | + }) |
| 22 | + |
| 23 | + t.Run("should struct types", func(t *testing.T) { |
| 24 | + type person struct { |
| 25 | + name string |
| 26 | + age int |
| 27 | + } |
| 28 | + input := []person{ |
| 29 | + {"john", 30}, |
| 30 | + {"doe", 20}, |
| 31 | + {"foo", 40}, |
| 32 | + {"bar", 10}, |
| 33 | + } |
| 34 | + var output []person |
| 35 | + |
| 36 | + err := godash.Filter(input, &output, func(p person) bool { |
| 37 | + return p.age > 20 |
| 38 | + }) |
| 39 | + expected := []person{ |
| 40 | + {"john", 30}, |
| 41 | + {"foo", 40}, |
| 42 | + } |
| 43 | + |
| 44 | + assert.NoError(t, err) |
| 45 | + assert.Equal(t, expected, output) |
| 46 | + }) |
| 47 | + |
| 48 | + t.Run("should validate predicate's arg", func(t *testing.T) { |
| 49 | + input := []int{1, 2, 3, 4, 5, 6, 7, 8} |
| 50 | + var output []int |
| 51 | + |
| 52 | + err := godash.Filter(input, &output, func(a string) bool { |
| 53 | + return a == "" |
| 54 | + }) |
| 55 | + |
| 56 | + assert.EqualError(t, err, "predicate function's first argument has to be the type (int) instead of (string)") |
| 57 | + }) |
| 58 | + |
| 59 | + t.Run("should validate predicate's return type", func(t *testing.T) { |
| 60 | + input := []int{1, 2, 3, 4, 5, 6, 7, 8} |
| 61 | + var output []int |
| 62 | + |
| 63 | + { |
| 64 | + err := godash.Filter(input, &output, func(a int) int { |
| 65 | + return a |
| 66 | + }) |
| 67 | + assert.EqualError(t, err, "predicate function should return only a (boolean) and not a (int)") |
| 68 | + } |
| 69 | + { |
| 70 | + err := godash.Filter(input, &output, func(int) (int, bool) { |
| 71 | + return 1, true |
| 72 | + }) |
| 73 | + assert.EqualError(t, err, "predicate function should return only one return value - a boolean") |
| 74 | + } |
| 75 | + }) |
| 76 | + |
| 77 | + t.Run("should validate output's type", func(t *testing.T) { |
| 78 | + input := []int{1, 2, 3, 4, 5, 6, 7, 8} |
| 79 | + var output []string |
| 80 | + |
| 81 | + err := godash.Filter(input, &output, func(a int) bool { |
| 82 | + return a == 0 |
| 83 | + }) |
| 84 | + |
| 85 | + assert.EqualError(t, err, "input([]int) and output([]string) should be of the same Type") |
| 86 | + }) |
| 87 | + |
| 88 | + t.Run("should not panic if output is nil", func(t *testing.T) { |
| 89 | + in := []int{1, 2, 3} |
| 90 | + { |
| 91 | + var out []int |
| 92 | + |
| 93 | + err := godash.Filter(in, out, func(int) bool { return true }) |
| 94 | + |
| 95 | + assert.EqualError(t, err, "output is nil. Pass a reference to set output") |
| 96 | + } |
| 97 | + { |
| 98 | + err := godash.Filter(in, nil, func(int) bool { return true }) |
| 99 | + |
| 100 | + assert.EqualError(t, err, "output is nil. Pass a reference to set output") |
| 101 | + } |
| 102 | + }) |
| 103 | +} |
0 commit comments