-
Notifications
You must be signed in to change notification settings - Fork 10.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix MOCK_METHOD to handle noexcept correctly #2498
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
10c1d8c
Use the verbatim noexcept spec in MOCKED_METHOD
thejcannon 0eadff8
Fix spacing
thejcannon 872b9ce
Avoid comma operator
thejcannon 7f8617a
Switch to free function to avoid GCC bug
thejcannon 20255e6
Use declval in noexcept expression
thejcannon bc996e0
Made noexcept condition more exciting
thejcannon 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
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 |
---|---|---|
|
@@ -42,6 +42,7 @@ | |
|
||
#include <map> | ||
#include <string> | ||
#include <type_traits> | ||
#include "gmock/gmock.h" | ||
#include "gtest/gtest.h" | ||
|
||
|
@@ -656,5 +657,31 @@ TEST(MockMethodMockFunctionTest, MockMethodSizeOverhead) { | |
EXPECT_EQ(sizeof(MockMethodSizes0), sizeof(MockMethodSizes4)); | ||
} | ||
|
||
void hasTwoParams(int, int); | ||
void MaybeThrows(); | ||
void DoesntThrow() noexcept; | ||
struct MockMethodNoexceptSpecifier { | ||
MOCK_METHOD(void, func1, (), (noexcept)); | ||
MOCK_METHOD(void, func2, (), (noexcept(true))); | ||
MOCK_METHOD(void, func3, (), (noexcept(false))); | ||
MOCK_METHOD(void, func4, (), (noexcept(noexcept(MaybeThrows())))); | ||
MOCK_METHOD(void, func5, (), (noexcept(noexcept(DoesntThrow())))); | ||
MOCK_METHOD(void, func6, (), (noexcept(noexcept(DoesntThrow())), const)); | ||
MOCK_METHOD(void, func7, (), (const, noexcept(noexcept(DoesntThrow())))); | ||
// Put commas in the noexcept expression | ||
MOCK_METHOD(void, func8, (), (noexcept(noexcept(hasTwoParams(1,2))), const)); | ||
}; | ||
|
||
TEST(MockMethodMockFunctionTest, NoexceptSpecifierPreserved) { | ||
EXPECT_TRUE(noexcept(std::declval<MockMethodNoexceptSpecifier>().func1())); | ||
EXPECT_TRUE(noexcept(std::declval<MockMethodNoexceptSpecifier>().func2())); | ||
EXPECT_FALSE(noexcept(std::declval<MockMethodNoexceptSpecifier>().func3())); | ||
EXPECT_FALSE(noexcept(std::declval<MockMethodNoexceptSpecifier>().func4())); | ||
EXPECT_TRUE(noexcept(std::declval<MockMethodNoexceptSpecifier>().func5())); | ||
EXPECT_TRUE(noexcept(std::declval<MockMethodNoexceptSpecifier>().func6())); | ||
EXPECT_TRUE(noexcept(std::declval<MockMethodNoexceptSpecifier>().func7())); | ||
EXPECT_EQ(noexcept(std::declval<MockMethodNoexceptSpecifier>().func8()), noexcept(hasTwoParams(1,2))); | ||
} | ||
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. I see no point in comparing with
|
||
|
||
} // namespace gmock_function_mocker_test | ||
} // namespace testing |
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.
Is there a reason to use declval here? I'd simply define a MockMethodNoexceptSpecifier object.
In case the compiler warns that it's unused, it can be suppressed by cast to void.
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 had that previously, but the compiler would always say the result of the expression is
noexcept(false)
.https://travis-ci.org/google/googletest/jobs/594800623#L2313
My guess would be it has to do with the constructor not being marked
noexcept
.But
declval
really is the "correct" solution as we just want to test thenoexcept
specifier for the function (and not the default constructor as well).Another solution would be to use
std::is_nothrow_invocable
, but I don't think there's a technical reason to prefer one over another.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.
My intention was to define one MockMethodNoexceptSpecifier object in the test, and use it in all expectations. Then it wouldn't matter if the constructor is noexcept.
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.
That's definitely another solution. Personally, I wouldn't like having to declare an instance of an object (which is a runtime construct) just so syntactically (since
noexcept
expressions are evaluated at compile-time) I can reason about the methods.But I also can't argue it would be less code.
If this comes up in the "internal review" I'd be alright changing it.
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.
Instead of EXPECT_TRUE/FALSE, these could be tested at compile time with
static_assert
.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.
Yeah, it's all about tradeoffs if you want to use
static_assert
s in your test code to test some logic.If you move it into a compile-time check, you know it'll always be true. The tests wouldn't compile otherwise. On the other hand, normally one test failing doesn't bar other tests from running and reporting their successes/failures. Since the
noexcept
spec wouldn't be absolutely fatal if it was wrong, I opted to implement them as runtime checks so they can report successes/failures just like other tests.