Skip to content

Commit 8299195

Browse files
authored
[parsing] Delete FormatFirstError; fix call sites (#22418)
1 parent 374c45b commit 8299195

File tree

3 files changed

+19
-46
lines changed

3 files changed

+19
-46
lines changed

common/test_utilities/diagnostic_policy_test_base.h

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -104,27 +104,6 @@ class DiagnosticPolicyTestBase : public ::testing::Test {
104104
});
105105
}
106106

107-
// Returns the first error as a string (or else fails the test case,
108-
// if there were no errors).
109-
std::string FormatFirstError() {
110-
ScopedTrace trace;
111-
if (error_records_.empty()) {
112-
auto format_warnings = [this]() {
113-
std::deque<std::string> warnings;
114-
for (const auto& record : warning_records_) {
115-
warnings.push_back(record.FormatWarning());
116-
}
117-
return warnings;
118-
};
119-
ADD_FAILURE() << fmt::format(
120-
"FormatFirstError did not get any errors\n"
121-
"found {} warnings:\n{}",
122-
NumWarnings(), fmt::join(format_warnings(), "\n"));
123-
return {};
124-
}
125-
return error_records_[0].FormatError();
126-
}
127-
128107
template <typename T>
129108
T Take(std::deque<T>* c) {
130109
if (c->empty()) {

multibody/parsing/test/detail_sdf_geometry_test.cc

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -268,9 +268,8 @@ TEST_F(SceneGraphParserDetail, CheckInvalidDrakeCapsules) {
268268
unique_ptr<Shape> shape_no_radius =
269269
MakeShapeFromSdfGeometry(*no_radius_geometry);
270270
EXPECT_EQ(shape_no_radius, nullptr);
271-
EXPECT_THAT(FormatFirstError(), ::testing::MatchesRegex(
271+
EXPECT_THAT(TakeError(), ::testing::MatchesRegex(
272272
".*Element <radius> is required within element <drake:capsule>."));
273-
ClearDiagnostics();
274273

275274
unique_ptr<sdf::Geometry> no_length_geometry = MakeSdfGeometryFromString(
276275
"<drake:capsule>"
@@ -279,9 +278,8 @@ TEST_F(SceneGraphParserDetail, CheckInvalidDrakeCapsules) {
279278
unique_ptr<Shape> shape_no_length =
280279
MakeShapeFromSdfGeometry(*no_length_geometry);
281280
EXPECT_EQ(shape_no_length, nullptr);
282-
EXPECT_THAT(FormatFirstError(), ::testing::MatchesRegex(
281+
EXPECT_THAT(TakeError(), ::testing::MatchesRegex(
283282
".*Element <length> is required within element <drake:capsule>."));
284-
ClearDiagnostics();
285283
}
286284

287285
// Verify MakeShapeFromSdfGeometry can make a capsule from an sdf::Geometry.
@@ -342,9 +340,8 @@ TEST_F(SceneGraphParserDetail, CheckInvalidEllipsoids) {
342340
"</drake:ellipsoid>");
343341
unique_ptr<Shape> shape_no_a = MakeShapeFromSdfGeometry(*no_a_geometry);
344342
EXPECT_EQ(shape_no_a, nullptr);
345-
EXPECT_THAT(FormatFirstError(), ::testing::MatchesRegex(
343+
EXPECT_THAT(TakeError(), ::testing::MatchesRegex(
346344
".*Element <a> is required within element <drake:ellipsoid>."));
347-
ClearDiagnostics();
348345

349346
unique_ptr<sdf::Geometry> no_b_geometry = MakeSdfGeometryFromString(
350347
"<drake:ellipsoid>"
@@ -353,9 +350,8 @@ TEST_F(SceneGraphParserDetail, CheckInvalidEllipsoids) {
353350
"</drake:ellipsoid>");
354351
unique_ptr<Shape> shape_no_b = MakeShapeFromSdfGeometry(*no_b_geometry);
355352
EXPECT_EQ(shape_no_b, nullptr);
356-
EXPECT_THAT(FormatFirstError(), ::testing::MatchesRegex(
353+
EXPECT_THAT(TakeError(), ::testing::MatchesRegex(
357354
".*Element <b> is required within element <drake:ellipsoid>."));
358-
ClearDiagnostics();
359355

360356
unique_ptr<sdf::Geometry> no_c_geometry = MakeSdfGeometryFromString(
361357
"<drake:ellipsoid>"
@@ -364,9 +360,8 @@ TEST_F(SceneGraphParserDetail, CheckInvalidEllipsoids) {
364360
"</drake:ellipsoid>");
365361
unique_ptr<Shape> shape_no_c = MakeShapeFromSdfGeometry(*no_c_geometry);
366362
EXPECT_EQ(shape_no_c, nullptr);
367-
EXPECT_THAT(FormatFirstError(), ::testing::MatchesRegex(
363+
EXPECT_THAT(TakeError(), ::testing::MatchesRegex(
368364
".*Element <c> is required within element <drake:ellipsoid>."));
369-
ClearDiagnostics();
370365
}
371366

372367

@@ -441,10 +436,9 @@ TEST_F(SceneGraphParserDetail, MakeMeshFromSdfGeometryIsotropicError) {
441436
"</mesh>");
442437
unique_ptr<Shape> shape = MakeShapeFromSdfGeometry(*sdf_geometry);
443438
EXPECT_EQ(shape, nullptr);
444-
EXPECT_THAT(FormatFirstError(), ::testing::MatchesRegex(
439+
EXPECT_THAT(TakeError(), ::testing::MatchesRegex(
445440
".*Drake meshes only support isotropic scaling. Therefore"
446441
" all three scaling factors must be exactly equal."));
447-
ClearDiagnostics();
448442
}
449443

450444
// Verify MakeShapeFromSdfGeometry can make a convex mesh from an sdf::Geometry.
@@ -1071,9 +1065,8 @@ TEST_F(SceneGraphParserDetail, ParseVisualMaterial) {
10711065
internal::MakeVisualPropertiesFromSdfVisual(sdf_diagnostic_,
10721066
*sdf_visual, [](const SDFormatDiagnostic&, std::string filename)
10731067
-> std::string {return {};});
1074-
EXPECT_THAT(FormatFirstError(), ::testing::MatchesRegex(
1068+
EXPECT_THAT(TakeError(), ::testing::MatchesRegex(
10751069
".*Unable to locate the texture file: empty.png"));
1076-
ClearDiagnostics();
10771070
}
10781071

10791072
// Case: drake:{illustration|perception}_properties is missing the enabled
@@ -1244,7 +1237,7 @@ TEST_F(SceneGraphParserDetail, AcceptingRenderers) {
12441237
" <drake:accepting_renderer> </drake:accepting_renderer>"
12451238
"</visual>");
12461239
MakeVisualPropertiesFromSdfVisual(*sdf_visual);
1247-
EXPECT_THAT(FormatFirstError(), ::testing::MatchesRegex(
1240+
EXPECT_THAT(TakeError(), ::testing::MatchesRegex(
12481241
".*<drake:accepting_renderer> tag given without any name"));
12491242
}
12501243

@@ -1260,8 +1253,6 @@ TEST_F(SceneGraphParserDetail, AcceptingRenderers) {
12601253
EXPECT_THAT(TakeWarning(), ::testing::MatchesRegex(
12611254
".*<drake:accepting_renderer> specified .* disabled perception role."));
12621255
}
1263-
1264-
ClearDiagnostics();
12651256
}
12661257

12671258
// Verify that the <drake:*_properties> are accounted for in disabling sets of
@@ -1548,11 +1539,10 @@ TEST_F(SceneGraphParserDetail, MakeProximityPropertiesForCollision) {
15481539
std::optional<ProximityProperties> properties =
15491540
MakeProximityPropertiesForCollision(sdf_diagnostic_, *sdf_collision);
15501541
EXPECT_FALSE(properties.has_value());
1551-
EXPECT_THAT(FormatFirstError(), ::testing::MatchesRegex(
1542+
EXPECT_THAT(TakeError(), ::testing::MatchesRegex(
15521543
".*A <collision> geometry has defined the unsupported tag "
15531544
"<drake:soft_hydroelastic>. Please change it to "
15541545
"<drake:compliant_hydroelastic>."));
1555-
ClearDiagnostics();
15561546
}
15571547

15581548
// Case: specifies both -- should be an error.
@@ -1565,10 +1555,9 @@ TEST_F(SceneGraphParserDetail, MakeProximityPropertiesForCollision) {
15651555
std::optional<ProximityProperties> properties =
15661556
MakeProximityPropertiesForCollision(sdf_diagnostic_, *sdf_collision);
15671557
EXPECT_FALSE(properties.has_value());
1568-
EXPECT_THAT(FormatFirstError(), ::testing::MatchesRegex(
1558+
EXPECT_THAT(TakeError(), ::testing::MatchesRegex(
15691559
".*A <collision> geometry has defined mutually-exclusive tags "
15701560
".*rigid.* and .*compliant.*"));
1571-
ClearDiagnostics();
15721561
}
15731562

15741563
// Case: has no drake coefficients, only mu & m2 in ode: contains mu, mu2

multibody/parsing/test/detail_sdf_parser_test.cc

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1997,6 +1997,8 @@ TEST_F(SdfParserTest, TestUnsupportedFrames) {
19971997
R"(.*(attached_to|relative_to) name\[world\] specified by frame )"
19981998
R"(with name\[.*\] does not match a nested model, link, joint, or )"
19991999
R"(frame name in model with name\[bad\].*)"));
2000+
// Ignore additional errors for this test.
2001+
EXPECT_EQ(NumErrors(), 5);
20002002
ClearDiagnostics();
20012003

20022004
ParseTestString(R"""(
@@ -2011,6 +2013,8 @@ TEST_F(SdfParserTest, TestUnsupportedFrames) {
20112013
R"(.*(attached_to|relative_to) name\[world\] specified by frame )"
20122014
R"(with name\[.*\] does not match a nested model, link, joint, or )"
20132015
R"(frame name in model with name\[bad\].*)"));
2016+
// Ignore additional errors for this test.
2017+
EXPECT_EQ(NumErrors(), 2);
20142018
ClearDiagnostics();
20152019

20162020
for (std::string bad_name : {"world", "__model__", "__anything__"}) {
@@ -2023,6 +2027,7 @@ TEST_F(SdfParserTest, TestUnsupportedFrames) {
20232027
)""", bad_name));
20242028
EXPECT_THAT(TakeError(), ::testing::MatchesRegex(
20252029
R"(.*The supplied frame name \[.*\] is reserved..*)"));
2030+
// Ignore additional errors for this test. The number ignored varies.
20262031
ClearDiagnostics();
20272032
}
20282033

@@ -2034,6 +2039,8 @@ TEST_F(SdfParserTest, TestUnsupportedFrames) {
20342039
EXPECT_THAT(TakeError(), ::testing::MatchesRegex(
20352040
R"(.*Attribute //pose\[@relative_to\] of top level model )"
20362041
R"(must be left empty.*)"));
2042+
// Ignore additional errors for this test.
2043+
EXPECT_EQ(NumErrors(), 2);
20372044
ClearDiagnostics();
20382045

20392046
ParseTestString(R"""(
@@ -3216,9 +3223,8 @@ TEST_F(SdfParserTest, ErrorsFromIncludedUrdf) {
32163223
<name>arm</name>
32173224
</include>
32183225
</model>)""", "1.8");
3219-
EXPECT_THAT(FormatFirstError(), ::testing::MatchesRegex(
3226+
EXPECT_THAT(TakeError(), ::testing::MatchesRegex(
32203227
".*bad.urdf.*XML_ERROR.*"));
3221-
ClearDiagnostics();
32223228
}
32233229

32243230
// TODO(SeanCurtis-TRI) The logic testing for collision filter group parsing
@@ -3647,9 +3653,8 @@ TEST_F(SdfParserTest, TestSingleModelEnforcement) {
36473653
resolver.Resolve(diagnostic_policy_);
36483654
EXPECT_FALSE(result.has_value());
36493655

3650-
EXPECT_THAT(FormatFirstError(), ::testing::MatchesRegex(
3656+
EXPECT_THAT(TakeError(), ::testing::MatchesRegex(
36513657
".*Root object can only contain one model.*"));
3652-
ClearDiagnostics();
36533658
}
36543659

36553660
// Verify merge-include works with Interface API.

0 commit comments

Comments
 (0)