Last active
June 10, 2022 01:11
-
-
Save aliabidzaidi/90c907370c1e8305a7e7f0bb11a6496b to your computer and use it in GitHub Desktop.
Google Test and Mock example
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
// To Build and Run | |
// g++ main.cpp -g -lgtest -lgmock | |
// ./a.out | |
// links | |
// https://github.com/google/googletest/blob/main/docs/gmock_cook_book.md | |
// https://stackoverflow.com/questions/58501850/googlemock-how-to-use-mocking-in-test | |
// https://qiangbo-workspace.oss-cn-shanghai.aliyuncs.com/2018-12-05-gtest-and-coverage/PlainGoogleQuickTestReferenceGuide1.pdf | |
#include "gtest/gtest.h" | |
#include "gmock/gmock.h" | |
using ::testing::_; | |
using ::testing::AtLeast; | |
using ::testing::Return; | |
class IUniverse | |
{ | |
public: | |
virtual ~IUniverse() = default; | |
virtual bool makeEarth() = 0; | |
virtual std::string getWeather() = 0; | |
virtual float cultivateSoil(double mineralCount, int treeCount, float waterCubics) = 0; | |
}; | |
class Universe : public IUniverse | |
{ | |
public: | |
~Universe() override {} | |
bool makeEarth() override | |
{ | |
throw std::runtime_error("Not Enough Powers"); | |
return false; | |
} | |
std::string getWeather() override | |
{ | |
throw std::runtime_error("Its gettin hot in here"); | |
} | |
float cultivateSoil(double mineralCount, int treeCount, float waterCubics) override | |
{ | |
auto cultivationPercentage = ((float)(mineralCount * treeCount * waterCubics) / 0); | |
return cultivationPercentage; | |
} | |
}; | |
class MockUniverse : public IUniverse | |
{ | |
public: | |
~MockUniverse() override {} | |
MOCK_METHOD(bool, makeEarth, (), (override)); | |
MOCK_METHOD(std::string, getWeather, (), (override)); | |
MOCK_METHOD(float, cultivateSoil, (double, int, float), (override)); | |
}; | |
class Human | |
{ | |
public: | |
IUniverse &universe; | |
Human(IUniverse &newUniverse) : universe{newUniverse} {} | |
bool tweakPlanet() | |
{ | |
std::string s = universe.getWeather(); | |
return true; | |
} | |
}; | |
TEST(Universe, makeEarth) | |
{ | |
MockUniverse universe; | |
EXPECT_CALL(universe, makeEarth()) // #3 | |
.WillOnce(Return(false)) | |
.WillRepeatedly(Return(true)); | |
EXPECT_FALSE(universe.makeEarth()); | |
EXPECT_TRUE(universe.makeEarth()); // #5 | |
} | |
TEST(Universe, getWeather) | |
{ | |
MockUniverse universe; | |
EXPECT_CALL(universe, getWeather()) // #3 | |
.WillOnce(Return("Summer")) | |
.WillOnce(Return("Winter")) | |
.WillOnce(Return("Spring")) | |
.WillOnce(Return("Autumn")) | |
.WillRepeatedly(Return("Raining")); | |
EXPECT_EQ(universe.getWeather(), "Summer"); | |
EXPECT_EQ(universe.getWeather(), "Winter"); | |
EXPECT_EQ(universe.getWeather(), "Spring"); | |
EXPECT_EQ(universe.getWeather(), "Autumn"); | |
EXPECT_EQ(universe.getWeather(), "Raining"); | |
} | |
TEST(Universe, cultivateSoil) | |
{ | |
MockUniverse universe; | |
EXPECT_CALL(universe, cultivateSoil(_, _, _)) // #3 | |
.WillOnce(Return(98.4234)) | |
.WillRepeatedly(Return(1.99999)); | |
ASSERT_FLOAT_EQ(universe.cultivateSoil(10.0, 0, 0), 98.4234); | |
ASSERT_FLOAT_EQ(universe.cultivateSoil(10.0, 0, 0), 1.99999); | |
ASSERT_FLOAT_EQ(universe.cultivateSoil(0, 0, 0), 1.99999); | |
} | |
TEST(Universe, humanMakesEarth) | |
{ | |
MockUniverse mUniverse; | |
Human human(mUniverse); | |
EXPECT_CALL(mUniverse, makeEarth()) // #3 | |
.WillOnce(Return(false)) | |
.WillRepeatedly(Return(true)); | |
EXPECT_FALSE(human.universe.makeEarth()); | |
EXPECT_TRUE(human.universe.makeEarth()); // #5 | |
} | |
TEST(Universe, humanGetsWeather) | |
{ | |
MockUniverse mUniverse; | |
Human human(mUniverse); | |
EXPECT_CALL(mUniverse, getWeather()) | |
.WillOnce(Return("Summer")) | |
.WillRepeatedly(Return("Raining")); | |
EXPECT_EQ(mUniverse.getWeather(), "Summer"); | |
EXPECT_NE(mUniverse.getWeather(), "Winter"); | |
EXPECT_EQ(mUniverse.getWeather(), "Raining"); | |
EXPECT_EQ(mUniverse.getWeather(), "Raining"); | |
} | |
int main(int argc, char **argv) | |
{ | |
testing::InitGoogleTest(&argc, argv); | |
return RUN_ALL_TESTS(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment