hub_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package di
  2. import (
  3. "strings"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/stretchr/testify/require"
  7. "go.uber.org/dig"
  8. )
  9. func TestHub_New(t *testing.T) {
  10. t.Run("basic di", func(t *testing.T) {
  11. hub, err := New(module1, ValidateHub(true))
  12. require.NoError(t, err)
  13. require.NotNil(t, hub)
  14. err = hub.Invoke(func(c C) {
  15. assert.Equal(t, c.Number, int64(100))
  16. })
  17. require.NoError(t, err)
  18. err = hub.Invoke(func(d D) {
  19. assert.Equal(t, d.C.Number, int64(100))
  20. })
  21. require.NoError(t, err)
  22. err = hub.Invoke(func(f1 F1) {
  23. assert.Equal(t, f1.Name, "f1")
  24. })
  25. require.NoError(t, err)
  26. })
  27. t.Run("annotation", func(t *testing.T) {
  28. k0 := &K0{}
  29. hub, err := New(module2, Annotation(func(ann *Annotated) {
  30. ann.Target = func() *K0 { return k0 }
  31. ann.Close = k0.Close
  32. }))
  33. require.NoError(t, err)
  34. require.NotNil(t, hub)
  35. // invoke now
  36. err = hub.Invoke(func(k1 K1) { assert.Equal(t, k1.J.Name, "j1") })
  37. require.NoError(t, err)
  38. err = hub.Invoke(func(k2 K2) { assert.Equal(t, k2.J.Name, "j2") })
  39. require.NoError(t, err)
  40. // cleanup and check it
  41. hub.Cleanup()
  42. require.True(t, k0.Closed)
  43. })
  44. t.Run("missing deps", func(t *testing.T) {
  45. hub, err := New(moduleFailed, ValidateHub(false))
  46. require.NoError(t, err) // won't check missing H in G, only check it after invoke
  47. require.NotNil(t, hub)
  48. err = hub.Invoke(func(g G) {})
  49. assert.Error(t, err)
  50. assert.True(t, strings.Contains(err.Error(), "missing dependencies"))
  51. })
  52. }
  53. var module1 = Options(
  54. Provide(func() A {
  55. return A{Name: "a"}
  56. }),
  57. Provide(func(a A) B {
  58. return B{
  59. A: a,
  60. Age: 20,
  61. }
  62. }),
  63. Provide(func(a A, b B) C {
  64. return C{
  65. A: a,
  66. B: b,
  67. Number: 100,
  68. }
  69. }),
  70. Provide(NewE),
  71. )
  72. var module2 = Options(
  73. Provide(Annotated{
  74. Name: "j1",
  75. Target: func() J {
  76. return J{Name: "j1"}
  77. },
  78. }),
  79. Provide(Annotated{
  80. Name: "j2",
  81. Target: func() J {
  82. return J{Name: "j2"}
  83. },
  84. }),
  85. )
  86. var moduleFailed = Options(
  87. Provide(func(g G) H {
  88. return H{
  89. G: g,
  90. Name: "h",
  91. }
  92. }),
  93. )
  94. type A struct {
  95. Name string
  96. }
  97. type B struct {
  98. A A
  99. Age int
  100. }
  101. type C struct {
  102. A A
  103. B B
  104. Number int64
  105. }
  106. type D struct {
  107. dig.In
  108. A A
  109. B B
  110. C C
  111. }
  112. func NewE() E {
  113. return E{
  114. F1: F1{
  115. Name: "f1",
  116. },
  117. F2: F2{
  118. Name: "f2",
  119. },
  120. }
  121. }
  122. type E struct {
  123. dig.Out
  124. F1 F1
  125. F2 F2
  126. }
  127. type F1 struct {
  128. Name string
  129. }
  130. type F2 struct {
  131. Name string
  132. }
  133. type G struct {
  134. Name string
  135. }
  136. type H struct {
  137. G G
  138. Name string
  139. }
  140. type J struct {
  141. Name string
  142. }
  143. type K0 struct {
  144. Closed bool
  145. }
  146. func (k *K0) Close() {
  147. k.Closed = true
  148. }
  149. type K1 struct {
  150. dig.In
  151. J J `name:"j1"`
  152. }
  153. type K2 struct {
  154. dig.In
  155. J J `name:"j2"`
  156. }