Skip to content
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

[GeoMechanicsApplication] First experiment with GMock #12841

Merged
merged 7 commits into from
Nov 13, 2024

Conversation

rfaasse
Copy link
Contributor

@rfaasse rfaasse commented Nov 11, 2024

📝 Description
This PR replaces manually created functions for two Mock/Stub/Spy classes with the MOCK_METHOD macro implemented in GMock. This has a couple of advantages:

  1. If we have to override functions (i.e. base class has purely virtual functions), this can be done easily with the MOCK_METHOD macro
  2. Functions created with these macro can be checked using the EXPECT_CALL macro. You can set the expectation for how many times a function is called (for example EXPECT_CALL(*converging_strategy, Predict()).Times(1)), you can also control what it will output (for example EXPECT_CALL(*converging_strategy, GetNumberOfIterations()).WillOnce(testing::Return(4)); or ON_CALL(*this, GetNumberOfIterations()).WillByDefault(testing::Return(4));)

@rfaasse rfaasse requested a review from a team as a code owner November 11, 2024 12:08
@rfaasse rfaasse marked this pull request as draft November 11, 2024 12:08
@@ -25,7 +25,7 @@ macro(kratos_add_gtests)
add_executable("${KRATOS_ADD_GTEST_TARGET}Test" ${KRATOS_ADD_GTEST_SOURCES} ${KRATOS_GTEST_MAIN_SOURCE})
target_link_libraries("${KRATOS_ADD_GTEST_TARGET}Test" ${KRATOS_ADD_GTEST_TARGET} KratosCoreTestUtilities "${TESTING_MPI_UTILITIES}" GTest::gmock_main)
set_target_properties("${KRATOS_ADD_GTEST_TARGET}Test" PROPERTIES COMPILE_DEFINITIONS "KRATOS_TEST_CORE=IMPORT,API")

target_compile_definitions("${KRATOS_ADD_GTEST_TARGET}Test" PUBLIC GTEST_LINKED_AS_SHARED_LIBRARY)
Copy link
Contributor Author

@rfaasse rfaasse Nov 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@roigcarlo It seems we need to set this compile definition since we're using GTest as a shared library (see e.g. https://stackoverflow.com/questions/46025439/unresolved-external-symbol-error-with-google-mock-and-vcpkg), otherwise I get linking errors when I try to use GMock

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, no problem 👍

@rfaasse rfaasse marked this pull request as ready for review November 11, 2024 12:11
@rfaasse rfaasse changed the title [DRAFT][GeoMechanicsApplication] First experiment with GMock [GeoMechanicsApplication] First experiment with GMock Nov 11, 2024
@rfaasse rfaasse requested a review from avdg81 November 11, 2024 12:12
@rfaasse rfaasse self-assigned this Nov 11, 2024
Copy link
Contributor

@avdg81 avdg81 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How nice that you have cleaned this up by using GMock. I have no comments, so please feel free to merge.

@rfaasse rfaasse merged commit 09c1d62 into master Nov 13, 2024
11 checks passed
@rfaasse rfaasse deleted the geo/experiment-gmock branch November 13, 2024 14:08
loumalouomega pushed a commit that referenced this pull request Nov 14, 2024
…dized GMock functionality for one set of unit tests (#12841)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants