forked from uber-go/dig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dig_go19_test.go
118 lines (95 loc) · 3.34 KB
/
dig_go19_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
// Copyright (c) 2018 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// +build go1.9
package dig
import (
"bytes"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestEndToEndSuccessWithAliases(t *testing.T) {
t.Run("pointer constructor", func(t *testing.T) {
type Buffer = *bytes.Buffer
c := New()
var b Buffer
require.NoError(t, c.Provide(func() *bytes.Buffer {
b = &bytes.Buffer{}
return b
}), "provide failed")
require.NoError(t, c.Invoke(func(got Buffer) {
require.NotNil(t, got, "invoke got nil buffer")
require.True(t, got == b, "invoke got wrong buffer")
}), "invoke failed")
})
t.Run("duplicate provide", func(t *testing.T) {
type A struct{}
type B = A
c := New()
require.NoError(t, c.Provide(func() A {
return A{}
}), "A should not fail to provide")
err := c.Provide(func() B { return B{} })
require.Error(t, err, "B should fail to provide")
assertErrorMatches(t, err,
`function "go.uber.org/dig".TestEndToEndSuccessWithAliases\S+ \(\S+:\d+\) cannot be provided:`,
`cannot provide dig.A from \[0\]:`,
`already provided by "go.uber.org/dig".TestEndToEndSuccessWithAliases\S+`,
)
})
t.Run("named instances", func(t *testing.T) {
c := New()
type A1 struct{ s string }
type A2 = A1
type A3 = A2
type ret struct {
Out
A A1 `name:"a"`
B A2 `name:"b"`
C A3 `name:"c"`
}
type param struct {
In
A1 A1 `name:"a"`
B1 A2 `name:"b"`
C1 A3 `name:"c"`
A2 A3 `name:"a"`
B2 A1 `name:"b"`
C2 A2 `name:"c"`
A3 A2 `name:"a"`
B3 A3 `name:"b"`
C3 A1 `name:"c"`
}
require.NoError(t, c.Provide(func() ret {
return ret{A: A2{"a"}, B: A3{"b"}, C: A1{"c"}}
}), "provide for three named instances should succeed")
require.NoError(t, c.Invoke(func(p param) {
assert.Equal(t, "a", p.A1.s, "A1 should match")
assert.Equal(t, "b", p.B1.s, "B1 should match")
assert.Equal(t, "c", p.C1.s, "C1 should match")
assert.Equal(t, "a", p.A2.s, "A2 should match")
assert.Equal(t, "b", p.B2.s, "B2 should match")
assert.Equal(t, "c", p.C2.s, "C2 should match")
assert.Equal(t, "a", p.A3.s, "A3 should match")
assert.Equal(t, "b", p.B3.s, "B3 should match")
assert.Equal(t, "c", p.C3.s, "C3 should match")
}), "invoke should succeed, pulling out two named instances")
})
}