mailer_minimock.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. package mocks
  2. // Code generated by http://github.com/gojuno/minimock (dev). DO NOT EDIT.
  3. //go:generate minimock -i git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/interfaces.Mailer -o ./mocks/mailer_minimock.go -n MailerMock
  4. import (
  5. "sync"
  6. mm_atomic "sync/atomic"
  7. mm_time "time"
  8. "github.com/gojuno/minimock/v3"
  9. )
  10. // MailerMock implements interfaces.Mailer
  11. type MailerMock struct {
  12. t minimock.Tester
  13. funcSend func(recipient string, subject string, text string) (err error)
  14. inspectFuncSend func(recipient string, subject string, text string)
  15. afterSendCounter uint64
  16. beforeSendCounter uint64
  17. SendMock mMailerMockSend
  18. }
  19. // NewMailerMock returns a mock for interfaces.Mailer
  20. func NewMailerMock(t minimock.Tester) *MailerMock {
  21. m := &MailerMock{t: t}
  22. if controller, ok := t.(minimock.MockController); ok {
  23. controller.RegisterMocker(m)
  24. }
  25. m.SendMock = mMailerMockSend{mock: m}
  26. m.SendMock.callArgs = []*MailerMockSendParams{}
  27. return m
  28. }
  29. type mMailerMockSend struct {
  30. mock *MailerMock
  31. defaultExpectation *MailerMockSendExpectation
  32. expectations []*MailerMockSendExpectation
  33. callArgs []*MailerMockSendParams
  34. mutex sync.RWMutex
  35. }
  36. // MailerMockSendExpectation specifies expectation struct of the Mailer.Send
  37. type MailerMockSendExpectation struct {
  38. mock *MailerMock
  39. params *MailerMockSendParams
  40. results *MailerMockSendResults
  41. Counter uint64
  42. }
  43. // MailerMockSendParams contains parameters of the Mailer.Send
  44. type MailerMockSendParams struct {
  45. recipient string
  46. subject string
  47. text string
  48. }
  49. // MailerMockSendResults contains results of the Mailer.Send
  50. type MailerMockSendResults struct {
  51. err error
  52. }
  53. // Expect sets up expected params for Mailer.Send
  54. func (mmSend *mMailerMockSend) Expect(recipient string, subject string, text string) *mMailerMockSend {
  55. if mmSend.mock.funcSend != nil {
  56. mmSend.mock.t.Fatalf("MailerMock.Send mock is already set by Set")
  57. }
  58. if mmSend.defaultExpectation == nil {
  59. mmSend.defaultExpectation = &MailerMockSendExpectation{}
  60. }
  61. mmSend.defaultExpectation.params = &MailerMockSendParams{recipient, subject, text}
  62. for _, e := range mmSend.expectations {
  63. if minimock.Equal(e.params, mmSend.defaultExpectation.params) {
  64. mmSend.mock.t.Fatalf("Expectation set by When has same params: %#v", *mmSend.defaultExpectation.params)
  65. }
  66. }
  67. return mmSend
  68. }
  69. // Inspect accepts an inspector function that has same arguments as the Mailer.Send
  70. func (mmSend *mMailerMockSend) Inspect(f func(recipient string, subject string, text string)) *mMailerMockSend {
  71. if mmSend.mock.inspectFuncSend != nil {
  72. mmSend.mock.t.Fatalf("Inspect function is already set for MailerMock.Send")
  73. }
  74. mmSend.mock.inspectFuncSend = f
  75. return mmSend
  76. }
  77. // Return sets up results that will be returned by Mailer.Send
  78. func (mmSend *mMailerMockSend) Return(err error) *MailerMock {
  79. if mmSend.mock.funcSend != nil {
  80. mmSend.mock.t.Fatalf("MailerMock.Send mock is already set by Set")
  81. }
  82. if mmSend.defaultExpectation == nil {
  83. mmSend.defaultExpectation = &MailerMockSendExpectation{mock: mmSend.mock}
  84. }
  85. mmSend.defaultExpectation.results = &MailerMockSendResults{err}
  86. return mmSend.mock
  87. }
  88. // Set uses given function f to mock the Mailer.Send method
  89. func (mmSend *mMailerMockSend) Set(f func(recipient string, subject string, text string) (err error)) *MailerMock {
  90. if mmSend.defaultExpectation != nil {
  91. mmSend.mock.t.Fatalf("Default expectation is already set for the Mailer.Send method")
  92. }
  93. if len(mmSend.expectations) > 0 {
  94. mmSend.mock.t.Fatalf("Some expectations are already set for the Mailer.Send method")
  95. }
  96. mmSend.mock.funcSend = f
  97. return mmSend.mock
  98. }
  99. // When sets expectation for the Mailer.Send which will trigger the result defined by the following
  100. // Then helper
  101. func (mmSend *mMailerMockSend) When(recipient string, subject string, text string) *MailerMockSendExpectation {
  102. if mmSend.mock.funcSend != nil {
  103. mmSend.mock.t.Fatalf("MailerMock.Send mock is already set by Set")
  104. }
  105. expectation := &MailerMockSendExpectation{
  106. mock: mmSend.mock,
  107. params: &MailerMockSendParams{recipient, subject, text},
  108. }
  109. mmSend.expectations = append(mmSend.expectations, expectation)
  110. return expectation
  111. }
  112. // Then sets up Mailer.Send return parameters for the expectation previously defined by the When method
  113. func (e *MailerMockSendExpectation) Then(err error) *MailerMock {
  114. e.results = &MailerMockSendResults{err}
  115. return e.mock
  116. }
  117. // Send implements interfaces.Mailer
  118. func (mmSend *MailerMock) Send(recipient string, subject string, text string) (err error) {
  119. mm_atomic.AddUint64(&mmSend.beforeSendCounter, 1)
  120. defer mm_atomic.AddUint64(&mmSend.afterSendCounter, 1)
  121. if mmSend.inspectFuncSend != nil {
  122. mmSend.inspectFuncSend(recipient, subject, text)
  123. }
  124. mm_params := &MailerMockSendParams{recipient, subject, text}
  125. // Record call args
  126. mmSend.SendMock.mutex.Lock()
  127. mmSend.SendMock.callArgs = append(mmSend.SendMock.callArgs, mm_params)
  128. mmSend.SendMock.mutex.Unlock()
  129. for _, e := range mmSend.SendMock.expectations {
  130. if minimock.Equal(e.params, mm_params) {
  131. mm_atomic.AddUint64(&e.Counter, 1)
  132. return e.results.err
  133. }
  134. }
  135. if mmSend.SendMock.defaultExpectation != nil {
  136. mm_atomic.AddUint64(&mmSend.SendMock.defaultExpectation.Counter, 1)
  137. mm_want := mmSend.SendMock.defaultExpectation.params
  138. mm_got := MailerMockSendParams{recipient, subject, text}
  139. if mm_want != nil && !minimock.Equal(*mm_want, mm_got) {
  140. mmSend.t.Errorf("MailerMock.Send got unexpected parameters, want: %#v, got: %#v%s\n", *mm_want, mm_got, minimock.Diff(*mm_want, mm_got))
  141. }
  142. mm_results := mmSend.SendMock.defaultExpectation.results
  143. if mm_results == nil {
  144. mmSend.t.Fatal("No results are set for the MailerMock.Send")
  145. }
  146. return (*mm_results).err
  147. }
  148. if mmSend.funcSend != nil {
  149. return mmSend.funcSend(recipient, subject, text)
  150. }
  151. mmSend.t.Fatalf("Unexpected call to MailerMock.Send. %v %v %v", recipient, subject, text)
  152. return
  153. }
  154. // SendAfterCounter returns a count of finished MailerMock.Send invocations
  155. func (mmSend *MailerMock) SendAfterCounter() uint64 {
  156. return mm_atomic.LoadUint64(&mmSend.afterSendCounter)
  157. }
  158. // SendBeforeCounter returns a count of MailerMock.Send invocations
  159. func (mmSend *MailerMock) SendBeforeCounter() uint64 {
  160. return mm_atomic.LoadUint64(&mmSend.beforeSendCounter)
  161. }
  162. // Calls returns a list of arguments used in each call to MailerMock.Send.
  163. // The list is in the same order as the calls were made (i.e. recent calls have a higher index)
  164. func (mmSend *mMailerMockSend) Calls() []*MailerMockSendParams {
  165. mmSend.mutex.RLock()
  166. argCopy := make([]*MailerMockSendParams, len(mmSend.callArgs))
  167. copy(argCopy, mmSend.callArgs)
  168. mmSend.mutex.RUnlock()
  169. return argCopy
  170. }
  171. // MinimockSendDone returns true if the count of the Send invocations corresponds
  172. // the number of defined expectations
  173. func (m *MailerMock) MinimockSendDone() bool {
  174. for _, e := range m.SendMock.expectations {
  175. if mm_atomic.LoadUint64(&e.Counter) < 1 {
  176. return false
  177. }
  178. }
  179. // if default expectation was set then invocations count should be greater than zero
  180. if m.SendMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterSendCounter) < 1 {
  181. return false
  182. }
  183. // if func was set then invocations count should be greater than zero
  184. if m.funcSend != nil && mm_atomic.LoadUint64(&m.afterSendCounter) < 1 {
  185. return false
  186. }
  187. return true
  188. }
  189. // MinimockSendInspect logs each unmet expectation
  190. func (m *MailerMock) MinimockSendInspect() {
  191. for _, e := range m.SendMock.expectations {
  192. if mm_atomic.LoadUint64(&e.Counter) < 1 {
  193. m.t.Errorf("Expected call to MailerMock.Send with params: %#v", *e.params)
  194. }
  195. }
  196. // if default expectation was set then invocations count should be greater than zero
  197. if m.SendMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterSendCounter) < 1 {
  198. if m.SendMock.defaultExpectation.params == nil {
  199. m.t.Error("Expected call to MailerMock.Send")
  200. } else {
  201. m.t.Errorf("Expected call to MailerMock.Send with params: %#v", *m.SendMock.defaultExpectation.params)
  202. }
  203. }
  204. // if func was set then invocations count should be greater than zero
  205. if m.funcSend != nil && mm_atomic.LoadUint64(&m.afterSendCounter) < 1 {
  206. m.t.Error("Expected call to MailerMock.Send")
  207. }
  208. }
  209. // MinimockFinish checks that all mocked methods have been called the expected number of times
  210. func (m *MailerMock) MinimockFinish() {
  211. if !m.minimockDone() {
  212. m.MinimockSendInspect()
  213. m.t.FailNow()
  214. }
  215. }
  216. // MinimockWait waits for all mocked methods to be called the expected number of times
  217. func (m *MailerMock) MinimockWait(timeout mm_time.Duration) {
  218. timeoutCh := mm_time.After(timeout)
  219. for {
  220. if m.minimockDone() {
  221. return
  222. }
  223. select {
  224. case <-timeoutCh:
  225. m.MinimockFinish()
  226. return
  227. case <-mm_time.After(10 * mm_time.Millisecond):
  228. }
  229. }
  230. }
  231. func (m *MailerMock) minimockDone() bool {
  232. done := true
  233. return done &&
  234. m.MinimockSendDone()
  235. }