Skip to content

Commit c021fad

Browse files
Add a pigweed-string-format adapter for CHIP_ERROR since that makes unit test debugability a LOT better (project-chip#33505)
* Define StringBuilderAdapters * Make use of string builder adapters, switch gtest to pw unittest framework * Restyle * Give explicit examples of how string adapters help * Place implementation of ToString in a cpp file * Add missing file --------- Co-authored-by: Andrei Litvin <andreilitvin@google.com>
1 parent 7a470f5 commit c021fad

8 files changed

+108
-5
lines changed

src/lib/core/BUILD.gn

+13
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import("//build_overrides/build.gni")
1616
import("//build_overrides/chip.gni")
1717
import("//build_overrides/nlio.gni")
18+
import("//build_overrides/pigweed.gni")
1819

1920
import("${chip_root}/build/chip/buildconfig_header.gni")
2021
import("${chip_root}/build/chip/tests.gni")
@@ -106,6 +107,18 @@ source_set("error") {
106107
]
107108
}
108109

110+
source_set("string-builder-adapters") {
111+
sources = [
112+
"StringBuilderAdapters.cpp",
113+
"StringBuilderAdapters.h",
114+
]
115+
116+
public_deps = [
117+
":error",
118+
"$dir_pw_string",
119+
]
120+
}
121+
109122
source_set("encoding") {
110123
sources = [ "CHIPEncoding.h" ]
111124
public_deps = [ "${nlio_root}:nlio" ]
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright (c) 2024 Project CHIP Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
#include <lib/core/StringBuilderAdapters.h>
17+
18+
namespace pw {
19+
20+
template <>
21+
StatusWithSize ToString<CHIP_ERROR>(const CHIP_ERROR & err, pw::span<char> buffer)
22+
{
23+
if (CHIP_ERROR::IsSuccess(err))
24+
{
25+
// source location probably does not matter
26+
return pw::string::Format(buffer, "CHIP_NO_ERROR");
27+
}
28+
return pw::string::Format(buffer, "CHIP_ERROR:<%" CHIP_ERROR_FORMAT ">", err.Format());
29+
}
30+
31+
} // namespace pw

src/lib/core/StringBuilderAdapters.h

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright (c) 2024 Project CHIP Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
#pragma once
17+
18+
/// This header includes pigweed stringbuilder adaptations for various chip types.
19+
/// You can see https://pigweed.dev/pw_string/guide.html as a reference.
20+
///
21+
/// In particular, pigweed code generally looks like:
22+
///
23+
/// pw::StringBuffer<42> sb;
24+
/// sb << "Here is a value: ";
25+
/// sb << value;
26+
///
27+
/// Where specific formatters exist for "value". In particular these are used when
28+
/// reporting unit test assertions such as ASSERT_EQ/EXPECT_EQ so if you write code
29+
/// like:
30+
///
31+
/// ASSERT_EQ(SomeCall(), CHIP_NO_ERROR);
32+
///
33+
/// On failure without adapters, the objects are reported as "24-byte object at 0x....."
34+
/// which is not as helpful as a full CHIP_ERROR formatted output.
35+
///
36+
/// Example output WITHOUT the adapters
37+
/// Expected: .... == CHIP_ERROR(0, "src/setup_payload/tests/TestAdditionalDataPayload.cpp", 234)
38+
/// Actual: <24-byte object at 0x7ffe39510b80> == <24-byte object at 0x7ffe39510ba0>
39+
///
40+
/// Example output WITH the adapters:
41+
/// Expected: .... == CHIP_ERROR(0, "src/setup_payload/tests/TestAdditionalDataPayload.cpp", 234)
42+
/// Actual: CHIP_ERROR:<src/lib/core/TLVReader.cpp:889: Error 0x00000022> == CHIP_NO_ERROR
43+
44+
#include <pw_string/string_builder.h>
45+
46+
#include <lib/core/CHIPError.h>
47+
48+
namespace pw {
49+
50+
template <>
51+
StatusWithSize ToString<CHIP_ERROR>(const CHIP_ERROR & err, pw::span<char> buffer);
52+
53+
} // namespace pw

src/setup_payload/tests/BUILD.gn

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ chip_test_suite("tests") {
3434
cflags = [ "-Wconversion" ]
3535

3636
public_deps = [
37+
"${chip_root}/src/lib/core:string-builder-adapters",
3738
"${chip_root}/src/platform",
3839
"${chip_root}/src/setup_payload",
3940
]

src/setup_payload/tests/TestAdditionalDataPayload.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
#include <gtest/gtest.h>
3030

31+
#include <lib/core/StringBuilderAdapters.h>
3132
#include <lib/support/BytesToHex.h>
3233
#include <lib/support/CHIPMem.h>
3334
#include <lib/support/CHIPMemString.h>

src/setup_payload/tests/TestManualCode.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@
2222
*
2323
*/
2424

25-
#include <gtest/gtest.h>
25+
#include <pw_unit_test/framework.h>
2626
#include <stdio.h>
2727

28+
#include <lib/core/StringBuilderAdapters.h>
29+
#include <lib/support/verhoeff/Verhoeff.h>
2830
#include <setup_payload/ManualSetupPayloadGenerator.h>
2931
#include <setup_payload/ManualSetupPayloadParser.h>
3032
#include <setup_payload/SetupPayload.h>
3133

32-
#include <lib/support/verhoeff/Verhoeff.h>
33-
3434
#include <algorithm>
3535
#include <math.h>
3636
#include <string>

src/setup_payload/tests/TestQRCode.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626

2727
#include <nlbyteorder.h>
2828

29-
#include <gtest/gtest.h>
29+
#include <pw_unit_test/framework.h>
30+
31+
#include <lib/core/StringBuilderAdapters.h>
3032
#include <lib/support/Span.h>
3133

3234
using namespace chip;

src/setup_payload/tests/TestQRCodeTLV.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616
*/
1717
#include "TestHelpers.h"
1818

19-
#include <gtest/gtest.h>
19+
#include <pw_unit_test/framework.h>
20+
2021
#include <nlbyteorder.h>
2122

23+
#include <lib/core/StringBuilderAdapters.h>
2224
#include <lib/support/ScopedBuffer.h>
2325

2426
using namespace chip;

0 commit comments

Comments
 (0)