Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 

740 lignes
20 KiB

  1. // Copyright 2011 Google Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package gomock_test
  15. import (
  16. "fmt"
  17. "reflect"
  18. "testing"
  19. "strings"
  20. "github.com/golang/mock/gomock"
  21. )
  22. type ErrorReporter struct {
  23. t *testing.T
  24. log []string
  25. failed bool
  26. fatalToken struct{}
  27. }
  28. func NewErrorReporter(t *testing.T) *ErrorReporter {
  29. return &ErrorReporter{t: t}
  30. }
  31. func (e *ErrorReporter) reportLog() {
  32. for _, entry := range e.log {
  33. e.t.Log(entry)
  34. }
  35. }
  36. func (e *ErrorReporter) assertPass(msg string) {
  37. if e.failed {
  38. e.t.Errorf("Expected pass, but got failure(s): %s", msg)
  39. e.reportLog()
  40. }
  41. }
  42. func (e *ErrorReporter) assertFail(msg string) {
  43. if !e.failed {
  44. e.t.Errorf("Expected failure, but got pass: %s", msg)
  45. }
  46. }
  47. // Use to check that code triggers a fatal test failure.
  48. func (e *ErrorReporter) assertFatal(fn func(), expectedErrMsgs ...string) {
  49. defer func() {
  50. err := recover()
  51. if err == nil {
  52. var actual string
  53. if e.failed {
  54. actual = "non-fatal failure"
  55. } else {
  56. actual = "pass"
  57. }
  58. e.t.Error("Expected fatal failure, but got a", actual)
  59. } else if token, ok := err.(*struct{}); ok && token == &e.fatalToken {
  60. // This is okay - the panic is from Fatalf().
  61. if expectedErrMsgs != nil {
  62. // assert that the actual error message
  63. // contains expectedErrMsgs
  64. // check the last actualErrMsg, because the previous messages come from previous errors
  65. actualErrMsg := e.log[len(e.log)-1]
  66. for _, expectedErrMsg := range expectedErrMsgs {
  67. if !strings.Contains(actualErrMsg, expectedErrMsg) {
  68. e.t.Errorf("Error message:\ngot: %q\nwant to contain: %q\n", actualErrMsg, expectedErrMsg)
  69. }
  70. }
  71. }
  72. return
  73. } else {
  74. // Some other panic.
  75. panic(err)
  76. }
  77. }()
  78. fn()
  79. }
  80. // recoverUnexpectedFatal can be used as a deferred call in test cases to
  81. // recover from and display a call to ErrorReporter.Fatalf().
  82. func (e *ErrorReporter) recoverUnexpectedFatal() {
  83. err := recover()
  84. if err == nil {
  85. // No panic.
  86. } else if token, ok := err.(*struct{}); ok && token == &e.fatalToken {
  87. // Unexpected fatal error happened.
  88. e.t.Error("Got unexpected fatal error(s). All errors up to this point:")
  89. e.reportLog()
  90. return
  91. } else {
  92. // Some other panic.
  93. panic(err)
  94. }
  95. }
  96. func (e *ErrorReporter) Logf(format string, args ...interface{}) {
  97. e.log = append(e.log, fmt.Sprintf(format, args...))
  98. }
  99. func (e *ErrorReporter) Errorf(format string, args ...interface{}) {
  100. e.Logf(format, args...)
  101. e.failed = true
  102. }
  103. func (e *ErrorReporter) Fatalf(format string, args ...interface{}) {
  104. e.Logf(format, args...)
  105. e.failed = true
  106. panic(&e.fatalToken)
  107. }
  108. type HelperReporter struct {
  109. gomock.TestReporter
  110. helper int
  111. }
  112. func (h *HelperReporter) Helper() {
  113. h.helper++
  114. }
  115. // A type purely for use as a receiver in testing the Controller.
  116. type Subject struct{}
  117. func (s *Subject) FooMethod(arg string) int {
  118. return 0
  119. }
  120. func (s *Subject) BarMethod(arg string) int {
  121. return 0
  122. }
  123. func (s *Subject) VariadicMethod(arg int, vararg ...string) {}
  124. // A type purely for ActOnTestStructMethod
  125. type TestStruct struct {
  126. Number int
  127. Message string
  128. }
  129. func (s *Subject) ActOnTestStructMethod(arg TestStruct, arg1 int) int {
  130. return 0
  131. }
  132. func (s *Subject) SetArgMethod(sliceArg []byte, ptrArg *int) {}
  133. func assertEqual(t *testing.T, expected interface{}, actual interface{}) {
  134. if !reflect.DeepEqual(expected, actual) {
  135. t.Errorf("Expected %+v, but got %+v", expected, actual)
  136. }
  137. }
  138. func createFixtures(t *testing.T) (reporter *ErrorReporter, ctrl *gomock.Controller) {
  139. // reporter acts as a testing.T-like object that we pass to the
  140. // Controller. We use it to test that the mock considered tests
  141. // successful or failed.
  142. reporter = NewErrorReporter(t)
  143. ctrl = gomock.NewController(reporter)
  144. return
  145. }
  146. func TestNoCalls(t *testing.T) {
  147. reporter, ctrl := createFixtures(t)
  148. ctrl.Finish()
  149. reporter.assertPass("No calls expected or made.")
  150. }
  151. func TestNoRecordedCallsForAReceiver(t *testing.T) {
  152. reporter, ctrl := createFixtures(t)
  153. subject := new(Subject)
  154. reporter.assertFatal(func() {
  155. ctrl.Call(subject, "NotRecordedMethod", "argument")
  156. }, "Unexpected call to", "there are no expected calls of the method \"NotRecordedMethod\" for that receiver")
  157. ctrl.Finish()
  158. }
  159. func TestNoRecordedMatchingMethodNameForAReceiver(t *testing.T) {
  160. reporter, ctrl := createFixtures(t)
  161. subject := new(Subject)
  162. ctrl.RecordCall(subject, "FooMethod", "argument")
  163. reporter.assertFatal(func() {
  164. ctrl.Call(subject, "NotRecordedMethod", "argument")
  165. }, "Unexpected call to", "there are no expected calls of the method \"NotRecordedMethod\" for that receiver")
  166. reporter.assertFatal(func() {
  167. // The expected call wasn't made.
  168. ctrl.Finish()
  169. })
  170. }
  171. // This tests that a call with an arguments of some primitive type matches a recorded call.
  172. func TestExpectedMethodCall(t *testing.T) {
  173. reporter, ctrl := createFixtures(t)
  174. subject := new(Subject)
  175. ctrl.RecordCall(subject, "FooMethod", "argument")
  176. ctrl.Call(subject, "FooMethod", "argument")
  177. ctrl.Finish()
  178. reporter.assertPass("Expected method call made.")
  179. }
  180. func TestUnexpectedMethodCall(t *testing.T) {
  181. reporter, ctrl := createFixtures(t)
  182. subject := new(Subject)
  183. reporter.assertFatal(func() {
  184. ctrl.Call(subject, "FooMethod", "argument")
  185. })
  186. ctrl.Finish()
  187. }
  188. func TestRepeatedCall(t *testing.T) {
  189. reporter, ctrl := createFixtures(t)
  190. subject := new(Subject)
  191. ctrl.RecordCall(subject, "FooMethod", "argument").Times(3)
  192. ctrl.Call(subject, "FooMethod", "argument")
  193. ctrl.Call(subject, "FooMethod", "argument")
  194. ctrl.Call(subject, "FooMethod", "argument")
  195. reporter.assertPass("After expected repeated method calls.")
  196. reporter.assertFatal(func() {
  197. ctrl.Call(subject, "FooMethod", "argument")
  198. })
  199. ctrl.Finish()
  200. reporter.assertFail("After calling one too many times.")
  201. }
  202. func TestUnexpectedArgCount(t *testing.T) {
  203. reporter, ctrl := createFixtures(t)
  204. defer reporter.recoverUnexpectedFatal()
  205. subject := new(Subject)
  206. ctrl.RecordCall(subject, "FooMethod", "argument")
  207. reporter.assertFatal(func() {
  208. // This call is made with the wrong number of arguments...
  209. ctrl.Call(subject, "FooMethod", "argument", "extra_argument")
  210. }, "Unexpected call to", "wrong number of arguments", "Got: 2, want: 1")
  211. reporter.assertFatal(func() {
  212. // ... so is this.
  213. ctrl.Call(subject, "FooMethod")
  214. }, "Unexpected call to", "wrong number of arguments", "Got: 0, want: 1")
  215. reporter.assertFatal(func() {
  216. // The expected call wasn't made.
  217. ctrl.Finish()
  218. })
  219. }
  220. // This tests that a call with complex arguments (a struct and some primitive type) matches a recorded call.
  221. func TestExpectedMethodCall_CustomStruct(t *testing.T) {
  222. reporter, ctrl := createFixtures(t)
  223. subject := new(Subject)
  224. expectedArg0 := TestStruct{Number: 123, Message: "hello"}
  225. ctrl.RecordCall(subject, "ActOnTestStructMethod", expectedArg0, 15)
  226. ctrl.Call(subject, "ActOnTestStructMethod", expectedArg0, 15)
  227. reporter.assertPass("Expected method call made.")
  228. }
  229. func TestUnexpectedArgValue_FirstArg(t *testing.T) {
  230. reporter, ctrl := createFixtures(t)
  231. defer reporter.recoverUnexpectedFatal()
  232. subject := new(Subject)
  233. expectedArg0 := TestStruct{Number: 123, Message: "hello"}
  234. ctrl.RecordCall(subject, "ActOnTestStructMethod", expectedArg0, 15)
  235. reporter.assertFatal(func() {
  236. // the method argument (of TestStruct type) has 1 unexpected value (for the Message field)
  237. ctrl.Call(subject, "ActOnTestStructMethod", TestStruct{Number: 123, Message: "no message"}, 15)
  238. }, "Unexpected call to", "doesn't match the argument at index 0",
  239. "Got: {123 no message}\nWant: is equal to {123 hello}")
  240. reporter.assertFatal(func() {
  241. // the method argument (of TestStruct type) has 2 unexpected values (for both fields)
  242. ctrl.Call(subject, "ActOnTestStructMethod", TestStruct{Number: 11, Message: "no message"}, 15)
  243. }, "Unexpected call to", "doesn't match the argument at index 0",
  244. "Got: {11 no message}\nWant: is equal to {123 hello}")
  245. reporter.assertFatal(func() {
  246. // The expected call wasn't made.
  247. ctrl.Finish()
  248. })
  249. }
  250. func TestUnexpectedArgValue_SecondtArg(t *testing.T) {
  251. reporter, ctrl := createFixtures(t)
  252. defer reporter.recoverUnexpectedFatal()
  253. subject := new(Subject)
  254. expectedArg0 := TestStruct{Number: 123, Message: "hello"}
  255. ctrl.RecordCall(subject, "ActOnTestStructMethod", expectedArg0, 15)
  256. reporter.assertFatal(func() {
  257. ctrl.Call(subject, "ActOnTestStructMethod", TestStruct{Number: 123, Message: "hello"}, 3)
  258. }, "Unexpected call to", "doesn't match the argument at index 1",
  259. "Got: 3\nWant: is equal to 15")
  260. reporter.assertFatal(func() {
  261. // The expected call wasn't made.
  262. ctrl.Finish()
  263. })
  264. }
  265. func TestAnyTimes(t *testing.T) {
  266. reporter, ctrl := createFixtures(t)
  267. subject := new(Subject)
  268. ctrl.RecordCall(subject, "FooMethod", "argument").AnyTimes()
  269. for i := 0; i < 100; i++ {
  270. ctrl.Call(subject, "FooMethod", "argument")
  271. }
  272. reporter.assertPass("After 100 method calls.")
  273. ctrl.Finish()
  274. }
  275. func TestMinTimes1(t *testing.T) {
  276. // It fails if there are no calls
  277. reporter, ctrl := createFixtures(t)
  278. subject := new(Subject)
  279. ctrl.RecordCall(subject, "FooMethod", "argument").MinTimes(1)
  280. reporter.assertFatal(func() {
  281. ctrl.Finish()
  282. })
  283. // It succeeds if there is one call
  284. reporter, ctrl = createFixtures(t)
  285. subject = new(Subject)
  286. ctrl.RecordCall(subject, "FooMethod", "argument").MinTimes(1)
  287. ctrl.Call(subject, "FooMethod", "argument")
  288. ctrl.Finish()
  289. // It succeeds if there are many calls
  290. reporter, ctrl = createFixtures(t)
  291. subject = new(Subject)
  292. ctrl.RecordCall(subject, "FooMethod", "argument").MinTimes(1)
  293. for i := 0; i < 100; i++ {
  294. ctrl.Call(subject, "FooMethod", "argument")
  295. }
  296. ctrl.Finish()
  297. }
  298. func TestMaxTimes1(t *testing.T) {
  299. // It succeeds if there are no calls
  300. _, ctrl := createFixtures(t)
  301. subject := new(Subject)
  302. ctrl.RecordCall(subject, "FooMethod", "argument").MaxTimes(1)
  303. ctrl.Finish()
  304. // It succeeds if there is one call
  305. _, ctrl = createFixtures(t)
  306. subject = new(Subject)
  307. ctrl.RecordCall(subject, "FooMethod", "argument").MaxTimes(1)
  308. ctrl.Call(subject, "FooMethod", "argument")
  309. ctrl.Finish()
  310. //It fails if there are more
  311. reporter, ctrl := createFixtures(t)
  312. subject = new(Subject)
  313. ctrl.RecordCall(subject, "FooMethod", "argument").MaxTimes(1)
  314. ctrl.Call(subject, "FooMethod", "argument")
  315. reporter.assertFatal(func() {
  316. ctrl.Call(subject, "FooMethod", "argument")
  317. })
  318. ctrl.Finish()
  319. }
  320. func TestMinMaxTimes(t *testing.T) {
  321. // It fails if there are less calls than specified
  322. reporter, ctrl := createFixtures(t)
  323. subject := new(Subject)
  324. ctrl.RecordCall(subject, "FooMethod", "argument").MinTimes(2).MaxTimes(2)
  325. ctrl.Call(subject, "FooMethod", "argument")
  326. reporter.assertFatal(func() {
  327. ctrl.Finish()
  328. })
  329. // It fails if there are more calls than specified
  330. reporter, ctrl = createFixtures(t)
  331. subject = new(Subject)
  332. ctrl.RecordCall(subject, "FooMethod", "argument").MinTimes(2).MaxTimes(2)
  333. ctrl.Call(subject, "FooMethod", "argument")
  334. ctrl.Call(subject, "FooMethod", "argument")
  335. reporter.assertFatal(func() {
  336. ctrl.Call(subject, "FooMethod", "argument")
  337. })
  338. // It succeeds if there is just the right number of calls
  339. reporter, ctrl = createFixtures(t)
  340. subject = new(Subject)
  341. ctrl.RecordCall(subject, "FooMethod", "argument").MaxTimes(2).MinTimes(2)
  342. ctrl.Call(subject, "FooMethod", "argument")
  343. ctrl.Call(subject, "FooMethod", "argument")
  344. ctrl.Finish()
  345. }
  346. func TestDo(t *testing.T) {
  347. _, ctrl := createFixtures(t)
  348. subject := new(Subject)
  349. doCalled := false
  350. var argument string
  351. ctrl.RecordCall(subject, "FooMethod", "argument").Do(
  352. func(arg string) {
  353. doCalled = true
  354. argument = arg
  355. })
  356. if doCalled {
  357. t.Error("Do() callback called too early.")
  358. }
  359. ctrl.Call(subject, "FooMethod", "argument")
  360. if !doCalled {
  361. t.Error("Do() callback not called.")
  362. }
  363. if "argument" != argument {
  364. t.Error("Do callback received wrong argument.")
  365. }
  366. ctrl.Finish()
  367. }
  368. func TestDoAndReturn(t *testing.T) {
  369. _, ctrl := createFixtures(t)
  370. subject := new(Subject)
  371. doCalled := false
  372. var argument string
  373. ctrl.RecordCall(subject, "FooMethod", "argument").DoAndReturn(
  374. func(arg string) int {
  375. doCalled = true
  376. argument = arg
  377. return 5
  378. })
  379. if doCalled {
  380. t.Error("Do() callback called too early.")
  381. }
  382. rets := ctrl.Call(subject, "FooMethod", "argument")
  383. if !doCalled {
  384. t.Error("Do() callback not called.")
  385. }
  386. if "argument" != argument {
  387. t.Error("Do callback received wrong argument.")
  388. }
  389. if len(rets) != 1 {
  390. t.Fatalf("Return values from Call: got %d, want 1", len(rets))
  391. }
  392. if ret, ok := rets[0].(int); !ok {
  393. t.Fatalf("Return value is not an int")
  394. } else if ret != 5 {
  395. t.Errorf("DoAndReturn return value: got %d, want 5", ret)
  396. }
  397. ctrl.Finish()
  398. }
  399. func TestSetArgSlice(t *testing.T) {
  400. _, ctrl := createFixtures(t)
  401. subject := new(Subject)
  402. var in = []byte{4, 5, 6}
  403. var set = []byte{1, 2, 3}
  404. ctrl.RecordCall(subject, "SetArgMethod", in, nil).SetArg(0, set)
  405. ctrl.Call(subject, "SetArgMethod", in, nil)
  406. if !reflect.DeepEqual(in, set) {
  407. t.Error("Expected SetArg() to modify input slice argument")
  408. }
  409. ctrl.Finish()
  410. }
  411. func TestSetArgPtr(t *testing.T) {
  412. _, ctrl := createFixtures(t)
  413. subject := new(Subject)
  414. var in int = 43
  415. const set = 42
  416. ctrl.RecordCall(subject, "SetArgMethod", nil, &in).SetArg(1, set)
  417. ctrl.Call(subject, "SetArgMethod", nil, &in)
  418. if in != set {
  419. t.Error("Expected SetArg() to modify value pointed to by argument")
  420. }
  421. ctrl.Finish()
  422. }
  423. func TestReturn(t *testing.T) {
  424. _, ctrl := createFixtures(t)
  425. subject := new(Subject)
  426. // Unspecified return should produce "zero" result.
  427. ctrl.RecordCall(subject, "FooMethod", "zero")
  428. ctrl.RecordCall(subject, "FooMethod", "five").Return(5)
  429. assertEqual(
  430. t,
  431. []interface{}{0},
  432. ctrl.Call(subject, "FooMethod", "zero"))
  433. assertEqual(
  434. t,
  435. []interface{}{5},
  436. ctrl.Call(subject, "FooMethod", "five"))
  437. ctrl.Finish()
  438. }
  439. func TestUnorderedCalls(t *testing.T) {
  440. reporter, ctrl := createFixtures(t)
  441. defer reporter.recoverUnexpectedFatal()
  442. subjectTwo := new(Subject)
  443. subjectOne := new(Subject)
  444. ctrl.RecordCall(subjectOne, "FooMethod", "1")
  445. ctrl.RecordCall(subjectOne, "BarMethod", "2")
  446. ctrl.RecordCall(subjectTwo, "FooMethod", "3")
  447. ctrl.RecordCall(subjectTwo, "BarMethod", "4")
  448. // Make the calls in a different order, which should be fine.
  449. ctrl.Call(subjectOne, "BarMethod", "2")
  450. ctrl.Call(subjectTwo, "FooMethod", "3")
  451. ctrl.Call(subjectTwo, "BarMethod", "4")
  452. ctrl.Call(subjectOne, "FooMethod", "1")
  453. reporter.assertPass("After making all calls in different order")
  454. ctrl.Finish()
  455. reporter.assertPass("After finish")
  456. }
  457. func commonTestOrderedCalls(t *testing.T) (reporter *ErrorReporter, ctrl *gomock.Controller, subjectOne, subjectTwo *Subject) {
  458. reporter, ctrl = createFixtures(t)
  459. subjectOne = new(Subject)
  460. subjectTwo = new(Subject)
  461. gomock.InOrder(
  462. ctrl.RecordCall(subjectOne, "FooMethod", "1").AnyTimes(),
  463. ctrl.RecordCall(subjectTwo, "FooMethod", "2"),
  464. ctrl.RecordCall(subjectTwo, "BarMethod", "3"),
  465. )
  466. return
  467. }
  468. func TestOrderedCallsCorrect(t *testing.T) {
  469. reporter, ctrl, subjectOne, subjectTwo := commonTestOrderedCalls(t)
  470. ctrl.Call(subjectOne, "FooMethod", "1")
  471. ctrl.Call(subjectTwo, "FooMethod", "2")
  472. ctrl.Call(subjectTwo, "BarMethod", "3")
  473. ctrl.Finish()
  474. reporter.assertPass("After finish")
  475. }
  476. func TestOrderedCallsInCorrect(t *testing.T) {
  477. reporter, ctrl, subjectOne, subjectTwo := commonTestOrderedCalls(t)
  478. ctrl.Call(subjectOne, "FooMethod", "1")
  479. reporter.assertFatal(func() {
  480. // FooMethod(2) should be called before BarMethod(3)
  481. ctrl.Call(subjectTwo, "BarMethod", "3")
  482. }, "Unexpected call to", "Subject.BarMethod([3])", "doesn't have a prerequisite call satisfied")
  483. }
  484. // Test that calls that are prerequisites to other calls but have maxCalls >
  485. // minCalls are removed from the expected call set.
  486. func TestOrderedCallsWithPreReqMaxUnbounded(t *testing.T) {
  487. reporter, ctrl, subjectOne, subjectTwo := commonTestOrderedCalls(t)
  488. // Initially we should be able to call FooMethod("1") as many times as we
  489. // want.
  490. ctrl.Call(subjectOne, "FooMethod", "1")
  491. ctrl.Call(subjectOne, "FooMethod", "1")
  492. // But calling something that has it as a prerequite should remove it from
  493. // the expected call set. This allows tests to ensure that FooMethod("1") is
  494. // *not* called after FooMethod("2").
  495. ctrl.Call(subjectTwo, "FooMethod", "2")
  496. // Therefore this call should fail:
  497. reporter.assertFatal(func() {
  498. ctrl.Call(subjectOne, "FooMethod", "1")
  499. })
  500. }
  501. func TestCallAfterLoopPanic(t *testing.T) {
  502. _, ctrl := createFixtures(t)
  503. subject := new(Subject)
  504. firstCall := ctrl.RecordCall(subject, "FooMethod", "1")
  505. secondCall := ctrl.RecordCall(subject, "FooMethod", "2")
  506. thirdCall := ctrl.RecordCall(subject, "FooMethod", "3")
  507. gomock.InOrder(firstCall, secondCall, thirdCall)
  508. defer func() {
  509. err := recover()
  510. if err == nil {
  511. t.Error("Call.After creation of dependency loop did not panic.")
  512. }
  513. }()
  514. // This should panic due to dependency loop.
  515. firstCall.After(thirdCall)
  516. }
  517. func TestPanicOverridesExpectationChecks(t *testing.T) {
  518. ctrl := gomock.NewController(t)
  519. reporter := NewErrorReporter(t)
  520. reporter.assertFatal(func() {
  521. ctrl.RecordCall(new(Subject), "FooMethod", "1")
  522. defer ctrl.Finish()
  523. reporter.Fatalf("Intentional panic")
  524. })
  525. }
  526. func TestSetArgWithBadType(t *testing.T) {
  527. rep, ctrl := createFixtures(t)
  528. defer ctrl.Finish()
  529. s := new(Subject)
  530. // This should catch a type error:
  531. rep.assertFatal(func() {
  532. ctrl.RecordCall(s, "FooMethod", "1").SetArg(0, "blah")
  533. })
  534. ctrl.Call(s, "FooMethod", "1")
  535. }
  536. func TestTimes0(t *testing.T) {
  537. rep, ctrl := createFixtures(t)
  538. defer ctrl.Finish()
  539. s := new(Subject)
  540. ctrl.RecordCall(s, "FooMethod", "arg").Times(0)
  541. rep.assertFatal(func() {
  542. ctrl.Call(s, "FooMethod", "arg")
  543. })
  544. }
  545. func TestVariadicMatching(t *testing.T) {
  546. rep, ctrl := createFixtures(t)
  547. defer rep.recoverUnexpectedFatal()
  548. s := new(Subject)
  549. ctrl.RecordCall(s, "VariadicMethod", 0, "1", "2")
  550. ctrl.Call(s, "VariadicMethod", 0, "1", "2")
  551. ctrl.Finish()
  552. rep.assertPass("variadic matching works")
  553. }
  554. func TestVariadicNoMatch(t *testing.T) {
  555. rep, ctrl := createFixtures(t)
  556. defer rep.recoverUnexpectedFatal()
  557. s := new(Subject)
  558. ctrl.RecordCall(s, "VariadicMethod", 0)
  559. rep.assertFatal(func() {
  560. ctrl.Call(s, "VariadicMethod", 1)
  561. }, "Expected call at", "doesn't match the argument at index 0",
  562. "Got: 1\nWant: is equal to 0")
  563. ctrl.Call(s, "VariadicMethod", 0)
  564. ctrl.Finish()
  565. }
  566. func TestVariadicMatchingWithSlice(t *testing.T) {
  567. testCases := [][]string{
  568. {"1"},
  569. {"1", "2"},
  570. }
  571. for _, tc := range testCases {
  572. t.Run(fmt.Sprintf("%d arguments", len(tc)), func(t *testing.T) {
  573. rep, ctrl := createFixtures(t)
  574. defer rep.recoverUnexpectedFatal()
  575. s := new(Subject)
  576. ctrl.RecordCall(s, "VariadicMethod", 1, tc)
  577. args := make([]interface{}, len(tc)+1)
  578. args[0] = 1
  579. for i, arg := range tc {
  580. args[i+1] = arg
  581. }
  582. ctrl.Call(s, "VariadicMethod", args...)
  583. ctrl.Finish()
  584. rep.assertPass("slices can be used as matchers for variadic arguments")
  585. })
  586. }
  587. }
  588. func TestDuplicateFinishCallFails(t *testing.T) {
  589. rep, ctrl := createFixtures(t)
  590. ctrl.Finish()
  591. rep.assertPass("the first Finish call should succeed")
  592. rep.assertFatal(ctrl.Finish, "Controller.Finish was called more than once. It has to be called exactly once.")
  593. }
  594. func TestNoHelper(t *testing.T) {
  595. ctrlNoHelper := gomock.NewController(NewErrorReporter(t))
  596. // doesn't panic
  597. ctrlNoHelper.T.Helper()
  598. }
  599. func TestWithHelper(t *testing.T) {
  600. withHelper := &HelperReporter{TestReporter: NewErrorReporter(t)}
  601. ctrlWithHelper := gomock.NewController(withHelper)
  602. ctrlWithHelper.T.Helper()
  603. if withHelper.helper == 0 {
  604. t.Fatal("expected Helper to be invoked")
  605. }
  606. }