diff options
author | Vasily Gerasimov <UgnineSirdis@gmail.com> | 2022-02-10 16:49:09 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:49:09 +0300 |
commit | 6cdc8f140213c595e4ad38bc3d97fcef1146b8c3 (patch) | |
tree | f69637041e6fed76ebae0c74ae1fa0c4be6ab5b4 /library/cpp/testing | |
parent | e5d4696304c6689379ac7ce334512404d4b7836c (diff) | |
download | ydb-6cdc8f140213c595e4ad38bc3d97fcef1146b8c3.tar.gz |
Restoring authorship annotation for Vasily Gerasimov <UgnineSirdis@gmail.com>. Commit 1 of 2.
Diffstat (limited to 'library/cpp/testing')
18 files changed, 748 insertions, 748 deletions
diff --git a/library/cpp/testing/common/env.h b/library/cpp/testing/common/env.h index 7b89aa1bed..c1247ec96a 100644 --- a/library/cpp/testing/common/env.h +++ b/library/cpp/testing/common/env.h @@ -23,16 +23,16 @@ TString BinaryPath(TStringBuf path); // @brief return true if environment is testenv otherwise false bool FromYaTest(); - + // @brief returns TestsData dir (from env:ARCADIA_TESTS_DATA_DIR or path to existing folder `arcadia_tests_data` within parent folders) TString GetArcadiaTestsData(); // @brief return current working dir (from env:TEST_WORK_PATH or cwd) TString GetWorkPath(); - + // @brief return tests output path (workdir + testing_out_stuff) TFsPath GetOutputPath(); - + // @brief return path from env:YA_TEST_RAM_DRIVE_PATH const TString& GetRamDrivePath(); diff --git a/library/cpp/testing/gmock_in_unittest/events.cpp b/library/cpp/testing/gmock_in_unittest/events.cpp index dbd65b727d..2155fb0d81 100644 --- a/library/cpp/testing/gmock_in_unittest/events.cpp +++ b/library/cpp/testing/gmock_in_unittest/events.cpp @@ -1,32 +1,32 @@ -#include "events.h" - +#include "events.h" + #include <library/cpp/testing/unittest/registar.h> - -#include <util/generic/strbuf.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()) { +#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()) + TStringBuilder msg; + if (result.file_name()) msg << result.file_name() << TStringBuf(":"); - if (result.line_number() != -1) + if (result.line_number() != -1) msg << result.line_number() << TStringBuf(":"); - if (summary) { - if (msg) { + if (summary) { + if (msg) { msg << TStringBuf("\n"); - } - msg << summary; - } - if (message && summary != message) { - if (msg) { + } + msg << summary; + } + if (message && summary != message) { + if (msg) { msg << TStringBuf("\n"); - } - msg << message; - } - NUnitTest::NPrivate::RaiseError(result.summary(), msg, result.fatally_failed()); - } -} + } + 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 index 84c10a93de..11af7dd64c 100644 --- a/library/cpp/testing/gmock_in_unittest/events.h +++ b/library/cpp/testing/gmock_in_unittest/events.h @@ -1,8 +1,8 @@ -#pragma once - -#include <gtest/gtest.h> - -class TGMockTestEventListener: public testing::EmptyTestEventListener { -public: - void OnTestPartResult(const testing::TestPartResult& result) override; -}; +#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 index 97f19050e4..a6c12b3e5a 100644 --- a/library/cpp/testing/gmock_in_unittest/example_ut/example_ut.cpp +++ b/library/cpp/testing/gmock_in_unittest/example_ut/example_ut.cpp @@ -1,105 +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; - + +// 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: +}; + +class TTestMock: public ITestIface { +public: MOCK_METHOD(void, Func1, (), (override)); MOCK_METHOD(int, Func2, (const TString&), (const, override)); -}; - -using namespace testing; - +}; + +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(); - } - } - + 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(); - } - } - + TTestMock mock; + EXPECT_CALL(mock, Func1()) + .Times(AtMost(1)); + mock.Func1(); + if (fail) { + mock.Func1(); + } + } + Y_UNIT_TEST(TReturnValuesTest) { - TTestMock mock; + TTestMock mock; EXPECT_CALL(mock, Func2(TString("1"))) - .WillOnce(Return(1)) - .WillRepeatedly(Return(42)); - + .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 - } - } - + .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(); - } - } - + 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(); - } - } -} + 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 index d2e5ee5d2a..81c9ee3fca 100644 --- a/library/cpp/testing/gmock_in_unittest/example_ut/ya.make +++ b/library/cpp/testing/gmock_in_unittest/example_ut/ya.make @@ -1,13 +1,13 @@ -UNITTEST() - -OWNER(galaxycrab) - -PEERDIR( +UNITTEST() + +OWNER(galaxycrab) + +PEERDIR( library/cpp/testing/gmock_in_unittest -) - -SRCS( - example_ut.cpp -) - -END() +) + +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 index 31f6aee1c3..65bfc5a5ed 100644 --- a/library/cpp/testing/gmock_in_unittest/gmock.h +++ b/library/cpp/testing/gmock_in_unittest/gmock.h @@ -1,5 +1,5 @@ -#pragma once +#pragma once #include <library/cpp/testing/gtest_extensions/gtest_extensions.h> - -#include <gmock/gmock.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 index c2872a4c27..f6b9e516d1 100644 --- a/library/cpp/testing/gmock_in_unittest/registration.cpp +++ b/library/cpp/testing/gmock_in_unittest/registration.cpp @@ -1,20 +1,20 @@ -#include "events.h" - -#include <gmock/gmock.h> - +#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()); - + +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 index 5de68ad98d..c8a6e33396 100644 --- a/library/cpp/testing/gmock_in_unittest/ya.make +++ b/library/cpp/testing/gmock_in_unittest/ya.make @@ -1,17 +1,17 @@ -LIBRARY() - -OWNER(galaxycrab) - -PEERDIR( +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() +) + +SRCS( + events.cpp + GLOBAL registration.cpp +) + +END() diff --git a/library/cpp/testing/unittest/plugin.cpp b/library/cpp/testing/unittest/plugin.cpp index 543112f7ac..fd80eb48ac 100644 --- a/library/cpp/testing/unittest/plugin.cpp +++ b/library/cpp/testing/unittest/plugin.cpp @@ -1,50 +1,50 @@ -#include "plugin.h" - -#include <util/generic/singleton.h> -#include <util/generic/vector.h> -#include <util/generic/utility.h> - -namespace NUnitTest { - namespace NPlugin { - namespace { - class TPlugins { - public: - void OnStartMain(int argc, char* argv[]) const { - for (const auto& plugin : Plugins) { - plugin->OnStartMain(argc, argv); - } - } - - void OnStopMain(int argc, char* argv[]) const { - for (const auto& plugin : Plugins) { - plugin->OnStopMain(argc, argv); - } - } - - void Register(TSimpleSharedPtr<IPlugin> plugin) { - Plugins.emplace_back(std::move(plugin)); - } - - static TPlugins& Instance() { - return *Singleton<TPlugins>(); - } - - private: +#include "plugin.h" + +#include <util/generic/singleton.h> +#include <util/generic/vector.h> +#include <util/generic/utility.h> + +namespace NUnitTest { + namespace NPlugin { + namespace { + class TPlugins { + public: + void OnStartMain(int argc, char* argv[]) const { + for (const auto& plugin : Plugins) { + plugin->OnStartMain(argc, argv); + } + } + + void OnStopMain(int argc, char* argv[]) const { + for (const auto& plugin : Plugins) { + plugin->OnStopMain(argc, argv); + } + } + + void Register(TSimpleSharedPtr<IPlugin> plugin) { + Plugins.emplace_back(std::move(plugin)); + } + + static TPlugins& Instance() { + return *Singleton<TPlugins>(); + } + + private: TVector<TSimpleSharedPtr<IPlugin>> Plugins; - }; - } // anonymous namespace - - TPluginRegistrator::TPluginRegistrator(TSimpleSharedPtr<IPlugin> plugin) { - TPlugins::Instance().Register(std::move(plugin)); - } - - void OnStartMain(int argc, char* argv[]) { - TPlugins::Instance().OnStartMain(argc, argv); - } - - void OnStopMain(int argc, char* argv[]) { - TPlugins::Instance().OnStopMain(argc, argv); - } - + }; + } // anonymous namespace + + TPluginRegistrator::TPluginRegistrator(TSimpleSharedPtr<IPlugin> plugin) { + TPlugins::Instance().Register(std::move(plugin)); + } + + void OnStartMain(int argc, char* argv[]) { + TPlugins::Instance().OnStartMain(argc, argv); + } + + void OnStopMain(int argc, char* argv[]) { + TPlugins::Instance().OnStopMain(argc, argv); + } + } } diff --git a/library/cpp/testing/unittest/plugin.h b/library/cpp/testing/unittest/plugin.h index 102f2c1469..7cce973c78 100644 --- a/library/cpp/testing/unittest/plugin.h +++ b/library/cpp/testing/unittest/plugin.h @@ -1,29 +1,29 @@ -#pragma once - -#include <util/generic/ptr.h> - -namespace NUnitTest { +#pragma once + +#include <util/generic/ptr.h> + +namespace NUnitTest { // Plugins are deprecated, please use Y_TEST_HOOK_* from library/cpp/hook/hook.h - namespace NPlugin { - class IPlugin { - public: - virtual ~IPlugin() { - } - - virtual void OnStartMain(int /*argc*/, char* /*argv*/ []) { - } - - virtual void OnStopMain(int /*argc*/, char* /*argv*/ []) { - } - }; - - void OnStartMain(int argc, char* argv[]); - void OnStopMain(int argc, char* argv[]); - - class TPluginRegistrator { - public: - TPluginRegistrator(TSimpleSharedPtr<IPlugin> plugin); - }; - + namespace NPlugin { + class IPlugin { + public: + virtual ~IPlugin() { + } + + virtual void OnStartMain(int /*argc*/, char* /*argv*/ []) { + } + + virtual void OnStopMain(int /*argc*/, char* /*argv*/ []) { + } + }; + + void OnStartMain(int argc, char* argv[]); + void OnStopMain(int argc, char* argv[]); + + class TPluginRegistrator { + public: + TPluginRegistrator(TSimpleSharedPtr<IPlugin> plugin); + }; + } } diff --git a/library/cpp/testing/unittest/registar.cpp b/library/cpp/testing/unittest/registar.cpp index 3679b768ed..fea174a65e 100644 --- a/library/cpp/testing/unittest/registar.cpp +++ b/library/cpp/testing/unittest/registar.cpp @@ -1,13 +1,13 @@ -#include "registar.h" - +#include "registar.h" + #include <library/cpp/diff/diff.h> #include <library/cpp/colorizer/colors.h> -#include <util/generic/bt_exception.h> -#include <util/random/fast.h> +#include <util/generic/bt_exception.h> +#include <util/random/fast.h> #include <util/string/printf.h> -#include <util/system/backtrace.h> -#include <util/system/guard.h> +#include <util/system/backtrace.h> +#include <util/system/guard.h> #include <util/system/tls.h> #include <util/system/error.h> #include <util/string/cast.h> @@ -32,46 +32,46 @@ Y_POD_STATIC_THREAD(bool) UnittestThread; Y_POD_STATIC_THREAD(NUnitTest::TTestBase*) currentTest; -::NUnitTest::TRaiseErrorHandler RaiseErrorHandler; +::NUnitTest::TRaiseErrorHandler RaiseErrorHandler; void ::NUnitTest::NPrivate::RaiseError(const char* what, const TString& msg, bool fatalFailure) { Y_VERIFY(UnittestThread, "%s in non-unittest thread with message:\n%s", what, msg.data()); - Y_VERIFY(GetCurrentTest()); - - if (RaiseErrorHandler) { - RaiseErrorHandler(what, msg, fatalFailure); - return; - } - - // Default handler - TBackTrace bt; - bt.Capture(); + Y_VERIFY(GetCurrentTest()); + + if (RaiseErrorHandler) { + RaiseErrorHandler(what, msg, fatalFailure); + return; + } + + // Default handler + TBackTrace bt; + bt.Capture(); GetCurrentTest()->AddError(msg.data(), bt.PrintToString()); - if (::NUnitTest::ContinueOnFail || !fatalFailure) { - return; - } + if (::NUnitTest::ContinueOnFail || !fatalFailure) { + return; + } throw TAssertException(); } -void ::NUnitTest::SetRaiseErrorHandler(::NUnitTest::TRaiseErrorHandler handler) { - Y_VERIFY(UnittestThread); - RaiseErrorHandler = std::move(handler); -} - +void ::NUnitTest::SetRaiseErrorHandler(::NUnitTest::TRaiseErrorHandler handler) { + Y_VERIFY(UnittestThread); + RaiseErrorHandler = std::move(handler); +} + void ::NUnitTest::NPrivate::SetUnittestThread(bool unittestThread) { Y_VERIFY(UnittestThread != unittestThread, "state check"); UnittestThread = unittestThread; } -void ::NUnitTest::NPrivate::SetCurrentTest(TTestBase* test) { - Y_VERIFY(!test || !currentTest, "state check"); - currentTest = test; -} - -NUnitTest::TTestBase* ::NUnitTest::NPrivate::GetCurrentTest() { - return currentTest; -} - +void ::NUnitTest::NPrivate::SetCurrentTest(TTestBase* test) { + Y_VERIFY(!test || !currentTest, "state check"); + currentTest = test; +} + +NUnitTest::TTestBase* ::NUnitTest::NPrivate::GetCurrentTest() { + return currentTest; +} + struct TDiffColorizer { NColorizer::TColors Colors; bool Reverse = false; @@ -321,12 +321,12 @@ void NUnitTest::TTestBase::AddError(const char* msg, TTestContext* context) { AddError(msg, TString(), context); } -void NUnitTest::TTestBase::RunAfterTest(std::function<void()> f) { - with_lock (AfterTestFunctionsLock_) { - AfterTestFunctions_.emplace_back(std::move(f)); - } -} - +void NUnitTest::TTestBase::RunAfterTest(std::function<void()> f) { + with_lock (AfterTestFunctionsLock_) { + AfterTestFunctions_.emplace_back(std::move(f)); + } +} + bool NUnitTest::TTestBase::CheckAccessTest(const char* test) { return Processor()->CheckAccessTest(Name(), test); } @@ -376,18 +376,18 @@ void NUnitTest::TTestBase::BeforeTest() { void NUnitTest::TTestBase::AfterTest() { TearDown(); - - TVector<std::function<void()>> afterTestFunctions; - with_lock (AfterTestFunctionsLock_) { - afterTestFunctions.swap(AfterTestFunctions_); - } - - for (auto i = afterTestFunctions.rbegin(); i != afterTestFunctions.rend(); ++i) { - std::function<void()>& f = *i; - if (f) { - f(); - } - } + + TVector<std::function<void()>> afterTestFunctions; + with_lock (AfterTestFunctionsLock_) { + afterTestFunctions.swap(AfterTestFunctions_); + } + + for (auto i = afterTestFunctions.rbegin(); i != afterTestFunctions.rend(); ++i) { + std::function<void()>& f = *i; + if (f) { + f(); + } + } } bool NUnitTest::TTestBase::GetIsForked() const { diff --git a/library/cpp/testing/unittest/registar.h b/library/cpp/testing/unittest/registar.h index 44517a0092..be38300e1e 100644 --- a/library/cpp/testing/unittest/registar.h +++ b/library/cpp/testing/unittest/registar.h @@ -2,41 +2,41 @@ #include <library/cpp/dbg_output/dump.h> -#include <util/generic/bt_exception.h> +#include <util/generic/bt_exception.h> #include <util/generic/hash.h> -#include <util/generic/intrlist.h> +#include <util/generic/intrlist.h> #include <util/generic/map.h> #include <util/generic/ptr.h> #include <util/generic/set.h> #include <util/generic/typetraits.h> #include <util/generic/vector.h> -#include <util/generic/yexception.h> +#include <util/generic/yexception.h> #include <util/string/builder.h> #include <util/string/cast.h> #include <util/string/printf.h> -#include <util/system/defaults.h> +#include <util/system/defaults.h> #include <util/system/type_name.h> -#include <util/system/spinlock.h> -#include <util/system/src_location.h> - +#include <util/system/spinlock.h> +#include <util/system/src_location.h> + #include <util/system/rusage.h> #include <cmath> #include <cstdio> -#include <functional> - +#include <functional> + extern bool CheckExceptionMessage(const char*, TString&); namespace NUnitTest { class TTestBase; - + namespace NPrivate { void RaiseError(const char* what, const TString& msg, bool fatalFailure); void SetUnittestThread(bool); - void SetCurrentTest(TTestBase*); - TTestBase* GetCurrentTest(); + void SetCurrentTest(TTestBase*); + TTestBase* GetCurrentTest(); } extern bool ShouldColorizeDiff; @@ -45,21 +45,21 @@ namespace NUnitTest { TString GetFormatTag(const char* name); TString GetResetTag(); - // Raise error handler + // Raise error handler // Used for testing library/cpp/testing/unittest macroses - // and unittest helpers. - // For all other unittests standard handler is used - using TRaiseErrorHandler = std::function<void(const char*, const TString&, bool)>; - - void SetRaiseErrorHandler(TRaiseErrorHandler handler); - - inline void ClearRaiseErrorHandler() { - SetRaiseErrorHandler(TRaiseErrorHandler()); - } - - class TAssertException: public yexception { - }; - + // and unittest helpers. + // For all other unittests standard handler is used + using TRaiseErrorHandler = std::function<void(const char*, const TString&, bool)>; + + void SetRaiseErrorHandler(TRaiseErrorHandler handler); + + inline void ClearRaiseErrorHandler() { + SetRaiseErrorHandler(TRaiseErrorHandler()); + } + + class TAssertException: public yexception { + }; + class ITestSuiteProcessor; struct TTestContext { @@ -97,10 +97,10 @@ namespace NUnitTest { TTestContext* Context; }; - struct TFinish { + struct TFinish { const TTest* test; TTestContext* Context; - bool Success; + bool Success; }; ITestSuiteProcessor(); @@ -159,9 +159,9 @@ namespace NUnitTest { virtual void OnBeforeTest(const TTest* /*test*/); void AddTestError(const TTest& test); - + void AddTestFinish(const TTest& test); - + private: TMap<TString, size_t> TestErrors_; TMap<TString, size_t> CurTestErrors_; @@ -203,11 +203,11 @@ namespace NUnitTest { virtual void TearDown(); void AddError(const char* msg, const TString& backtrace = TString(), TTestContext* context = nullptr); - + void AddError(const char* msg, TTestContext* context); - - void RunAfterTest(std::function<void()> f); // function like atexit to run after current unit test - + + void RunAfterTest(std::function<void()> f); // function like atexit to run after current unit test + protected: bool CheckAccessTest(const char* test); @@ -243,10 +243,10 @@ namespace NUnitTest { private: TTestFactory* Parent_; - size_t TestErrors_; - const char* CurrentSubtest_; - TAdaptiveLock AfterTestFunctionsLock_; - TVector<std::function<void()>> AfterTestFunctions_; + size_t TestErrors_; + const char* CurrentSubtest_; + TAdaptiveLock AfterTestFunctionsLock_; + TVector<std::function<void()>> AfterTestFunctions_; }; #define UNIT_TEST_SUITE(N) \ @@ -304,8 +304,8 @@ private: \ thiz->F(); \ } \ }; \ - this->TTestBase::Run(std::bind(&T##F##Caller::X, this, context), StaticName(), (#F), FF); \ - } + this->TTestBase::Run(std::bind(&T##F##Caller::X, this, context), StaticName(), (#F), FF); \ + } #define UNIT_TEST_IMPL(F, FF) \ UNIT_TEST_CHECK_TEST_IS_DECLARED_ONLY_ONCE(F) { \ @@ -336,20 +336,20 @@ private: \ /* forked process (or main without "--fork-tests") treats some exceptions as success - it's exception test! */ \ } else { \ NUnitTest::TTestContext context(this->TTestBase::Processor()); \ - if (this->CheckAccessTest((#F))) { \ - try { \ - UNIT_TEST_RUN(F, false, context) \ - this->AddError("exception expected", &context); \ - } catch (const ::NUnitTest::TAssertException&) { \ - } catch (const E& e) { \ + if (this->CheckAccessTest((#F))) { \ + try { \ + UNIT_TEST_RUN(F, false, context) \ + this->AddError("exception expected", &context); \ + } catch (const ::NUnitTest::TAssertException&) { \ + } catch (const E& e) { \ TString err; \ - if (!CheckExceptionMessage(e.what(), err)) \ - this->AddError(err.c_str(), &context); \ - } catch (const std::exception& e) { \ - this->AddError(e.what(), &context); \ - } catch (...) { \ - this->AddError("non-std exception!", &context); \ - } \ + if (!CheckExceptionMessage(e.what(), err)) \ + this->AddError(err.c_str(), &context); \ + } catch (const std::exception& e) { \ + this->AddError(e.what(), &context); \ + } catch (...) { \ + this->AddError("non-std exception!", &context); \ + } \ this->Finish((#F), &context); \ } \ } @@ -366,13 +366,13 @@ public: \ ::NUnitTest::NPrivate::RaiseError(R, ::TStringBuilder() << R << " at " << __LOCATION__ << ", " << __PRETTY_FUNCTION__ << ": " << M, true); \ } while (false) -#define UNIT_FAIL_NONFATAL_IMPL(R, M) \ - do { \ +#define UNIT_FAIL_NONFATAL_IMPL(R, M) \ + do { \ ::NUnitTest::NPrivate::RaiseError(R, ::TStringBuilder() << R << " at " << __LOCATION__ << ", " << __PRETTY_FUNCTION__ << ": " << M, false); \ - } while (false) - + } while (false) + #define UNIT_FAIL(M) UNIT_FAIL_IMPL("forced failure", M) -#define UNIT_FAIL_NONFATAL(M) UNIT_FAIL_NONFATAL_IMPL("forced failure", M) +#define UNIT_FAIL_NONFATAL(M) UNIT_FAIL_NONFATAL_IMPL("forced failure", M) //types #define UNIT_ASSERT_TYPES_EQUAL(A, B) \ @@ -536,20 +536,20 @@ public: \ #define UNIT_ASSERT_GE(A, B) UNIT_ASSERT_GE_C(A, B, "") -#define UNIT_CHECK_GENERATED_EXCEPTION_C(A, E, C) \ +#define UNIT_CHECK_GENERATED_EXCEPTION_C(A, E, C) \ do { \ - try { \ - (void)(A); \ - } catch (const ::NUnitTest::TAssertException&) { \ - throw; \ - } catch (const E&) { \ - break; \ - } \ - UNIT_ASSERT_C(0, "Exception hasn't been thrown, but it should have happened " << C); \ + try { \ + (void)(A); \ + } catch (const ::NUnitTest::TAssertException&) { \ + throw; \ + } catch (const E&) { \ + break; \ + } \ + UNIT_ASSERT_C(0, "Exception hasn't been thrown, but it should have happened " << C); \ } while (false) -#define UNIT_CHECK_GENERATED_EXCEPTION(A, E) UNIT_CHECK_GENERATED_EXCEPTION_C(A, E, "") - +#define UNIT_CHECK_GENERATED_EXCEPTION(A, E) UNIT_CHECK_GENERATED_EXCEPTION_C(A, E, "") + #define UNIT_CHECK_GENERATED_NO_EXCEPTION_C(A, E, C) \ do { \ try { \ @@ -562,7 +562,7 @@ public: \ } while (false) #define UNIT_CHECK_GENERATED_NO_EXCEPTION(A, E) UNIT_CHECK_GENERATED_NO_EXCEPTION_C(A, E, "and exception message is:\n" << CurrentExceptionMessage()) - + // Same as UNIT_ASSERT_EXCEPTION_SATISFIES but prints additional string C when nothing was thrown #define UNIT_ASSERT_EXCEPTION_SATISFIES_C(A, E, pred, C) \ do { \ @@ -588,7 +588,7 @@ public: \ #A << " did not throw any exception" \ << " (expected " << #E << ") " << C); \ } \ - } while (false) + } while (false) // Assert that a specific exception is thrown and satisfies predicate pred(e), where e is the exception instance. // Example: @@ -618,15 +618,15 @@ public: \ } while (false) // Assert that a specific exception is thrown and CurrentExceptionMessage() contains substr -#define UNIT_ASSERT_EXCEPTION_CONTAINS(A, E, substr) \ - UNIT_ASSERT_EXCEPTION_CONTAINS_C(A, E, substr, "") - +#define UNIT_ASSERT_EXCEPTION_CONTAINS(A, E, substr) \ + UNIT_ASSERT_EXCEPTION_CONTAINS_C(A, E, substr, "") + // Same as UNIT_ASSERT_EXCEPTION but prints additional string C when nothing was thrown #define UNIT_ASSERT_EXCEPTION_C(A, E, C) UNIT_ASSERT_EXCEPTION_SATISFIES_C(A, E, [](const E&){ return true; }, C) - + // Assert that a specific exception is thrown -#define UNIT_ASSERT_EXCEPTION(A, E) UNIT_ASSERT_EXCEPTION_C(A, E, "") - +#define UNIT_ASSERT_EXCEPTION(A, E) UNIT_ASSERT_EXCEPTION_C(A, E, "") + #define UNIT_ASSERT_NO_EXCEPTION_RESULT_C(A, C) \ [&] () mutable -> decltype(A) { \ static_assert(!std::is_void_v<decltype(A)>); \ @@ -653,10 +653,10 @@ public: \ } catch (...) { \ UNIT_FAIL_IMPL("exception-free assertion failed", Sprintf("%s throws %s\nException message: %s", #A, (::TStringBuilder() << C).data(), CurrentExceptionMessage().data())); \ } \ - } while (false) - -#define UNIT_ASSERT_NO_EXCEPTION(A) UNIT_ASSERT_NO_EXCEPTION_C(A, "") + } while (false) +#define UNIT_ASSERT_NO_EXCEPTION(A) UNIT_ASSERT_NO_EXCEPTION_C(A, "") + namespace NPrivate { template <class T, class U, bool Integers> struct TCompareValuesImpl { @@ -719,7 +719,7 @@ public: \ } \ UNIT_FAIL_IMPL("assertion failed", failMsg); \ } \ - } while (false) + } while (false) #define UNIT_ASSERT_VALUES_EQUAL_C(A, B, C) \ UNIT_ASSERT_VALUES_EQUAL_IMPL(A, B, C, true, "==", "!=") @@ -730,28 +730,28 @@ public: \ #define UNIT_ASSERT_VALUES_EQUAL(A, B) UNIT_ASSERT_VALUES_EQUAL_C(A, B, "") #define UNIT_ASSERT_VALUES_UNEQUAL(A, B) UNIT_ASSERT_VALUES_UNEQUAL_C(A, B, "") -// Checks that test will fail while executing given expression -// Macro for using in unitests for ut helpers -#define UNIT_ASSERT_TEST_FAILS_C(A, C) \ - do { \ - ::NUnitTest::TUnitTestFailChecker checker; \ - try { \ - auto guard = checker.InvokeGuard(); \ - (void)(A); \ - } catch (...) { \ - UNIT_FAIL_IMPL("fail test assertion failure", \ - "code is expected to generate test failure, " \ - "but it throws exception with message: " \ +// Checks that test will fail while executing given expression +// Macro for using in unitests for ut helpers +#define UNIT_ASSERT_TEST_FAILS_C(A, C) \ + do { \ + ::NUnitTest::TUnitTestFailChecker checker; \ + try { \ + auto guard = checker.InvokeGuard(); \ + (void)(A); \ + } catch (...) { \ + UNIT_FAIL_IMPL("fail test assertion failure", \ + "code is expected to generate test failure, " \ + "but it throws exception with message: " \ << CurrentExceptionMessage()); \ - } \ - if (!checker.Failed()) { \ - UNIT_FAIL_IMPL("fail test assertion failure", \ - "code is expected to generate test failure"); \ - } \ - } while (false) - -#define UNIT_ASSERT_TEST_FAILS(A) UNIT_ASSERT_TEST_FAILS_C(A, "") - + } \ + if (!checker.Failed()) { \ + UNIT_FAIL_IMPL("fail test assertion failure", \ + "code is expected to generate test failure"); \ + } \ + } while (false) + +#define UNIT_ASSERT_TEST_FAILS(A) UNIT_ASSERT_TEST_FAILS_C(A, "") + #define UNIT_ADD_METRIC(name, value) ut_context.Metrics[name] = value class TTestFactory { @@ -840,77 +840,77 @@ public: \ using TBaseFixture = TBaseTestCase; - // Class for checking that code raises unittest failure - class TUnitTestFailChecker { - public: - struct TInvokeGuard { - explicit TInvokeGuard(TUnitTestFailChecker& parent) - : Parent(&parent) - { - Parent->SetHandler(); - } - + // Class for checking that code raises unittest failure + class TUnitTestFailChecker { + public: + struct TInvokeGuard { + explicit TInvokeGuard(TUnitTestFailChecker& parent) + : Parent(&parent) + { + Parent->SetHandler(); + } + TInvokeGuard(TInvokeGuard&& guard) noexcept - : Parent(guard.Parent) - { - guard.Parent = nullptr; - } - - ~TInvokeGuard() { - if (Parent) { - ClearRaiseErrorHandler(); - } - } - - TUnitTestFailChecker* Parent; - }; - - TUnitTestFailChecker() = default; - TUnitTestFailChecker(const TUnitTestFailChecker&) = delete; - TUnitTestFailChecker(TUnitTestFailChecker&&) = delete; - - TInvokeGuard InvokeGuard() { - return TInvokeGuard(*this); - } - - const TString& What() const { - return What_; - } - - const TString& Msg() const { - return Msg_; - } - - bool FatalFailure() const { - return FatalFailure_; - } - - bool Failed() const { - return Failed_; - } - - private: - void Handler(const char* what, const TString& msg, bool fatalFailure) { - What_ = what; - Msg_ = msg; - FatalFailure_ = fatalFailure; - Failed_ = true; - } - - void SetHandler() { - TRaiseErrorHandler handler = [this](const char* what, const TString& msg, bool fatalFailure) { - Handler(what, msg, fatalFailure); - }; - SetRaiseErrorHandler(std::move(handler)); - } - - private: - TString What_; - TString Msg_; - bool FatalFailure_ = false; - bool Failed_ = false; - }; - + : Parent(guard.Parent) + { + guard.Parent = nullptr; + } + + ~TInvokeGuard() { + if (Parent) { + ClearRaiseErrorHandler(); + } + } + + TUnitTestFailChecker* Parent; + }; + + TUnitTestFailChecker() = default; + TUnitTestFailChecker(const TUnitTestFailChecker&) = delete; + TUnitTestFailChecker(TUnitTestFailChecker&&) = delete; + + TInvokeGuard InvokeGuard() { + return TInvokeGuard(*this); + } + + const TString& What() const { + return What_; + } + + const TString& Msg() const { + return Msg_; + } + + bool FatalFailure() const { + return FatalFailure_; + } + + bool Failed() const { + return Failed_; + } + + private: + void Handler(const char* what, const TString& msg, bool fatalFailure) { + What_ = what; + Msg_ = msg; + FatalFailure_ = fatalFailure; + Failed_ = true; + } + + void SetHandler() { + TRaiseErrorHandler handler = [this](const char* what, const TString& msg, bool fatalFailure) { + Handler(what, msg, fatalFailure); + }; + SetRaiseErrorHandler(std::move(handler)); + } + + private: + TString What_; + TString Msg_; + bool FatalFailure_ = false; + bool Failed_ = false; + }; + #define UNIT_TEST_SUITE_REGISTRATION(T) \ static const ::NUnitTest::TTestBaseFactory<T> Y_GENERATE_UNIQUE_ID(UTREG_); diff --git a/library/cpp/testing/unittest/registar_ut.cpp b/library/cpp/testing/unittest/registar_ut.cpp index 1f36d53abb..9442eb3d0a 100644 --- a/library/cpp/testing/unittest/registar_ut.cpp +++ b/library/cpp/testing/unittest/registar_ut.cpp @@ -1,132 +1,132 @@ #include <library/cpp/testing/unittest/registar.h> - + Y_UNIT_TEST_SUITE(TUnitTestMacroTest) { Y_UNIT_TEST(Assert) { - auto unitAssert = [] { - UNIT_ASSERT(false); - }; - UNIT_ASSERT_TEST_FAILS(unitAssert()); - - UNIT_ASSERT(true); - } - + auto unitAssert = [] { + UNIT_ASSERT(false); + }; + UNIT_ASSERT_TEST_FAILS(unitAssert()); + + UNIT_ASSERT(true); + } + Y_UNIT_TEST(TypesEqual) { - auto typesEqual = [] { - UNIT_ASSERT_TYPES_EQUAL(int, long); - }; - UNIT_ASSERT_TEST_FAILS(typesEqual()); - - UNIT_ASSERT_TYPES_EQUAL(TString, TString); - } - + auto typesEqual = [] { + UNIT_ASSERT_TYPES_EQUAL(int, long); + }; + UNIT_ASSERT_TEST_FAILS(typesEqual()); + + UNIT_ASSERT_TYPES_EQUAL(TString, TString); + } + Y_UNIT_TEST(DoublesEqual) { - auto doublesEqual = [](double d1, double d2, double precision) { - UNIT_ASSERT_DOUBLES_EQUAL(d1, d2, precision); - }; - UNIT_ASSERT_TEST_FAILS(doublesEqual(0.0, 0.5, 0.1)); - UNIT_ASSERT_TEST_FAILS(doublesEqual(0.1, -0.1, 0.1)); - - UNIT_ASSERT_DOUBLES_EQUAL(0.0, 0.01, 0.1); - UNIT_ASSERT_DOUBLES_EQUAL(0.01, 0.0, 0.1); + auto doublesEqual = [](double d1, double d2, double precision) { + UNIT_ASSERT_DOUBLES_EQUAL(d1, d2, precision); + }; + UNIT_ASSERT_TEST_FAILS(doublesEqual(0.0, 0.5, 0.1)); + UNIT_ASSERT_TEST_FAILS(doublesEqual(0.1, -0.1, 0.1)); + + UNIT_ASSERT_DOUBLES_EQUAL(0.0, 0.01, 0.1); + UNIT_ASSERT_DOUBLES_EQUAL(0.01, 0.0, 0.1); constexpr auto nan = std::numeric_limits<double>::quiet_NaN(); UNIT_ASSERT_TEST_FAILS(doublesEqual(nan, 0.5, 0.1)); UNIT_ASSERT_TEST_FAILS(doublesEqual(0.5, nan, 0.1)); UNIT_ASSERT_DOUBLES_EQUAL(nan, nan, 0.1); - } - + } + Y_UNIT_TEST(StringsEqual) { - auto stringsEqual = [](auto s1, auto s2) { - UNIT_ASSERT_STRINGS_EQUAL(s1, s2); - }; - UNIT_ASSERT_TEST_FAILS(stringsEqual("q", "w")); - UNIT_ASSERT_TEST_FAILS(stringsEqual("q", TString("w"))); - UNIT_ASSERT_TEST_FAILS(stringsEqual(TString("q"), "w")); - UNIT_ASSERT_TEST_FAILS(stringsEqual(TString("a"), TString("b"))); + auto stringsEqual = [](auto s1, auto s2) { + UNIT_ASSERT_STRINGS_EQUAL(s1, s2); + }; + UNIT_ASSERT_TEST_FAILS(stringsEqual("q", "w")); + UNIT_ASSERT_TEST_FAILS(stringsEqual("q", TString("w"))); + UNIT_ASSERT_TEST_FAILS(stringsEqual(TString("q"), "w")); + UNIT_ASSERT_TEST_FAILS(stringsEqual(TString("a"), TString("b"))); UNIT_ASSERT_TEST_FAILS(stringsEqual(TString("a"), TStringBuf("b"))); UNIT_ASSERT_TEST_FAILS(stringsEqual("a", TStringBuf("b"))); UNIT_ASSERT_TEST_FAILS(stringsEqual(TStringBuf("a"), "b")); - - TString empty; - TStringBuf emptyBuf; - UNIT_ASSERT_STRINGS_EQUAL("", empty); - UNIT_ASSERT_STRINGS_EQUAL(empty, emptyBuf); - UNIT_ASSERT_STRINGS_EQUAL("", static_cast<const char*>(nullptr)); - } - + + TString empty; + TStringBuf emptyBuf; + UNIT_ASSERT_STRINGS_EQUAL("", empty); + UNIT_ASSERT_STRINGS_EQUAL(empty, emptyBuf); + UNIT_ASSERT_STRINGS_EQUAL("", static_cast<const char*>(nullptr)); + } + Y_UNIT_TEST(StringContains) { - auto stringContains = [](auto s, auto substr) { - UNIT_ASSERT_STRING_CONTAINS(s, substr); - }; - UNIT_ASSERT_TEST_FAILS(stringContains("", "a")); - UNIT_ASSERT_TEST_FAILS(stringContains("lurkmore", "moar")); - - UNIT_ASSERT_STRING_CONTAINS("", ""); - UNIT_ASSERT_STRING_CONTAINS("a", ""); - UNIT_ASSERT_STRING_CONTAINS("failure", "fail"); - UNIT_ASSERT_STRING_CONTAINS("lurkmore", "more"); - } - + auto stringContains = [](auto s, auto substr) { + UNIT_ASSERT_STRING_CONTAINS(s, substr); + }; + UNIT_ASSERT_TEST_FAILS(stringContains("", "a")); + UNIT_ASSERT_TEST_FAILS(stringContains("lurkmore", "moar")); + + UNIT_ASSERT_STRING_CONTAINS("", ""); + UNIT_ASSERT_STRING_CONTAINS("a", ""); + UNIT_ASSERT_STRING_CONTAINS("failure", "fail"); + UNIT_ASSERT_STRING_CONTAINS("lurkmore", "more"); + } + Y_UNIT_TEST(NoDiff) { - auto noDiff = [](auto s1, auto s2) { - UNIT_ASSERT_NO_DIFF(s1, s2); - }; - UNIT_ASSERT_TEST_FAILS(noDiff("q", "w")); - UNIT_ASSERT_TEST_FAILS(noDiff("q", "")); - - UNIT_ASSERT_NO_DIFF("", ""); - UNIT_ASSERT_NO_DIFF("a", "a"); - } - + auto noDiff = [](auto s1, auto s2) { + UNIT_ASSERT_NO_DIFF(s1, s2); + }; + UNIT_ASSERT_TEST_FAILS(noDiff("q", "w")); + UNIT_ASSERT_TEST_FAILS(noDiff("q", "")); + + UNIT_ASSERT_NO_DIFF("", ""); + UNIT_ASSERT_NO_DIFF("a", "a"); + } + Y_UNIT_TEST(StringsUnequal) { - auto stringsUnequal = [](auto s1, auto s2) { - UNIT_ASSERT_STRINGS_UNEQUAL(s1, s2); - }; - UNIT_ASSERT_TEST_FAILS(stringsUnequal("1", "1")); - UNIT_ASSERT_TEST_FAILS(stringsUnequal("", "")); - UNIT_ASSERT_TEST_FAILS(stringsUnequal("42", TString("42"))); - UNIT_ASSERT_TEST_FAILS(stringsUnequal(TString("4"), "4")); + auto stringsUnequal = [](auto s1, auto s2) { + UNIT_ASSERT_STRINGS_UNEQUAL(s1, s2); + }; + UNIT_ASSERT_TEST_FAILS(stringsUnequal("1", "1")); + UNIT_ASSERT_TEST_FAILS(stringsUnequal("", "")); + UNIT_ASSERT_TEST_FAILS(stringsUnequal("42", TString("42"))); + UNIT_ASSERT_TEST_FAILS(stringsUnequal(TString("4"), "4")); UNIT_ASSERT_TEST_FAILS(stringsUnequal("d", TStringBuf("d"))); UNIT_ASSERT_TEST_FAILS(stringsUnequal(TStringBuf("yandex"), "yandex")); UNIT_ASSERT_TEST_FAILS(stringsUnequal(TStringBuf("index"), TString("index"))); UNIT_ASSERT_TEST_FAILS(stringsUnequal(TString("diff"), TStringBuf("diff"))); - - UNIT_ASSERT_STRINGS_UNEQUAL("1", "2"); - UNIT_ASSERT_STRINGS_UNEQUAL("", "3"); + + UNIT_ASSERT_STRINGS_UNEQUAL("1", "2"); + UNIT_ASSERT_STRINGS_UNEQUAL("", "3"); UNIT_ASSERT_STRINGS_UNEQUAL("green", TStringBuf("red")); UNIT_ASSERT_STRINGS_UNEQUAL(TStringBuf("solomon"), "golovan"); - UNIT_ASSERT_STRINGS_UNEQUAL("d", TString("f")); - UNIT_ASSERT_STRINGS_UNEQUAL(TString("yandex"), "index"); + UNIT_ASSERT_STRINGS_UNEQUAL("d", TString("f")); + UNIT_ASSERT_STRINGS_UNEQUAL(TString("yandex"), "index"); UNIT_ASSERT_STRINGS_UNEQUAL(TString("mail"), TStringBuf("yandex")); UNIT_ASSERT_STRINGS_UNEQUAL(TStringBuf("C++"), TString("python")); - } - + } + Y_UNIT_TEST(Equal) { - auto equal = [](auto v1, auto v2) { - UNIT_ASSERT_EQUAL(v1, v2); - }; - UNIT_ASSERT_TEST_FAILS(equal("1", TString("2"))); - UNIT_ASSERT_TEST_FAILS(equal(1, 2)); - UNIT_ASSERT_TEST_FAILS(equal(42ul, static_cast<unsigned short>(24))); - - UNIT_ASSERT_EQUAL("abc", TString("abc")); - UNIT_ASSERT_EQUAL(12l, 12); - UNIT_ASSERT_EQUAL(55, 55); - } - + auto equal = [](auto v1, auto v2) { + UNIT_ASSERT_EQUAL(v1, v2); + }; + UNIT_ASSERT_TEST_FAILS(equal("1", TString("2"))); + UNIT_ASSERT_TEST_FAILS(equal(1, 2)); + UNIT_ASSERT_TEST_FAILS(equal(42ul, static_cast<unsigned short>(24))); + + UNIT_ASSERT_EQUAL("abc", TString("abc")); + UNIT_ASSERT_EQUAL(12l, 12); + UNIT_ASSERT_EQUAL(55, 55); + } + Y_UNIT_TEST(Unequal) { - auto unequal = [](auto v1, auto v2) { - UNIT_ASSERT_UNEQUAL(v1, v2); - }; - UNIT_ASSERT_TEST_FAILS(unequal("x", TString("x"))); - UNIT_ASSERT_TEST_FAILS(unequal(1, 1)); - UNIT_ASSERT_TEST_FAILS(unequal(static_cast<unsigned short>(42), 42ul)); - - UNIT_ASSERT_UNEQUAL("abc", TString("cba")); - UNIT_ASSERT_UNEQUAL(12l, 10); - UNIT_ASSERT_UNEQUAL(33, 50); - } - + auto unequal = [](auto v1, auto v2) { + UNIT_ASSERT_UNEQUAL(v1, v2); + }; + UNIT_ASSERT_TEST_FAILS(unequal("x", TString("x"))); + UNIT_ASSERT_TEST_FAILS(unequal(1, 1)); + UNIT_ASSERT_TEST_FAILS(unequal(static_cast<unsigned short>(42), 42ul)); + + UNIT_ASSERT_UNEQUAL("abc", TString("cba")); + UNIT_ASSERT_UNEQUAL(12l, 10); + UNIT_ASSERT_UNEQUAL(33, 50); + } + Y_UNIT_TEST(LessThan) { auto lt = [](auto v1, auto v2) { UNIT_ASSERT_LT(v1, v2); @@ -236,45 +236,45 @@ Y_UNIT_TEST_SUITE(TUnitTestMacroTest) { } Y_UNIT_TEST(ValuesEqual) { - auto valuesEqual = [](auto v1, auto v2) { - UNIT_ASSERT_VALUES_EQUAL(v1, v2); - }; - UNIT_ASSERT_TEST_FAILS(valuesEqual(1, 2)); - UNIT_ASSERT_TEST_FAILS(valuesEqual(1l, static_cast<short>(2))); - - UNIT_ASSERT_VALUES_EQUAL("yandex", TString("yandex")); - UNIT_ASSERT_VALUES_EQUAL(1.0, 1.0); - } - + auto valuesEqual = [](auto v1, auto v2) { + UNIT_ASSERT_VALUES_EQUAL(v1, v2); + }; + UNIT_ASSERT_TEST_FAILS(valuesEqual(1, 2)); + UNIT_ASSERT_TEST_FAILS(valuesEqual(1l, static_cast<short>(2))); + + UNIT_ASSERT_VALUES_EQUAL("yandex", TString("yandex")); + UNIT_ASSERT_VALUES_EQUAL(1.0, 1.0); + } + Y_UNIT_TEST(ValuesUnequal) { - auto valuesUnequal = [](auto v1, auto v2) { - UNIT_ASSERT_VALUES_UNEQUAL(v1, v2); - }; - UNIT_ASSERT_TEST_FAILS(valuesUnequal(5, 5)); - UNIT_ASSERT_TEST_FAILS(valuesUnequal(static_cast<char>(5), 5l)); - TString test("test"); + auto valuesUnequal = [](auto v1, auto v2) { + UNIT_ASSERT_VALUES_UNEQUAL(v1, v2); + }; + UNIT_ASSERT_TEST_FAILS(valuesUnequal(5, 5)); + UNIT_ASSERT_TEST_FAILS(valuesUnequal(static_cast<char>(5), 5l)); + TString test("test"); UNIT_ASSERT_TEST_FAILS(valuesUnequal("test", test.data())); - - UNIT_ASSERT_VALUES_UNEQUAL("UNIT_ASSERT_VALUES_UNEQUAL", "UNIT_ASSERT_VALUES_EQUAL"); - UNIT_ASSERT_VALUES_UNEQUAL(1.0, 1.1); - } - - class TTestException: public yexception { - public: - TTestException(const TString& text = "test exception", bool throwMe = true) - : ThrowMe(throwMe) - { - *this << text; - } - - virtual ~TTestException() = default; - - virtual void Throw() { - if (ThrowMe) { - throw *this; - } - } - + + UNIT_ASSERT_VALUES_UNEQUAL("UNIT_ASSERT_VALUES_UNEQUAL", "UNIT_ASSERT_VALUES_EQUAL"); + UNIT_ASSERT_VALUES_UNEQUAL(1.0, 1.1); + } + + class TTestException: public yexception { + public: + TTestException(const TString& text = "test exception", bool throwMe = true) + : ThrowMe(throwMe) + { + *this << text; + } + + virtual ~TTestException() = default; + + virtual void Throw() { + if (ThrowMe) { + throw *this; + } + } + std::string ThrowStr() { if (ThrowMe) { throw *this; @@ -283,24 +283,24 @@ Y_UNIT_TEST_SUITE(TUnitTestMacroTest) { return {}; } - void AssertNoException() { - UNIT_ASSERT_NO_EXCEPTION(Throw()); - } - + void AssertNoException() { + UNIT_ASSERT_NO_EXCEPTION(Throw()); + } + void AssertNoExceptionRet() { const TString res = UNIT_ASSERT_NO_EXCEPTION_RESULT(ThrowStr()); } - template <class TExpectedException> - void AssertException() { - UNIT_ASSERT_EXCEPTION(Throw(), TExpectedException); - } - - template <class TExpectedException, class T> - void AssertExceptionContains(const T& substr) { - UNIT_ASSERT_EXCEPTION_CONTAINS(Throw(), TExpectedException, substr); - } - + template <class TExpectedException> + void AssertException() { + UNIT_ASSERT_EXCEPTION(Throw(), TExpectedException); + } + + template <class TExpectedException, class T> + void AssertExceptionContains(const T& substr) { + UNIT_ASSERT_EXCEPTION_CONTAINS(Throw(), TExpectedException, substr); + } + template <class TExpectedException, class P> void AssertExceptionSatisfies(const P& predicate) { UNIT_ASSERT_EXCEPTION_SATISFIES(Throw(), TExpectedException, predicate); @@ -310,58 +310,58 @@ Y_UNIT_TEST_SUITE(TUnitTestMacroTest) { return 5; // just some value for predicate testing } - bool ThrowMe; - }; - - class TOtherTestException: public TTestException { - public: - using TTestException::TTestException; - - // Throws other type of exception - void Throw() override { - if (ThrowMe) { - throw *this; - } - } - }; - + bool ThrowMe; + }; + + class TOtherTestException: public TTestException { + public: + using TTestException::TTestException; + + // Throws other type of exception + void Throw() override { + if (ThrowMe) { + throw *this; + } + } + }; + Y_UNIT_TEST(Exception) { - UNIT_ASSERT_TEST_FAILS(TTestException("", false).AssertException<TTestException>()); - UNIT_ASSERT_TEST_FAILS(TTestException().AssertException<TOtherTestException>()); - - UNIT_ASSERT_EXCEPTION(TOtherTestException().Throw(), TTestException); - UNIT_ASSERT_EXCEPTION(TTestException().Throw(), TTestException); - } - + UNIT_ASSERT_TEST_FAILS(TTestException("", false).AssertException<TTestException>()); + UNIT_ASSERT_TEST_FAILS(TTestException().AssertException<TOtherTestException>()); + + UNIT_ASSERT_EXCEPTION(TOtherTestException().Throw(), TTestException); + UNIT_ASSERT_EXCEPTION(TTestException().Throw(), TTestException); + } + Y_UNIT_TEST(ExceptionAssertionContainsOtherExceptionMessage) { - NUnitTest::TUnitTestFailChecker checker; - { - auto guard = checker.InvokeGuard(); - TTestException("custom exception message").AssertException<TOtherTestException>(); - } - UNIT_ASSERT(checker.Failed()); - UNIT_ASSERT_STRING_CONTAINS(checker.Msg(), "custom exception message"); - } - + NUnitTest::TUnitTestFailChecker checker; + { + auto guard = checker.InvokeGuard(); + TTestException("custom exception message").AssertException<TOtherTestException>(); + } + UNIT_ASSERT(checker.Failed()); + UNIT_ASSERT_STRING_CONTAINS(checker.Msg(), "custom exception message"); + } + Y_UNIT_TEST(NoException) { - UNIT_ASSERT_TEST_FAILS(TTestException().AssertNoException()); + UNIT_ASSERT_TEST_FAILS(TTestException().AssertNoException()); UNIT_ASSERT_TEST_FAILS(TTestException().AssertNoExceptionRet()); - - UNIT_ASSERT_NO_EXCEPTION(TTestException("", false).Throw()); - } - + + UNIT_ASSERT_NO_EXCEPTION(TTestException("", false).Throw()); + } + Y_UNIT_TEST(ExceptionContains) { - UNIT_ASSERT_TEST_FAILS(TTestException("abc").AssertExceptionContains<TTestException>("cba")); + UNIT_ASSERT_TEST_FAILS(TTestException("abc").AssertExceptionContains<TTestException>("cba")); UNIT_ASSERT_TEST_FAILS(TTestException("abc").AssertExceptionContains<TTestException>(TStringBuf("cba"))); - UNIT_ASSERT_TEST_FAILS(TTestException("abc").AssertExceptionContains<TTestException>(TString("cba"))); - UNIT_ASSERT_TEST_FAILS(TTestException("abc").AssertExceptionContains<TTestException>(TStringBuilder() << "cba")); - - UNIT_ASSERT_TEST_FAILS(TTestException("abc", false).AssertExceptionContains<TTestException>("bc")); - - UNIT_ASSERT_TEST_FAILS(TTestException("abc").AssertExceptionContains<TOtherTestException>("b")); - - UNIT_ASSERT_EXCEPTION_CONTAINS(TTestException("abc").Throw(), TTestException, "a"); - } + UNIT_ASSERT_TEST_FAILS(TTestException("abc").AssertExceptionContains<TTestException>(TString("cba"))); + UNIT_ASSERT_TEST_FAILS(TTestException("abc").AssertExceptionContains<TTestException>(TStringBuilder() << "cba")); + + UNIT_ASSERT_TEST_FAILS(TTestException("abc", false).AssertExceptionContains<TTestException>("bc")); + + UNIT_ASSERT_TEST_FAILS(TTestException("abc").AssertExceptionContains<TOtherTestException>("b")); + + UNIT_ASSERT_EXCEPTION_CONTAINS(TTestException("abc").Throw(), TTestException, "a"); + } Y_UNIT_TEST(ExceptionSatisfies) { const auto goodPredicate = [](const TTestException& e) { return e.GetValue() == 5; }; @@ -370,4 +370,4 @@ Y_UNIT_TEST_SUITE(TUnitTestMacroTest) { UNIT_ASSERT_TEST_FAILS(TTestException().AssertExceptionSatisfies<TTestException>(badPredicate)); UNIT_ASSERT_TEST_FAILS(TTestException().AssertExceptionSatisfies<TOtherTestException>(goodPredicate)); } -} +} diff --git a/library/cpp/testing/unittest/tests_data.cpp b/library/cpp/testing/unittest/tests_data.cpp index b51cbc4b87..a131a54ddf 100644 --- a/library/cpp/testing/unittest/tests_data.cpp +++ b/library/cpp/testing/unittest/tests_data.cpp @@ -1,5 +1,5 @@ #include "tests_data.h" -#include "registar.h" +#include "registar.h" #include <library/cpp/testing/common/network.h> @@ -35,10 +35,10 @@ public: return GetPort(port); } - ui16 GetTcpAndUdpPort(ui16 port) { + ui16 GetTcpAndUdpPort(ui16 port) { return GetPort(port); - } - + } + ui16 GetPortsRange(const ui16 startPort, const ui16 range) { Y_UNUSED(startPort); auto ports = NTesting::NLegacy::GetFreePortsRange(range); @@ -54,16 +54,16 @@ public: private: void ReservePortForCurrentTest(const TAtomicSharedPtr<NTesting::IPort>& portGuard) { if (EnableReservePortsForCurrentTest) { - TTestBase* currentTest = NUnitTest::NPrivate::GetCurrentTest(); - if (currentTest != nullptr) { - currentTest->RunAfterTest([guard = portGuard]() mutable { - guard = nullptr; // remove reference for allocated port - }); - } - } - } - -private: + TTestBase* currentTest = NUnitTest::NPrivate::GetCurrentTest(); + if (currentTest != nullptr) { + currentTest->RunAfterTest([guard = portGuard]() mutable { + guard = nullptr; // remove reference for allocated port + }); + } + } + } + +private: TMutex Lock; TVector<TAtomicSharedPtr<NTesting::IPort>> ReservedPorts; const bool EnableReservePortsForCurrentTest; @@ -81,18 +81,18 @@ TPortManager::~TPortManager() { ui16 TPortManager::GetPort(ui16 port) { return Impl_->GetTcpPort(port); } - -ui16 TPortManager::GetTcpPort(ui16 port) { + +ui16 TPortManager::GetTcpPort(ui16 port) { return Impl_->GetTcpPort(port); -} - -ui16 TPortManager::GetUdpPort(ui16 port) { +} + +ui16 TPortManager::GetUdpPort(ui16 port) { return Impl_->GetUdpPort(port); -} - -ui16 TPortManager::GetTcpAndUdpPort(ui16 port) { +} + +ui16 TPortManager::GetTcpAndUdpPort(ui16 port) { return Impl_->GetTcpAndUdpPort(port); -} +} ui16 TPortManager::GetPortsRange(const ui16 startPort, const ui16 range) { return Impl_->GetPortsRange(startPort, range); diff --git a/library/cpp/testing/unittest/tests_data.h b/library/cpp/testing/unittest/tests_data.h index 6536bc1ae6..fcc56520bd 100644 --- a/library/cpp/testing/unittest/tests_data.h +++ b/library/cpp/testing/unittest/tests_data.h @@ -31,19 +31,19 @@ class TPortManager: public TNonCopyable { public: TPortManager(bool reservePortsForCurrentTest = true); ~TPortManager(); - - // Gets free TCP port + + // Gets free TCP port ui16 GetPort(ui16 port = 0); - // Gets free TCP port - ui16 GetTcpPort(ui16 port = 0); - - // Gets free UDP port - ui16 GetUdpPort(ui16 port = 0); - - // Gets one free port for use in both TCP and UDP protocols - ui16 GetTcpAndUdpPort(ui16 port = 0); - + // Gets free TCP port + ui16 GetTcpPort(ui16 port = 0); + + // Gets free UDP port + ui16 GetUdpPort(ui16 port = 0); + + // Gets one free port for use in both TCP and UDP protocols + ui16 GetTcpAndUdpPort(ui16 port = 0); + ui16 GetPortsRange(const ui16 startPort, const ui16 range); private: diff --git a/library/cpp/testing/unittest/ut/ya.make b/library/cpp/testing/unittest/ut/ya.make index 6d4c0959cc..b5676b187c 100644 --- a/library/cpp/testing/unittest/ut/ya.make +++ b/library/cpp/testing/unittest/ut/ya.make @@ -4,7 +4,7 @@ OWNER(snowball) SRCS( main.cpp - registar_ut.cpp + registar_ut.cpp ) END() diff --git a/library/cpp/testing/unittest/utmain.cpp b/library/cpp/testing/unittest/utmain.cpp index 305bc6b40f..b7e1259cab 100644 --- a/library/cpp/testing/unittest/utmain.cpp +++ b/library/cpp/testing/unittest/utmain.cpp @@ -1,4 +1,4 @@ -#include "plugin.h" +#include "plugin.h" #include "registar.h" #include "utmain.h" @@ -8,27 +8,27 @@ #include <library/cpp/json/writer/json_value.h> #include <library/cpp/testing/common/env.h> #include <library/cpp/testing/hook/hook.h> - -#include <util/datetime/base.h> - + +#include <util/datetime/base.h> + #include <util/generic/hash.h> -#include <util/generic/hash_set.h> +#include <util/generic/hash_set.h> #include <util/generic/scope.h> #include <util/generic/string.h> #include <util/generic/yexception.h> - + #include <util/network/init.h> - + #include <util/stream/file.h> #include <util/stream/output.h> #include <util/string/join.h> -#include <util/string/util.h> - -#include <util/system/defaults.h> -#include <util/system/execpath.h> -#include <util/system/valgrind.h> -#include <util/system/shellcommand.h> +#include <util/string/util.h> +#include <util/system/defaults.h> +#include <util/system/execpath.h> +#include <util/system/valgrind.h> +#include <util/system/shellcommand.h> + #if defined(_win_) #include <fcntl.h> #include <io.h> @@ -141,26 +141,26 @@ private: void OnError(const TError* descr) override { const TString comment = BuildComment(descr->msg, descr->BackTrace.data()); - ErrorMessages.push_back(comment); - } - - void OnFinish(const TFinish* descr) override { - if (descr->Success) { + ErrorMessages.push_back(comment); + } + + void OnFinish(const TFinish* descr) override { + if (descr->Success) { TraceSubtestFinished(descr->test->unit->name.data(), descr->test->name, "good", "", descr->Context); - } else { - TStringBuilder msgs; + } else { + TStringBuilder msgs; for (const TString& m : ErrorMessages) { - if (msgs) { + if (msgs) { msgs << TStringBuf("\n"); - } - msgs << m; - } - if (msgs) { + } + msgs << m; + } + if (msgs) { msgs << TStringBuf("\n"); - } + } TraceSubtestFinished(descr->test->unit->name.data(), descr->test->name, "fail", msgs, descr->Context); - ErrorMessages.clear(); - } + ErrorMessages.clear(); + } } }; @@ -364,8 +364,8 @@ private: } } - void OnFinish(const TFinish* descr) override { - TraceProcessor->Finish(*descr); + void OnFinish(const TFinish* descr) override { + TraceProcessor->Finish(*descr); if (!IsForked && ForkExitedCorrectly) { return; } @@ -373,15 +373,15 @@ private: return; } - if (descr->Success) { + if (descr->Success) { fprintf(stderr, "[%sgood%s] %s::%s\n", LightGreenColor().data(), OldColor().data(), descr->test->unit->name.data(), - descr->test->name); - NOTE_IN_VALGRIND(descr->test); + descr->test->name); + NOTE_IN_VALGRIND(descr->test); PrintTimes(SaveTestDuration()); - if (IsForked) { - fprintf(stderr, "%s", ForkCorrectExitMsg); - } + if (IsForked) { + fprintf(stderr, "%s", ForkCorrectExitMsg); + } } } @@ -613,7 +613,7 @@ static int DoUsage(const char* progname) { << " -h, --help print this help message\n" << " -l, --list print a list of available tests\n" << " -A --list-verbose print a list of available subtests\n" - << " --print-before-test print each test name before running it\n" + << " --print-before-test print each test name before running it\n" << " --print-before-suite print each test suite name before running it\n" << " --show-fails print a list of all failed tests at the end\n" << " --dont-show-fails do not print a list of all failed tests at the end\n" @@ -661,9 +661,9 @@ int NUnitTest::RunMain(int argc, char** argv) { NTesting::THook::CallBeforeRun(); Y_DEFER { NTesting::THook::CallAfterRun(); }; - NPlugin::OnStartMain(argc, argv); + NPlugin::OnStartMain(argc, argv); Y_DEFER { NPlugin::OnStopMain(argc, argv); }; - + TColoredProcessor processor(GetExecPath()); IOutputStream* listStream = &Cout; THolder<IOutputStream> listFile; diff --git a/library/cpp/testing/unittest/ya.make b/library/cpp/testing/unittest/ya.make index aaa4f2ba85..93d23f396f 100644 --- a/library/cpp/testing/unittest/ya.make +++ b/library/cpp/testing/unittest/ya.make @@ -2,10 +2,10 @@ LIBRARY() PROVIDES(test_framework) -OWNER( - pg - galaxycrab -) +OWNER( + pg + galaxycrab +) PEERDIR( library/cpp/colorizer @@ -19,7 +19,7 @@ PEERDIR( SRCS( gtest.cpp checks.cpp - plugin.cpp + plugin.cpp registar.cpp tests_data.cpp utmain.cpp |