Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

World: sdfwarns to sdf::Errors when warnings policy set to sdf::EnforcementPolicy::ERR #1131

Merged
merged 2 commits into from
Sep 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/sdf/Error.hh
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ namespace sdf
/// \brief Generic error to be thrown with SDF_ASSERT by the caller.
/// This has been created to help preserve behavior.
FATAL_ERROR,

/// \brief Generic warning saved as error due to WarningsPolicy config
WARNING,
};

class SDFORMAT_VISIBLE Error
Expand Down
33 changes: 15 additions & 18 deletions src/World.cc
Original file line number Diff line number Diff line change
Expand Up @@ -206,20 +206,13 @@ Errors World::Load(sdf::ElementPtr _sdf, const ParserConfig &_config)
{
if (size > 1)
{
sdfwarn << "Non-unique name[" << name << "] detected " << size
<< " times in XML children of world with name[" << this->Name()
<< "].\n";
}
}

for (const auto &[name, size] :
_sdf->CountNamedElements("", Element::NameUniquenessExceptions()))
{
if (size > 1)
{
sdfwarn << "Non-unique name[" << name << "] detected " << size
<< " times in XML children of world with name[" << this->Name()
<< "].\n";
std::stringstream ss;
ss << "Non-unique name[" << name << "] detected " << size
<< " times in XML children of world with name[" << this->Name()
<< "].";
Error err(ErrorCode::WARNING, ss.str());
enforceConfigurablePolicyCondition(
_config.WarningsPolicy(), err, errors);
}
}

Expand Down Expand Up @@ -288,10 +281,14 @@ Errors World::Load(sdf::ElementPtr _sdf, const ParserConfig &_config)
{
frameName = frame.Name() + "_frame" + std::to_string(i++);
}
sdfwarn << "Frame with name [" << frame.Name() << "] "
<< "in world with name [" << this->Name() << "] "
<< "has a name collision, changing frame name to ["
<< frameName << "].\n";
std::stringstream ss;
ss << "Frame with name [" << frame.Name() << "] "
<< "in world with name [" << this->Name() << "] "
<< "has a name collision, changing frame name to ["
<< frameName << "].\n";
Error err(ErrorCode::WARNING, ss.str());
enforceConfigurablePolicyCondition(
_config.WarningsPolicy(), err, errors);
frame.SetName(frameName);
}
frameNames.insert(frameName);
Expand Down
51 changes: 51 additions & 0 deletions test/integration/error_output.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
#include "sdf/Error.hh"
#include "sdf/Param.hh"
#include "sdf/Types.hh"
#include "sdf/World.hh"
#include "test_utils.hh"
#include "test_config.hh"

////////////////////////////////////////
// Test Param class for sdf::Errors outputs
Expand Down Expand Up @@ -243,3 +245,52 @@ TEST(ErrorOutput, PrintConfigErrorOutput)
// Check nothing has been printed
EXPECT_TRUE(buffer.str().empty()) << buffer.str();
}

////////////////////////////////////////
// Test World class for sdf::Errors outputs
TEST(ErrorOutput, WorldErrorOutput)
{
std::stringstream buffer;
sdf::testing::RedirectConsoleStream redir(
sdf::Console::Instance()->GetMsgStream(), &buffer);

sdf::Errors errors;

std::ostringstream stream;
stream << "<?xml version=\"1.0\"?>"
<< "<sdf version='1.8'>"
<< " <world name='test_world'>"
<< " <model name='common_name'>"
<< " <link name='a'>"
<< " </link>"
<< " </model>"
<< " <model name='common_name'>"
<< " <link name='a'>"
<< " </link>"
<< " </model>"
<< " <frame name='common_name'/>"
<< " </world>"
<< "</sdf>";

sdf::SDFPtr sdfParsed(new sdf::SDF());
sdf::init(sdfParsed);

sdf::ParserConfig parserConfig;
parserConfig.SetWarningsPolicy(sdf::EnforcementPolicy::ERR);
sdf::readString(stream.str(), parserConfig, sdfParsed, errors);
EXPECT_TRUE(errors.empty());

sdf::World world;
errors = world.Load(sdfParsed->Root()->GetElement("world"), parserConfig);
ASSERT_EQ(errors.size(), 3u);
EXPECT_NE(std::string::npos, errors[0].Message().find(
"Non-unique name[common_name] detected 3 times in XML children of world"
" with name[test_world]."));
EXPECT_NE(std::string::npos, errors[1].Message().find(
"model with name[common_name] already exists."));
EXPECT_NE(std::string::npos, errors[2].Message().find(
"Frame with name [common_name] in world with name [test_world] has a name"
" collision, changing frame name to [common_name_frame]."));
// Check nothing has been printed
EXPECT_TRUE(buffer.str().empty()) << buffer.str();
}