[K/N][Runtime] Support nice mocks for scoped mock functions
This commit is contained in:
@@ -16,6 +16,7 @@
|
|||||||
#include "Types.h"
|
#include "Types.h"
|
||||||
|
|
||||||
using namespace kotlin;
|
using namespace kotlin;
|
||||||
|
using namespace kotlin::test_support;
|
||||||
using testing::_;
|
using testing::_;
|
||||||
|
|
||||||
// TODO: Also test disposal. (This requires extracting Worker interface)
|
// TODO: Also test disposal. (This requires extracting Worker interface)
|
||||||
|
|||||||
@@ -15,6 +15,8 @@
|
|||||||
using namespace kotlin;
|
using namespace kotlin;
|
||||||
|
|
||||||
using ::testing::_;
|
using ::testing::_;
|
||||||
|
using ::kotlin::test_support::ScopedReportUnhandledExceptionMock;
|
||||||
|
using ::kotlin::test_support::ScopedKotlin_runUnhandledExceptionHookMock;
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
|
|||||||
@@ -12,29 +12,43 @@
|
|||||||
#include "Types.h"
|
#include "Types.h"
|
||||||
#include "Utils.hpp"
|
#include "Utils.hpp"
|
||||||
|
|
||||||
template <class F>
|
namespace kotlin {
|
||||||
class ScopedStrictMockFunction : private kotlin::MoveOnly {
|
namespace test_support {
|
||||||
public:
|
|
||||||
using Mock = testing::StrictMock<testing::MockFunction<F>>;
|
|
||||||
|
|
||||||
explicit ScopedStrictMockFunction(Mock** globalMockLocation) : globalMockLocation_(globalMockLocation) {
|
namespace internal {
|
||||||
RuntimeCheck(globalMockLocation != nullptr, "ScopedStrictMockFunction needs non-null global mock location");
|
|
||||||
RuntimeCheck(*globalMockLocation == nullptr, "ScopedStrictMockFunction needs null global mock");
|
extern testing::MockFunction<KInt()>* createCleanerWorkerMock;
|
||||||
|
extern testing::MockFunction<void(KInt, bool)>* shutdownCleanerWorkerMock;
|
||||||
|
extern testing::MockFunction<void(KRef)>* reportUnhandledExceptionMock;
|
||||||
|
extern testing::MockFunction<void(KRef)>* Kotlin_runUnhandledExceptionHookMock;
|
||||||
|
|
||||||
|
} // namespace internal
|
||||||
|
|
||||||
|
template <class F, bool Strict = true>
|
||||||
|
class ScopedMockFunction : private kotlin::MoveOnly {
|
||||||
|
public:
|
||||||
|
using Mock = typename std::conditional_t<Strict,
|
||||||
|
testing::StrictMock<testing::MockFunction<F>>,
|
||||||
|
testing::NiceMock<testing::MockFunction<F>>>;
|
||||||
|
|
||||||
|
explicit ScopedMockFunction(testing::MockFunction<F>** globalMockLocation) : globalMockLocation_(globalMockLocation) {
|
||||||
|
RuntimeCheck(globalMockLocation != nullptr, "ScopedMockFunction needs non-null global mock location");
|
||||||
|
RuntimeCheck(*globalMockLocation == nullptr, "ScopedMockFunction needs null global mock");
|
||||||
mock_ = make_unique<Mock>();
|
mock_ = make_unique<Mock>();
|
||||||
*globalMockLocation_ = mock_.get();
|
*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;
|
rhs.globalMockLocation_ = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
ScopedStrictMockFunction& operator=(ScopedStrictMockFunction&& rhs) {
|
ScopedMockFunction& operator=(ScopedMockFunction&& rhs) {
|
||||||
ScopedStrictMockFunction tmp(std::move(rhs));
|
ScopedMockFunction tmp(std::move(rhs));
|
||||||
swap(tmp);
|
swap(tmp);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
~ScopedStrictMockFunction() {
|
~ScopedMockFunction() {
|
||||||
if (!globalMockLocation_) return;
|
if (!globalMockLocation_) return;
|
||||||
|
|
||||||
RuntimeCheck(*globalMockLocation_ == mock_.get(), "unexpected global mock location");
|
RuntimeCheck(*globalMockLocation_ == mock_.get(), "unexpected global mock location");
|
||||||
@@ -44,7 +58,7 @@ public:
|
|||||||
*globalMockLocation_ = nullptr;
|
*globalMockLocation_ = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
void swap(ScopedStrictMockFunction& other) {
|
void swap(ScopedMockFunction& other) {
|
||||||
std::swap(globalMockLocation_, other.globalMockLocation_);
|
std::swap(globalMockLocation_, other.globalMockLocation_);
|
||||||
std::swap(mock_, other.mock_);
|
std::swap(mock_, other.mock_);
|
||||||
}
|
}
|
||||||
@@ -54,11 +68,29 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
// Can be null if moved-out of.
|
// Can be null if moved-out of.
|
||||||
Mock** globalMockLocation_;
|
testing::MockFunction<F>** globalMockLocation_;
|
||||||
KStdUniquePtr<Mock> mock_;
|
KStdUniquePtr<Mock> mock_;
|
||||||
};
|
};
|
||||||
|
|
||||||
ScopedStrictMockFunction<KInt()> ScopedCreateCleanerWorkerMock();
|
template<bool Strict = true>
|
||||||
ScopedStrictMockFunction<void(KInt, bool)> ScopedShutdownCleanerWorkerMock();
|
ScopedMockFunction<KInt(), Strict> ScopedCreateCleanerWorkerMock() {
|
||||||
ScopedStrictMockFunction<void(KRef)> ScopedReportUnhandledExceptionMock();
|
return ScopedMockFunction<KInt(), Strict>(&internal::createCleanerWorkerMock);
|
||||||
ScopedStrictMockFunction<void(KRef)> ScopedKotlin_runUnhandledExceptionHookMock();
|
}
|
||||||
|
|
||||||
|
template<bool Strict = true>
|
||||||
|
ScopedMockFunction<void(KInt, bool), Strict> ScopedShutdownCleanerWorkerMock() {
|
||||||
|
return ScopedMockFunction<void(KInt, bool), Strict>(&internal::shutdownCleanerWorkerMock);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<bool Strict = true>
|
||||||
|
ScopedMockFunction<void(KRef), Strict> ScopedReportUnhandledExceptionMock() {
|
||||||
|
return ScopedMockFunction<void(KRef), Strict>(&internal::reportUnhandledExceptionMock);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<bool Strict = true>
|
||||||
|
ScopedMockFunction<void(KRef), Strict> ScopedKotlin_runUnhandledExceptionHookMock() {
|
||||||
|
return ScopedMockFunction<void(KRef), Strict>(&internal::Kotlin_runUnhandledExceptionHookMock);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace test_support
|
||||||
|
} // namespace kotlin
|
||||||
@@ -8,6 +8,16 @@
|
|||||||
#include "ObjectTestSupport.hpp"
|
#include "ObjectTestSupport.hpp"
|
||||||
#include "Types.h"
|
#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<KInt()>* kotlin::test_support::internal::createCleanerWorkerMock = nullptr;
|
||||||
|
testing::MockFunction<void(KInt, bool)>* kotlin::test_support::internal::shutdownCleanerWorkerMock = nullptr;
|
||||||
|
testing::MockFunction<void(KRef)>* kotlin::test_support::internal::reportUnhandledExceptionMock = nullptr;
|
||||||
|
testing::MockFunction<void(KRef)>* kotlin::test_support::internal::Kotlin_runUnhandledExceptionHookMock = nullptr;
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
struct EmptyPayload {
|
struct EmptyPayload {
|
||||||
@@ -49,11 +59,6 @@ struct KBox {
|
|||||||
const T value;
|
const T value;
|
||||||
};
|
};
|
||||||
|
|
||||||
testing::StrictMock<testing::MockFunction<KInt()>>* createCleanerWorkerMock = nullptr;
|
|
||||||
testing::StrictMock<testing::MockFunction<void(KInt, bool)>>* shutdownCleanerWorkerMock = nullptr;
|
|
||||||
testing::StrictMock<testing::MockFunction<void(KRef)>>* reportUnhandledExceptionMock = nullptr;
|
|
||||||
testing::StrictMock<testing::MockFunction<void(KRef)>>* Kotlin_runUnhandledExceptionHookMock = nullptr;
|
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
@@ -285,18 +290,3 @@ KInt Kotlin_CleanerImpl_createCleanerWorker() {
|
|||||||
|
|
||||||
} // extern "C"
|
} // extern "C"
|
||||||
|
|
||||||
ScopedStrictMockFunction<KInt()> ScopedCreateCleanerWorkerMock() {
|
|
||||||
return ScopedStrictMockFunction<KInt()>(&createCleanerWorkerMock);
|
|
||||||
}
|
|
||||||
|
|
||||||
ScopedStrictMockFunction<void(KInt, bool)> ScopedShutdownCleanerWorkerMock() {
|
|
||||||
return ScopedStrictMockFunction<void(KInt, bool)>(&shutdownCleanerWorkerMock);
|
|
||||||
}
|
|
||||||
|
|
||||||
ScopedStrictMockFunction<void(KRef)> ScopedReportUnhandledExceptionMock() {
|
|
||||||
return ScopedStrictMockFunction<void(KRef)>(&reportUnhandledExceptionMock);
|
|
||||||
}
|
|
||||||
|
|
||||||
ScopedStrictMockFunction<void(KRef)> ScopedKotlin_runUnhandledExceptionHookMock() {
|
|
||||||
return ScopedStrictMockFunction<void(KRef)>(&Kotlin_runUnhandledExceptionHookMock);
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user