OpenWalnut  1.5.0dev
WPropertyStruct_test.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WPROPERTYSTRUCT_TEST_H
26 #define WPROPERTYSTRUCT_TEST_H
27 
28 #include <string>
29 
30 #include <cxxtest/TestSuite.h>
31 
32 #include "../WPropertyTypes.h"
33 #include "../WPropertyVariable.h"
34 #include "../WPropertyStruct.h"
35 
36 /**
37  * Test WPropertyStruct.
38  */
39 class WPropertyStructTest : public CxxTest::TestSuite
40 {
41 public:
42  /**
43  * Test instantiation, also test name and description and type (from WPropertyBase)
44  */
45  void testInstantiation( void )
46  {
47  typedef WPropertyStruct< WPropInt, WPropBool > TestStruct;
48 
49  TestStruct* prop = new TestStruct( "Hallo", "Description Text" );
50  TS_ASSERT( prop->size() == 2 );
51 
52  // although this is not a proper test, it fails compilation and therefore informs the programmer that he did something wrong
53  BOOST_MPL_ASSERT( ( boost::is_same< TestStruct::TupleType, boost::tuple< WPropInt, WPropBool > > ) );
54  BOOST_MPL_ASSERT( ( boost::is_same< TestStruct::TypeVector,
55  boost::mpl::vector< WPropInt, WPropBool > > // NOLINT
56  ) );
57  }
58 
59  /**
60  * Test the set method
61  */
62  void testSet( void )
63  {
64  // do not test setting the properties here using one of the getProperty methods. Setting properties directly is tested in the appropriate
65  // tests
66 
67  // we test getting/setting via string here
68 
69  // create the prop
71  TestStruct prop( new TestStruct::element_type( "Hallo", "Description Text" ) );
72 
73  // set some defaults
74  prop->getProperty< 0 >()->set( 12 );
75  prop->getProperty< 1 >()->set( true );
76 
77  // get as string
78  std::string got = prop->getAsString();
79 
80  // change the value a little bit
81  prop->getProperty< 0 >()->set( 111 );
82  prop->getProperty< 1 >()->set( false );
83 
84  // set by string and check values
85  prop->setAsString( got );
86 
87  TS_ASSERT( prop->getProperty< 0 >()->get() == 12 );
88  TS_ASSERT( prop->getProperty< 1 >()->get() == true );
89 
90  // also test setting via property
91  TestStruct prop2( new TestStruct::element_type( "Hallo2", "Description Text" ) );
92  prop2->set( prop );
93  TS_ASSERT( prop2->getProperty< 0 >()->get() == 12 );
94  TS_ASSERT( prop2->getProperty< 1 >()->get() == true );
95  }
96 
97  /**
98  * Test getter
99  */
100  void testGet( void )
101  {
102  typedef WPropertyStruct< WPropInt, WPropBool > TestStruct;
103  TestStruct prop( "Hallo", "Description Text" );
104 
105  // compile time test: this fails during compilation if something is wrong
106  WPropInt i = prop.getProperty< 0 >();
107  TS_ASSERT( i.get() );
108  WPropBool b = prop.getProperty< 1 >();
109  TS_ASSERT( b.get() );
110 
111  // get the second prop
112  WPropertyBase::SPtr base = prop.getProperty( 1 );
113  TS_ASSERT( base.get() );
114 
115  // this has to be a bool prop
116  TS_ASSERT( base->toPropBool().get() );
117  }
118 };
119 
120 #endif // WPROPERTYSTRUCT_TEST_H
121 
122 
123 
std::shared_ptr< WPropertyBase > SPtr
Convenience typedef for a std::shared_ptr< WPropertyBase >
Definition: WPropertyBase.h:53
Test WPropertyStruct.
void testInstantiation(void)
Test instantiation, also test name and description and type (from WPropertyBase)
void testGet(void)
Test getter.
void testSet(void)
Test the set method.
This is a property which encapsulates a given, fixed number of other properties.
std::shared_ptr< WPropertyStructType > SPtr
Convenience typedef for a std::shared_ptr< WPropertyStructType >