|
| 1 | +package godash_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "github.com/stretchr/testify/assert" |
| 6 | + "github.com/thecasualcoder/godash" |
| 7 | + "testing" |
| 8 | +) |
| 9 | + |
| 10 | +func TestAny(t *testing.T) { |
| 11 | + |
| 12 | + t.Run("should return err if predicate is not a function", func(t *testing.T) { |
| 13 | + in := []int{1, 2, 3} |
| 14 | + |
| 15 | + _, err := godash.Any(in, "not a func") |
| 16 | + |
| 17 | + assert.EqualError(t, err, "predicateFn has to be a function") |
| 18 | + }) |
| 19 | + |
| 20 | + t.Run("should return err if predicate function do not take exactly one argument", func(t *testing.T) { |
| 21 | + in := []int{1, 2, 3} |
| 22 | + |
| 23 | + { |
| 24 | + _, err := godash.Any(in, func() {}) |
| 25 | + |
| 26 | + assert.EqualError(t, err, "predicate function has to take only one argument") |
| 27 | + } |
| 28 | + { |
| 29 | + _, err := godash.Any(in, func(int, int) {}) |
| 30 | + |
| 31 | + assert.EqualError(t, err, "predicate function has to take only one argument") |
| 32 | + } |
| 33 | + }) |
| 34 | + |
| 35 | + t.Run("should return err if predicate function do not return exactly one value", func(t *testing.T) { |
| 36 | + in := []int{1, 2, 3} |
| 37 | + |
| 38 | + { |
| 39 | + _, err := godash.Any(in, func(int) {}) |
| 40 | + |
| 41 | + assert.EqualError(t, err, "predicate function should return only one return value") |
| 42 | + } |
| 43 | + { |
| 44 | + _, err := godash.Any(in, func(int) (bool, bool) { return true, true }) |
| 45 | + |
| 46 | + assert.EqualError(t, err, "predicate function should return only one return value") |
| 47 | + |
| 48 | + } |
| 49 | + }) |
| 50 | + |
| 51 | + t.Run("should return err if predicate function's return value is not a boolean", func(t *testing.T) { |
| 52 | + in := []int{1, 2, 3} |
| 53 | + |
| 54 | + _, err := godash.Any(in, func(int) int { return 0 }) |
| 55 | + |
| 56 | + assert.EqualError(t, err, "predicate function should return a boolean value") |
| 57 | + }) |
| 58 | + |
| 59 | + t.Run("should return err if input is not a slice", func(t *testing.T) { |
| 60 | + in := 1 |
| 61 | + |
| 62 | + _, err := godash.Any(in, func(int) bool { return true }) |
| 63 | + |
| 64 | + assert.EqualError(t, err, "not implemented for (int)") |
| 65 | + }) |
| 66 | + |
| 67 | + t.Run("should return err if there is a type mismatch between predicate function's argument and input slice", func(t *testing.T) { |
| 68 | + in := []string{"hello", "world"} |
| 69 | + |
| 70 | + _, err := godash.Any(in, func(int) bool { return true }) |
| 71 | + |
| 72 | + assert.EqualError(t, err, "predicate function's argument (int) has to be (string)") |
| 73 | + }) |
| 74 | + |
| 75 | + t.Run("should return true if predicate passes for any of the element in input slice", func(t *testing.T) { |
| 76 | + in := []int{1, 1, 2, 3, 5, 8, 13} |
| 77 | + |
| 78 | + output, err := godash.Any(in, func(elem int) bool { return elem%5 == 0 }) |
| 79 | + |
| 80 | + assert.NoError(t, err) |
| 81 | + assert.True(t, output) |
| 82 | + }) |
| 83 | + |
| 84 | + t.Run("should return false if predicate fails for all the elements in input slice", func(t *testing.T) { |
| 85 | + in := []int{1, 1, 2, 3, 5, 8, 13} |
| 86 | + |
| 87 | + output, err := godash.Any(in, func(num int) bool { return num%6 == 0 }) |
| 88 | + |
| 89 | + assert.NoError(t, err) |
| 90 | + assert.False(t, output) |
| 91 | + }) |
| 92 | + |
| 93 | + t.Run("should support structs", func(t *testing.T) { |
| 94 | + type person struct { |
| 95 | + name string |
| 96 | + age int |
| 97 | + } |
| 98 | + in := []person{ |
| 99 | + {name: "John", age: 12}, |
| 100 | + {name: "Doe", age: 25}, |
| 101 | + } |
| 102 | + |
| 103 | + { |
| 104 | + output, err := godash.Any(in, func(person person) bool { return person.age < 18 }) |
| 105 | + |
| 106 | + assert.NoError(t, err) |
| 107 | + assert.True(t, output) |
| 108 | + } |
| 109 | + { |
| 110 | + output, err := godash.Any(in, func(person person) bool { return person.age < 30 }) |
| 111 | + |
| 112 | + assert.NoError(t, err) |
| 113 | + assert.True(t, output) |
| 114 | + } |
| 115 | + }) |
| 116 | +} |
| 117 | + |
| 118 | +func ExampleAny() { |
| 119 | + input := []int{0, 1, 2, 3, 4} |
| 120 | + |
| 121 | + output, _ := godash.Any(input, func(num int) bool { |
| 122 | + return num%3 == 0 |
| 123 | + }) |
| 124 | + |
| 125 | + fmt.Println(output) |
| 126 | + |
| 127 | + // Output: true |
| 128 | +} |
0 commit comments