7 #define BOOST_TEST_DYN_LINK
8 #define BOOST_TEST_MODULE "Test module for XML options manipulation"
12 #include "rapidxml/rapidxml.hpp"
13 #include <boost/test/unit_test.hpp>
14 #include <boost/assign/list_of.hpp>
40 Core::instance().environment().options().set(
"exception_backtrace",
false);
41 Core::instance().environment().options().set(
"exception_outputs",
false);
45 BOOST_FIXTURE_TEST_SUITE( XmlSignalOptions_TestSuite,
XmlFixture )
58 std::vector<std::string> data(4);
60 data[0] =
"This is the first item";
61 data[1] =
"Here is the second item";
62 data[2] =
"Now we have the third item";
63 data[3] =
"And then the last item";
65 map.set_value(
"my_bool", class_name<bool>(),
"false",
"MyBool description").set_attribute(
"mode",
"adv" );
66 map.set_value(
"cfl", class_name<Real>(),
"3.1415",
"CFL number").set_attribute(
"mode",
"basic" );
67 map.set_array(
"strings", class_name<std::string>(),
option_vector_to_str(data,
";"),
";",
"Some special data");
68 map.set_value(
"website", class_name<URI>(),
"http://coolfluidsrv.vki.ac.be",
"CF website")
69 .set_attribute( Protocol::Tags::attr_uri_schemes(),
"http");
76 boost::shared_ptr< OptionT<bool> > my_bool;
77 BOOST_CHECK( options.
check(
"my_bool") );
79 BOOST_CHECK( my_bool.get() !=
nullptr );
80 BOOST_CHECK_EQUAL( my_bool->name(), std::string(
"my_bool") );
81 BOOST_CHECK_EQUAL( my_bool->type(), std::string( common::class_name<bool>() ) );
82 BOOST_CHECK_EQUAL( my_bool->description(), std::string(
"MyBool description") );
83 BOOST_CHECK_EQUAL( my_bool->value<
bool>(),
false );
84 BOOST_CHECK_EQUAL( my_bool->has_tag(
"basic"), false );
90 boost::shared_ptr< OptionT<cf3::Real> > cfl;
91 BOOST_CHECK( options.
check(
"cfl") );
93 BOOST_CHECK( cfl.get() !=
nullptr );
94 BOOST_CHECK_EQUAL( cfl->name(), std::string(
"cfl") );
95 BOOST_CHECK_EQUAL( cfl->type(), std::string( common::class_name<cf3::Real>() ) );
96 BOOST_CHECK_EQUAL( cfl->description(), std::string(
"CFL number") );
97 BOOST_CHECK_EQUAL( cfl->value<cf3::Real>(), 3.1415 );
98 BOOST_CHECK_EQUAL( cfl->has_tag(
"basic"), true );
103 boost::shared_ptr< OptionArray<std::string> > strings;
104 BOOST_CHECK( options.
check(
"strings") );
106 BOOST_CHECK( strings.get() !=
nullptr );
107 BOOST_CHECK_EQUAL( strings->name(), std::string(
"strings") );
108 BOOST_CHECK_EQUAL( std::string(strings->element_type()), std::string(common::class_name<std::string>()) );
109 BOOST_CHECK_EQUAL( strings->description(), std::string(
"Some special data") );
110 BOOST_CHECK_EQUAL( strings->has_tag(
"basic"), false );
112 std::vector<std::string> data_read = strings->value< std::vector<std::string> >();
113 BOOST_CHECK_EQUAL( data.size(), data_read.size() );
115 for(
int i = 0 ; i < data.size() ; ++i)
116 BOOST_CHECK_EQUAL( data[i], data_read[i] );
121 boost::shared_ptr< OptionURI > website;
122 BOOST_CHECK( options.
check(
"website") );
123 website = boost::dynamic_pointer_cast<
OptionURI>(options.
store[
"website"]);
124 BOOST_CHECK( website.get() !=
nullptr );
125 BOOST_CHECK_EQUAL( website->name(), std::string(
"website") );
126 BOOST_CHECK_EQUAL( website->type(), std::string( common::class_name<URI>() ) );
127 BOOST_CHECK_EQUAL( website->description(), std::string(
"CF website") );
128 BOOST_CHECK_EQUAL( website->value<
URI>().
string(), std::string(
"http://coolfluidsrv.vki.ac.be") );
129 BOOST_CHECK_EQUAL( website->has_tag(
"basic"), false );
130 BOOST_CHECK_EQUAL( website->supported_protocols().size(), 1 );
131 BOOST_CHECK_EQUAL( website->supported_protocols()[0], URI::Scheme::HTTP );
138 boost::shared_ptr< XmlDoc > xmldoc;
139 boost::shared_ptr<Option> option;
146 "<value key=\"theOption\" is_option=\"true\" descr=\"This is a description\">"
149 node.
content = xmldoc->content->first_node(
"value");
150 BOOST_REQUIRE_NO_THROW( option = SignalOptions::xml_to_option(node) );
152 BOOST_CHECK_EQUAL( option->name(), std::string(
"theOption"));
154 BOOST_CHECK_EQUAL( option->type(), std::string(
"real") );
156 BOOST_CHECK_EQUAL( option->value<Real>(), Real(2.71) );
158 BOOST_CHECK_EQUAL( option->description(), std::string(
"This is a description") );
164 "<value key=\"theAnswer\" is_option=\"true\">"
165 " <integer>42</integer>"
167 node.
content = xmldoc->content->first_node(
"value");
168 BOOST_REQUIRE_NO_THROW( option = SignalOptions::xml_to_option(node) );
170 BOOST_CHECK_EQUAL( option->name(), std::string(
"theAnswer"));
172 BOOST_CHECK_EQUAL( option->type(), std::string(
"integer") );
174 BOOST_CHECK_EQUAL( option->value<
int>(),
int(42) );
176 BOOST_CHECK_EQUAL( option->description(), std::string(
"") );
182 "<value is_option=\"true\">"
183 " <integer>42</integer>"
185 node.
content = xmldoc->content->first_node(
"value");
186 BOOST_CHECK_THROW( SignalOptions::xml_to_option(node),
ProtocolError );
194 XmlNode theAnswer =
map.set_value(
"theAnswer", class_name<int>(),
"42");
195 XmlNode pi =
map.set_value(
"pi", class_name<Real>(),
"3.14159" );
196 XmlNode euler =
map.set_value(
"euler", class_name<Real>(),
"2.71" );
202 BOOST_CHECK( !SignalOptions::xml_to_option(theAnswer)->has_tag(
"basic") );
203 BOOST_CHECK( SignalOptions::xml_to_option(pi)->has_tag(
"basic") );
204 BOOST_CHECK( !SignalOptions::xml_to_option(euler)->has_tag(
"basic") );
211 boost::shared_ptr< XmlDoc > xmldoc(
new XmlDoc());
212 boost::shared_ptr<Option> option;
214 Map map(xmldoc->add_node(
"map"));
216 XmlNode optBool =
map.set_value(
"optBool", class_name<bool>(),
"true");
217 XmlNode optInt =
map.set_value(
"optInt", class_name<int>(),
"-15468");
218 XmlNode optUint =
map.set_value(
"optUint", class_name<Uint>(),
"17513214");
219 XmlNode optReal =
map.set_value(
"optReal", class_name<Real>(),
"3.14159");
220 XmlNode optString =
map.set_value(
"optString", class_name<std::string>(), std::string(
"I am a string value"));
221 XmlNode optURI =
map.set_value(
"optURI", class_name<URI>(),
"cpath:/");
223 XmlNode wrongOpt =
map.content.add_node( Protocol::Tags::node_value() );
225 wrongOpt.
set_attribute( Protocol::Tags::attr_key(),
"optOfUnknownType");
226 wrongOpt.
add_node(
"unknown_type",
"Don't know how to interpret this.");
229 BOOST_CHECK_NO_THROW( option = SignalOptions::xml_to_option(optBool) );
230 BOOST_CHECK_EQUAL( option->type(), std::string(
"bool") );
231 BOOST_CHECK_EQUAL( option->value<
bool>(),
true);
234 BOOST_CHECK_NO_THROW( option = SignalOptions::xml_to_option(optInt) );
235 BOOST_CHECK_EQUAL( option->type(), std::string(
"integer") );
236 BOOST_CHECK_EQUAL( option->value<
int>(), -15468 );
239 BOOST_CHECK_NO_THROW( option = SignalOptions::xml_to_option(optUint) );
240 BOOST_CHECK_EQUAL( option->type(), std::string(
"unsigned") );
241 BOOST_CHECK_EQUAL( option->value<
Uint>(),
Uint(17513214) );
244 BOOST_CHECK_NO_THROW( option = SignalOptions::xml_to_option(optReal) );
245 BOOST_CHECK_EQUAL( option->type(), std::string(
"real") );
246 BOOST_CHECK_EQUAL( option->value<Real>(), Real(3.14159) );
249 BOOST_CHECK_NO_THROW( option = SignalOptions::xml_to_option(optString) );
250 BOOST_CHECK_EQUAL( option->type(), std::string(
"string") );
251 BOOST_CHECK_EQUAL( option->value<std::string>(), std::string(
"I am a string value") );
254 BOOST_CHECK_NO_THROW( option = SignalOptions::xml_to_option(optURI) );
255 BOOST_CHECK_EQUAL( option->type(), std::string(
"uri") );
256 BOOST_CHECK_EQUAL( option->value<
URI>().
string(), std::string(
"cpath:/") );
259 BOOST_CHECK_THROW( SignalOptions::xml_to_option(wrongOpt),
ValueNotFound );
266 boost::shared_ptr< XmlDoc > xmldoc(
new XmlDoc());
267 Map map(xmldoc->add_node(
"map"));
268 const char * tag = Protocol::Tags::attr_uri_schemes();
270 XmlNode opt1 =
map.set_value(
"opt1", class_name<URI>(),
URI().
string());
271 XmlNode opt2 =
map.set_value(
"opt2", class_name<URI>(),
URI().
string());
272 XmlNode opt3 =
map.set_value(
"opt3", class_name<URI>(),
URI().
string());
273 XmlNode opt4 =
map.set_value(
"opt4", class_name<URI>(),
URI().
string());
274 XmlNode opt5 =
map.set_value(
"opt5", class_name<URI>(),
URI().
string());
282 boost::shared_ptr< OptionURI > opt;
283 std::vector<URI::Scheme::Type> vect;
286 opt = boost::dynamic_pointer_cast<
OptionURI>( SignalOptions::xml_to_option(opt1) );
288 BOOST_CHECK( opt->supported_protocols().empty() );
291 opt = boost::dynamic_pointer_cast<
OptionURI>( SignalOptions::xml_to_option(opt2) );
293 BOOST_CHECK( opt->supported_protocols().empty() );
296 opt = boost::dynamic_pointer_cast<
OptionURI>( SignalOptions::xml_to_option(opt3) );
298 vect = opt->supported_protocols();
300 BOOST_CHECK_EQUAL( vect.size() , size_t(1) );
301 BOOST_CHECK_EQUAL( vect[0], URI::Scheme::HTTP );
304 opt = boost::dynamic_pointer_cast<
OptionURI>( SignalOptions::xml_to_option(opt4) );
308 BOOST_CHECK_EQUAL( vect.size() , size_t(3) );
309 BOOST_CHECK_EQUAL( vect[0], URI::Scheme::CPATH );
310 BOOST_CHECK_EQUAL( vect[1], URI::Scheme::HTTPS );
314 BOOST_CHECK_THROW( SignalOptions::xml_to_option( opt5 ),
CastingFailed);
321 boost::shared_ptr< XmlDoc > xmldoc(
new XmlDoc());
322 Map map(xmldoc->add_node(
"map"));
323 boost::shared_ptr<Option> option;
324 std::vector<int> vectInt = list_of<int>(344646)(544684)(446454)
327 XmlNode optInt =
map.set_value(
"optInt", class_name<int>(),
"13");
328 XmlNode optIntRestrList =
map.set_value(
"optIntRestrList", class_name<int>(),
"-15468");
330 Map(optIntRestrList).set_array(Protocol::Tags::key_restricted_values(), class_name<int>(),
option_vector_to_str(vectInt,
" ; "),
" ; ");
333 BOOST_CHECK_NO_THROW(option = SignalOptions::xml_to_option( optInt ) );
334 BOOST_CHECK( !option->has_restricted_list() );
337 BOOST_CHECK_NO_THROW(option = SignalOptions::xml_to_option( optIntRestrList ) );
339 BOOST_CHECK( option->has_restricted_list() );
341 std::vector<boost::any> & restr_list = option->restricted_list();
343 BOOST_CHECK_EQUAL( restr_list.size(), size_t( vectInt.size() ) );
344 BOOST_CHECK_EQUAL( boost::any_cast<int>(restr_list[0]), vectInt[0] );
345 BOOST_CHECK_EQUAL( boost::any_cast<int>(restr_list[1]), vectInt[1] );
346 BOOST_CHECK_EQUAL( boost::any_cast<int>(restr_list[2]), vectInt[2] );
347 BOOST_CHECK_EQUAL( boost::any_cast<int>(restr_list[3]), vectInt[3] );
348 BOOST_CHECK_EQUAL( boost::any_cast<int>(restr_list[4]), vectInt[4] );
355 std::vector<std::string> args(12);
356 std::vector<std::string> args_mod(2);
360 args[0] =
"my_bool:bool=true";
361 args[1] =
"my_uint:unsigned=7489";
362 args[2] =
"my_int:integer=-4567";
363 args[3] =
"my_real:real=3.1415";
364 args[4] =
"my_string:string=Hello World!";
365 args[5] =
"my_uri:uri=cpath:/Tools";
368 args[6] =
"my_bool_array:array[bool]=true,false,true, true";
369 args[7] =
"my_uint_array:array[unsigned]=7489, 12,65,0";
370 args[8] =
"my_int_array:array[integer]=-4567,42, 730";
371 args[9] =
"my_real_array:array[real]=3.1415,2.71,0.99,-12.35";
372 args[10] =
"my_string_array:array[string]=Hello World!, VKI,COOLFluiD";
373 args[11] =
"my_uri_array:array[uri]=cpath:/Tools, http://coolfluidsrv.vki.ac.be/cdash,file:/usr/bin/gcc";
376 args_mod[0] =
"my_int:integer=42";
377 args_mod[1] =
"my_real_array:array[real]=1.56, 7894.012, 32.768";
381 BOOST_CHECK_EQUAL ( options.
store.size(), args.size() );
388 BOOST_CHECK ( options.
check(
"my_bool") );
389 BOOST_CHECK_EQUAL ( std::string(options[
"my_bool"].type()), std::string(
"bool") );
390 BOOST_CHECK ( options[
"my_bool"].value<bool>() );
393 BOOST_CHECK ( options.
check(
"my_uint") );
394 BOOST_CHECK_EQUAL ( std::string(options[
"my_uint"].type()), std::string(
"unsigned") );
395 BOOST_CHECK_EQUAL ( options[
"my_uint"].value<Uint>(), 7489 );
398 BOOST_CHECK ( options.
check(
"my_int") );
399 BOOST_CHECK_EQUAL ( std::string(options[
"my_int"].type()), std::string(
"integer") );
400 BOOST_CHECK_EQUAL ( options[
"my_int"].value<int>(), -4567 );
403 BOOST_CHECK ( options.
check(
"my_real") );
404 BOOST_CHECK_EQUAL ( std::string(options[
"my_real"].type()), std::string(
"real") );
405 BOOST_CHECK_EQUAL ( options[
"my_real"].value<Real>(), 3.1415 );
408 BOOST_CHECK ( options.
check(
"my_string") );
409 BOOST_CHECK_EQUAL ( std::string(options[
"my_string"].type()), std::string(
"string") );
410 BOOST_CHECK_EQUAL ( options[
"my_string"].value<std::string>(), std::string(
"Hello World!") );
413 BOOST_CHECK ( options.
check(
"my_uri") );
414 BOOST_CHECK_EQUAL ( std::string(options[
"my_uri"].type()), std::string(
"uri") );
415 BOOST_CHECK_EQUAL ( options[
"my_uri"].value<URI>().path(),
URI(
"cpath:/Tools").path() );
423 BOOST_CHECK ( options.
check(
"my_bool_array") );
424 BOOST_CHECK_EQUAL ( std::string(options[
"my_bool_array"].type()), std::string(
"array[bool]") );
425 std::vector<bool> bool_array = options[
"my_bool_array"].
value< std::vector<bool> >();
426 BOOST_CHECK_EQUAL ( bool_array.size(), 4 );
427 BOOST_CHECK ( bool_array[0] );
428 BOOST_CHECK ( !bool_array[1] );
429 BOOST_CHECK ( bool_array[2] );
430 BOOST_CHECK ( bool_array[3] );
433 BOOST_CHECK ( options.
check(
"my_uint_array") );
434 BOOST_CHECK_EQUAL ( std::string(options[
"my_uint_array"].type()), std::string(
"array[unsigned]") );
435 std::vector<Uint> uint_array = options[
"my_uint_array"].
value< std::vector<Uint> >();
436 BOOST_CHECK_EQUAL ( uint_array.size(), 4 );
437 BOOST_CHECK_EQUAL ( uint_array[0], 7489 );
438 BOOST_CHECK_EQUAL ( uint_array[1], 12 );
439 BOOST_CHECK_EQUAL ( uint_array[2], 65 );
440 BOOST_CHECK_EQUAL ( uint_array[3], 0 );
443 BOOST_CHECK ( options.
check(
"my_int_array") );
444 BOOST_CHECK_EQUAL ( std::string(options[
"my_int_array"].type()), std::string(
"array[integer]") );
445 std::vector<int> int_array = options[
"my_int_array"].
value< std::vector<int> >();
446 BOOST_CHECK_EQUAL ( int_array.size(), 3 );
447 BOOST_CHECK_EQUAL ( int_array[0], -4567 );
448 BOOST_CHECK_EQUAL ( int_array[1], 42 );
449 BOOST_CHECK_EQUAL ( int_array[2], 730 );
452 BOOST_CHECK ( options.
check(
"my_real_array") );
453 BOOST_CHECK_EQUAL ( std::string(options[
"my_real_array"].type()), std::string(
"array[real]") );
454 std::vector<Real> real_array = options[
"my_real_array"].
value< std::vector<Real> >();
455 BOOST_CHECK_EQUAL ( real_array.size(), 4 );
456 BOOST_CHECK_EQUAL ( real_array[0], 3.1415 );
457 BOOST_CHECK_EQUAL ( real_array[1], 2.71 );
458 BOOST_CHECK_EQUAL ( real_array[2], 0.99 );
459 BOOST_CHECK_EQUAL ( real_array[3], -12.35 );
462 BOOST_CHECK ( options.
check(
"my_string_array") );
463 BOOST_CHECK_EQUAL ( std::string(options[
"my_string_array"].type()), std::string(
"array[string]") );
464 std::vector<std::string> string_array = options[
"my_string_array"].
value< std::vector<std::string> >();
465 BOOST_CHECK_EQUAL ( string_array.size(), 3 );
466 BOOST_CHECK_EQUAL ( string_array[0], std::string(
"Hello World!") );
467 BOOST_CHECK_EQUAL ( string_array[1], std::string(
"VKI") );
468 BOOST_CHECK_EQUAL ( string_array[2], std::string(
"COOLFluiD") );
471 BOOST_CHECK ( options.
check(
"my_uri_array") );
472 BOOST_CHECK_EQUAL ( std::string(options[
"my_uri_array"].type()), std::string(
"array[uri]") );
473 std::vector<URI> uri_array = options[
"my_uri_array"].
value< std::vector<URI> >();
474 BOOST_CHECK_EQUAL ( uri_array.size(), 3 );
475 BOOST_CHECK_EQUAL ( uri_array[0].path(),
URI(
"cpath:/Tools").path() );
476 BOOST_CHECK_EQUAL ( uri_array[1].path(),
URI(
"http://coolfluidsrv.vki.ac.be/cdash").path() );
477 BOOST_CHECK_EQUAL ( uri_array[2].path(),
URI(
"file:/usr/bin/gcc").path() );
483 options.
set( args_mod );
485 BOOST_CHECK_EQUAL ( options.
store.size(), args.size() );
488 BOOST_CHECK_EQUAL ( options[
"my_int"].value<int>(), 42 );
491 real_array = options[
"my_real_array"].
value< std::vector<Real> >();
492 BOOST_CHECK_EQUAL ( real_array.size(), 3 );
493 BOOST_CHECK_EQUAL ( real_array[0], 1.56 );
494 BOOST_CHECK_EQUAL ( real_array[1], 7894.012 );
495 BOOST_CHECK_EQUAL ( real_array[2], 32.768 );
500 BOOST_AUTO_TEST_SUITE_END()
502 Abstracts the use of XML when adding options to a signal frame.
XmlNode add_node(const std::string &name, const std::string &value=std::string()) const
boost::shared_ptr< XmlDoc > parse_cstring(const char *str, std::size_t length)
Classes that implement the XML protocol for use in COOLFluiD.
rapidxml::xml_node< char > * content
Pointer to the underlying XML implementation.
OptionStorage_t store
storage of options
Real pi()
Definition of the Pi constant.
void set_attribute(const std::string &name, const std::string &value)
Top-level namespace for coolfluid.
const TYPE value(const std::string &opt_name) const
Get the value of the option with given name.
BOOST_AUTO_TEST_CASE(contructors)
std::string option_vector_to_str(const std::vector< T > &vec, const std::string &delim)
Helper function to convert a vector to string, skipping empty entries.
unsigned int Uint
typedef for unsigned int
bool check(const std::string &opt_name) const
void set(const std::string &pname, const boost::any &val)
SignalFrame & map(const std::string &name)
void map(Field &field, Eigen::Map< Derived > &v, const Uint row_idx, const Uint var_idx)
Most basic kernel library.
std::string string() const
const std::vector< URI::Scheme::Type > & supported_protocols() const
Map main_map
The main map.