This repository has been archived by the owner on Jun 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 607
add default calling of ctrl.Finish() in go1.14+ #422
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,8 +2,8 @@ language: go | |
|
||
go: | ||
- 1.11.x | ||
- 1.12.x | ||
- 1.13.x | ||
- 1.14.x | ||
|
||
env: | ||
- GO111MODULE=on | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright 2020 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// +build !go1.14 | ||
|
||
package gomock_test | ||
|
||
import "testing" | ||
|
||
func TestDuplicateFinishCallFails(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was the previous behavior, lets keep things the same for versions less than 1.14 |
||
rep, ctrl := createFixtures(t) | ||
|
||
ctrl.Finish() | ||
rep.assertPass("the first Finish call should succeed") | ||
|
||
rep.assertFatal(ctrl.Finish, "Controller.Finish was called more than once. It has to be called exactly once.") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
// Copyright 2020 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// +build go1.14 | ||
|
||
package gomock_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/golang/mock/gomock" | ||
) | ||
|
||
func (e *ErrorReporter) Cleanup(f func()) { | ||
e.t.Helper() | ||
e.t.Cleanup(f) | ||
} | ||
|
||
func TestMultipleDefers(t *testing.T) { | ||
reporter := NewErrorReporter(t) | ||
reporter.Cleanup(func() { | ||
reporter.assertLogf("In Go 1.14+ you no longer need to `ctrl.Finish()` if a *testing.T is passed to `NewController(...)`") | ||
}) | ||
ctrl := gomock.NewController(reporter) | ||
ctrl.Finish() | ||
} | ||
|
||
// Equivalent to the TestNoRecordedCallsForAReceiver, but without explicitly | ||
// calling Finish. | ||
func TestDeferNotNeededFail(t *testing.T) { | ||
reporter := NewErrorReporter(t) | ||
subject := new(Subject) | ||
var ctrl *gomock.Controller | ||
reporter.Cleanup(func() { | ||
reporter.assertFatal(func() { | ||
ctrl.Call(subject, "NotRecordedMethod", "argument") | ||
}, "Unexpected call to", "there are no expected calls of the method \"NotRecordedMethod\" for that receiver") | ||
}) | ||
ctrl = gomock.NewController(reporter) | ||
} | ||
|
||
func TestDeferNotNeededPass(t *testing.T) { | ||
reporter := NewErrorReporter(t) | ||
subject := new(Subject) | ||
var ctrl *gomock.Controller | ||
reporter.Cleanup(func() { | ||
reporter.assertPass("Expected method call made.") | ||
}) | ||
ctrl = gomock.NewController(reporter) | ||
ctrl.RecordCall(subject, "FooMethod", "argument") | ||
ctrl.Call(subject, "FooMethod", "argument") | ||
} | ||
|
||
func TestOrderedCallsInCorrect(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With a couple of these tests I had to get a little fancy. Their implementation relied on Finish not being called to verify things. Cleanup acts in a LIFO matter. By registering a cleanup before the one in NewController, I can assert things after Finish() has been called. |
||
reporter := NewErrorReporter(t) | ||
subjectOne := new(Subject) | ||
subjectTwo := new(Subject) | ||
var ctrl *gomock.Controller | ||
reporter.Cleanup(func() { | ||
reporter.assertFatal(func() { | ||
gomock.InOrder( | ||
ctrl.RecordCall(subjectOne, "FooMethod", "1").AnyTimes(), | ||
ctrl.RecordCall(subjectTwo, "FooMethod", "2"), | ||
ctrl.RecordCall(subjectTwo, "BarMethod", "3"), | ||
) | ||
ctrl.Call(subjectOne, "FooMethod", "1") | ||
// FooMethod(2) should be called before BarMethod(3) | ||
ctrl.Call(subjectTwo, "BarMethod", "3") | ||
}, "Unexpected call to", "Subject.BarMethod([3])", "doesn't have a prerequisite call satisfied") | ||
}) | ||
ctrl = gomock.NewController(reporter) | ||
} | ||
|
||
// Test that calls that are prerequisites to other calls but have maxCalls > | ||
// minCalls are removed from the expected call set. | ||
func TestOrderedCallsWithPreReqMaxUnbounded(t *testing.T) { | ||
reporter := NewErrorReporter(t) | ||
subjectOne := new(Subject) | ||
subjectTwo := new(Subject) | ||
var ctrl *gomock.Controller | ||
reporter.Cleanup(func() { | ||
reporter.assertFatal(func() { | ||
// Initially we should be able to call FooMethod("1") as many times as we | ||
// want. | ||
ctrl.Call(subjectOne, "FooMethod", "1") | ||
ctrl.Call(subjectOne, "FooMethod", "1") | ||
|
||
// But calling something that has it as a prerequite should remove it from | ||
// the expected call set. This allows tests to ensure that FooMethod("1") is | ||
// *not* called after FooMethod("2"). | ||
ctrl.Call(subjectTwo, "FooMethod", "2") | ||
|
||
ctrl.Call(subjectOne, "FooMethod", "1") | ||
}) | ||
}) | ||
ctrl = gomock.NewController(reporter) | ||
} | ||
|
||
func TestCallAfterLoopPanic(t *testing.T) { | ||
reporter := NewErrorReporter(t) | ||
subject := new(Subject) | ||
var ctrl *gomock.Controller | ||
reporter.Cleanup(func() { | ||
firstCall := ctrl.RecordCall(subject, "FooMethod", "1") | ||
secondCall := ctrl.RecordCall(subject, "FooMethod", "2") | ||
thirdCall := ctrl.RecordCall(subject, "FooMethod", "3") | ||
|
||
gomock.InOrder(firstCall, secondCall, thirdCall) | ||
|
||
defer func() { | ||
err := recover() | ||
if err == nil { | ||
t.Error("Call.After creation of dependency loop did not panic.") | ||
} | ||
}() | ||
|
||
// This should panic due to dependency loop. | ||
firstCall.After(thirdCall) | ||
}) | ||
ctrl = gomock.NewController(reporter) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't decide if this is a good or bad idea. Do we let users know about this new behavior? I think this only shows up with
-v
. WDYT?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's a good idea. I think you're correct that it would only show up if
-v
is passed