Skip to content

Commit 8ebf089

Browse files
authored
Merge pull request #4 from repeale/test/add-code-coverage
test: add code coverage
2 parents c08d100 + 2df122d commit 8ebf089

11 files changed

+367
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ func sumTwo(x int) int {
196196
return x + 2
197197
}
198198

199-
Pipe2(fp.Filter(isPositive), fp.Map(sumTwo))([]int{1, 2, 3, -1})
199+
Compose2(fp.Filter(isPositive), fp.Map(sumTwo))([]int{1, 2, 3, -1})
200200

201201
// => []int{3,4,5,1}
202202
```

compose_test.go

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package fp
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func TestCompose2_Example(t *testing.T) {
9+
res := Compose2(
10+
Map(func(x int) int { return x + 2 }),
11+
Filter(func(x int) bool { return x > 0 }))([]int{1, 2, 3, -1})
12+
expected := []int{3, 4, 5}
13+
14+
if reflect.DeepEqual(res, expected) == false {
15+
t.Error("Should perform right-to-left function composition of two functions. Received:", res)
16+
}
17+
}
18+
19+
func TestCompose3_Example(t *testing.T) {
20+
res := Compose3(
21+
Map(func(x int) int { return x + 2 }),
22+
Map(func(x int) int { return x + 2 }),
23+
Filter(func(x int) bool { return x > 0 }))([]int{1, 2, 3, -1})
24+
expected := []int{5, 6, 7}
25+
26+
if reflect.DeepEqual(res, expected) == false {
27+
t.Error("Should perform right-to-left function composition of two functions. Received:", res)
28+
}
29+
}
30+
31+
func TestCompose4_Example(t *testing.T) {
32+
res := Compose4(
33+
Map(func(x int) int { return x + 2 }),
34+
Map(func(x int) int { return x + 2 }),
35+
Map(func(x int) int { return x + 2 }),
36+
Filter(func(x int) bool { return x > 0 }))([]int{1, 2, 3, -1})
37+
expected := []int{7, 8, 9}
38+
39+
if reflect.DeepEqual(res, expected) == false {
40+
t.Error("Should perform right-to-left function composition of two functions. Received:", res)
41+
}
42+
}

curry_test.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package fp
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestCurry2_Example(t *testing.T) {
8+
res := Curry2(func(a int, b int) int { return a + b })(1)(2)
9+
10+
if res != 3 {
11+
t.Error("Should allow to curry a sum. Received:", res)
12+
}
13+
}
14+
15+
func TestCurry3_Example(t *testing.T) {
16+
res := Curry3(func(a int, b int, c int) int { return a + b + c })(1)(2)(3)
17+
18+
if res != 6 {
19+
t.Error("Should allow to curry a sum. Received:", res)
20+
}
21+
}
22+
23+
func TestCurry4_Example(t *testing.T) {
24+
res := Curry4(func(a int, b int, c int, d int) int { return a + b + c + d })(1)(2)(3)(4)
25+
26+
if res != 10 {
27+
t.Error("Should allow to curry a sum. Received:", res)
28+
}
29+
}

every_test.go

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package fp
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestEvery_TrueExample(t *testing.T) {
8+
res := Every(func(x int) bool { return x > 0 })([]int{1, 2, 3})
9+
10+
if res != true {
11+
t.Error("Every should return true as all elements match the condition. Received:", res)
12+
}
13+
}
14+
15+
func TestEvery_FalseExample(t *testing.T) {
16+
res := Every(func(x int) bool { return x < 0 })([]int{-1, -2, 3})
17+
18+
if res != false {
19+
t.Error("Every should return false if at least one element does not match the condition. Received:", res)
20+
}
21+
}
22+
23+
func TestEveryWithIndex_TrueExample(t *testing.T) {
24+
res := EveryWithIndex(func(x int, i int) bool { return x+i > 0 })([]int{1, 2, -1})
25+
26+
if res != true {
27+
t.Error("Every should return true as all elements match the condition. Received:", res)
28+
}
29+
}
30+
31+
func TestEveryWithIndex_FalseExample(t *testing.T) {
32+
res := EveryWithIndex(func(x int, i int) bool { return x+i < 0 })([]int{-1, -2, 3})
33+
34+
if res != false {
35+
t.Error("Every should return false if at least one element does not match the condition. Received:", res)
36+
}
37+
}
38+
39+
func TestEveryWithSlice_TrueExample(t *testing.T) {
40+
res := EveryWithSlice(func(x int, i int, xs []int) bool { return x+i+xs[0] > 0 })([]int{1, 2, -2})
41+
42+
if res != true {
43+
t.Error("Every should return true as all elements match the condition. Received:", res)
44+
}
45+
}
46+
47+
func TestEveryWithSlice_FalseExample(t *testing.T) {
48+
res := EveryWithSlice(func(x int, i int, xs []int) bool { return x+i+xs[0] < 0 })([]int{-1, -2, 3})
49+
50+
if res != false {
51+
t.Error("Every should return false if at least one element does not match the condition. Received:", res)
52+
}
53+
}

filter_test.go

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package fp
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func TestFilter(t *testing.T) {
9+
res := Filter(func(x int) bool { return x > 0 })([]int{-1, 2, -3, 4})
10+
expected := []int{2, 4}
11+
12+
if reflect.DeepEqual(res, expected) == false {
13+
t.Error("Filter should return only elements for which the callback returns true. Received:", res)
14+
}
15+
}
16+
17+
func TestFilterWithIndex(t *testing.T) {
18+
res := FilterWithIndex(func(x int, i int) bool { return x+i > 0 })([]int{-3, 2, -1, 4})
19+
expected := []int{2, -1, 4}
20+
21+
if reflect.DeepEqual(res, expected) == false {
22+
t.Error("Filter should return only elements for which the callback returns true. Received:", res)
23+
}
24+
}
25+
26+
func TestFilterWithSlice(t *testing.T) {
27+
res := FilterWithSlice(func(x int, i int, xs []int) bool { return x+i+xs[0] > 0 })([]int{2, -1, 4, -3})
28+
expected := []int{2, -1, 4, -3}
29+
30+
if reflect.DeepEqual(res, expected) == false {
31+
t.Error("Filter should return only elements for which the callback returns true. Received:", res)
32+
}
33+
}

flatMap_test.go

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package fp
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func TestFlatMap_Example(t *testing.T) {
9+
res := FlatMap(func(x int) []int { return []int{x, x} })([]int{1, 2})
10+
expected := []int{1, 1, 2, 2}
11+
12+
if reflect.DeepEqual(res, expected) == false {
13+
t.Error("Should call a defined callback function on each element of an array. Then, flattens the result into a new array. Received:", res)
14+
}
15+
}
16+
17+
func TestFlatMapWithIndex_Example(t *testing.T) {
18+
res := FlatMapWithIndex(func(x int, i int) []int { return []int{x + i, x + i} })([]int{1, 2})
19+
expected := []int{1, 1, 3, 3}
20+
21+
if reflect.DeepEqual(res, expected) == false {
22+
t.Error("Should call a defined callback function on each element of an array. Then, flattens the result into a new array. Received:", res)
23+
}
24+
}
25+
func TestFlatMapWithSlice_Example(t *testing.T) {
26+
res := FlatMapWithSlice(func(x int, i int, xs []int) []int { return []int{x + i + xs[0], x + i + xs[0]} })([]int{1, 2})
27+
expected := []int{2, 2, 4, 4}
28+
29+
if reflect.DeepEqual(res, expected) == false {
30+
t.Error("Should call a defined callback function on each element of an array. Then, flattens the result into a new array. Received:", res)
31+
}
32+
}

flat_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package fp
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func TestFlat_Example(t *testing.T) {
9+
res := Flat([][]int{{1, 2}, {3, 4}})
10+
expected := []int{1, 2, 3, 4}
11+
12+
if reflect.DeepEqual(res, expected) == false {
13+
t.Error("Should return a new array with all sub-array elements concatenated into it recursively up to the specified depth. Received:", res)
14+
}
15+
}

map_test.go

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package fp
2+
3+
import (
4+
"reflect"
5+
"strconv"
6+
"testing"
7+
)
8+
9+
func TestMap_ExampleTestMapWithIndex_Example(t *testing.T) {
10+
res := Map(func(x int64) string {
11+
return strconv.FormatInt(x, 10)
12+
})([]int64{1, 2, 3})
13+
expected := []string{"1", "2", "3"}
14+
15+
if reflect.DeepEqual(res, expected) == false {
16+
t.Error("Should map int64 into string. Received:", res)
17+
}
18+
}
19+
20+
func TestMapWithIndex_Example(t *testing.T) {
21+
res := MapWithIndex(
22+
func(x int64, i int) string { return strconv.FormatInt(x+int64(i), 10) })([]int64{1, 2, 3})
23+
expected := []string{"1", "3", "5"}
24+
25+
if reflect.DeepEqual(res, expected) == false {
26+
t.Error("Should map int64 into string. Received:", res)
27+
}
28+
}
29+
30+
func TestMapWithSlice_Example(t *testing.T) {
31+
res := MapWithSlice(
32+
func(x int64, i int, xs []int64) string { return strconv.FormatInt(x+int64(i)+xs[0], 10) })([]int64{1, 2, 3})
33+
expected := []string{"2", "4", "6"}
34+
35+
if reflect.DeepEqual(res, expected) == false {
36+
t.Error("Should map int64 into string. Received:", res)
37+
}
38+
}

pipe_test.go

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package fp
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func TestPipe2_Example(t *testing.T) {
9+
res := Pipe2(
10+
Filter(func(x int) bool { return x > 0 }),
11+
Map(func(x int) int { return x + 2 }))([]int{1, 2, 3, -1})
12+
expected := []int{3, 4, 5}
13+
14+
if reflect.DeepEqual(res, expected) == false {
15+
t.Error("Should perform left-to-right function composition of two functions. Received:", res)
16+
}
17+
}
18+
19+
func TestPipe3_Example(t *testing.T) {
20+
res := Pipe3(
21+
Filter(func(x int) bool { return x > 0 }),
22+
Map(func(x int) int { return x + 2 }),
23+
Map(func(x int) int { return x + 2 }))([]int{1, 2, 3, -1})
24+
expected := []int{5, 6, 7}
25+
26+
if reflect.DeepEqual(res, expected) == false {
27+
t.Error("Should perform left-to-right function composition of two functions. Received:", res)
28+
}
29+
}
30+
31+
func TestPipe4_Example(t *testing.T) {
32+
res := Pipe4(
33+
Filter(func(x int) bool { return x > 0 }),
34+
Map(func(x int) int { return x + 2 }),
35+
Map(func(x int) int { return x + 2 }),
36+
Map(func(x int) int { return x + 2 }))([]int{1, 2, 3, -1})
37+
expected := []int{7, 8, 9}
38+
39+
if reflect.DeepEqual(res, expected) == false {
40+
t.Error("Should perform left-to-right function composition of two functions. Received:", res)
41+
}
42+
}

reduce_test.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package fp
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestReduce_Example(t *testing.T) {
8+
res := Reduce(func(acc int, curr int) int { return acc + curr }, 0)([]int{1, 2, 3})
9+
10+
if res != 6 {
11+
t.Error("Reduce should call the specified callback function for all the elements in an array. The return value of the callback function should be the accumulated result, and is provided as an argument in the next call to the callback function. Received:", res)
12+
}
13+
}
14+
15+
func TestReduceWithIndex_Example(t *testing.T) {
16+
res := ReduceWithIndex(func(acc int, curr int, i int) int { return acc + curr + i }, 0)([]int{1, 2, 3})
17+
18+
if res != 9 {
19+
t.Error("Reduce should call the specified callback function for all the elements in an array. The return value of the callback function should be the accumulated result, and is provided as an argument in the next call to the callback function. Received:", res)
20+
}
21+
}
22+
23+
func TestReduceWithSlice_Example(t *testing.T) {
24+
res := ReduceWithSlice(func(acc int, curr int, i int, xs []int) int { return acc + curr + i + xs[0] }, 0)([]int{1, 2, 3})
25+
26+
if res != 12 {
27+
t.Error("Reduce should call the specified callback function for all the elements in an array. The return value of the callback function should be the accumulated result, and is provided as an argument in the next call to the callback function. Received:", res)
28+
}
29+
}

some_test.go

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package fp
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestSome_TrueExmple(t *testing.T) {
8+
res := Some(func(x int) bool { return x > 0 })([]int{-1, -2, 3})
9+
10+
if res != true {
11+
t.Error("Some should return true as at least one element match the condition. Received:", res)
12+
}
13+
}
14+
15+
func TestSome_FalseExmple(t *testing.T) {
16+
res := Some(func(x int) bool { return x < 0 })([]int{1, 2, 3})
17+
18+
if res != false {
19+
t.Error("Some should return false as none of the element match the condition. Received:", res)
20+
}
21+
}
22+
23+
func TestSomeWithIndex_TrueExmple(t *testing.T) {
24+
res := SomeWithIndex(func(x int, i int) bool { return x+i > 0 })([]int{-10, -20, -1})
25+
26+
if res != true {
27+
t.Error("Some should return true as at least one element match the condition. Received:", res)
28+
}
29+
}
30+
31+
func TestSomeWithIndex_FalseExmple(t *testing.T) {
32+
res := SomeWithIndex(func(x int, i int) bool { return x+i < 0 })([]int{1, 2, 3})
33+
34+
if res != false {
35+
t.Error("Some should return false as none of the element match the condition. Received:", res)
36+
}
37+
}
38+
39+
func TestSomeWithSlice_TrueExmple(t *testing.T) {
40+
res := SomeWithSlice(func(x int, i int, xs []int) bool { return x+i-xs[0] > 0 })([]int{-1, -20, -2})
41+
42+
if res != true {
43+
t.Error("Some should return true as at least one element match the condition. Received:", res)
44+
}
45+
}
46+
47+
func TestSomeWithSlice_FalseExmple(t *testing.T) {
48+
res := SomeWithSlice(func(x int, i int, xs []int) bool { return x+i+xs[0] < 0 })([]int{1, 2, 3, -4})
49+
50+
if res != false {
51+
t.Error("Some should return false as none of the element match the condition. Received:", res)
52+
}
53+
}

0 commit comments

Comments
 (0)