Improve formatting of Error Logs related to CHIP_ERROR in GoogleTest (#36265)
* Improve formatting of errors related to CHIP_ERROR in GoogleTest
* Improve formatting of errors related to CHIP_ERROR in GoogleTest
* Add a buildconfig header for GoogleTest
* Activating CHIP_ERROR log formatter for pw_fuzzer fuzztests
* Restyled by clang-format
---------
Co-authored-by: Restyled.io <commits@restyled.io>
diff --git a/build/chip/tests.gni b/build/chip/tests.gni
index 0c2c742..57d0311 100755
--- a/build/chip/tests.gni
+++ b/build/chip/tests.gni
@@ -26,6 +26,10 @@
declare_args() {
# Enable building tests.
chip_build_tests = current_os != "freertos"
+
+ # Enabling useful support functions when building using GoogleTest framework (used in unit tests and pw_fuzzer FuzzTests)
+ # For unit tests: this should only be enabled through build_examples.py, see PR #36268
+ chip_build_tests_googletest = false
}
declare_args() {
diff --git a/build/toolchain/pw_fuzzer/BUILD.gn b/build/toolchain/pw_fuzzer/BUILD.gn
index 468a615..b5a751b 100644
--- a/build/toolchain/pw_fuzzer/BUILD.gn
+++ b/build/toolchain/pw_fuzzer/BUILD.gn
@@ -64,6 +64,9 @@
dir_pw_third_party_fuzztest = "//third_party/fuzztest"
dir_pw_third_party_googletest = "$dir_googletest"
+ # Since pw_fuzzer uses GoogleTest, activating this allows us to benefit from supporting functions
+ chip_build_tests_googletest = true
+
# TODO: Seems that re2 support within FuzzTest was deprecated, keeping it defined is triggering warning
# Remove if re2 is indeed not needed
# dir_pw_third_party_re2 = "//third_party/re2/src"
diff --git a/scripts/build/builders/host.py b/scripts/build/builders/host.py
index 114f47f..dbb85f9 100644
--- a/scripts/build/builders/host.py
+++ b/scripts/build/builders/host.py
@@ -502,6 +502,7 @@
self.extra_gn_options.append('import("//build_overrides/googletest.gni")')
self.extra_gn_options.append('pw_unit_test_BACKEND="$dir_pw_unit_test:googletest"')
self.extra_gn_options.append('dir_pw_third_party_googletest="$dir_googletest"')
+ self.extra_gn_options.append('chip_build_tests_googletest=true')
def GnBuildArgs(self):
if self.board == HostBoard.NATIVE:
diff --git a/src/lib/core/BUILD.gn b/src/lib/core/BUILD.gn
index 7068064..5a6e6e1 100644
--- a/src/lib/core/BUILD.gn
+++ b/src/lib/core/BUILD.gn
@@ -71,6 +71,7 @@
"CHIP_CONFIG_TLV_VALIDATE_CHAR_STRING_ON_WRITE=${chip_tlv_validate_char_string_on_write}",
"CHIP_CONFIG_TLV_VALIDATE_CHAR_STRING_ON_READ=${chip_tlv_validate_char_string_on_read}",
"CHIP_CONFIG_COMMAND_SENDER_BUILTIN_SUPPORT_FOR_BATCHED_COMMANDS=${chip_enable_sending_batch_commands}",
+ "CHIP_CONFIG_TEST_GOOGLETEST=${chip_build_tests_googletest}",
]
visibility = [ ":chip_config_header" ]
@@ -116,6 +117,7 @@
public_deps = [
":error",
"$dir_pw_string",
+ "$dir_pw_unit_test",
]
}
diff --git a/src/lib/core/CHIPConfig.h b/src/lib/core/CHIPConfig.h
index 382c634..fca3472 100644
--- a/src/lib/core/CHIPConfig.h
+++ b/src/lib/core/CHIPConfig.h
@@ -1846,5 +1846,16 @@
#endif // CHIP_CONFIG_MAX_BDX_LOG_TRANSFERS
/**
+ * @def CHIP_CONFIG_TEST_GOOGLETEST
+ *
+ * @brief
+ * If asserted (1), enable APIs that support unit tests built with the GoogleTest framework
+ *
+ */
+#ifndef CHIP_CONFIG_TEST_GOOGLETEST
+#define CHIP_CONFIG_TEST_GOOGLETEST 0
+#endif // CHIP_CONFIG_TEST_GOOGLETEST
+
+/**
* @}
*/
diff --git a/src/lib/core/StringBuilderAdapters.cpp b/src/lib/core/StringBuilderAdapters.cpp
index d072c1e..be07ae9 100644
--- a/src/lib/core/StringBuilderAdapters.cpp
+++ b/src/lib/core/StringBuilderAdapters.cpp
@@ -29,3 +29,18 @@
}
} // namespace pw
+
+#if CHIP_CONFIG_TEST_GOOGLETEST
+namespace chip {
+
+void PrintTo(const CHIP_ERROR & err, std::ostream * os)
+{
+ if (CHIP_ERROR::IsSuccess(err))
+ {
+ *os << "CHIP_NO_ERROR";
+ return;
+ }
+ *os << "CHIP_ERROR:<" << err.Format() << ">";
+}
+} // namespace chip
+#endif // CHIP_CONFIG_TEST_GOOGLETEST
diff --git a/src/lib/core/StringBuilderAdapters.h b/src/lib/core/StringBuilderAdapters.h
index f173d56..ad3bfb7 100644
--- a/src/lib/core/StringBuilderAdapters.h
+++ b/src/lib/core/StringBuilderAdapters.h
@@ -42,6 +42,7 @@
/// Actual: CHIP_ERROR:<src/lib/core/TLVReader.cpp:889: Error 0x00000022> == CHIP_NO_ERROR
#include <pw_string/string_builder.h>
+#include <pw_unit_test/framework.h>
#include <lib/core/CHIPError.h>
@@ -51,3 +52,22 @@
StatusWithSize ToString<CHIP_ERROR>(const CHIP_ERROR & err, pw::span<char> buffer);
} // namespace pw
+#if CHIP_CONFIG_TEST_GOOGLETEST
+
+namespace chip {
+
+/// The following function is for usage with GoogleTest.
+/// This implementation of PrintTo allows GoogleTest to print CHIP_ERROR for better logs in the event of a failure.
+/// Example output with PrintTo():
+///
+/// src/lib/core/tests/TestTLV.cpp:382: Failure
+/// Expected equality of these values:
+/// err
+/// Which is: CHIP_ERROR:<src/lib/core/TLVWriter.cpp:674: Error 0x00000024>
+/// CHIP_ERROR(0, "src/lib/core/tests/TestTLV.cpp", 382)
+/// Which is: CHIP_NO_ERROR
+///
+/// This enhances the readability and diagnostic information in GoogleTest test logs.
+void PrintTo(const CHIP_ERROR & err, std::ostream * os);
+} // namespace chip
+#endif // CHIP_CONFIG_TEST_GOOGLETEST