From e85761bc47b8df302da00e0f14d23a9f00fa6461 Mon Sep 17 00:00:00 2001 From: Ilya Matveev Date: Mon, 19 Jul 2021 17:01:10 +0700 Subject: [PATCH] [K/N][Runtime] Support nice mocks for scoped mock functions --- .../runtime/src/main/cpp/CleanerTest.cpp | 1 + .../runtime/src/main/cpp/ExceptionsTest.cpp | 2 + .../main/cpp/TestSupportCompilerGenerated.hpp | 66 ++++++++++++++----- .../test_support/cpp/CompilerGenerated.cpp | 30 +++------ 4 files changed, 62 insertions(+), 37 deletions(-) diff --git a/kotlin-native/runtime/src/main/cpp/CleanerTest.cpp b/kotlin-native/runtime/src/main/cpp/CleanerTest.cpp index 75ed0ad4ba1..7ba19da3af8 100644 --- a/kotlin-native/runtime/src/main/cpp/CleanerTest.cpp +++ b/kotlin-native/runtime/src/main/cpp/CleanerTest.cpp @@ -16,6 +16,7 @@ #include "Types.h" using namespace kotlin; +using namespace kotlin::test_support; using testing::_; // TODO: Also test disposal. (This requires extracting Worker interface) diff --git a/kotlin-native/runtime/src/main/cpp/ExceptionsTest.cpp b/kotlin-native/runtime/src/main/cpp/ExceptionsTest.cpp index 7213a2815ce..6442b32ea25 100644 --- a/kotlin-native/runtime/src/main/cpp/ExceptionsTest.cpp +++ b/kotlin-native/runtime/src/main/cpp/ExceptionsTest.cpp @@ -15,6 +15,8 @@ using namespace kotlin; using ::testing::_; +using ::kotlin::test_support::ScopedReportUnhandledExceptionMock; +using ::kotlin::test_support::ScopedKotlin_runUnhandledExceptionHookMock; namespace { diff --git a/kotlin-native/runtime/src/main/cpp/TestSupportCompilerGenerated.hpp b/kotlin-native/runtime/src/main/cpp/TestSupportCompilerGenerated.hpp index b53d0054ebf..c34b0b7642e 100644 --- a/kotlin-native/runtime/src/main/cpp/TestSupportCompilerGenerated.hpp +++ b/kotlin-native/runtime/src/main/cpp/TestSupportCompilerGenerated.hpp @@ -12,29 +12,43 @@ #include "Types.h" #include "Utils.hpp" -template -class ScopedStrictMockFunction : private kotlin::MoveOnly { -public: - using Mock = testing::StrictMock>; +namespace kotlin { +namespace test_support { - explicit ScopedStrictMockFunction(Mock** globalMockLocation) : globalMockLocation_(globalMockLocation) { - RuntimeCheck(globalMockLocation != nullptr, "ScopedStrictMockFunction needs non-null global mock location"); - RuntimeCheck(*globalMockLocation == nullptr, "ScopedStrictMockFunction needs null global mock"); +namespace internal { + +extern testing::MockFunction* createCleanerWorkerMock; +extern testing::MockFunction* shutdownCleanerWorkerMock; +extern testing::MockFunction* reportUnhandledExceptionMock; +extern testing::MockFunction* Kotlin_runUnhandledExceptionHookMock; + +} // namespace internal + +template +class ScopedMockFunction : private kotlin::MoveOnly { +public: + using Mock = typename std::conditional_t>, + testing::NiceMock>>; + + explicit ScopedMockFunction(testing::MockFunction** globalMockLocation) : globalMockLocation_(globalMockLocation) { + RuntimeCheck(globalMockLocation != nullptr, "ScopedMockFunction needs non-null global mock location"); + RuntimeCheck(*globalMockLocation == nullptr, "ScopedMockFunction needs null global mock"); mock_ = make_unique(); *globalMockLocation_ = mock_.get(); } - ScopedStrictMockFunction(ScopedStrictMockFunction&& rhs) : globalMockLocation_(rhs.globalMockLocation_), mock_(std::move(rhs.mock_)) { + ScopedMockFunction(ScopedMockFunction&& rhs) : globalMockLocation_(rhs.globalMockLocation_), mock_(std::move(rhs.mock_)) { rhs.globalMockLocation_ = nullptr; } - ScopedStrictMockFunction& operator=(ScopedStrictMockFunction&& rhs) { - ScopedStrictMockFunction tmp(std::move(rhs)); + ScopedMockFunction& operator=(ScopedMockFunction&& rhs) { + ScopedMockFunction tmp(std::move(rhs)); swap(tmp); return *this; } - ~ScopedStrictMockFunction() { + ~ScopedMockFunction() { if (!globalMockLocation_) return; RuntimeCheck(*globalMockLocation_ == mock_.get(), "unexpected global mock location"); @@ -44,7 +58,7 @@ public: *globalMockLocation_ = nullptr; } - void swap(ScopedStrictMockFunction& other) { + void swap(ScopedMockFunction& other) { std::swap(globalMockLocation_, other.globalMockLocation_); std::swap(mock_, other.mock_); } @@ -54,11 +68,29 @@ public: private: // Can be null if moved-out of. - Mock** globalMockLocation_; + testing::MockFunction** globalMockLocation_; KStdUniquePtr mock_; }; -ScopedStrictMockFunction ScopedCreateCleanerWorkerMock(); -ScopedStrictMockFunction ScopedShutdownCleanerWorkerMock(); -ScopedStrictMockFunction ScopedReportUnhandledExceptionMock(); -ScopedStrictMockFunction ScopedKotlin_runUnhandledExceptionHookMock(); +template +ScopedMockFunction ScopedCreateCleanerWorkerMock() { + return ScopedMockFunction(&internal::createCleanerWorkerMock); +} + +template +ScopedMockFunction ScopedShutdownCleanerWorkerMock() { + return ScopedMockFunction(&internal::shutdownCleanerWorkerMock); +} + +template +ScopedMockFunction ScopedReportUnhandledExceptionMock() { + return ScopedMockFunction(&internal::reportUnhandledExceptionMock); +} + +template +ScopedMockFunction ScopedKotlin_runUnhandledExceptionHookMock() { + return ScopedMockFunction(&internal::Kotlin_runUnhandledExceptionHookMock); +} + +} // namespace test_support +} // namespace kotlin \ No newline at end of file diff --git a/kotlin-native/runtime/src/test_support/cpp/CompilerGenerated.cpp b/kotlin-native/runtime/src/test_support/cpp/CompilerGenerated.cpp index b0956eb509a..1d2ad8b1d94 100644 --- a/kotlin-native/runtime/src/test_support/cpp/CompilerGenerated.cpp +++ b/kotlin-native/runtime/src/test_support/cpp/CompilerGenerated.cpp @@ -8,6 +8,16 @@ #include "ObjectTestSupport.hpp" #include "Types.h" +using kotlin::test_support::internal::createCleanerWorkerMock; +using kotlin::test_support::internal::shutdownCleanerWorkerMock; +using kotlin::test_support::internal::reportUnhandledExceptionMock; +using kotlin::test_support::internal::Kotlin_runUnhandledExceptionHookMock; + +testing::MockFunction* kotlin::test_support::internal::createCleanerWorkerMock = nullptr; +testing::MockFunction* kotlin::test_support::internal::shutdownCleanerWorkerMock = nullptr; +testing::MockFunction* kotlin::test_support::internal::reportUnhandledExceptionMock = nullptr; +testing::MockFunction* kotlin::test_support::internal::Kotlin_runUnhandledExceptionHookMock = nullptr; + namespace { struct EmptyPayload { @@ -49,11 +59,6 @@ struct KBox { const T value; }; -testing::StrictMock>* createCleanerWorkerMock = nullptr; -testing::StrictMock>* shutdownCleanerWorkerMock = nullptr; -testing::StrictMock>* reportUnhandledExceptionMock = nullptr; -testing::StrictMock>* Kotlin_runUnhandledExceptionHookMock = nullptr; - } // namespace extern "C" { @@ -285,18 +290,3 @@ KInt Kotlin_CleanerImpl_createCleanerWorker() { } // extern "C" -ScopedStrictMockFunction ScopedCreateCleanerWorkerMock() { - return ScopedStrictMockFunction(&createCleanerWorkerMock); -} - -ScopedStrictMockFunction ScopedShutdownCleanerWorkerMock() { - return ScopedStrictMockFunction(&shutdownCleanerWorkerMock); -} - -ScopedStrictMockFunction ScopedReportUnhandledExceptionMock() { - return ScopedStrictMockFunction(&reportUnhandledExceptionMock); -} - -ScopedStrictMockFunction ScopedKotlin_runUnhandledExceptionHookMock() { - return ScopedStrictMockFunction(&Kotlin_runUnhandledExceptionHookMock); -}