aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/testing/hook
diff options
context:
space:
mode:
authorDevtools Arcadia <arcadia-devtools@yandex-team.ru>2022-02-07 18:08:42 +0300
committerDevtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net>2022-02-07 18:08:42 +0300
commit1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch)
treee26c9fed0de5d9873cce7e00bc214573dc2195b7 /library/cpp/testing/hook
downloadydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'library/cpp/testing/hook')
-rw-r--r--library/cpp/testing/hook/README.md25
-rw-r--r--library/cpp/testing/hook/hook.cpp45
-rw-r--r--library/cpp/testing/hook/hook.h128
-rw-r--r--library/cpp/testing/hook/ya.make13
4 files changed, 211 insertions, 0 deletions
diff --git a/library/cpp/testing/hook/README.md b/library/cpp/testing/hook/README.md
new file mode 100644
index 00000000000..fac8b32ef5c
--- /dev/null
+++ b/library/cpp/testing/hook/README.md
@@ -0,0 +1,25 @@
+# Hook for google benchmark and gtest
+
+Y_TEST_HOOK_BEFORE_INIT - вызывается перед инициализацией соотвествующего фреймворка
+Y_TEST_HOOK_BEFORE_RUN - вызывается перед запуском тестов
+Y_TEST_HOOK_AFTER_RUN - вызывается всегда после завершения выполнения тестов,
+ если этап инициализации был успешным
+
+## Примеры:
+
+```
+Y_TEST_HOOK_BEFORE_INIT(SetupMyApp) {
+ // ваш код для выполнения перед инициализацией фреймворка
+}
+
+Y_TEST_HOOK_BEFORE_RUN(InitMyApp) {
+ // ваш код для выполнения перед запуском тестов
+}
+
+Y_TEST_HOOK_AFTER_RUN(CleanMyApp) {
+ // ваш код для выполнения после завершения тестов
+}
+```
+
+## Тесты:
+тесты лаунчерах соотвествующих фреймворков (gtest, gbenchmark и unittest)
diff --git a/library/cpp/testing/hook/hook.cpp b/library/cpp/testing/hook/hook.cpp
new file mode 100644
index 00000000000..b3c599da890
--- /dev/null
+++ b/library/cpp/testing/hook/hook.cpp
@@ -0,0 +1,45 @@
+#include "hook.h"
+
+namespace {
+ NTesting::THook* BeforeInitHead = nullptr;
+ NTesting::THook* BeforeRunHead = nullptr;
+ NTesting::THook* AfterRunHead = nullptr;
+
+ void RegisterHook(NTesting::THook*& head, NTesting::THook* hook) {
+ hook->Next = head;
+ head = hook;
+ }
+
+ void CallHooks(NTesting::THook* head) {
+ while (nullptr != head) {
+ if (nullptr != head->Fn) {
+ (*head->Fn)();
+ }
+ head = head->Next;
+ }
+ }
+}
+
+void NTesting::THook::RegisterBeforeInit(NTesting::THook* hook) noexcept {
+ RegisterHook(BeforeInitHead, hook);
+}
+
+void NTesting::THook::CallBeforeInit() {
+ CallHooks(BeforeInitHead);
+}
+
+void NTesting::THook::RegisterBeforeRun(NTesting::THook* hook) noexcept {
+ RegisterHook(BeforeRunHead, hook);
+}
+
+void NTesting::THook::CallBeforeRun() {
+ CallHooks(BeforeRunHead);
+}
+
+void NTesting::THook::RegisterAfterRun(NTesting::THook* hook) noexcept {
+ RegisterHook(AfterRunHead, hook);
+}
+
+void NTesting::THook::CallAfterRun() {
+ CallHooks(AfterRunHead);
+}
diff --git a/library/cpp/testing/hook/hook.h b/library/cpp/testing/hook/hook.h
new file mode 100644
index 00000000000..c45289cb22c
--- /dev/null
+++ b/library/cpp/testing/hook/hook.h
@@ -0,0 +1,128 @@
+#pragma once
+
+namespace NTesting {
+ /**
+ * Hook class and registration system.
+ *
+ * Default implementation of the `main` function for G_BENCHMARK and GTEST calls these hooks when executing.
+ * This is a useful feature if you want to customize behaviour of the `main` function,
+ * but you don't want to write `main` yourself.
+ *
+ * Hooks form an intrusive linked list that's built at application startup. Note that hooks execute
+ * in arbitrary order.
+ *
+ * Use macros below to define hooks.
+ */
+ struct THook {
+ using TFn = void (*)();
+
+ TFn Fn = nullptr;
+ THook* Next = nullptr;
+
+ static void RegisterBeforeInit(THook* hook) noexcept;
+
+ static void CallBeforeInit();
+
+ struct TRegisterBeforeInit {
+ explicit TRegisterBeforeInit(THook* hook) noexcept {
+ THook::RegisterBeforeInit(hook);
+ }
+ };
+
+ static void RegisterBeforeRun(THook* hook) noexcept;
+
+ static void CallBeforeRun();
+
+ struct TRegisterBeforeRun {
+ explicit TRegisterBeforeRun(THook* hook) noexcept {
+ THook::RegisterBeforeRun(hook);
+ }
+ };
+
+ static void RegisterAfterRun(THook* hook) noexcept;
+
+ static void CallAfterRun();
+
+ struct TRegisterAfterRun {
+ explicit TRegisterAfterRun(THook* hook) noexcept {
+ THook::RegisterAfterRun(hook);
+ }
+ };
+ };
+
+ /**
+ * Called right before initializing test programm
+ *
+ * This hook is intended for setting up default parameters. If you're doing initialization, consider
+ * using `Y_TEST_HOOK_BEFORE_RUN` instead.
+ *
+ * *Note:* hooks execute in arbitrary order.
+ *
+ *
+ * # Usage
+ *
+ * Instantiate this class in a cpp file. Pass a unique name for your hook,
+ * implement body right after macro instantiation:
+ *
+ * ```
+ * Y_TEST_HOOK_BEFORE_INIT(SetupParams) {
+ * // hook code
+ * }
+ * ```
+ */
+#define Y_TEST_HOOK_BEFORE_INIT(N) \
+ void N(); \
+ ::NTesting::THook N##Hook{&N, nullptr}; \
+ ::NTesting::THook::TRegisterBeforeInit N##HookReg{&N##Hook}; \
+ void N()
+
+ /**
+ * Called right before launching tests.
+ *
+ * Hooks execute in arbitrary order. As such, we recommend using this hook to set up an event listener,
+ * and performing initialization and cleanup in the corresponding event handlers. This is better than performing
+ * initialization and cleanup directly in the hook's code because it gives more control over
+ * order in which initialization is performed.
+ *
+ *
+ * # Usage
+ *
+ * Instantiate this class in a cpp file. Pass a unique name for your hook,
+ * implement body right after macro instantiation:
+ *
+ * ```
+ * Y_TEST_HOOK_BEFORE_RUN(InitMyApp) {
+ * // hook code
+ * }
+ * ```
+ */
+#define Y_TEST_HOOK_BEFORE_RUN(N) \
+ void N(); \
+ ::NTesting::THook N##Hook{&N, nullptr}; \
+ ::NTesting::THook::TRegisterBeforeRun N##HookReg{&N##Hook}; \
+ void N()
+
+ /**
+ * Called after all tests has finished, just before program exit.
+ *
+ * This hook is intended for simple cleanup routines that don't care about order in which hooks are executed.
+ * For more complex cases, we recommend using `Y_TEST_HOOK_BEFORE_RUN`.
+ *
+ *
+ * # Usage
+ *
+ * Instantiate this class in a cpp file. Pass a unique name for your hook,
+ * implement body right after macro instantiation:
+ *
+ * ```
+ * Y_TEST_HOOK_AFTER_RUN(StopMyApp) {
+ * // hook code
+ * }
+ * ```
+ */
+#define Y_TEST_HOOK_AFTER_RUN(N) \
+ void N(); \
+ ::NTesting::THook N##Hook{&N, nullptr}; \
+ ::NTesting::THook::TRegisterAfterRun N##HookReg{&N##Hook}; \
+ void N()
+}
diff --git a/library/cpp/testing/hook/ya.make b/library/cpp/testing/hook/ya.make
new file mode 100644
index 00000000000..db58f4e0ae8
--- /dev/null
+++ b/library/cpp/testing/hook/ya.make
@@ -0,0 +1,13 @@
+LIBRARY()
+OWNER(
+ amatanhead
+ bulatman
+ thegeorg
+ g:cpp-contrib
+)
+
+SRCS(
+ hook.cpp
+)
+
+END()