aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/testing/gmock_in_unittest/example_ut/example_ut.cpp
diff options
context:
space:
mode:
authorDevtools Arcadia <arcadia-devtools@yandex-team.ru>2022-02-07 18:08:42 +0300
committerDevtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net>2022-02-07 18:08:42 +0300
commit1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch)
treee26c9fed0de5d9873cce7e00bc214573dc2195b7 /library/cpp/testing/gmock_in_unittest/example_ut/example_ut.cpp
downloadydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'library/cpp/testing/gmock_in_unittest/example_ut/example_ut.cpp')
-rw-r--r--library/cpp/testing/gmock_in_unittest/example_ut/example_ut.cpp105
1 files changed, 105 insertions, 0 deletions
diff --git a/library/cpp/testing/gmock_in_unittest/example_ut/example_ut.cpp b/library/cpp/testing/gmock_in_unittest/example_ut/example_ut.cpp
new file mode 100644
index 0000000000..97f19050e4
--- /dev/null
+++ b/library/cpp/testing/gmock_in_unittest/example_ut/example_ut.cpp
@@ -0,0 +1,105 @@
+#include <library/cpp/testing/gmock_in_unittest/gmock.h>
+
+#include <library/cpp/testing/unittest/registar.h>
+
+#include <util/generic/string.h>
+
+// Set this variable to true if you want to see failures
+/////////////////////////////////////////////////////////
+static const bool fail = false;
+/////////////////////////////////////////////////////////
+
+class ITestIface {
+public:
+ virtual ~ITestIface() {
+ }
+
+ virtual void Func1() = 0;
+
+ virtual int Func2(const TString&) const = 0;
+};
+
+class TTestMock: public ITestIface {
+public:
+ MOCK_METHOD(void, Func1, (), (override));
+ MOCK_METHOD(int, Func2, (const TString&), (const, override));
+};
+
+using namespace testing;
+
+Y_UNIT_TEST_SUITE(TExampleGMockTest) {
+ Y_UNIT_TEST(TSimpleTest) {
+ TTestMock mock;
+ EXPECT_CALL(mock, Func1())
+ .Times(AtLeast(1));
+
+ if (!fail) {
+ mock.Func1();
+ }
+ }
+
+ Y_UNIT_TEST(TNonExpectedCallTest) {
+ TTestMock mock;
+ EXPECT_CALL(mock, Func1())
+ .Times(AtMost(1));
+ mock.Func1();
+ if (fail) {
+ mock.Func1();
+ }
+ }
+
+ Y_UNIT_TEST(TReturnValuesTest) {
+ TTestMock mock;
+ EXPECT_CALL(mock, Func2(TString("1")))
+ .WillOnce(Return(1))
+ .WillRepeatedly(Return(42));
+
+ EXPECT_CALL(mock, Func2(TString("hello")))
+ .WillOnce(Return(-1));
+
+ UNIT_ASSERT_VALUES_EQUAL(mock.Func2("hello"), -1);
+
+ UNIT_ASSERT_VALUES_EQUAL(mock.Func2("1"), 1);
+ UNIT_ASSERT_VALUES_EQUAL(mock.Func2("1"), 42);
+ UNIT_ASSERT_VALUES_EQUAL(mock.Func2("1"), 42);
+ UNIT_ASSERT_VALUES_EQUAL(mock.Func2("1"), 42);
+ UNIT_ASSERT_VALUES_EQUAL(mock.Func2("1"), 42);
+
+ if (fail) {
+ UNIT_ASSERT_VALUES_EQUAL(mock.Func2("hello"), -1); // expected to return -1 only once
+ }
+ }
+
+ Y_UNIT_TEST(TStrictCallSequenceTest) {
+ TTestMock mock;
+ {
+ InSequence seq;
+ EXPECT_CALL(mock, Func1())
+ .Times(1);
+ EXPECT_CALL(mock, Func2(_))
+ .Times(2)
+ .WillOnce(Return(1))
+ .WillOnce(Return(2));
+ EXPECT_CALL(mock, Func1());
+ }
+ mock.Func1();
+ UNIT_ASSERT_VALUES_EQUAL(mock.Func2("sample"), 1);
+ if (fail) {
+ mock.Func1();
+ }
+ UNIT_ASSERT_VALUES_EQUAL(mock.Func2(""), 2);
+ if (!fail) {
+ mock.Func1();
+ }
+ }
+
+ Y_UNIT_TEST(TUninterestingMethodIsFailureTest) {
+ StrictMock<TTestMock> mock;
+ EXPECT_CALL(mock, Func1())
+ .Times(1);
+ mock.Func1();
+ if (fail) {
+ mock.Func1();
+ }
+ }
+}