i_mailer_minimock.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. package mocks
  2. // Code generated by http://github.com/gojuno/minimock (dev). DO NOT EDIT.
  3. //go:generate minimock -i github.com/dmitriygnatenko/internal/interfaces.IMailer -o ./mocks/i_mailer_minimock.go -n IMailerMock
  4. import (
  5. "sync"
  6. mm_atomic "sync/atomic"
  7. mm_time "time"
  8. "github.com/gojuno/minimock/v3"
  9. )
  10. // IMailerMock implements interfaces.IMailer
  11. type IMailerMock 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 mIMailerMockSend
  18. }
  19. // NewIMailerMock returns a mock for interfaces.IMailer
  20. func NewIMailerMock(t minimock.Tester) *IMailerMock {
  21. m := &IMailerMock{t: t}
  22. if controller, ok := t.(minimock.MockController); ok {
  23. controller.RegisterMocker(m)
  24. }
  25. m.SendMock = mIMailerMockSend{mock: m}
  26. m.SendMock.callArgs = []*IMailerMockSendParams{}
  27. return m
  28. }
  29. type mIMailerMockSend struct {
  30. mock *IMailerMock
  31. defaultExpectation *IMailerMockSendExpectation
  32. expectations []*IMailerMockSendExpectation
  33. callArgs []*IMailerMockSendParams
  34. mutex sync.RWMutex
  35. }
  36. // IMailerMockSendExpectation specifies expectation struct of the IMailer.Send
  37. type IMailerMockSendExpectation struct {
  38. mock *IMailerMock
  39. params *IMailerMockSendParams
  40. results *IMailerMockSendResults
  41. Counter uint64
  42. }
  43. // IMailerMockSendParams contains parameters of the IMailer.Send
  44. type IMailerMockSendParams struct {
  45. recipient string
  46. subject string
  47. text string
  48. }
  49. // IMailerMockSendResults contains results of the IMailer.Send
  50. type IMailerMockSendResults struct {
  51. err error
  52. }
  53. // Expect sets up expected params for IMailer.Send
  54. func (mmSend *mIMailerMockSend) Expect(recipient string, subject string, text string) *mIMailerMockSend {
  55. if mmSend.mock.funcSend != nil {
  56. mmSend.mock.t.Fatalf("IMailerMock.Send mock is already set by Set")
  57. }
  58. if mmSend.defaultExpectation == nil {
  59. mmSend.defaultExpectation = &IMailerMockSendExpectation{}
  60. }
  61. mmSend.defaultExpectation.params = &IMailerMockSendParams{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 IMailer.Send
  70. func (mmSend *mIMailerMockSend) Inspect(f func(recipient string, subject string, text string)) *mIMailerMockSend {
  71. if mmSend.mock.inspectFuncSend != nil {
  72. mmSend.mock.t.Fatalf("Inspect function is already set for IMailerMock.Send")
  73. }
  74. mmSend.mock.inspectFuncSend = f
  75. return mmSend
  76. }
  77. // Return sets up results that will be returned by IMailer.Send
  78. func (mmSend *mIMailerMockSend) Return(err error) *IMailerMock {
  79. if mmSend.mock.funcSend != nil {
  80. mmSend.mock.t.Fatalf("IMailerMock.Send mock is already set by Set")
  81. }
  82. if mmSend.defaultExpectation == nil {
  83. mmSend.defaultExpectation = &IMailerMockSendExpectation{mock: mmSend.mock}
  84. }
  85. mmSend.defaultExpectation.results = &IMailerMockSendResults{err}
  86. return mmSend.mock
  87. }
  88. //Set uses given function f to mock the IMailer.Send method
  89. func (mmSend *mIMailerMockSend) Set(f func(recipient string, subject string, text string) (err error)) *IMailerMock {
  90. if mmSend.defaultExpectation != nil {
  91. mmSend.mock.t.Fatalf("Default expectation is already set for the IMailer.Send method")
  92. }
  93. if len(mmSend.expectations) > 0 {
  94. mmSend.mock.t.Fatalf("Some expectations are already set for the IMailer.Send method")
  95. }
  96. mmSend.mock.funcSend = f
  97. return mmSend.mock
  98. }
  99. // When sets expectation for the IMailer.Send which will trigger the result defined by the following
  100. // Then helper
  101. func (mmSend *mIMailerMockSend) When(recipient string, subject string, text string) *IMailerMockSendExpectation {
  102. if mmSend.mock.funcSend != nil {
  103. mmSend.mock.t.Fatalf("IMailerMock.Send mock is already set by Set")
  104. }
  105. expectation := &IMailerMockSendExpectation{
  106. mock: mmSend.mock,
  107. params: &IMailerMockSendParams{recipient, subject, text},
  108. }
  109. mmSend.expectations = append(mmSend.expectations, expectation)
  110. return expectation
  111. }
  112. // Then sets up IMailer.Send return parameters for the expectation previously defined by the When method
  113. func (e *IMailerMockSendExpectation) Then(err error) *IMailerMock {
  114. e.results = &IMailerMockSendResults{err}
  115. return e.mock
  116. }
  117. // Send implements interfaces.IMailer
  118. func (mmSend *IMailerMock) 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 := &IMailerMockSendParams{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 := IMailerMockSendParams{recipient, subject, text}
  139. if mm_want != nil && !minimock.Equal(*mm_want, mm_got) {
  140. mmSend.t.Errorf("IMailerMock.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 IMailerMock.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 IMailerMock.Send. %v %v %v", recipient, subject, text)
  152. return
  153. }
  154. // SendAfterCounter returns a count of finished IMailerMock.Send invocations
  155. func (mmSend *IMailerMock) SendAfterCounter() uint64 {
  156. return mm_atomic.LoadUint64(&mmSend.afterSendCounter)
  157. }
  158. // SendBeforeCounter returns a count of IMailerMock.Send invocations
  159. func (mmSend *IMailerMock) SendBeforeCounter() uint64 {
  160. return mm_atomic.LoadUint64(&mmSend.beforeSendCounter)
  161. }
  162. // Calls returns a list of arguments used in each call to IMailerMock.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 *mIMailerMockSend) Calls() []*IMailerMockSendParams {
  165. mmSend.mutex.RLock()
  166. argCopy := make([]*IMailerMockSendParams, 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 *IMailerMock) 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 *IMailerMock) MinimockSendInspect() {
  191. for _, e := range m.SendMock.expectations {
  192. if mm_atomic.LoadUint64(&e.Counter) < 1 {
  193. m.t.Errorf("Expected call to IMailerMock.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 IMailerMock.Send")
  200. } else {
  201. m.t.Errorf("Expected call to IMailerMock.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 IMailerMock.Send")
  207. }
  208. }
  209. // MinimockFinish checks that all mocked methods have been called the expected number of times
  210. func (m *IMailerMock) 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 *IMailerMock) 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 *IMailerMock) minimockDone() bool {
  232. done := true
  233. return done &&
  234. m.MinimockSendDone()
  235. }