-
-
Notifications
You must be signed in to change notification settings - Fork 87
/
do_test.go
116 lines (94 loc) · 2.88 KB
/
do_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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package mo
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
)
func TestDo_Success(t *testing.T) {
is := assert.New(t)
result := Do(func() string {
return "Hello, World!"
})
is.False(result.IsError())
is.Equal("Hello, World!", result.MustGet())
}
func TestDo_Error(t *testing.T) {
is := assert.New(t)
result := Do(func() string {
panic(errors.New("something went wrong"))
})
is.True(result.IsError())
is.EqualError(result.Error(), "something went wrong")
}
func TestDo_ComplexSuccess(t *testing.T) {
is := assert.New(t)
validateBooking := func(params map[string]string) Result[map[string]string] {
if params["guest"] != "" && params["roomType"] != "" {
return Ok(params)
}
return Err[map[string]string](errors.New("validation failed"))
}
createBooking := func(guest string) Result[string] {
if guest != "" {
return Ok("Booking Created for: " + guest)
}
return Err[string](errors.New("booking creation failed"))
}
assignRoom := func(booking string, roomType string) Result[string] {
if roomType != "" {
return Ok("Room Assigned: " + roomType + " for " + booking)
}
return Err[string](errors.New("room assignment failed"))
}
bookRoom := func(params map[string]string) Result[[]string] {
return Do(func() []string {
values := validateBooking(params).MustGet()
booking := createBooking(values["guest"]).MustGet()
room := assignRoom(booking, values["roomType"]).MustGet()
return []string{booking, room}
})
}
params := map[string]string{
"guest": "Foo Bar",
"roomType": "Suite",
}
result := bookRoom(params)
is.False(result.IsError())
is.Equal([]string{"Booking Created for: Foo Bar", "Room Assigned: Suite for Booking Created for: Foo Bar"}, result.MustGet())
}
func TestDo_ComplexError(t *testing.T) {
is := assert.New(t)
validateBooking := func(params map[string]string) Result[map[string]string] {
if params["guest"] != "" && params["roomType"] != "" {
return Ok(params)
}
return Err[map[string]string](errors.New("validation failed"))
}
createBooking := func(guest string) Result[string] {
if guest != "" {
return Ok("Booking Created for: " + guest)
}
return Err[string](errors.New("booking creation failed"))
}
assignRoom := func(booking string, roomType string) Result[string] {
if roomType != "" {
return Ok("Room Assigned: " + roomType + " for " + booking)
}
return Err[string](errors.New("room assignment failed"))
}
bookRoom := func(params map[string]string) Result[[]string] {
return Do(func() []string {
values := validateBooking(params).MustGet()
booking := createBooking(values["guest"]).MustGet()
room := assignRoom(booking, values["roomType"]).MustGet()
return []string{booking, room}
})
}
params := map[string]string{
"guest": "",
"roomType": "Suite",
}
result := bookRoom(params)
is.True(result.IsError())
is.EqualError(result.Error(), "validation failed")
}