Skip to content

Commit

Permalink
Improve formatting of errors related to CHIP_ERROR in GoogleTest
Browse files Browse the repository at this point in the history
  • Loading branch information
Alami-Amine committed Oct 28, 2024
1 parent f1457a2 commit 654741a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib/core/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ source_set("string-builder-adapters") {
public_deps = [
":error",
"$dir_pw_string",
"$dir_pw_unit_test",
]
}

Expand Down
13 changes: 13 additions & 0 deletions src/lib/core/StringBuilderAdapters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,16 @@ StatusWithSize ToString<CHIP_ERROR>(const CHIP_ERROR & err, pw::span<char> buffe
}

} // namespace pw

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
18 changes: 18 additions & 0 deletions src/lib/core/StringBuilderAdapters.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand All @@ -51,3 +52,20 @@ template <>
StatusWithSize ToString<CHIP_ERROR>(const CHIP_ERROR & err, pw::span<char> buffer);

} // namespace pw

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

0 comments on commit 654741a

Please sign in to comment.