-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils_test.go
96 lines (83 loc) · 2.14 KB
/
utils_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
package pirog
import (
"bytes"
"context"
"github.com/stretchr/testify/assert"
"io"
"os"
"testing"
)
type S1 struct {
S2 *S2 `inject:""`
}
type S2 struct {
S1 S1 `inject:""`
SAny any `inject:"S1"`
}
type I1 struct{ I int }
type I2 struct {
S1 S1 `inject:""`
}
type I3 struct {
I1 *I1 `inject:"I1"`
I2 *I2 `inject:"I2"`
I3 *I3 `inject:"I3"`
I4 I1I `inject:"I1"`
}
func (s *S1) InitTest(ctx context.Context) error { TEST_VARIABLE++; return nil }
func (i *I1) InitTest(ctx context.Context) error { TEST_VARIABLE++; return nil }
func (i *I2) InitTest(ctx context.Context) error { TEST_VARIABLE++; return nil }
func (i *I3) InitTest(ctx context.Context) error { TEST_VARIABLE++; return nil }
func (i *I1) isI1I() {}
func (i *I2) isI2I() {}
func (i *I3) isI3I() {}
type I1I interface{ isI1I() }
type I2I interface{ isI2I() }
type I3I interface{ isI3I() }
type App struct {
S1 *S1 `injectable:""`
S2 *S2 `injectable:""`
R io.Reader
I1 I1I `injectable:""`
I2 I2I `injectable:""`
I3 I3I `injectable:""`
I int
}
var TEST_VARIABLE = 0
func TestExecuteOnAllFields(t *testing.T) {
app := &App{}
app.S1 = &S1{}
app.S2 = &S2{}
app.R = os.Stdin
app.I1 = &I1{}
app.I2 = &I2{}
assert.NoError(t, ExecuteOnAllFields(context.Background(), app, "InitTest"))
assert.Equal(t, 3, TEST_VARIABLE)
app.I3 = &I3{}
assert.NoError(t, ExecuteOnAllFields(context.Background(), app, "InitTest"))
assert.Equal(t, 7, TEST_VARIABLE)
}
func TestInjectComponents(t *testing.T) {
app := &App{}
app.S1 = &S1{}
app.S2 = &S2{}
app.R = os.Stdin
app.I1 = &I1{}
app.I2 = &I2{}
app.I3 = &I3{}
InjectComponents(app)
assert.NotNil(t, app.S1)
assert.NotNil(t, app.S2)
assert.NotNil(t, app.S2.S1.S2)
assert.NotNil(t, app.S1.S2)
assert.NotNil(t, app.I3.(*I3).I1)
assert.NotNil(t, app.I3.(*I3).I2)
}
func TestEXEC(t *testing.T) {
ctx := context.Background()
code, stdout, stderr, err := EXEC(ctx, "cat", bytes.NewBufferString("lala\nlolo\nlulu\n"))
assert.NoError(t, err)
assert.Equal(t, 0, code)
assert.Equal(t, "lala\nlolo\nlulu\n", stdout.String())
assert.Equal(t, "", stderr.String())
}