-
Notifications
You must be signed in to change notification settings - Fork 9
/
fp_test.go
73 lines (65 loc) · 1.56 KB
/
fp_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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package gofp
import (
"strings"
"testing"
)
var mockedUser = map[string]interface{}{
"name": "John Doe",
"age": 30,
"contacts": map[string]interface{}{
"email": "[email protected]",
"office": "Google Inc.",
"fax": map[string]interface{}{
"uk": "+44-208-1234567",
},
},
}
func Test_Pipe(t *testing.T) {
mockedUser := map[string]interface{}{
"name": "John Doe",
"age": 30,
"contacts": map[string]interface{}{
"email": "[email protected]",
"office": "Google Inc.",
"fax": map[string]interface{}{
"uk": "+44-208-1234567",
},
},
}
getContacts := func(data interface{}) interface{} {
return data.(map[string]interface{})["contacts"]
}
getEmail := func(data interface{}) interface{} {
return data.(map[string]interface{})["email"]
}
getUpperCaseEmail := func(data interface{}) interface{} {
return strings.ToUpper(data.(string))
}
email := Pipe(
getContacts,
getEmail,
getUpperCaseEmail,
)(mockedUser)
if email != "[email protected]" {
t.Errorf("Pipe() = %v, want %v", email, "[email protected]")
}
}
func Test_Compose(t *testing.T) {
getContacts := func(data interface{}) interface{} {
return data.(map[string]interface{})["contacts"]
}
getEmail := func(data interface{}) interface{} {
return data.(map[string]interface{})["email"]
}
getUpperCaseEmail := func(data interface{}) interface{} {
return strings.ToUpper(data.(string))
}
email := Compose(
getUpperCaseEmail,
getEmail,
getContacts,
)(mockedUser)
if email != "[email protected]" {
t.Errorf("Pipe() = %v, want %v", email, "[email protected]")
}
}