diff options
author | robot-piglet <robot-piglet@yandex-team.com> | 2023-02-13 19:01:57 +0300 |
---|---|---|
committer | robot-piglet <robot-piglet@yandex-team.com> | 2023-02-13 19:01:57 +0300 |
commit | b5b3d520c01646610f376c55455d9cad735e3e0d (patch) | |
tree | 0e113dcc316476655a9457eec509b5a1ebd967ed | |
parent | b4781eb14f1b19ff7bb90d703ebf9ad470aa1e89 (diff) | |
download | ydb-b5b3d520c01646610f376c55455d9cad735e3e0d.tar.gz |
Intermediate changes
-rw-r--r-- | library/cpp/testing/README.md | 2 | ||||
-rw-r--r-- | library/cpp/testing/gtest/gtest.h | 2 | ||||
-rw-r--r-- | library/cpp/testing/gtest/matchers.cpp | 23 | ||||
-rw-r--r-- | library/cpp/testing/gtest/matchers.h | 40 |
4 files changed, 66 insertions, 1 deletions
diff --git a/library/cpp/testing/README.md b/library/cpp/testing/README.md index 2151cb1bdb..8c6768fa68 100644 --- a/library/cpp/testing/README.md +++ b/library/cpp/testing/README.md @@ -7,7 +7,7 @@ * `gmock` — прокси-библиотека для подключения `contrib/resticted/googltest/googlemock` без нарушения PEERDIR policy. * `gtest` — реализация модуля `GTEST` — средства для интеграции фреймворка _googletest_ в Аркадию. * `gtest_boost_extensions` — расширения gtest и gmock, улучшающие поддержку типов из boost. -* `gtest_extensions` — расширения gtest и gmock, улучшающие поддержку Аркадийных типов. Все расширения включены в модуле `GTEST` по-умолчаниiю. +* `gtest_extensions` — расширения gtest и gmock, улучшающие поддержку Аркадийных типов. Все расширения включены в модуле `GTEST` по-умолчанию. * `gtest_main` — реализация `int main(argc, argv)` для модуля `GTEST` (вынесена в отдельную библиотеку, чтобы в перспективе была возможна реализация `GTEST_WITH_CUSTOM_ENTRY_POINT`). * `gtest_protobuf` — утилиты для работы с протобуфом в тестах. * `hook` — хуки для выполнения пользовательских функций в тестах и бенчмарках. diff --git a/library/cpp/testing/gtest/gtest.h b/library/cpp/testing/gtest/gtest.h index 4ef30c3ada..a4d0c775d0 100644 --- a/library/cpp/testing/gtest/gtest.h +++ b/library/cpp/testing/gtest/gtest.h @@ -1,5 +1,7 @@ #pragma once +#include <library/cpp/testing/gtest/matchers.h> + #include <library/cpp/testing/gtest_extensions/gtest_extensions.h> #include <gtest/gtest.h> diff --git a/library/cpp/testing/gtest/matchers.cpp b/library/cpp/testing/gtest/matchers.cpp new file mode 100644 index 0000000000..421de85da5 --- /dev/null +++ b/library/cpp/testing/gtest/matchers.cpp @@ -0,0 +1,23 @@ +#include <library/cpp/testing/gtest/matchers.h> + +#include <library/cpp/testing/common/env.h> + +#include <util/folder/path.h> +#include <util/stream/file.h> +#include <util/system/fs.h> + +bool NGTest::NDetail::MatchOrUpdateGolden(std::string_view actualContent, const TString& goldenFilename) { + if (!GetTestParam("GTEST_UPDATE_GOLDEN").empty()) { + Y_ENSURE(NFs::MakeDirectoryRecursive(TFsPath(goldenFilename).Parent())); + TFile file(goldenFilename, CreateAlways); + file.Write(actualContent.data(), actualContent.size()); + Cerr << "The data[" << actualContent.size() << "] has written to golden file " << goldenFilename << Endl; + return true; + } + + if (!NFs::Exists(goldenFilename)) { + return actualContent.empty(); + } + TFile goldenFile(goldenFilename, RdOnly); + return actualContent == TFileInput(goldenFile).ReadAll(); +} diff --git a/library/cpp/testing/gtest/matchers.h b/library/cpp/testing/gtest/matchers.h new file mode 100644 index 0000000000..0db0597304 --- /dev/null +++ b/library/cpp/testing/gtest/matchers.h @@ -0,0 +1,40 @@ +#pragma once + +#include <util/generic/string.h> + +#include <gmock/gmock.h> + +namespace NGTest { + namespace NDetail { + [[nodiscard]] bool MatchOrUpdateGolden(std::string_view actualContent, const TString& goldenFilename); + } + + /** + * Matches a string or std::vector<char> equal to the specified file content. + * The file must be brought to the test using macro DATA in ya.make. + * + * The matcher is able to update the file by the actual content during + * the special test run with the argument '--test-param GTEST_UPDATE_GOLDEN=1'. + * Any change in such files should be added to VCS manually. + * + * The matcher should not be used for a binary data. Use it for a content whose + * diff will be visual during a code review: text, config, image. + * + * Example: + * ``` + * TEST(Suite, Name) { + * std::string data = RenderSomeTextData(); + * EXPECT_THAT(data, NGTest::GoldenFileEq(SRC_("golden/data.txt"))); + * } + * ``` + */ + MATCHER_P(GoldenFileEq, filename, "") + { + if (!NDetail::MatchOrUpdateGolden(std::string_view(arg.data(), arg.size()), TString(filename))) { + *result_listener + << "\nCall `ya m -rA --test-param GTEST_UPDATE_GOLDEN=1` to update the golden file"; + return false; + } + return true; + } +} |