Skip to content

Commit 19ed749

Browse files
Marco A. Gutiérrezscpeters
Marco A. Gutiérrez
andauthored
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 bbb9c8f commit 19ed749

File tree

3 files changed

+103
-47
lines changed

3 files changed

+103
-47
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

+67-43
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,37 +110,37 @@ 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
EXPECT_NE(std::string::npos, errors[1].Message().find(
118-
"Failed to set value '1 2 3 0.40000000000000002 0.5 "
119-
"0.59999999999999987' to key [] for new parent element of name '',"
120-
" reverting to previous value '1 2 3 0.40000000000000002 0.5 "
121-
"0.59999999999999987'."));
117+
"Failed to set value '1 2 3 0.40000000000000002 0.5 "
118+
"0.59999999999999987' to key [] for new parent element of name '',"
119+
" reverting to previous value '1 2 3 0.40000000000000002 0.5 "
120+
"0.59999999999999987'."));
122121

123122
errors.clear();
124123
sdf::Param param4("key", "bool", "15", false, "a", "b", errors,
125124
"description");
126125
ASSERT_EQ(errors.size(), 6u);
127126
EXPECT_EQ(errors[0].Code(), sdf::ErrorCode::PARAMETER_ERROR);
128127
EXPECT_NE(std::string::npos, errors[0].Message().find(
129-
"Invalid boolean value"));
128+
"Invalid boolean value"));
130129
EXPECT_EQ(errors[1].Code(), sdf::ErrorCode::PARAMETER_ERROR);
131130
EXPECT_NE(std::string::npos, errors[1].Message().find(
132-
"Invalid parameter"));
131+
"Invalid parameter"));
133132
EXPECT_EQ(errors[2].Code(), sdf::ErrorCode::PARAMETER_ERROR);
134133
EXPECT_NE(std::string::npos, errors[2].Message().find(
135-
"Invalid boolean value"));
134+
"Invalid boolean value"));
136135
EXPECT_EQ(errors[3].Code(), sdf::ErrorCode::PARAMETER_ERROR);
137136
EXPECT_NE(std::string::npos, errors[3].Message().find(
138-
"Invalid [min] parameter in SDFormat description of [key]"));
137+
"Invalid [min] parameter in SDFormat description of [key]"));
139138
EXPECT_EQ(errors[4].Code(), sdf::ErrorCode::PARAMETER_ERROR);
140139
EXPECT_NE(std::string::npos, errors[4].Message().find(
141-
"Invalid boolean value"));
140+
"Invalid boolean value"));
142141
EXPECT_EQ(errors[5].Code(), sdf::ErrorCode::PARAMETER_ERROR);
143142
EXPECT_NE(std::string::npos, errors[5].Message().find(
144-
"Invalid [max] parameter in SDFormat description of [key]"));
143+
"Invalid [max] parameter in SDFormat description of [key]"));
145144

146145
// Check nothing has been printed
147146
EXPECT_TRUE(buffer.str().empty()) << buffer.str();
@@ -153,7 +152,7 @@ TEST(ErrorOutput, ElementErrorOutput)
153152
{
154153
std::stringstream buffer;
155154
sdf::testing::RedirectConsoleStream redir(
156-
sdf::Console::Instance()->GetMsgStream(), &buffer);
155+
sdf::Console::Instance()->GetMsgStream(), &buffer);
157156

158157
sdf::Errors errors;
159158
sdf::ElementPtr elem = std::make_shared<sdf::Element>();
@@ -163,59 +162,84 @@ TEST(ErrorOutput, ElementErrorOutput)
163162
ASSERT_EQ(errors.size(), 1u);
164163
EXPECT_EQ(errors[0].Code(), sdf::ErrorCode::ELEMENT_ERROR);
165164
EXPECT_NE(std::string::npos, errors[0].Message().find(
166-
"Unable to find value for key [test]"));
165+
"Unable to find value for key [test]"));
167166

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

174173
errors.clear();
175174
elem->AddAttribute(
176-
"invalidAttribute", "int", "invalidFormat", false, errors);
175+
"invalidAttribute", "int", "invalidFormat", false, errors);
177176
ASSERT_EQ(errors.size(), 2u);
178177
EXPECT_NE(std::string::npos, errors[0].Message().find(
179-
"Invalid argument. Unable to set value [invalidFormat]"
180-
" for key[invalidAttribute]"));
178+
"Invalid argument. Unable to set value [invalidFormat]"
179+
" for key[invalidAttribute]"));
181180
EXPECT_NE(std::string::npos, errors[1].Message().find(
182-
"Invalid parameter"));
181+
"Invalid parameter"));
183182

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

208207
elem->GetElement("nonExistentElement", errors);
209208
ASSERT_EQ(errors.size(), 1u);
210209
EXPECT_NE(std::string::npos, errors[0].Message().find(
211-
"Missing element description for [nonExistentElement]"));
210+
"Missing element description for [nonExistentElement]"));
212211
errors.clear();
213212

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

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

0 commit comments

Comments
 (0)