Skip to content

Commit f61612a

Browse files
Marco A. Gutiérrezscpeters
Marco A. Gutiérrez
andcommitted
PrintConfig: add sdf::Errors output to API methods (#1098)
Signed-off-by: Marco A. Gutierrez <marco@openrobotics.org> Co-authored-by: Steve Peters <scpeters@openrobotics.org>
1 parent 3cdf7c3 commit f61612a

File tree

3 files changed

+99
-43
lines changed

3 files changed

+99
-43
lines changed

include/sdf/PrintConfig.hh

+14
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#include "sdf/sdf_config.h"
2424
#include "sdf/system_util.hh"
25+
#include "sdf/Types.hh"
2526

2627
namespace sdf
2728
{
@@ -54,6 +55,19 @@ namespace sdf
5455
public: bool SetRotationSnapToDegrees(unsigned int _interval,
5556
double _tolerance);
5657

58+
/// \brief Sets the option for printing pose rotation in degrees as well as
59+
/// snapping the rotation to the desired interval, with the provided
60+
/// tolerance.
61+
/// \param[in] _interval Degrees interval to snap to, this value must be
62+
/// larger than 0, and less than or equal to 360.
63+
/// \param[in] _tolerance Tolerance which snapping occurs, this value must
64+
/// be larger than 0, less than 360, and less than the provided interval.
65+
/// \param[out] _errors Vector of Errors.
66+
/// \return True, unless any of the provided values are not valid.
67+
public: bool SetRotationSnapToDegrees(unsigned int _interval,
68+
double _tolerance,
69+
sdf::Errors &_errors);
70+
5771
/// \brief Returns the current degree value that pose rotations will snap to
5872
/// when printed.
5973
/// \return The assigned degrees interval value to snap to. If it has not

src/PrintConfig.cc

+22-4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include "sdf/PrintConfig.hh"
2020
#include "sdf/Console.hh"
21+
#include "Utils.hh"
2122

2223
using namespace sdf;
2324

@@ -75,19 +76,36 @@ bool PrintConfig::PreserveIncludes() const
7576
/////////////////////////////////////////////////
7677
bool PrintConfig::SetRotationSnapToDegrees(unsigned int _interval,
7778
double _tolerance)
79+
{
80+
sdf::Errors errors;
81+
bool result = this->SetRotationSnapToDegrees(_interval,
82+
_tolerance,
83+
errors);
84+
throwOrPrintErrors(errors);
85+
return result;
86+
}
87+
88+
/////////////////////////////////////////////////
89+
bool PrintConfig::SetRotationSnapToDegrees(unsigned int _interval,
90+
double _tolerance,
91+
sdf::Errors &_errors)
7892
{
7993
if (_interval == 0 || _interval > 360)
8094
{
81-
sdferr << "Interval value to snap to must be larger than 0, and less than "
82-
<< "or equal to 360.\n";
95+
std::stringstream ss;
96+
ss << "Interval value to snap to must be larger than 0, and less than "
97+
<< "or equal to 360.";
98+
_errors.push_back({ErrorCode::ROTATION_SNAP_CONFIG_ERROR, ss.str()});
8399
return false;
84100
}
85101

86102
if (_tolerance <= 0 || _tolerance > 360 ||
87103
_tolerance >= static_cast<double>(_interval))
88104
{
89-
sdferr << "Tolerance must be larger than 0, less than or equal to "
90-
<< "360, and less than the provided interval.\n";
105+
std::stringstream ss;
106+
ss << "Tolerance must be larger than 0, less than or equal to "
107+
<< "360, and less than the provided interval.";
108+
_errors.push_back({ErrorCode::ROTATION_SNAP_CONFIG_ERROR, ss.str()});
91109
return false;
92110
}
93111

test/integration/error_output.cc

+63-39
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,16 @@ TEST(ErrorOutput, ParamErrorOutput)
3232
{
3333
std::stringstream buffer;
3434
sdf::testing::RedirectConsoleStream redir(
35-
sdf::Console::Instance()->GetMsgStream(), &buffer);
35+
sdf::Console::Instance()->GetMsgStream(), &buffer);
3636

3737
sdf::Errors errors;
3838
ASSERT_NO_THROW(sdf::Param param1("key", "not_valid_type", "true", false,
39-
errors, "description"));
39+
errors, "description"));
4040
ASSERT_GE(2u, errors.size());
4141
EXPECT_EQ(errors[0].Code(), sdf::ErrorCode::UNKNOWN_PARAMETER_TYPE);
4242
EXPECT_NE(std::string::npos,
4343
errors[0].Message().find(
44-
"Unknown parameter type[not_valid_type]"));
44+
"Unknown parameter type[not_valid_type]"));
4545
EXPECT_EQ(errors[1].Code(), sdf::ErrorCode::PARAMETER_ERROR);
4646
EXPECT_NE(std::string::npos,
4747
errors[1].Message().find("Invalid parameter"));
@@ -52,10 +52,10 @@ TEST(ErrorOutput, ParamErrorOutput)
5252
ASSERT_GE(2u, errors.size());
5353
EXPECT_EQ(errors[0].Code(), sdf::ErrorCode::UNKNOWN_PARAMETER_TYPE);
5454
EXPECT_NE(std::string::npos,
55-
errors[0].Message().find("Unknown parameter type[not_valid_type]"));
55+
errors[0].Message().find("Unknown parameter type[not_valid_type]"));
5656
EXPECT_EQ(errors[1].Code(), sdf::ErrorCode::PARAMETER_ERROR);
5757
EXPECT_NE(std::string::npos,
58-
errors[1].Message().find("Invalid parameter"));
58+
errors[1].Message().find("Invalid parameter"));
5959

6060
errors.clear();
6161
sdf::Param param3("key", "bool", "true", false, errors, "description");
@@ -73,15 +73,15 @@ TEST(ErrorOutput, ParamErrorOutput)
7373
ASSERT_GE(errors.size(), 1u);
7474
EXPECT_EQ(errors[0].Code(), sdf::ErrorCode::PARAMETER_ERROR);
7575
EXPECT_NE(std::string::npos, errors[0].Message().find(
76-
"The value for //pose[@rotation_format='euler_rpy'] must have 6 "
77-
"values, but 1 were found instead in '1'."));
76+
"The value for //pose[@rotation_format='euler_rpy'] must have 6 "
77+
"values, but 1 were found instead in '1'."));
7878

7979
errors.clear();
8080
param3.Update(errors);
8181
ASSERT_GE(errors.size(), 1u);
8282
EXPECT_EQ(errors[0].Code(), sdf::ErrorCode::PARAMETER_ERROR);
8383
EXPECT_NE(std::string::npos, errors[0].Message().find(
84-
"[updateFunc] is not set."));
84+
"[updateFunc] is not set."));
8585

8686
errors.clear();
8787
sdf::Param requiredParam("key", "int", "1", true, "2", "4", errors,
@@ -92,14 +92,13 @@ TEST(ErrorOutput, ParamErrorOutput)
9292
ASSERT_GE(errors.size(), 1u);
9393
EXPECT_EQ(errors[0].Code(), sdf::ErrorCode::PARAMETER_ERROR);
9494
EXPECT_NE(std::string::npos, errors[0].Message().find(
95-
"Empty string used when setting a required parameter. Key[key]"));
95+
"Empty string used when setting a required parameter. Key[key]"));
9696
EXPECT_FALSE(requiredParam.ValidateValue(errors));
9797
ASSERT_GE(errors.size(), 2u);
9898
EXPECT_EQ(errors[1].Code(), sdf::ErrorCode::PARAMETER_ERROR);
9999
EXPECT_NE(std::string::npos, errors[1].Message().find(
100-
"The value [1] is less than the minimum allowed value of [2] for "
101-
"key [key]"));
102-
100+
"The value [1] is less than the minimum allowed value of [2] for "
101+
"key [key]"));
103102

104103
errors.clear();
105104
// Adding a parent with @rotation_format to something invalid
@@ -111,8 +110,8 @@ TEST(ErrorOutput, ParamErrorOutput)
111110
ASSERT_EQ(errors.size(), 2u);
112111
EXPECT_EQ(errors[0].Code(), sdf::ErrorCode::PARAMETER_ERROR);
113112
EXPECT_NE(std::string::npos, errors[0].Message().find(
114-
"Undefined attribute //pose[@rotation_format='invalid_format'], "
115-
"only 'euler_rpy' and 'quat_xyzw' is supported."));
113+
"Undefined attribute //pose[@rotation_format='invalid_format'], "
114+
"only 'euler_rpy' and 'quat_xyzw' is supported."));
116115
EXPECT_EQ(errors[1].Code(), sdf::ErrorCode::PARAMETER_ERROR);
117116
#if !defined __ARM_ARCH
118117
EXPECT_NE(std::string::npos, errors[1].Message().find(
@@ -128,22 +127,22 @@ TEST(ErrorOutput, ParamErrorOutput)
128127
ASSERT_EQ(errors.size(), 6u);
129128
EXPECT_EQ(errors[0].Code(), sdf::ErrorCode::PARAMETER_ERROR);
130129
EXPECT_NE(std::string::npos, errors[0].Message().find(
131-
"Invalid boolean value"));
130+
"Invalid boolean value"));
132131
EXPECT_EQ(errors[1].Code(), sdf::ErrorCode::PARAMETER_ERROR);
133132
EXPECT_NE(std::string::npos, errors[1].Message().find(
134-
"Invalid parameter"));
133+
"Invalid parameter"));
135134
EXPECT_EQ(errors[2].Code(), sdf::ErrorCode::PARAMETER_ERROR);
136135
EXPECT_NE(std::string::npos, errors[2].Message().find(
137-
"Invalid boolean value"));
136+
"Invalid boolean value"));
138137
EXPECT_EQ(errors[3].Code(), sdf::ErrorCode::PARAMETER_ERROR);
139138
EXPECT_NE(std::string::npos, errors[3].Message().find(
140-
"Invalid [min] parameter in SDFormat description of [key]"));
139+
"Invalid [min] parameter in SDFormat description of [key]"));
141140
EXPECT_EQ(errors[4].Code(), sdf::ErrorCode::PARAMETER_ERROR);
142141
EXPECT_NE(std::string::npos, errors[4].Message().find(
143-
"Invalid boolean value"));
142+
"Invalid boolean value"));
144143
EXPECT_EQ(errors[5].Code(), sdf::ErrorCode::PARAMETER_ERROR);
145144
EXPECT_NE(std::string::npos, errors[5].Message().find(
146-
"Invalid [max] parameter in SDFormat description of [key]"));
145+
"Invalid [max] parameter in SDFormat description of [key]"));
147146

148147
// Check nothing has been printed
149148
EXPECT_TRUE(buffer.str().empty()) << buffer.str();
@@ -155,7 +154,7 @@ TEST(ErrorOutput, ElementErrorOutput)
155154
{
156155
std::stringstream buffer;
157156
sdf::testing::RedirectConsoleStream redir(
158-
sdf::Console::Instance()->GetMsgStream(), &buffer);
157+
sdf::Console::Instance()->GetMsgStream(), &buffer);
159158

160159
sdf::Errors errors;
161160
sdf::ElementPtr elem = std::make_shared<sdf::Element>();
@@ -165,59 +164,84 @@ TEST(ErrorOutput, ElementErrorOutput)
165164
ASSERT_EQ(errors.size(), 1u);
166165
EXPECT_EQ(errors[0].Code(), sdf::ErrorCode::ELEMENT_ERROR);
167166
EXPECT_NE(std::string::npos, errors[0].Message().find(
168-
"Unable to find value for key [test]"));
167+
"Unable to find value for key [test]"));
169168

170169
errors.clear();
171170
elem->GetElement("missingElement", errors);
172171
ASSERT_EQ(errors.size(), 1u);
173172
EXPECT_NE(std::string::npos, errors[0].Message().find(
174-
"Missing element description for [missingElement]"));
173+
"Missing element description for [missingElement]"));
175174

176175
errors.clear();
177176
elem->AddAttribute(
178-
"invalidAttribute", "int", "invalidFormat", false, errors);
177+
"invalidAttribute", "int", "invalidFormat", false, errors);
179178
ASSERT_EQ(errors.size(), 2u);
180179
EXPECT_NE(std::string::npos, errors[0].Message().find(
181-
"Invalid argument. Unable to set value [invalidFormat]"
182-
" for key[invalidAttribute]"));
180+
"Invalid argument. Unable to set value [invalidFormat]"
181+
" for key[invalidAttribute]"));
183182
EXPECT_NE(std::string::npos, errors[1].Message().find(
184-
"Invalid parameter"));
183+
"Invalid parameter"));
185184

186185
errors.clear();
187186
elem->AddValue("type", "value", true, "a", "b", errors);
188187
ASSERT_EQ(errors.size(), 9u);
189188
EXPECT_NE(std::string::npos, errors[0].Message().find(
190-
"Unknown parameter type[type]"));
189+
"Unknown parameter type[type]"));
191190
EXPECT_NE(std::string::npos, errors[1].Message().find(
192-
"Invalid parameter"));
191+
"Invalid parameter"));
193192
EXPECT_NE(std::string::npos, errors[2].Message().find(
194-
"Unknown parameter type[type]"));
193+
"Unknown parameter type[type]"));
195194
EXPECT_NE(std::string::npos, errors[3].Message().find(
196-
"Invalid [min] parameter in SDFormat description of [testElement]"));
195+
"Invalid [min] parameter in SDFormat description of [testElement]"));
197196
EXPECT_NE(std::string::npos, errors[4].Message().find(
198-
"Unknown parameter type[type]"));
197+
"Unknown parameter type[type]"));
199198
EXPECT_NE(std::string::npos, errors[5].Message().find(
200-
"Invalid [max] parameter in SDFormat description of [testElement]"));
199+
"Invalid [max] parameter in SDFormat description of [testElement]"));
201200
EXPECT_NE(std::string::npos, errors[6].Message().find(
202-
"Unknown parameter type[type]"));
201+
"Unknown parameter type[type]"));
203202
EXPECT_NE(std::string::npos, errors[7].Message().find(
204-
"Failed to set value '0' to key [testElement] for new parent element"
205-
" of name 'testElement', reverting to previous value '0'."));
203+
"Failed to set value '0' to key [testElement] for new parent element"
204+
" of name 'testElement', reverting to previous value '0'."));
206205
EXPECT_NE(std::string::npos, errors[8].Message().find(
207-
"Cannot set parent Element of value to itself."));
206+
"Cannot set parent Element of value to itself."));
208207
errors.clear();
209208

210209
elem->GetElement("nonExistentElement", errors);
211210
ASSERT_EQ(errors.size(), 1u);
212211
EXPECT_NE(std::string::npos, errors[0].Message().find(
213-
"Missing element description for [nonExistentElement]"));
212+
"Missing element description for [nonExistentElement]"));
214213
errors.clear();
215214

216215
elem->RemoveChild(sdf::ElementPtr(), errors);
217216
ASSERT_EQ(errors.size(), 1u);
218217
EXPECT_NE(std::string::npos, errors[0].Message().find(
219-
"Cannot remove a nullptr child pointer"));
218+
"Cannot remove a nullptr child pointer"));
219+
// Check nothing has been printed
220+
EXPECT_TRUE(buffer.str().empty()) << buffer.str();
221+
}
220222

223+
////////////////////////////////////////
224+
// Test PrintConfig class for sdf::Errors outputs
225+
TEST(ErrorOutput, PrintConfigErrorOutput)
226+
{
227+
std::stringstream buffer;
228+
sdf::testing::RedirectConsoleStream redir(
229+
sdf::Console::Instance()->GetMsgStream(), &buffer);
230+
231+
sdf::Errors errors;
232+
sdf::PrintConfig printConfig;
233+
ASSERT_FALSE(printConfig.SetRotationSnapToDegrees(361, 300, errors));
234+
ASSERT_EQ(errors.size(), 1u);
235+
EXPECT_NE(std::string::npos, errors[0].Message().find(
236+
"Interval value to snap to must be larger than 0,"
237+
" and less than or equal to 360."));
238+
errors.clear();
239+
240+
ASSERT_FALSE(printConfig.SetRotationSnapToDegrees(300, 300, errors));
241+
ASSERT_EQ(errors.size(), 1u);
242+
EXPECT_NE(std::string::npos, errors[0].Message().find(
243+
"Tolerance must be larger than 0, less than or equal to 360, "
244+
"and less than the provided interval."));
221245
// Check nothing has been printed
222246
EXPECT_TRUE(buffer.str().empty()) << buffer.str();
223247
}

0 commit comments

Comments
 (0)