Skip to content

Commit 6304b31

Browse files
olivier-stasseOlivier Stasse
authored andcommitted
[command] Fix makeCommandVerbose
This command is used to display some information in a ostream and get back a string. Add an unit-test to verify it. Additionnally the unit-test improves makeCommandReturnType.
1 parent d4af652 commit 6304b31

File tree

2 files changed

+44
-16
lines changed

2 files changed

+44
-16
lines changed

include/dynamic-graph/command-bind.h

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -382,23 +382,14 @@ makeCommandVerbose(E &entity, typename CommandVerbose<E>::function_t function,
382382
return NULL;
383383
}
384384

385-
template <class E>
386-
CommandVerbose<E> *makeCommandVerbose(
387-
E &entity,
388-
// void (E::*function) (std::ostream&) const,
389-
typename CommandVerbose<E>::memberFunctionConst_ptr_t function,
390-
const std::string &docString) {
391-
return new CommandVerbose<E>(entity, boost::bind(function, &entity, _1),
392-
docString);
393-
return NULL;
394-
}
395385

396386
template <class E>
397387
CommandVerbose<E> *
398388
makeCommandVerbose(E &entity,
399-
typename CommandVerbose<E>::memberFunction_ptr_t function,
389+
void (E::*function)(std::ostream &),
400390
const std::string &docString) {
401-
return new CommandVerbose<E>(entity, boost::bind(function, &entity, _1),
391+
return new CommandVerbose<E>(entity,
392+
boost::bind(function, &entity,_1),
402393
docString);
403394
return NULL;
404395
}

tests/command-test.cpp

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,19 @@ class CustomEntity : public Entity {
3535
bool test_two_args_;
3636
bool test_three_args_;
3737
bool test_four_args_;
38-
38+
bool test_one_arg_ret_;
39+
bool test_two_args_ret_;
40+
3941
virtual const std::string &getClassName() const { return CLASS_NAME; }
4042
explicit CustomEntity(const std::string &n) : Entity(n) {
4143
test_zero_arg_ = false;
4244
test_one_arg_ = false;
4345
test_two_args_ = false;
4446
test_three_args_ = false;
4547
test_four_args_ = false;
46-
48+
test_one_arg_ret_ = false;
49+
test_two_args_ret_ = false;
50+
4751
addCommand("0_arg", makeCommandVoid0(*this, &CustomEntity::zero_arg,
4852
docCommandVoid0("zero arg")));
4953

@@ -73,6 +77,12 @@ class CustomEntity : public Entity {
7377
*this, &CustomEntity::two_args_ret,
7478
docCommandVoid2("two args", "int","int")));
7579

80+
81+
addCommand("cmd_verbose",
82+
makeCommandVerbose(
83+
*this,&CustomEntity::cmd_verbose,
84+
docCommandVerbose("Display some information")));
85+
7686
/// Generating an exception by adding a command which already exist
7787
bool res = false;
7888
std::string e_1_arg("1_arg");
@@ -100,11 +110,15 @@ class CustomEntity : public Entity {
100110
test_four_args_ = true;
101111
}
102112

103-
int one_arg_ret(const int &) { test_one_arg_ = true; return 2;}
113+
int one_arg_ret(const int &) { test_one_arg_ret_ = true; return 2;}
104114

105115
std::string two_args_ret(const int &, const int &)
106-
{ test_two_args_ = true; return std::string("return");}
116+
{ test_two_args_ret_ = true; return std::string("return");}
107117

118+
void cmd_verbose(std::ostream &oss)
119+
{ std::string as("print verbose");
120+
oss << as;
121+
}
108122
};
109123
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(CustomEntity, "CustomEntity");
110124
} // namespace dynamicgraph
@@ -152,6 +166,29 @@ BOOST_AUTO_TEST_CASE(command_test) {
152166
BOOST_CHECK(entity.test_three_args_);
153167
BOOST_CHECK(entity.test_four_args_);
154168

169+
// With return type.
170+
vec_fname.clear();
171+
vec_fname.push_back(std::string("1_arg_r"));
172+
vec_fname.push_back(std::string("2_args_r"));
173+
values.clear();
174+
175+
for (unsigned int i = 0; i < 2; i++) {
176+
it_map = aCommandMap.find(vec_fname[i]);
177+
if (it_map == aCommandMap.end())
178+
{
179+
BOOST_CHECK(false);
180+
exit(-1);
181+
}
182+
values.push_back(aValue);
183+
it_map->second->setParameterValues(values);
184+
Value aValue =it_map->second->execute();
185+
it_map->second->owner();
186+
it_map->second->getDocstring();
187+
}
188+
189+
BOOST_CHECK(entity.test_one_arg_ret_);
190+
BOOST_CHECK(entity.test_two_args_ret_);
191+
155192
std::vector<Value> values_two;
156193
values_two.push_back(aValue);
157194
/// Wrong number of arguments

0 commit comments

Comments
 (0)