-
Notifications
You must be signed in to change notification settings - Fork 2
/
condense_test.go
134 lines (124 loc) · 2.65 KB
/
condense_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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package nject_test
import (
"fmt"
"testing"
"github.com/muir/nject"
"github.com/stretchr/testify/assert"
)
func TestCondenseTerminalError(t *testing.T) {
t.Parallel()
var x func(int) error
nject.Sequence("s1",
nject.Sequence("s",
func(i int) nject.TerminalError {
if i == 1 {
return fmt.Errorf("1")
}
return nil
},
func(i int) error {
if i == 2 {
return fmt.Errorf("2")
}
return nil
}).MustCondense(true),
func(i int) nject.TerminalError {
if i == 3 {
return fmt.Errorf("3")
}
return nil
},
func(i int) error {
if i == 4 {
return fmt.Errorf("4")
}
return nil
}).Bind(&x, nil)
c := func(i int) string {
err := x(i)
if err == nil {
return ""
}
return err.Error()
}
assert.Equal(t, "1", c(1))
assert.Equal(t, "2", c(2))
assert.Equal(t, "3", c(3))
assert.Equal(t, "4", c(4))
assert.Equal(t, "", c(0))
}
func TestCondenseErrorTreatment(t *testing.T) {
t.Parallel()
run := func(behavior bool) string {
var x func() error
nject.Sequence("s1",
nject.Sequence("s",
func() error {
return fmt.Errorf("1")
}).MustCondense(behavior),
nject.Shun(func() error {
return fmt.Errorf("3")
}),
func(err error) error {
return fmt.Errorf("2: %w", err)
}).Bind(&x, nil)
return x().Error()
}
assert.Equal(t, "1", run(true), "treat errors as terminal")
assert.Equal(t, "2: 1", run(false), "treat errors as regular")
}
func TestCondenseDebugging(t *testing.T) {
var called bool
var x func()
nject.Sequence("s1",
nject.Sequence("s",
func(d *nject.Debugging) {
assert.NotNil(t, d.Outer, "outer debug")
called = true
},
func() {},
).MustCondense(true),
func() {},
).Bind(&x, nil)
x()
assert.True(t, called, "called")
}
func TestCondenseSelfSatisfied(t *testing.T) {
var called bool
var alsoCalled bool
var x func()
condensed := nject.Required(nject.Sequence("c",
func(_ int) string {
return "foo"
},
func(_ string) {
called = true
},
).MustCondense(true))
nject.Sequence("s1",
func() int {
return 7
},
condensed,
func() {
alsoCalled = true
},
).Bind(&x, nil)
x()
assert.True(t, called, "condensed called")
assert.True(t, alsoCalled, "main called")
assert.Equal(t,
" [<reflectiveFunc>(int)]",
condensed.String(),
"presentation of condensed")
}
func TestCondenseStringified(t *testing.T) {
c1 := nject.Sequence("x",
func() (int, string) { return 7, "foo" },
).MustCondense(true)
assert.Equal(t, " [<reflectiveFunc>() (int, string)]", c1.String(), "c1")
c2 := nject.Sequence("x",
func() float32 { return 7 },
).MustCondense(true)
assert.Equal(t, " [<reflectiveFunc>() float32]", c2.String(), "c2")
}