Teach googletest how to pretty-print ByteString
Add a test-only PrintTo() function for ByteString.
Change-Id: I98ca3e602cff84eb634cd5d78640c40865060807
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/106690
Reviewed-by: K. Moon <kmoon@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/core/fxcrt/BUILD.gn b/core/fxcrt/BUILD.gn
index 09400e8..f91be3c 100644
--- a/core/fxcrt/BUILD.gn
+++ b/core/fxcrt/BUILD.gn
@@ -162,6 +162,13 @@
}
}
+source_set("test_support") {
+ testonly = true
+ sources = [ "string_test_support.cpp" ]
+ configs += [ "../../:pdfium_strict_config" ]
+ deps = [ ":fxcrt" ]
+}
+
source_set("unit_test_support") {
testonly = true
sources = [
@@ -171,6 +178,7 @@
configs += [ "../../:pdfium_strict_config" ]
deps = [
":fxcrt",
+ ":test_support",
"//testing/gtest",
]
}
diff --git a/core/fxcrt/bytestring.h b/core/fxcrt/bytestring.h
index 5f1ed53..8bf13e4 100644
--- a/core/fxcrt/bytestring.h
+++ b/core/fxcrt/bytestring.h
@@ -286,6 +286,13 @@
std::ostream& operator<<(std::ostream& os, const ByteString& str);
std::ostream& operator<<(std::ostream& os, ByteStringView str);
+// This is declared here for use in gtest-based tests but is defined in a test
+// support target. This should not be used in production code. Just use
+// operator<< from above instead.
+// In some cases, gtest will automatically use operator<< as well, but in this
+// case, it needs PrintTo() because ByteString looks like a container to gtest.
+void PrintTo(const ByteString& str, std::ostream* os);
+
} // namespace fxcrt
using ByteString = fxcrt::ByteString;
diff --git a/core/fxcrt/string_test_support.cpp b/core/fxcrt/string_test_support.cpp
new file mode 100644
index 0000000..254e37a
--- /dev/null
+++ b/core/fxcrt/string_test_support.cpp
@@ -0,0 +1,15 @@
+// Copyright 2023 The PDFium Authors
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include <ostream>
+
+#include "core/fxcrt/bytestring.h"
+
+namespace fxcrt {
+
+void PrintTo(const ByteString& str, std::ostream* os) {
+ *os << str;
+}
+
+} // namespace fxcrt
diff --git a/fpdfsdk/BUILD.gn b/fpdfsdk/BUILD.gn
index b97d881..b3dff66 100644
--- a/fpdfsdk/BUILD.gn
+++ b/fpdfsdk/BUILD.gn
@@ -159,6 +159,7 @@
"../core/fpdfapi/font",
"../core/fpdfapi/page",
"../core/fpdfapi/parser",
+ "../core/fxcrt:test_support",
"../core/fxge",
]
pdfium_root_dir = "../"