-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoreturn_test.go
More file actions
116 lines (110 loc) · 2.21 KB
/
goreturn_test.go
File metadata and controls
116 lines (110 loc) · 2.21 KB
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 goreturn_test
import (
"testing"
"time"
"github.com/Eyal-Shalev/goreturn"
)
func TestReturn0(t *testing.T) {
called := false
fn := func() {
called = true
}
c := goreturn.Return0(fn)
select {
case <-c:
if !called {
t.Errorf("Return0() did not call the function")
}
case <-time.After(time.Millisecond):
t.Errorf("Return0() took too long to return")
}
select {
case v := <-c:
if v != nil {
t.Errorf("Return0() sent a non-nil value")
}
default:
t.Errorf("Return0() did not close the channel")
}
}
func TestReturn1(t *testing.T) {
called := false
fn := func() *int {
called = true
val := 42
return &val
}
c := goreturn.Return1(fn)
select {
case v := <-c:
if !called {
t.Errorf("Return1() did not call the function")
}
if *v != 42 {
t.Errorf("Return1() did not return the expected value")
}
case <-time.After(time.Millisecond):
t.Errorf("Return1() took too long to return")
}
select {
case v := <-c:
if v != nil {
t.Errorf("Return1() sent a non-nil value")
}
default:
t.Errorf("Return1() did not close the channel")
}
}
func TestReturn2(t *testing.T) {
called := false
fn := func() (int, string) {
called = true
return 42, "hello"
}
c := goreturn.Return2(fn)
select {
case v := <-c:
if !called {
t.Errorf("Return2() did not call the function")
}
if v.V1 != 42 || v.V2 != "hello" {
t.Errorf("Return2() did not return the expected values")
}
case <-time.After(time.Millisecond):
t.Errorf("Return2() took too long to return")
}
select {
case v := <-c:
if v != nil {
t.Errorf("Return2() sent a non-nil value")
}
default:
t.Errorf("Return2() did not close the channel")
}
}
func TestReturn3(t *testing.T) {
called := false
fn := func() (int, string, bool) {
called = true
return 42, "hello", true
}
c := goreturn.Return3(fn)
select {
case v := <-c:
if !called {
t.Errorf("Return3() did not call the function")
}
if v.V1 != 42 || v.V2 != "hello" || !v.V3 {
t.Errorf("Return3() did not return the expected values")
}
case <-time.After(time.Millisecond):
t.Errorf("Return3() took too long to return")
}
select {
case v := <-c:
if v != nil {
t.Errorf("Return3() did not close the channel")
}
default:
}
}