Skip to content

Commit 2282329

Browse files
author
Marco A. Gutiérrez
committed
World: sdfwarns to sdf::Errors when warnings policy set to sdf::EnforcementPolicy::ERR (#1131)
Signed-off-by: Marco A. Gutierrez <marco@openrobotics.org>
1 parent f61612a commit 2282329

File tree

3 files changed

+69
-18
lines changed

3 files changed

+69
-18
lines changed

include/sdf/Error.hh

+3
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,9 @@ namespace sdf
165165
/// \brief Generic error to be thrown with SDF_ASSERT by the caller.
166166
/// This has been created to help preserve behavior.
167167
FATAL_ERROR,
168+
169+
/// \brief Generic warning saved as error due to WarningsPolicy config
170+
WARNING,
168171
};
169172

170173
class SDFORMAT_VISIBLE Error

src/World.cc

+15-18
Original file line numberDiff line numberDiff line change
@@ -206,20 +206,13 @@ Errors World::Load(sdf::ElementPtr _sdf, const ParserConfig &_config)
206206
{
207207
if (size > 1)
208208
{
209-
sdfwarn << "Non-unique name[" << name << "] detected " << size
210-
<< " times in XML children of world with name[" << this->Name()
211-
<< "].\n";
212-
}
213-
}
214-
215-
for (const auto &[name, size] :
216-
_sdf->CountNamedElements("", Element::NameUniquenessExceptions()))
217-
{
218-
if (size > 1)
219-
{
220-
sdfwarn << "Non-unique name[" << name << "] detected " << size
221-
<< " times in XML children of world with name[" << this->Name()
222-
<< "].\n";
209+
std::stringstream ss;
210+
ss << "Non-unique name[" << name << "] detected " << size
211+
<< " times in XML children of world with name[" << this->Name()
212+
<< "].";
213+
Error err(ErrorCode::WARNING, ss.str());
214+
enforceConfigurablePolicyCondition(
215+
_config.WarningsPolicy(), err, errors);
223216
}
224217
}
225218

@@ -288,10 +281,14 @@ Errors World::Load(sdf::ElementPtr _sdf, const ParserConfig &_config)
288281
{
289282
frameName = frame.Name() + "_frame" + std::to_string(i++);
290283
}
291-
sdfwarn << "Frame with name [" << frame.Name() << "] "
292-
<< "in world with name [" << this->Name() << "] "
293-
<< "has a name collision, changing frame name to ["
294-
<< frameName << "].\n";
284+
std::stringstream ss;
285+
ss << "Frame with name [" << frame.Name() << "] "
286+
<< "in world with name [" << this->Name() << "] "
287+
<< "has a name collision, changing frame name to ["
288+
<< frameName << "].\n";
289+
Error err(ErrorCode::WARNING, ss.str());
290+
enforceConfigurablePolicyCondition(
291+
_config.WarningsPolicy(), err, errors);
295292
frame.SetName(frameName);
296293
}
297294
frameNames.insert(frameName);

test/integration/error_output.cc

+51
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
#include "sdf/Error.hh"
2525
#include "sdf/Param.hh"
2626
#include "sdf/Types.hh"
27+
#include "sdf/World.hh"
2728
#include "test_utils.hh"
29+
#include "test_config.hh"
2830

2931
////////////////////////////////////////
3032
// Test Param class for sdf::Errors outputs
@@ -245,3 +247,52 @@ TEST(ErrorOutput, PrintConfigErrorOutput)
245247
// Check nothing has been printed
246248
EXPECT_TRUE(buffer.str().empty()) << buffer.str();
247249
}
250+
251+
////////////////////////////////////////
252+
// Test World class for sdf::Errors outputs
253+
TEST(ErrorOutput, WorldErrorOutput)
254+
{
255+
std::stringstream buffer;
256+
sdf::testing::RedirectConsoleStream redir(
257+
sdf::Console::Instance()->GetMsgStream(), &buffer);
258+
259+
sdf::Errors errors;
260+
261+
std::ostringstream stream;
262+
stream << "<?xml version=\"1.0\"?>"
263+
<< "<sdf version='1.8'>"
264+
<< " <world name='test_world'>"
265+
<< " <model name='common_name'>"
266+
<< " <link name='a'>"
267+
<< " </link>"
268+
<< " </model>"
269+
<< " <model name='common_name'>"
270+
<< " <link name='a'>"
271+
<< " </link>"
272+
<< " </model>"
273+
<< " <frame name='common_name'/>"
274+
<< " </world>"
275+
<< "</sdf>";
276+
277+
sdf::SDFPtr sdfParsed(new sdf::SDF());
278+
sdf::init(sdfParsed);
279+
280+
sdf::ParserConfig parserConfig;
281+
parserConfig.SetWarningsPolicy(sdf::EnforcementPolicy::ERR);
282+
sdf::readString(stream.str(), parserConfig, sdfParsed, errors);
283+
EXPECT_TRUE(errors.empty());
284+
285+
sdf::World world;
286+
errors = world.Load(sdfParsed->Root()->GetElement("world"), parserConfig);
287+
ASSERT_EQ(errors.size(), 3u);
288+
EXPECT_NE(std::string::npos, errors[0].Message().find(
289+
"Non-unique name[common_name] detected 3 times in XML children of world"
290+
" with name[test_world]."));
291+
EXPECT_NE(std::string::npos, errors[1].Message().find(
292+
"model with name[common_name] already exists."));
293+
EXPECT_NE(std::string::npos, errors[2].Message().find(
294+
"Frame with name [common_name] in world with name [test_world] has a name"
295+
" collision, changing frame name to [common_name_frame]."));
296+
// Check nothing has been printed
297+
EXPECT_TRUE(buffer.str().empty()) << buffer.str();
298+
}

0 commit comments

Comments
 (0)