property_loader_test.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002   tag: The SourceWorks  Tue Sep 7 00:54:57 CEST 2010  property_loader_test.cpp
00003 
00004                         property_loader_test.cpp -  description
00005                            -------------------
00006     begin                : Tue September 07 2010
00007     copyright            : (C) 2010 The SourceWorks
00008     email                : peter@thesourceworks.com
00009 
00010  ***************************************************************************
00011  *                                                                         *
00012  *   This program is free software; you can redistribute it and/or modify  *
00013  *   it under the terms of the GNU General Public License as published by  *
00014  *   the Free Software Foundation; either version 2 of the License, or     *
00015  *   (at your option) any later version.                                   *
00016  *                                                                         *
00017  ***************************************************************************/
00018 
00019 
00020 #include "unit.hpp"
00021 #include "marsh/PropertyLoader.hpp"
00022 #include "TaskContext.hpp"
00023 
00024 struct LoaderTest {
00025     LoaderTest() : tc("tc"), pl(&tc),
00026             pstring("pstring","pstringd","Hello World"),
00027             pchar("pchar","pchard",'H'),
00028             pdouble("pdouble", "pdoubled", 1.23456),
00029             pbag("pbag","pbagd"),
00030             pints("pints", "pintsd", vector<int>(3,4)),
00031             pdoubles("pdoubles", "pdoublesd", vector<double>(3,4.123))
00032     {
00033 
00034     }
00035     TaskContext tc;
00036     PropertyLoader pl;
00037     Property<string> pstring;
00038     Property<char> pchar;
00039     Property<double> pdouble;
00040     Property<PropertyBag> pbag;
00041     Property<std::vector<int> > pints;
00042     Property<std::vector<double> > pdoubles;
00043     PropertyBag bag;
00044 };
00045 
00046 
00047 BOOST_FIXTURE_TEST_SUITE( PropertyLoaderTest, LoaderTest )
00048 
00049 
00053 BOOST_AUTO_TEST_CASE( testPropSaveLoad )
00054 {
00055     std::string filename = "property_writing.tst";
00056     tc.addProperty(pstring);
00057     tc.addProperty(pchar);
00058     tc.addProperty(pdouble);
00059     tc.addProperty(pdoubles);
00060     tc.addProperty("newbag", bag).doc("newbag doc"); // takes reference to bag (PropertyBag&)
00061     bag.addProperty( pdouble );
00062     bag.addProperty( pdoubles );
00063     bag.addProperty( pbag ); // takes reference to bpag (PropertyBase&)
00064     pbag.value().addProperty( pchar );
00065 
00066     // save all to fresh file
00067     BOOST_CHECK( pl.save(filename, true) );
00068     // configure all from file.
00069     BOOST_CHECK( pl.configure(filename, true) );
00070 
00071     // configure all fails with one missing element.
00072     bag.addProperty( pstring );
00073 
00074     // TEST FAILS : TO BE FIXED in updateProperties()
00075     //BOOST_REQUIRE( !pl.configure(filename, true) );
00076 
00077     // configure some does not fail with one missing element.
00078     BOOST_CHECK( pl.configure(filename, false) );
00079 
00080     // save some (does not add new props to file:
00081     BOOST_CHECK( pl.save(filename, false) );
00082 
00083     // TEST FAILS : TO BE FIXED in updateProperties()
00084     //BOOST_REQUIRE( !pl.configure(filename, true) ); // must still fail, not all were saved.
00085 
00086     // save all:
00087     BOOST_CHECK( pl.save(filename, true) );
00088     BOOST_CHECK( pl.configure(filename, true) ); // all were saved.
00089 }
00090 
00094 BOOST_AUTO_TEST_CASE( testPropUnknown )
00095 {
00096     std::string filename = "property_unknown.tst";
00097     tc.addProperty(pints);
00098 
00099     BOOST_CHECK( pl.save(filename, true) ); // produces empty file.
00100     BOOST_CHECK( !pl.configure(filename, true) ); // must fail, was not serialized !
00101     BOOST_CHECK( pl.configure(filename, false) );
00102 
00103     // test unknown in bag:
00104     tc.properties()->removeProperty( &pints );
00105     tc.addProperty( "bag", bag ).doc( "bag doc" );
00106     bag.addProperty(pints);
00107 
00108     BOOST_CHECK( pl.save(filename, true) ); // produces file with bag.
00109 
00110     // TEST FAILS : TO BE FIXED in updateProperties()
00111     //BOOST_CHECK( !pl.configure(filename, true) ); // must fail, was not serialized !
00112 
00113     BOOST_CHECK( pl.configure(filename, false) );
00114 }
00115 
00116 BOOST_AUTO_TEST_CASE( testPropLoading )
00117 {
00118     std::string filename = "property_loading.cpf";
00119     BOOST_REQUIRE( pl.load(filename) );
00120 
00121     BOOST_CHECK( tc.provides()->hasProperty("load1") );
00122     BOOST_CHECK( tc.provides()->hasProperty("load2") );
00123     BOOST_CHECK( tc.provides()->hasProperty("bag1"));
00124     Property<PropertyBag> bag1 = tc.properties()->getProperty("bag1");
00125     BOOST_REQUIRE( bag1.ready() );
00126     BOOST_CHECK( bag1.getDescription() == "Bag1");
00127     PropertyBag& bag = bag1.value();
00128     BOOST_CHECK_EQUAL( bag.size(), 2);
00129     BOOST_CHECK( bag.find("bagload1"));
00130     BOOST_CHECK( bag.find("bagload2"));
00131     Property<int> bagload1 = bag.getProperty("bagload1");
00132     Property<string> bagload2 = bag.getProperty("bagload2");
00133 
00134     BOOST_REQUIRE(bagload1.ready());
00135     BOOST_CHECK_EQUAL(bagload1.get(), 3);
00136     BOOST_CHECK_EQUAL(bagload2.get(), "3 3 3");
00137 }
00138 
00139 BOOST_AUTO_TEST_CASE( testPropStoring )
00140 {
00141     tc.addProperty(pstring);
00142     tc.addProperty(pchar);
00143     tc.addProperty(pdouble);
00144     tc.addProperty(pdoubles);
00145     tc.addProperty("newbag", bag).doc("newbag doc"); // takes reference to bag (PropertyBag&)
00146     bag.addProperty( pdouble );
00147     bag.addProperty( pdoubles );
00148     bag.addProperty( pbag ); // takes reference to bpag (PropertyBase&)
00149     pbag.value().addProperty( pchar );
00150 
00151     std::string filename = "property_storing.tst";
00152     BOOST_REQUIRE( pl.store(filename) );
00153 
00154     tc.properties()->clear();
00155 
00156     // check by using load:
00157     BOOST_REQUIRE( pl.load(filename) );
00158 
00159     BOOST_CHECK( tc.provides()->hasProperty("pstring") );
00160     BOOST_CHECK( tc.provides()->hasProperty("pchar") );
00161     BOOST_CHECK( tc.provides()->hasProperty("pdouble") );
00162     BOOST_CHECK( tc.provides()->hasProperty("pdoubles") );
00163     BOOST_CHECK( tc.provides()->hasProperty("newbag"));
00164     Property<PropertyBag> bag1 = tc.properties()->getProperty("newbag");
00165     BOOST_REQUIRE( bag1.ready() );
00166     BOOST_CHECK( bag1.getDescription() == "newbag doc");
00167     PropertyBag& bag = bag1.value();
00168     BOOST_CHECK_EQUAL( bag.size(), 3);
00169     BOOST_CHECK( bag.find("pdouble"));
00170     BOOST_CHECK( bag.find("pdoubles"));
00171     BOOST_CHECK( bag.find("pbag"));
00172     Property<double> bagdouble = bag.getProperty("pdouble");
00173     Property<vector<double> > bagvector = bag.getProperty("pdoubles");
00174 
00175     BOOST_REQUIRE(bagdouble.ready());
00176     BOOST_CHECK_EQUAL( bagdouble.value(), 1.23456);
00177     BOOST_REQUIRE(bagvector.ready());
00178     BOOST_REQUIRE_EQUAL(bagvector.value().size(), 3);
00179     BOOST_CHECK_EQUAL(bagvector.value()[0], 4.123);
00180     BOOST_CHECK_EQUAL(bagvector.value()[1], 4.123);
00181     BOOST_CHECK_EQUAL(bagvector.value()[2], 4.123);
00182 }
00183 
00184 BOOST_AUTO_TEST_SUITE_END()


rtt
Author(s): RTT Developers
autogenerated on Mon Oct 6 2014 03:13:38