From 4e78187c66e333a8c9af7ef9284554f2eaf728c7 Mon Sep 17 00:00:00 2001 From: Alami-Amine Date: Mon, 28 Oct 2024 12:20:22 +0100 Subject: [PATCH] Improve formatting of errors related to CHIP_ERROR in GoogleTest --- src/lib/core/BUILD.gn | 1 + src/lib/core/StringBuilderAdapters.cpp | 16 ++++++++++++++++ src/lib/core/StringBuilderAdapters.h | 21 +++++++++++++++++++++ 3 files changed, 38 insertions(+) diff --git a/src/lib/core/BUILD.gn b/src/lib/core/BUILD.gn index 70680640b025b2..0a5d4c4a7bc9a0 100644 --- a/src/lib/core/BUILD.gn +++ b/src/lib/core/BUILD.gn @@ -116,6 +116,7 @@ source_set("string-builder-adapters") { public_deps = [ ":error", "$dir_pw_string", + "$dir_pw_unit_test", ] } diff --git a/src/lib/core/StringBuilderAdapters.cpp b/src/lib/core/StringBuilderAdapters.cpp index d072c1ee905ab7..2de6ca3c0b2494 100644 --- a/src/lib/core/StringBuilderAdapters.cpp +++ b/src/lib/core/StringBuilderAdapters.cpp @@ -29,3 +29,19 @@ StatusWithSize ToString(const CHIP_ERROR & err, pw::span buffe } } // namespace pw + +#if CHIP_DEVICE_LAYER_TARGET_LINUX || CHIP_DEVICE_LAYER_TARGET_DARWIN || CHIP_DEVICE_LAYER_TARGET_TIZEN || \ + CHIP_DEVICE_LAYER_TARGET_ANDROID +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() << ">"; +} +#endif +} // namespace chip diff --git a/src/lib/core/StringBuilderAdapters.h b/src/lib/core/StringBuilderAdapters.h index f173d56b46e6a1..44550b17279dda 100644 --- a/src/lib/core/StringBuilderAdapters.h +++ b/src/lib/core/StringBuilderAdapters.h @@ -42,6 +42,7 @@ /// Actual: CHIP_ERROR: == CHIP_NO_ERROR #include +#include #include @@ -51,3 +52,23 @@ template <> StatusWithSize ToString(const CHIP_ERROR & err, pw::span buffer); } // namespace pw +#if CHIP_DEVICE_LAYER_TARGET_LINUX || CHIP_DEVICE_LAYER_TARGET_DARWIN || CHIP_DEVICE_LAYER_TARGET_TIZEN || \ + CHIP_DEVICE_LAYER_TARGET_ANDROID + +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: +/// 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