-
Notifications
You must be signed in to change notification settings - Fork 1
/
map_test.go
51 lines (42 loc) · 1.1 KB
/
map_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package xgo_test
import (
"errors"
"strconv"
"testing"
"github.com/glassonion1/xgo"
"github.com/google/go-cmp/cmp"
)
func TestMap(t *testing.T) {
input := []int{1, 2, 3, 4, 5}
expected := []string{"1", "2", "3", "4", "5"}
// Define mapping function
f := func(i int) (string, error) {
return strconv.Itoa(i), nil
}
// Call Map function
output, err := xgo.Map(input, f)
// Verify output and error
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if diff := cmp.Diff(output, expected); diff != "" {
t.Errorf("Output does not match expected (-got +want):\n%s", diff)
}
// Test error case
input2 := []int{1, 2, 3, 4, 5}
expected2 := []string{"1", "2", "4", "5"}
expectedErr := errors.New("failed to map")
f2 := func(i int) (string, error) {
if i == 3 {
return "", expectedErr
}
return strconv.Itoa(i), nil
}
output2, err2 := xgo.Map(input2, f2)
if !errors.Is(err2, expectedErr) {
t.Errorf("Expected error %v but got %v", expectedErr, err2)
}
if diff := cmp.Diff(output2, expected2); diff != "" {
t.Errorf("Output does not match expected (-got +want):\n%s", diff)
}
}