diff options
author | Devtools Arcadia <arcadia-devtools@yandex-team.ru> | 2022-02-07 18:08:42 +0300 |
---|---|---|
committer | Devtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net> | 2022-02-07 18:08:42 +0300 |
commit | 1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch) | |
tree | e26c9fed0de5d9873cce7e00bc214573dc2195b7 /library/cpp/testing/gmock_in_unittest | |
download | ydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz |
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'library/cpp/testing/gmock_in_unittest')
7 files changed, 200 insertions, 0 deletions
diff --git a/library/cpp/testing/gmock_in_unittest/events.cpp b/library/cpp/testing/gmock_in_unittest/events.cpp new file mode 100644 index 0000000000..dbd65b727d --- /dev/null +++ b/library/cpp/testing/gmock_in_unittest/events.cpp @@ -0,0 +1,32 @@ +#include "events.h" + +#include <library/cpp/testing/unittest/registar.h> + +#include <util/generic/strbuf.h> +#include <util/generic/string.h> +#include <util/string/builder.h> + +void TGMockTestEventListener::OnTestPartResult(const testing::TestPartResult& result) { + if (result.failed()) { + const TString message = result.message(); + const TString summary = result.summary(); + TStringBuilder msg; + if (result.file_name()) + msg << result.file_name() << TStringBuf(":"); + if (result.line_number() != -1) + msg << result.line_number() << TStringBuf(":"); + if (summary) { + if (msg) { + msg << TStringBuf("\n"); + } + msg << summary; + } + if (message && summary != message) { + if (msg) { + msg << TStringBuf("\n"); + } + msg << message; + } + NUnitTest::NPrivate::RaiseError(result.summary(), msg, result.fatally_failed()); + } +} diff --git a/library/cpp/testing/gmock_in_unittest/events.h b/library/cpp/testing/gmock_in_unittest/events.h new file mode 100644 index 0000000000..84c10a93de --- /dev/null +++ b/library/cpp/testing/gmock_in_unittest/events.h @@ -0,0 +1,8 @@ +#pragma once + +#include <gtest/gtest.h> + +class TGMockTestEventListener: public testing::EmptyTestEventListener { +public: + void OnTestPartResult(const testing::TestPartResult& result) override; +}; 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(); + } + } +} diff --git a/library/cpp/testing/gmock_in_unittest/example_ut/ya.make b/library/cpp/testing/gmock_in_unittest/example_ut/ya.make new file mode 100644 index 0000000000..d2e5ee5d2a --- /dev/null +++ b/library/cpp/testing/gmock_in_unittest/example_ut/ya.make @@ -0,0 +1,13 @@ +UNITTEST() + +OWNER(galaxycrab) + +PEERDIR( + library/cpp/testing/gmock_in_unittest +) + +SRCS( + example_ut.cpp +) + +END() diff --git a/library/cpp/testing/gmock_in_unittest/gmock.h b/library/cpp/testing/gmock_in_unittest/gmock.h new file mode 100644 index 0000000000..31f6aee1c3 --- /dev/null +++ b/library/cpp/testing/gmock_in_unittest/gmock.h @@ -0,0 +1,5 @@ +#pragma once + +#include <library/cpp/testing/gtest_extensions/gtest_extensions.h> + +#include <gmock/gmock.h> diff --git a/library/cpp/testing/gmock_in_unittest/registration.cpp b/library/cpp/testing/gmock_in_unittest/registration.cpp new file mode 100644 index 0000000000..c2872a4c27 --- /dev/null +++ b/library/cpp/testing/gmock_in_unittest/registration.cpp @@ -0,0 +1,20 @@ +#include "events.h" + +#include <gmock/gmock.h> + +#include <library/cpp/testing/unittest/plugin.h> + +namespace { + class TGMockUnittestPlugin: public NUnitTest::NPlugin::IPlugin { + public: + void OnStartMain(int argc, char* argv[]) override { + testing::InitGoogleMock(&argc, argv); + testing::TestEventListeners& listeners = testing::UnitTest::GetInstance()->listeners(); + delete listeners.Release(listeners.default_result_printer()); + listeners.Append(new TGMockTestEventListener()); + } + }; + + NUnitTest::NPlugin::TPluginRegistrator registerGMock(new TGMockUnittestPlugin()); + +} diff --git a/library/cpp/testing/gmock_in_unittest/ya.make b/library/cpp/testing/gmock_in_unittest/ya.make new file mode 100644 index 0000000000..5de68ad98d --- /dev/null +++ b/library/cpp/testing/gmock_in_unittest/ya.make @@ -0,0 +1,17 @@ +LIBRARY() + +OWNER(galaxycrab) + +PEERDIR( + contrib/restricted/googletest/googlemock + contrib/restricted/googletest/googletest + library/cpp/testing/gtest_extensions + library/cpp/testing/unittest +) + +SRCS( + events.cpp + GLOBAL registration.cpp +) + +END() |