diff options
author | Anton Samokhvalov <pg83@yandex.ru> | 2022-02-10 16:45:17 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:45:17 +0300 |
commit | d3a398281c6fd1d3672036cb2d63f842d2cb28c5 (patch) | |
tree | dd4bd3ca0f36b817e96812825ffaf10d645803f2 /library/cpp/testing/unittest/registar.h | |
parent | 72cb13b4aff9bc9cf22e49251bc8fd143f82538f (diff) | |
download | ydb-d3a398281c6fd1d3672036cb2d63f842d2cb28c5.tar.gz |
Restoring authorship annotation for Anton Samokhvalov <pg83@yandex.ru>. Commit 2 of 2.
Diffstat (limited to 'library/cpp/testing/unittest/registar.h')
-rw-r--r-- | library/cpp/testing/unittest/registar.h | 664 |
1 files changed, 332 insertions, 332 deletions
diff --git a/library/cpp/testing/unittest/registar.h b/library/cpp/testing/unittest/registar.h index 3307ba7e55..44517a0092 100644 --- a/library/cpp/testing/unittest/registar.h +++ b/library/cpp/testing/unittest/registar.h @@ -1,20 +1,20 @@ #pragma once - + #include <library/cpp/dbg_output/dump.h> - + #include <util/generic/bt_exception.h> #include <util/generic/hash.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/typetraits.h> #include <util/generic/vector.h> #include <util/generic/yexception.h> - -#include <util/string/builder.h> -#include <util/string/cast.h> -#include <util/string/printf.h> + +#include <util/string/builder.h> +#include <util/string/cast.h> +#include <util/string/printf.h> #include <util/system/defaults.h> #include <util/system/type_name.h> @@ -23,21 +23,21 @@ #include <util/system/rusage.h> -#include <cmath> -#include <cstdio> +#include <cmath> +#include <cstdio> #include <functional> extern bool CheckExceptionMessage(const char*, TString&); - -namespace NUnitTest { - class TTestBase; - namespace NPrivate { +namespace NUnitTest { + class TTestBase; + + namespace NPrivate { void RaiseError(const char* what, const TString& msg, bool fatalFailure); - void SetUnittestThread(bool); + void SetUnittestThread(bool); void SetCurrentTest(TTestBase*); TTestBase* GetCurrentTest(); - } + } extern bool ShouldColorizeDiff; extern bool ContinueOnFail; @@ -64,13 +64,13 @@ namespace NUnitTest { struct TTestContext { TTestContext() - : Processor(nullptr) - { + : Processor(nullptr) + { } explicit TTestContext(ITestSuiteProcessor* processor) - : Processor(processor) - { + : Processor(processor) + { } using TMetrics = THashMap<TString, double>; @@ -79,59 +79,59 @@ namespace NUnitTest { ITestSuiteProcessor* Processor; }; - class ITestSuiteProcessor { - public: - struct TUnit { + class ITestSuiteProcessor { + public: + struct TUnit { const TString name; - }; - - struct TTest { + }; + + struct TTest { const TUnit* unit; const char* name; - }; - - struct TError { + }; + + struct TError { const TTest* test; const char* msg; TString BackTrace; TTestContext* Context; - }; - + }; + struct TFinish { const TTest* test; TTestContext* Context; bool Success; - }; - + }; + ITestSuiteProcessor(); - + virtual ~ITestSuiteProcessor(); - + void Start(); - + void End(); - + void UnitStart(const TUnit& unit); - + void UnitStop(const TUnit& unit); - + void Error(const TError& descr); - + void BeforeTest(const TTest& test); - + void Finish(const TFinish& descr); - + unsigned GoodTests() const noexcept; unsigned FailTests() const noexcept; - + unsigned GoodTestsInCurrentUnit() const noexcept; - + unsigned FailTestsInCurrentUnit() const noexcept; - + // Should execute test suite? virtual bool CheckAccess(TString /*name*/, size_t /*num*/); - + // Should execute a test whitin suite? virtual bool CheckAccessTest(TString /*suite*/, const char* /*name*/); @@ -143,142 +143,142 @@ namespace NUnitTest { // --fork-tests is set (warning: this may be false, but never the less test will be forked if called inside UNIT_FORKED_TEST) virtual bool GetForkTests() const; - private: + private: virtual void OnStart(); - + virtual void OnEnd(); - + virtual void OnUnitStart(const TUnit* /*unit*/); - + virtual void OnUnitStop(const TUnit* /*unit*/); - + virtual void OnError(const TError* /*error*/); - + virtual void OnFinish(const TFinish* /*finish*/); - + virtual void OnBeforeTest(const TTest* /*test*/); void AddTestError(const TTest& test); void AddTestFinish(const TTest& test); - private: + private: TMap<TString, size_t> TestErrors_; TMap<TString, size_t> CurTestErrors_; - }; - - class TTestBase; - class TTestFactory; - - class ITestBaseFactory: public TIntrusiveListItem<ITestBaseFactory> { - public: + }; + + class TTestBase; + class TTestFactory; + + class ITestBaseFactory: public TIntrusiveListItem<ITestBaseFactory> { + public: ITestBaseFactory(); - + virtual ~ITestBaseFactory(); - + // name of test suite virtual TString Name() const noexcept = 0; - virtual TTestBase* ConstructTest() = 0; - - private: + virtual TTestBase* ConstructTest() = 0; + + private: void Register() noexcept; - }; - - class TTestBase { - friend class TTestFactory; + }; + + class TTestBase { + friend class TTestFactory; TRusage rusage; - - public: + + public: TTestBase() noexcept; - + virtual ~TTestBase(); - + virtual TString TypeId() const; - + virtual TString Name() const noexcept = 0; - virtual void Execute() = 0; - + virtual void Execute() = 0; + virtual void SetUp(); - + 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 - protected: + protected: bool CheckAccessTest(const char* test); void BeforeTest(const char* func); void Finish(const char* func, TTestContext* context); - + void AtStart(); - + void AtEnd(); - + void Run(std::function<void()> f, const TString& suite, const char* name, bool forceFork); - - class TCleanUp { - public: + + class TCleanUp { + public: explicit TCleanUp(TTestBase* base); - + ~TCleanUp(); - - private: - TTestBase* Base_; - }; - + + private: + TTestBase* Base_; + }; + void BeforeTest(); - + void AfterTest(); - + bool GetIsForked() const; bool GetForkTests() const; ITestSuiteProcessor* Processor() const noexcept; - - private: - TTestFactory* Parent_; + + private: + TTestFactory* Parent_; size_t TestErrors_; const char* CurrentSubtest_; TAdaptiveLock AfterTestFunctionsLock_; TVector<std::function<void()>> AfterTestFunctions_; - }; - -#define UNIT_TEST_SUITE(N) \ - typedef N TThisUnitTestSuite; \ - \ -public: \ - static TString StaticName() noexcept { \ - return TString(#N); \ - } \ - \ -private: \ - virtual TString Name() const noexcept override { \ - return this->StaticName(); \ - } \ - \ - virtual void Execute() override { \ - this->AtStart(); - -#define UNIT_TEST_SUITE_DEMANGLE(N) \ - typedef N TThisUnitTestSuite; \ - \ -public: \ - static TString StaticName() noexcept { \ + }; + +#define UNIT_TEST_SUITE(N) \ + typedef N TThisUnitTestSuite; \ + \ +public: \ + static TString StaticName() noexcept { \ + return TString(#N); \ + } \ + \ +private: \ + virtual TString Name() const noexcept override { \ + return this->StaticName(); \ + } \ + \ + virtual void Execute() override { \ + this->AtStart(); + +#define UNIT_TEST_SUITE_DEMANGLE(N) \ + typedef N TThisUnitTestSuite; \ + \ +public: \ + static TString StaticName() noexcept { \ return TypeName<N>(); \ - } \ - \ -private: \ - virtual TString Name() const noexcept override { \ - return this->StaticName(); \ - } \ - \ - virtual void Execute() override { \ + } \ + \ +private: \ + virtual TString Name() const noexcept override { \ + return this->StaticName(); \ + } \ + \ + virtual void Execute() override { \ this->AtStart(); #ifndef UT_SKIP_EXCEPTIONS @@ -289,21 +289,21 @@ private: \ #define CATCH_REACTION_BT(FN, e, context) throw #endif -#define UNIT_TEST_CHECK_TEST_IS_DECLARED_ONLY_ONCE(F) \ +#define UNIT_TEST_CHECK_TEST_IS_DECLARED_ONLY_ONCE(F) \ /* If you see this message - delete multiple UNIT_TEST(TestName) with same TestName. */ \ - /* It's forbidden to declare same test twice because it breaks --fork-tests logic. */ \ - int You_have_declared_test_##F##_multiple_times_This_is_forbidden; \ + /* It's forbidden to declare same test twice because it breaks --fork-tests logic. */ \ + int You_have_declared_test_##F##_multiple_times_This_is_forbidden; \ Y_UNUSED(You_have_declared_test_##F##_multiple_times_This_is_forbidden); -#define UNIT_TEST_RUN(F, FF, context) \ - this->BeforeTest((#F)); \ - { \ - struct T##F##Caller { \ - static void X(TThisUnitTestSuite* thiz, NUnitTest::TTestContext&) { \ - TCleanUp cleaner(thiz); \ - thiz->F(); \ - } \ - }; \ +#define UNIT_TEST_RUN(F, FF, context) \ + this->BeforeTest((#F)); \ + { \ + struct T##F##Caller { \ + static void X(TThisUnitTestSuite* thiz, NUnitTest::TTestContext&) { \ + TCleanUp cleaner(thiz); \ + thiz->F(); \ + } \ + }; \ this->TTestBase::Run(std::bind(&T##F##Caller::X, this, context), StaticName(), (#F), FF); \ } @@ -323,17 +323,17 @@ private: \ } \ this->Finish((#F), &context); \ } \ - } - + } + #define UNIT_TEST(F) UNIT_TEST_IMPL(F, false) - + #define UNIT_FORKED_TEST(F) UNIT_TEST_IMPL(F, true) -#define UNIT_TEST_EXCEPTION(F, E) \ - /* main process with "--fork-tests" flag treats exceptions as errors - it's result of forked test run */ \ - if (this->GetForkTests() && !this->GetIsForked()) { \ - UNIT_TEST_IMPL(F, false); \ - /* forked process (or main without "--fork-tests") treats some exceptions as success - it's exception test! */ \ +#define UNIT_TEST_EXCEPTION(F, E) \ + /* main process with "--fork-tests" flag treats exceptions as errors - it's result of forked test run */ \ + if (this->GetForkTests() && !this->GetIsForked()) { \ + UNIT_TEST_IMPL(F, false); \ + /* 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))) { \ @@ -342,7 +342,7 @@ private: \ this->AddError("exception expected", &context); \ } catch (const ::NUnitTest::TAssertException&) { \ } catch (const E& e) { \ - TString err; \ + TString err; \ if (!CheckExceptionMessage(e.what(), err)) \ this->AddError(err.c_str(), &context); \ } catch (const std::exception& e) { \ @@ -353,27 +353,27 @@ private: \ this->Finish((#F), &context); \ } \ } - -#define UNIT_TEST_SUITE_END() \ - this->AtEnd(); \ - } \ - \ -public: \ - /*for ; after macros*/ void sub##F() - -#define UNIT_FAIL_IMPL(R, M) \ - do { \ + +#define UNIT_TEST_SUITE_END() \ + this->AtEnd(); \ + } \ + \ +public: \ + /*for ; after macros*/ void sub##F() + +#define UNIT_FAIL_IMPL(R, M) \ + do { \ ::NUnitTest::NPrivate::RaiseError(R, ::TStringBuilder() << R << " at " << __LOCATION__ << ", " << __PRETTY_FUNCTION__ << ": " << M, true); \ } while (false) - + #define UNIT_FAIL_NONFATAL_IMPL(R, M) \ do { \ ::NUnitTest::NPrivate::RaiseError(R, ::TStringBuilder() << R << " at " << __LOCATION__ << ", " << __PRETTY_FUNCTION__ << ": " << M, false); \ } while (false) -#define UNIT_FAIL(M) UNIT_FAIL_IMPL("forced failure", M) +#define UNIT_FAIL(M) UNIT_FAIL_IMPL("forced failure", M) #define UNIT_FAIL_NONFATAL(M) UNIT_FAIL_NONFATAL_IMPL("forced failure", M) - + //types #define UNIT_ASSERT_TYPES_EQUAL(A, B) \ do { \ @@ -395,7 +395,7 @@ public: \ UNIT_FAIL_IMPL("assertion failure", failMsg); \ } \ } while (false) - + #define UNIT_ASSERT_DOUBLES_EQUAL_DEPRECATED(E, A, D) UNIT_ASSERT_DOUBLES_EQUAL_DEPRECATED_C(E, A, D, "") #define UNIT_ASSERT_DOUBLES_EQUAL_C(E, A, D, C) \ @@ -423,24 +423,24 @@ public: \ } while (false) #define UNIT_ASSERT_DOUBLES_EQUAL(E, A, D) UNIT_ASSERT_DOUBLES_EQUAL_C(E, A, D, "") - + //strings -#define UNIT_ASSERT_STRINGS_EQUAL_C(A, B, C) \ - do { \ - const TString _a(A); \ - const TString _b(B); \ - if (_a != _b) { \ +#define UNIT_ASSERT_STRINGS_EQUAL_C(A, B, C) \ + do { \ + const TString _a(A); \ + const TString _b(B); \ + if (_a != _b) { \ auto&& failMsg = Sprintf("%s != %s %s", ToString(_a).data(), ToString(_b).data(), (::TStringBuilder() << C).data()); \ UNIT_FAIL_IMPL("strings equal assertion failed", failMsg); \ - } \ + } \ } while (false) - + #define UNIT_ASSERT_STRINGS_EQUAL(A, B) UNIT_ASSERT_STRINGS_EQUAL_C(A, B, "") #define UNIT_ASSERT_STRING_CONTAINS_C(A, B, C) \ do { \ - const TString _a(A); \ - const TString _b(B); \ + const TString _a(A); \ + const TString _b(B); \ if (!_a.Contains(_b)) { \ auto&& msg = Sprintf("\"%s\" does not contain \"%s\", %s", ToString(_a).data(), ToString(_b).data(), (::TStringBuilder() << C).data()); \ UNIT_FAIL_IMPL("strings contains assertion failed", msg); \ @@ -449,26 +449,26 @@ public: \ #define UNIT_ASSERT_STRING_CONTAINS(A, B) UNIT_ASSERT_STRING_CONTAINS_C(A, B, "") -#define UNIT_ASSERT_NO_DIFF(A, B) \ - do { \ - const TString _a(A); \ - const TString _b(B); \ - if (_a != _b) { \ +#define UNIT_ASSERT_NO_DIFF(A, B) \ + do { \ + const TString _a(A); \ + const TString _b(B); \ + if (_a != _b) { \ UNIT_FAIL_IMPL("strings (" #A ") and (" #B ") are different", Sprintf("\n%s", ::NUnitTest::ColoredDiff(_a, _b, " \t\n.,:;'\"").data())); \ - } \ + } \ } while (false) //strings -#define UNIT_ASSERT_STRINGS_UNEQUAL_C(A, B, C) \ - do { \ - const TString _a(A); \ - const TString _b(B); \ - if (_a == _b) { \ +#define UNIT_ASSERT_STRINGS_UNEQUAL_C(A, B, C) \ + do { \ + const TString _a(A); \ + const TString _b(B); \ + if (_a == _b) { \ auto&& msg = Sprintf("%s == %s %s", ToString(_a).data(), ToString(_b).data(), (::TStringBuilder() << C).data()); \ - UNIT_FAIL_IMPL("strings unequal assertion failed", msg); \ - } \ + UNIT_FAIL_IMPL("strings unequal assertion failed", msg); \ + } \ } while (false) - + #define UNIT_ASSERT_STRINGS_UNEQUAL(A, B) UNIT_ASSERT_STRINGS_UNEQUAL_C(A, B, "") //bool @@ -478,9 +478,9 @@ public: \ UNIT_FAIL_IMPL("assertion failed", Sprintf("(%s) %s", #A, (::TStringBuilder() << C).data())); \ } \ } while (false) - + #define UNIT_ASSERT(A) UNIT_ASSERT_C(A, "") - + //general #define UNIT_ASSERT_EQUAL_C(A, B, C) \ do { \ @@ -488,16 +488,16 @@ public: \ UNIT_FAIL_IMPL("equal assertion failed", Sprintf("%s == %s %s", #A, #B, (::TStringBuilder() << C).data())); \ } \ } while (false) - + #define UNIT_ASSERT_EQUAL(A, B) UNIT_ASSERT_EQUAL_C(A, B, "") - + #define UNIT_ASSERT_UNEQUAL_C(A, B, C) \ do { \ if ((A) == (B)) { \ UNIT_FAIL_IMPL("unequal assertion failed", Sprintf("%s != %s %s", #A, #B, (::TStringBuilder() << C).data()));\ } \ } while (false) - + #define UNIT_ASSERT_UNEQUAL(A, B) UNIT_ASSERT_UNEQUAL_C(A, B, "") #define UNIT_ASSERT_LT_C(A, B, C) \ @@ -644,86 +644,86 @@ public: \ #define UNIT_ASSERT_NO_EXCEPTION_RESULT(A) UNIT_ASSERT_NO_EXCEPTION_RESULT_C(A, "") -#define UNIT_ASSERT_NO_EXCEPTION_C(A, C) \ - do { \ - try { \ - (void)(A); \ - } catch (const ::NUnitTest::TAssertException&) { \ - throw; \ - } catch (...) { \ +#define UNIT_ASSERT_NO_EXCEPTION_C(A, C) \ + do { \ + try { \ + (void)(A); \ + } catch (const ::NUnitTest::TAssertException&) { \ + throw; \ + } 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, "") - namespace NPrivate { - template <class T, class U, bool Integers> - struct TCompareValuesImpl { - static inline bool Compare(const T& a, const U& b) { - return a == b; - } - }; - - template <class T, class U> - struct TCompareValuesImpl<T, U, true> { - static inline bool Compare(const T& a, const U& b) { - return ::ToString(a) == ::ToString(b); - } - }; - - template <class T, class U> + namespace NPrivate { + template <class T, class U, bool Integers> + struct TCompareValuesImpl { + static inline bool Compare(const T& a, const U& b) { + return a == b; + } + }; + + template <class T, class U> + struct TCompareValuesImpl<T, U, true> { + static inline bool Compare(const T& a, const U& b) { + return ::ToString(a) == ::ToString(b); + } + }; + + template <class T, class U> using TCompareValues = TCompareValuesImpl<T, U, std::is_integral<T>::value && std::is_integral<U>::value>; - - template <typename T, typename U> - static inline bool CompareEqual(const T& a, const U& b) { - return TCompareValues<T, U>::Compare(a, b); - } - - static inline bool CompareEqual(const char* a, const char* b) { - return 0 == strcmp(a, b); - } - - // helper method to avoid double evaluation of A and B expressions in UNIT_ASSERT_VALUES_EQUAL_C - template <typename T, typename U> + + template <typename T, typename U> + static inline bool CompareEqual(const T& a, const U& b) { + return TCompareValues<T, U>::Compare(a, b); + } + + static inline bool CompareEqual(const char* a, const char* b) { + return 0 == strcmp(a, b); + } + + // helper method to avoid double evaluation of A and B expressions in UNIT_ASSERT_VALUES_EQUAL_C + template <typename T, typename U> static inline bool CompareAndMakeStrings(const T& a, const U& b, TString& as, TString& asInd, TString& bs, TString& bsInd, bool& usePlainDiff, bool want) { - const bool have = CompareEqual(a, b); + const bool have = CompareEqual(a, b); usePlainDiff = std::is_integral<T>::value && std::is_integral<U>::value; - - if (want == have) { - return true; - } - - as = ::TStringBuilder() << ::DbgDump(a); - bs = ::TStringBuilder() << ::DbgDump(b); - asInd = ::TStringBuilder() << ::DbgDump(a).SetIndent(true); - bsInd = ::TStringBuilder() << ::DbgDump(b).SetIndent(true); - - return false; - } + + if (want == have) { + return true; + } + + as = ::TStringBuilder() << ::DbgDump(a); + bs = ::TStringBuilder() << ::DbgDump(b); + asInd = ::TStringBuilder() << ::DbgDump(a).SetIndent(true); + bsInd = ::TStringBuilder() << ::DbgDump(b).SetIndent(true); + + return false; + } } //values -#define UNIT_ASSERT_VALUES_EQUAL_IMPL(A, B, C, EQflag, EQstr, NEQstr) \ - do { \ - TString _as; \ - TString _bs; \ - TString _asInd; \ - TString _bsInd; \ - bool _usePlainDiff; \ - if (!::NUnitTest::NPrivate::CompareAndMakeStrings(A, B, _as, _asInd, _bs, _bsInd, _usePlainDiff, EQflag)) { \ +#define UNIT_ASSERT_VALUES_EQUAL_IMPL(A, B, C, EQflag, EQstr, NEQstr) \ + do { \ + TString _as; \ + TString _bs; \ + TString _asInd; \ + TString _bsInd; \ + bool _usePlainDiff; \ + if (!::NUnitTest::NPrivate::CompareAndMakeStrings(A, B, _as, _asInd, _bs, _bsInd, _usePlainDiff, EQflag)) { \ auto&& failMsg = Sprintf("(%s %s %s) failed: (%s %s %s) %s", #A, EQstr, #B, _as.data(), NEQstr, _bs.data(), (::TStringBuilder() << C).data()); \ - if (EQflag && !_usePlainDiff) { \ + if (EQflag && !_usePlainDiff) { \ failMsg += ", with diff:\n"; \ failMsg += ::NUnitTest::ColoredDiff(_asInd, _bsInd); \ - } \ + } \ UNIT_FAIL_IMPL("assertion failed", failMsg); \ - } \ + } \ } while (false) #define UNIT_ASSERT_VALUES_EQUAL_C(A, B, C) \ UNIT_ASSERT_VALUES_EQUAL_IMPL(A, B, C, true, "==", "!=") - + #define UNIT_ASSERT_VALUES_UNEQUAL_C(A, B, C) \ UNIT_ASSERT_VALUES_EQUAL_IMPL(A, B, C, false, "!=", "==") @@ -742,7 +742,7 @@ public: \ UNIT_FAIL_IMPL("fail test assertion failure", \ "code is expected to generate test failure, " \ "but it throws exception with message: " \ - << CurrentExceptionMessage()); \ + << CurrentExceptionMessage()); \ } \ if (!checker.Failed()) { \ UNIT_FAIL_IMPL("fail test assertion failure", \ @@ -754,46 +754,46 @@ public: \ #define UNIT_ADD_METRIC(name, value) ut_context.Metrics[name] = value - class TTestFactory { - friend class TTestBase; - friend class ITestBaseFactory; - - public: + class TTestFactory { + friend class TTestBase; + friend class ITestBaseFactory; + + public: static TTestFactory& Instance(); - + unsigned Execute(); - + void SetProcessor(ITestSuiteProcessor* processor); - - private: + + private: void Register(ITestBaseFactory* b) noexcept; - + ITestSuiteProcessor* Processor() const noexcept; - - private: + + private: explicit TTestFactory(ITestSuiteProcessor* processor); - + ~TTestFactory(); - - private: - TIntrusiveList<ITestBaseFactory> Items_; - ITestSuiteProcessor* Processor_; - }; - - template <class T> - class TTestBaseFactory: public ITestBaseFactory { - public: + + private: + TIntrusiveList<ITestBaseFactory> Items_; + ITestSuiteProcessor* Processor_; + }; + + template <class T> + class TTestBaseFactory: public ITestBaseFactory { + public: ~TTestBaseFactory() override = default; - + inline TTestBase* ConstructTest() override { - return new T; - } - + return new T; + } + inline TString Name() const noexcept override { return T::StaticName(); - } - }; - + } + }; + struct TBaseTestCase { // NOTE: since EACH test case is instantiated for listing tests, its // ctor/dtor are not the best place to do heavy preparations in test fixtures. @@ -802,15 +802,15 @@ public: \ inline TBaseTestCase() : TBaseTestCase(nullptr, nullptr, false) - { - } + { + } inline TBaseTestCase(const char* name, std::function<void(TTestContext&)> body, bool forceFork) : Name_(name) , Body_(std::move(body)) , ForceFork_(forceFork) - { - } + { + } virtual ~TBaseTestCase() = default; @@ -836,7 +836,7 @@ public: \ const char* Name_; std::function<void(TTestContext&)> Body_; bool ForceFork_; - }; + }; using TBaseFixture = TBaseTestCase; @@ -911,76 +911,76 @@ public: \ bool Failed_ = false; }; -#define UNIT_TEST_SUITE_REGISTRATION(T) \ +#define UNIT_TEST_SUITE_REGISTRATION(T) \ static const ::NUnitTest::TTestBaseFactory<T> Y_GENERATE_UNIQUE_ID(UTREG_); - + #define Y_UNIT_TEST_SUITE_IMPL_F(N, T, F) \ - namespace NTestSuite##N { \ + namespace NTestSuite##N { \ class TCurrentTestCase: public F { \ }; \ - class TCurrentTest: public T { \ - private: \ + class TCurrentTest: public T { \ + private: \ typedef std::function<THolder<NUnitTest::TBaseTestCase>()> TTestCaseFactory; \ typedef TVector<TTestCaseFactory> TTests; \ - \ - static TTests& Tests() { \ - static TTests tests; \ - return tests; \ - } \ - \ - public: \ - static TString StaticName() { \ - return #N; \ - } \ - virtual TString Name() const noexcept { \ - return StaticName(); \ - } \ - \ + \ + static TTests& Tests() { \ + static TTests tests; \ + return tests; \ + } \ + \ + public: \ + static TString StaticName() { \ + return #N; \ + } \ + virtual TString Name() const noexcept { \ + return StaticName(); \ + } \ + \ static void AddTest(const char* name, \ const std::function<void(NUnitTest::TTestContext&)>& body, bool forceFork) \ { \ Tests().push_back([=]{ return MakeHolder<NUnitTest::TBaseTestCase>(name, body, forceFork); }); \ - } \ - \ + } \ + \ static void AddTest(TTestCaseFactory testCaseFactory) { \ Tests().push_back(std::move(testCaseFactory)); \ } \ \ - virtual void Execute() { \ - this->AtStart(); \ + virtual void Execute() { \ + this->AtStart(); \ for (TTests::iterator it = Tests().begin(), ie = Tests().end(); it != ie; ++it) { \ const auto i = (*it)(); \ if (!this->CheckAccessTest(i->Name_)) { \ - continue; \ - } \ + continue; \ + } \ NUnitTest::TTestContext context(this->TTestBase::Processor()); \ - try { \ + try { \ this->BeforeTest(i->Name_); \ - { \ - TCleanUp cleaner(this); \ + { \ + TCleanUp cleaner(this); \ auto testCase = [&i, &context] { \ i->SetUp(context); \ i->Execute_(context); \ i->TearDown(context); \ }; \ this->T::Run(testCase, StaticName(), i->Name_, i->ForceFork_); \ - } \ - } catch (const ::NUnitTest::TAssertException&) { \ - } catch (const yexception& e) { \ + } \ + } catch (const ::NUnitTest::TAssertException&) { \ + } catch (const yexception& e) { \ CATCH_REACTION_BT(i->Name_, e, &context); \ - } catch (const std::exception& e) { \ + } catch (const std::exception& e) { \ CATCH_REACTION(i->Name_, e, &context); \ - } catch (...) { \ - this->AddError("non-std exception!", &context); \ - } \ + } catch (...) { \ + this->AddError("non-std exception!", &context); \ + } \ this->Finish(i->Name_, &context); \ - } \ - this->AtEnd(); \ - } \ - }; \ - UNIT_TEST_SUITE_REGISTRATION(TCurrentTest) \ - } \ - namespace NTestSuite##N + } \ + this->AtEnd(); \ + } \ + }; \ + UNIT_TEST_SUITE_REGISTRATION(TCurrentTest) \ + } \ + namespace NTestSuite##N #define Y_UNIT_TEST_SUITE_IMPL(N, T) Y_UNIT_TEST_SUITE_IMPL_F(N, T, ::NUnitTest::TBaseTestCase) #define Y_UNIT_TEST_SUITE(N) Y_UNIT_TEST_SUITE_IMPL(N, TTestBase) @@ -1016,7 +1016,7 @@ public: \ #define SIMPLE_UNIT_FORKED_TEST(N) Y_UNIT_TEST_IMPL(N, true, TCurrentTestCase) #define Y_UNIT_TEST_SUITE_IMPLEMENTATION(N) \ - namespace NTestSuite##N + namespace NTestSuite##N #define Y_UNIT_TEST_DECLARE(N) \ struct TTestCase##N @@ -1025,6 +1025,6 @@ public: \ friend NTestSuite##N::TTestCase##T \ TString RandomString(size_t len, ui32 seed = 0); -} - +} + using ::NUnitTest::TTestBase; |