Skip to content

Commit d93f3f6

Browse files
Improve formatting of Error Logs related to CHIP_ERROR in GoogleTest (project-chip#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>
1 parent bc83fc2 commit d93f3f6

File tree

7 files changed

+56
-0
lines changed

7 files changed

+56
-0
lines changed

build/chip/tests.gni

+4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ declare_args() {
2626
declare_args() {
2727
# Enable building tests.
2828
chip_build_tests = current_os != "freertos"
29+
30+
# Enabling useful support functions when building using GoogleTest framework (used in unit tests and pw_fuzzer FuzzTests)
31+
# For unit tests: this should only be enabled through build_examples.py, see PR #36268
32+
chip_build_tests_googletest = false
2933
}
3034

3135
declare_args() {

build/toolchain/pw_fuzzer/BUILD.gn

+3
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ gcc_toolchain("chip_pw_fuzztest") {
6464
dir_pw_third_party_fuzztest = "//third_party/fuzztest"
6565
dir_pw_third_party_googletest = "$dir_googletest"
6666

67+
# Since pw_fuzzer uses GoogleTest, activating this allows us to benefit from supporting functions
68+
chip_build_tests_googletest = true
69+
6770
# TODO: Seems that re2 support within FuzzTest was deprecated, keeping it defined is triggering warning
6871
# Remove if re2 is indeed not needed
6972
# dir_pw_third_party_re2 = "//third_party/re2/src"

scripts/build/builders/host.py

+1
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,7 @@ def __init__(self, root, runner, app: HostApp, board=HostBoard.NATIVE,
502502
self.extra_gn_options.append('import("//build_overrides/googletest.gni")')
503503
self.extra_gn_options.append('pw_unit_test_BACKEND="$dir_pw_unit_test:googletest"')
504504
self.extra_gn_options.append('dir_pw_third_party_googletest="$dir_googletest"')
505+
self.extra_gn_options.append('chip_build_tests_googletest=true')
505506

506507
def GnBuildArgs(self):
507508
if self.board == HostBoard.NATIVE:

src/lib/core/BUILD.gn

+2
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ buildconfig_header("chip_buildconfig") {
7171
"CHIP_CONFIG_TLV_VALIDATE_CHAR_STRING_ON_WRITE=${chip_tlv_validate_char_string_on_write}",
7272
"CHIP_CONFIG_TLV_VALIDATE_CHAR_STRING_ON_READ=${chip_tlv_validate_char_string_on_read}",
7373
"CHIP_CONFIG_COMMAND_SENDER_BUILTIN_SUPPORT_FOR_BATCHED_COMMANDS=${chip_enable_sending_batch_commands}",
74+
"CHIP_CONFIG_TEST_GOOGLETEST=${chip_build_tests_googletest}",
7475
]
7576

7677
visibility = [ ":chip_config_header" ]
@@ -116,6 +117,7 @@ source_set("string-builder-adapters") {
116117
public_deps = [
117118
":error",
118119
"$dir_pw_string",
120+
"$dir_pw_unit_test",
119121
]
120122
}
121123

src/lib/core/CHIPConfig.h

+11
Original file line numberDiff line numberDiff line change
@@ -1845,6 +1845,17 @@ extern const char CHIP_NON_PRODUCTION_MARKER[];
18451845
#define CHIP_CONFIG_MAX_BDX_LOG_TRANSFERS 5
18461846
#endif // CHIP_CONFIG_MAX_BDX_LOG_TRANSFERS
18471847

1848+
/**
1849+
* @def CHIP_CONFIG_TEST_GOOGLETEST
1850+
*
1851+
* @brief
1852+
* If asserted (1), enable APIs that support unit tests built with the GoogleTest framework
1853+
*
1854+
*/
1855+
#ifndef CHIP_CONFIG_TEST_GOOGLETEST
1856+
#define CHIP_CONFIG_TEST_GOOGLETEST 0
1857+
#endif // CHIP_CONFIG_TEST_GOOGLETEST
1858+
18481859
/**
18491860
* @}
18501861
*/

src/lib/core/StringBuilderAdapters.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,18 @@ StatusWithSize ToString<CHIP_ERROR>(const CHIP_ERROR & err, pw::span<char> buffe
2929
}
3030

3131
} // namespace pw
32+
33+
#if CHIP_CONFIG_TEST_GOOGLETEST
34+
namespace chip {
35+
36+
void PrintTo(const CHIP_ERROR & err, std::ostream * os)
37+
{
38+
if (CHIP_ERROR::IsSuccess(err))
39+
{
40+
*os << "CHIP_NO_ERROR";
41+
return;
42+
}
43+
*os << "CHIP_ERROR:<" << err.Format() << ">";
44+
}
45+
} // namespace chip
46+
#endif // CHIP_CONFIG_TEST_GOOGLETEST

src/lib/core/StringBuilderAdapters.h

+20
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
/// Actual: CHIP_ERROR:<src/lib/core/TLVReader.cpp:889: Error 0x00000022> == CHIP_NO_ERROR
4343

4444
#include <pw_string/string_builder.h>
45+
#include <pw_unit_test/framework.h>
4546

4647
#include <lib/core/CHIPError.h>
4748

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

5354
} // namespace pw
55+
#if CHIP_CONFIG_TEST_GOOGLETEST
56+
57+
namespace chip {
58+
59+
/// The following function is for usage with GoogleTest.
60+
/// This implementation of PrintTo allows GoogleTest to print CHIP_ERROR for better logs in the event of a failure.
61+
/// Example output with PrintTo():
62+
///
63+
/// src/lib/core/tests/TestTLV.cpp:382: Failure
64+
/// Expected equality of these values:
65+
/// err
66+
/// Which is: CHIP_ERROR:<src/lib/core/TLVWriter.cpp:674: Error 0x00000024>
67+
/// CHIP_ERROR(0, "src/lib/core/tests/TestTLV.cpp", 382)
68+
/// Which is: CHIP_NO_ERROR
69+
///
70+
/// This enhances the readability and diagnostic information in GoogleTest test logs.
71+
void PrintTo(const CHIP_ERROR & err, std::ostream * os);
72+
} // namespace chip
73+
#endif // CHIP_CONFIG_TEST_GOOGLETEST

0 commit comments

Comments
 (0)