Skip to content

Commit e0c1242

Browse files
authored
Merge 79fd6fb into 19ed749
2 parents 19ed749 + 79fd6fb commit e0c1242

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

0 commit comments

Comments
 (0)