aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/testing/gtest
diff options
context:
space:
mode:
authorrobot-piglet <robot-piglet@yandex-team.com>2023-02-13 19:01:57 +0300
committerrobot-piglet <robot-piglet@yandex-team.com>2023-02-13 19:01:57 +0300
commitb5b3d520c01646610f376c55455d9cad735e3e0d (patch)
tree0e113dcc316476655a9457eec509b5a1ebd967ed /library/cpp/testing/gtest
parentb4781eb14f1b19ff7bb90d703ebf9ad470aa1e89 (diff)
downloadydb-b5b3d520c01646610f376c55455d9cad735e3e0d.tar.gz
Intermediate changes
Diffstat (limited to 'library/cpp/testing/gtest')
-rw-r--r--library/cpp/testing/gtest/gtest.h2
-rw-r--r--library/cpp/testing/gtest/matchers.cpp23
-rw-r--r--library/cpp/testing/gtest/matchers.h40
3 files changed, 65 insertions, 0 deletions
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;
+ }
+}