@@ -170,6 +170,31 @@ static const CHIP_ERROR kTestElements[] =
170
170
};
171
171
// clang-format on
172
172
173
+ void CheckCoreErrorStrHelper (const char * errStr, CHIP_ERROR err)
174
+ {
175
+ char expectedText[9 ];
176
+
177
+ // Assert that the error string contains the error number in hex.
178
+ snprintf (expectedText, sizeof (expectedText), " %08" PRIX32, static_cast <uint32_t >(err.AsInteger ()));
179
+ EXPECT_TRUE ((strstr (errStr, expectedText) != nullptr ));
180
+
181
+ #if !CHIP_CONFIG_SHORT_ERROR_STR
182
+ // Assert that the error string contains a description, which is signaled
183
+ // by a presence of a colon proceeding the description.
184
+ EXPECT_TRUE ((strchr (errStr, ' :' ) != nullptr ));
185
+ #endif // !CHIP_CONFIG_SHORT_ERROR_STR
186
+
187
+ #if CHIP_CONFIG_ERROR_SOURCE
188
+ // GetFile() should be relative to ${chip_root}
189
+ char const * const file = err.GetFile ();
190
+ ASSERT_NE (file, nullptr );
191
+ EXPECT_EQ (strstr (file, " src/lib/core/" ), file);
192
+
193
+ // File should be included in the error.
194
+ EXPECT_NE (strstr (errStr, file), nullptr );
195
+ #endif // CHIP_CONFIG_ERROR_SOURCE
196
+ }
197
+
173
198
TEST (TestCHIPErrorStr, CheckCoreErrorStr)
174
199
{
175
200
// Register the layer error formatter
@@ -179,29 +204,46 @@ TEST(TestCHIPErrorStr, CheckCoreErrorStr)
179
204
// For each defined error...
180
205
for (const auto & err : kTestElements )
181
206
{
182
- const char * errStr = ErrorStr (err);
183
- char expectedText[9 ];
207
+ // ErrorStr with static char array.
208
+ CheckCoreErrorStrHelper (ErrorStr (err, /* withSourceLocation=*/ true ), err);
209
+ }
210
+ }
211
+
212
+ TEST (TestCHIPErrorStr, CheckCoreErrorStrStorage)
213
+ {
214
+ // Register the layer error formatter
215
+
216
+ RegisterCHIPLayerErrorFormatter ();
217
+
218
+ // For each defined error...
219
+ for (const auto & err : kTestElements )
220
+ {
221
+ // ErrorStr with given storage.
222
+ ErrorStrStorage storage;
223
+ CheckCoreErrorStrHelper (ErrorStr (err, /* withSourceLocation=*/ true , storage), err);
224
+ }
225
+ }
226
+
227
+ void CheckCoreErrorStrWithoutSourceLocationHelper (const char * errStr, CHIP_ERROR err)
228
+ {
229
+ char expectedText[9 ];
184
230
185
- // Assert that the error string contains the error number in hex.
186
- snprintf (expectedText, sizeof (expectedText), " %08" PRIX32, static_cast <uint32_t >(err.AsInteger ()));
187
- EXPECT_TRUE ((strstr (errStr, expectedText) != nullptr ));
231
+ // Assert that the error string contains the error number in hex.
232
+ snprintf (expectedText, sizeof (expectedText), " %08" PRIX32, static_cast <uint32_t >(err.AsInteger ()));
233
+ EXPECT_TRUE ((strstr (errStr, expectedText) != nullptr ));
188
234
189
235
#if !CHIP_CONFIG_SHORT_ERROR_STR
190
- // Assert that the error string contains a description, which is signaled
191
- // by a presence of a colon proceeding the description.
192
- EXPECT_TRUE ((strchr (errStr, ' :' ) != nullptr ));
236
+ // Assert that the error string contains a description, which is signaled
237
+ // by a presence of a colon proceeding the description.
238
+ EXPECT_TRUE ((strchr (errStr, ' :' ) != nullptr ));
193
239
#endif // !CHIP_CONFIG_SHORT_ERROR_STR
194
240
195
241
#if CHIP_CONFIG_ERROR_SOURCE
196
- // GetFile() should be relative to ${chip_root}
197
- char const * const file = err.GetFile ();
198
- ASSERT_NE (file, nullptr );
199
- EXPECT_EQ (strstr (file, " src/lib/core/" ), file);
200
-
201
- // File should be included in the error.
202
- EXPECT_NE (strstr (errStr, file), nullptr );
242
+ char const * const file = err.GetFile ();
243
+ ASSERT_NE (file, nullptr );
244
+ // File should not be included in the error.
245
+ EXPECT_EQ (strstr (errStr, file), nullptr );
203
246
#endif // CHIP_CONFIG_ERROR_SOURCE
204
- }
205
247
}
206
248
207
249
TEST (TestCHIPErrorStr, CheckCoreErrorStrWithoutSourceLocation)
@@ -213,24 +255,22 @@ TEST(TestCHIPErrorStr, CheckCoreErrorStrWithoutSourceLocation)
213
255
// For each defined error...
214
256
for (const auto & err : kTestElements )
215
257
{
216
- const char * errStr = ErrorStr (err, /* withSourceLocation=*/ false );
217
- char expectedText[9 ];
258
+ // ErrorStr with static char array.
259
+ CheckCoreErrorStrWithoutSourceLocationHelper (ErrorStr (err, /* withSourceLocation=*/ false ), err);
260
+ }
261
+ }
218
262
219
- // Assert that the error string contains the error number in hex.
220
- snprintf (expectedText, sizeof (expectedText), " %08 " PRIX32, static_cast < uint32_t >(err. AsInteger ()));
221
- EXPECT_TRUE (( strstr (errStr, expectedText) != nullptr ));
263
+ TEST (TestCHIPErrorStr, CheckCoreErrorStrStorageWithoutSourceLocation)
264
+ {
265
+ // Register the layer error formatter
222
266
223
- #if !CHIP_CONFIG_SHORT_ERROR_STR
224
- // Assert that the error string contains a description, which is signaled
225
- // by a presence of a colon proceeding the description.
226
- EXPECT_TRUE ((strchr (errStr, ' :' ) != nullptr ));
227
- #endif // !CHIP_CONFIG_SHORT_ERROR_STR
267
+ RegisterCHIPLayerErrorFormatter ();
228
268
229
- # if CHIP_CONFIG_ERROR_SOURCE
230
- char const * const file = err. GetFile ();
231
- ASSERT_NE (file, nullptr );
232
- // File should not be included in the error .
233
- EXPECT_EQ ( strstr (errStr, file), nullptr ) ;
234
- # endif // CHIP_CONFIG_ERROR_SOURCE
269
+ // For each defined error...
270
+ for ( const auto & err : kTestElements )
271
+ {
272
+ // ErrorStr with given storage .
273
+ ErrorStrStorage storage ;
274
+ CheckCoreErrorStrWithoutSourceLocationHelper ( ErrorStr (err, /* withSourceLocation= */ false , storage), err);
235
275
}
236
276
}
0 commit comments