OpenWalnut  1.4.0
WPropertyStruct_test.h
00001 //---------------------------------------------------------------------------
00002 //
00003 // Project: OpenWalnut ( http://www.openwalnut.org )
00004 //
00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
00006 // For more information see http://www.openwalnut.org/copying
00007 //
00008 // This file is part of OpenWalnut.
00009 //
00010 // OpenWalnut is free software: you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as published by
00012 // the Free Software Foundation, either version 3 of the License, or
00013 // (at your option) any later version.
00014 //
00015 // OpenWalnut is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 // GNU Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public License
00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
00022 //
00023 //---------------------------------------------------------------------------
00024 
00025 #ifndef WPROPERTYSTRUCT_TEST_H
00026 #define WPROPERTYSTRUCT_TEST_H
00027 
00028 #include <string>
00029 
00030 #include <cxxtest/TestSuite.h>
00031 
00032 #include "../WPropertyTypes.h"
00033 #include "../WPropertyVariable.h"
00034 #include "../WPropertyStruct.h"
00035 
00036 /**
00037  * Test WPropertyStruct.
00038  */
00039 class WPropertyStructTest : public CxxTest::TestSuite
00040 {
00041 public:
00042     /**
00043      * Test instantiation, also test name and description and type (from WPropertyBase)
00044      */
00045     void testInstantiation( void )
00046     {
00047         typedef WPropertyStruct< WPropInt, WPropBool > TestStruct;
00048 
00049         TestStruct* prop = new TestStruct( "Hallo", "Description Text" );
00050         TS_ASSERT( prop->size() == 2 );
00051 
00052         // although this is not a proper test, it fails compilation and therefore informs the programmer that he did something wrong
00053         BOOST_MPL_ASSERT( ( boost::is_same< TestStruct::TupleType, boost::tuple<  WPropInt, WPropBool > > ) );
00054         BOOST_MPL_ASSERT( ( boost::is_same< TestStruct::TypeVector,
00055                                             boost::mpl::vector<  WPropInt, WPropBool > > // NOLINT
00056                         ) );
00057     }
00058 
00059     /**
00060      * Test the set method
00061      */
00062     void testSet( void )
00063     {
00064         // do not test setting the properties here using one of the getProperty methods. Setting properties directly is tested in the appropriate
00065         // tests
00066 
00067         // we test getting/setting via string here
00068 
00069         // create the prop
00070         typedef WPropertyStruct< WPropInt, WPropBool >::SPtr TestStruct;
00071         TestStruct prop( new TestStruct::element_type( "Hallo", "Description Text" ) );
00072 
00073         // set some defaults
00074         prop->getProperty< 0 >()->set( 12 );
00075         prop->getProperty< 1 >()->set( true );
00076 
00077         // get as string
00078         std::string got = prop->getAsString();
00079 
00080         // change the value a little bit
00081         prop->getProperty< 0 >()->set( 111 );
00082         prop->getProperty< 1 >()->set( false );
00083 
00084         // set by string and check values
00085         prop->setAsString( got );
00086 
00087         TS_ASSERT( prop->getProperty< 0 >()->get() == 12 );
00088         TS_ASSERT( prop->getProperty< 1 >()->get() == true );
00089 
00090         // also test setting via property
00091         TestStruct prop2( new TestStruct::element_type( "Hallo2", "Description Text" ) );
00092         prop2->set( prop );
00093         TS_ASSERT( prop2->getProperty< 0 >()->get() == 12 );
00094         TS_ASSERT( prop2->getProperty< 1 >()->get() == true );
00095     }
00096 
00097     /**
00098      * Test getter
00099      */
00100     void testGet( void )
00101     {
00102         typedef WPropertyStruct< WPropInt, WPropBool > TestStruct;
00103         TestStruct prop( "Hallo", "Description Text" );
00104 
00105         // compile time test: this fails during compilation if something is wrong
00106         WPropInt i = prop.getProperty< 0 >();
00107         TS_ASSERT( i.get() );
00108         WPropBool b = prop.getProperty< 1 >();
00109         TS_ASSERT( b.get() );
00110 
00111         // get the second prop
00112         WPropertyBase::SPtr base = prop.getProperty( 1 );
00113         TS_ASSERT( base.get() );
00114 
00115         // this has to be a bool prop
00116         TS_ASSERT( base->toPropBool().get() );
00117     }
00118 };
00119 
00120 #endif  // WPROPERTYSTRUCT_TEST_H
00121 
00122 
00123