00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef WPROPERTYVARIABLE_TEST_H
00026 #define WPROPERTYVARIABLE_TEST_H
00027
00028 #include <string>
00029
00030 #include <cxxtest/TestSuite.h>
00031
00032 #include "../WPropertyVariable.h"
00033 #include "../constraints/WPropertyConstraintMin.h"
00034 #include "../constraints/WPropertyConstraintMax.h"
00035
00036 #include "../exceptions/WPropertyNotUnique.h"
00037 #include "../exceptions/WPropertyUnknown.h"
00038 #include "../exceptions/WPropertyNameMalformed.h"
00039
00040
00041
00042
00043 class WPropertyVariableTest : public CxxTest::TestSuite
00044 {
00045 public:
00046
00047
00048
00049
00050 bool m_testTemporary1;
00051
00052
00053
00054
00055 bool m_testTemporary2;
00056
00057
00058
00059
00060 void setTemporary1()
00061 {
00062 m_testTemporary1 = true;
00063 }
00064
00065
00066
00067
00068 void setTemporary2()
00069 {
00070 m_testTemporary2 = true;
00071 }
00072
00073
00074
00075
00076 void testInstantiation( void )
00077 {
00078 boost::shared_ptr< WPropertyVariable< bool > > p;
00079 TS_ASSERT_THROWS_NOTHING( p = boost::shared_ptr< WPropertyVariable< bool > >( new WPropertyVariable< bool >( "hey", "you", false ) ) );
00080
00081
00082 TS_ASSERT( p->getName() == "hey" );
00083 TS_ASSERT( p->getDescription() == "you" );
00084 TS_ASSERT( p->getType() == PV_BOOL );
00085
00086 TS_ASSERT_THROWS_NOTHING( p.reset() );
00087 }
00088
00089
00090
00091
00092 void testNameConvention( void )
00093 {
00094 WException::disableBacktrace();
00095
00096 boost::shared_ptr< WPropertyVariable< bool > > p;
00097 TS_ASSERT_THROWS( p = boost::shared_ptr< WPropertyVariable< bool > >( new WPropertyVariable< bool >( "hey/you", "you", false ) ),
00098 WPropertyNameMalformed );
00099 }
00100
00101
00102
00103
00104 void testClone()
00105 {
00106 WException::disableBacktrace();
00107
00108
00109
00110
00111
00112 boost::shared_ptr< WPropertyVariable< int > > p =
00113 boost::shared_ptr< WPropertyVariable< int > >( new WPropertyVariable< int >( "hey", "you", false ) );
00114
00115 WPropertyVariable< int >::PropertyConstraintMin cmin = p->setMin( 0 );
00116 WPropertyVariable< int >::PropertyConstraintMax cmax = p->setMax( 9 );
00117 p->set( 5 );
00118
00119
00120
00121
00122 boost::shared_ptr< WPropertyVariable< int > > clone = p->clone()->toPropInt();
00123
00124
00125 TS_ASSERT( clone );
00126 TS_ASSERT( clone->getType() == PV_INT );
00127 TS_ASSERT( clone->getName() == p->getName() );
00128 TS_ASSERT( clone->getDescription() == p->getDescription() );
00129 TS_ASSERT( clone->getPurpose() == p->getPurpose() );
00130
00131
00132 TS_ASSERT( p->get() == clone->get() );
00133
00134
00135 TS_ASSERT( p->getContraintsChangedCondition() != clone->getContraintsChangedCondition() );
00136 TS_ASSERT( p->getUpdateCondition() != clone->getUpdateCondition() );
00137
00138
00139 TS_ASSERT( p->getMin() != clone->getMin() );
00140 TS_ASSERT( p->getMax() != clone->getMax() );
00141
00142 TS_ASSERT( p->getMin()->getMin() == clone->getMin()->getMin() );
00143
00144 TS_ASSERT( p->getMax()->getMax() == clone->getMax()->getMax() );
00145
00146
00147
00148 p->getUpdateCondition()->subscribeSignal( boost::bind( &WPropertyVariableTest::setTemporary1, this ) );
00149 clone->getUpdateCondition()->subscribeSignal( boost::bind( &WPropertyVariableTest::setTemporary2, this ) );
00150
00151
00152 m_testTemporary1 = false;
00153 m_testTemporary2 = false;
00154 clone->set( 4 );
00155 TS_ASSERT( !m_testTemporary1 );
00156 TS_ASSERT( m_testTemporary2 );
00157
00158
00159 m_testTemporary1 = false;
00160 m_testTemporary2 = false;
00161 p->set( 2 );
00162 TS_ASSERT( m_testTemporary1 );
00163 TS_ASSERT( !m_testTemporary2 );
00164
00165
00166 m_testTemporary1 = false;
00167 m_testTemporary2 = false;
00168 clone->removeConstraint( PC_MIN );
00169 TS_ASSERT( !m_testTemporary1 );
00170 TS_ASSERT( m_testTemporary2 );
00171
00172
00173 m_testTemporary1 = false;
00174 m_testTemporary2 = false;
00175 p->removeConstraint( PC_MIN );
00176 TS_ASSERT( m_testTemporary1 );
00177 TS_ASSERT( !m_testTemporary2 );
00178 }
00179
00180
00181
00182
00183 void testMinMaxWithSetAndAccept()
00184 {
00185 WException::disableBacktrace();
00186
00187
00188 boost::shared_ptr< WPropertyVariable< int > > p =
00189 boost::shared_ptr< WPropertyVariable< int > >( new WPropertyVariable< int >( "hey", "you", false ) );
00190
00191
00192 WPropertyVariable< int >::PropertyConstraintMin cmin = p->getMin();
00193 WPropertyVariable< int >::PropertyConstraintMax cmax = p->getMax();
00194 TS_ASSERT( !cmin );
00195 TS_ASSERT( !cmax );
00196
00197
00198 TS_ASSERT( p->set( 123 ) );
00199 TS_ASSERT( p->get() == 123 );
00200 TS_ASSERT( p->accept( 12345 ) );
00201
00202
00203 cmin = p->setMin( 10 );
00204 cmax = p->setMax( 15 );
00205 TS_ASSERT( cmin );
00206 TS_ASSERT( cmax );
00207
00208
00209 TS_ASSERT( cmin == p->getMin() );
00210 TS_ASSERT( cmax == p->getMax() );
00211
00212
00213 TS_ASSERT( p->set( 10 ) );
00214 TS_ASSERT( p->get() == 10 );
00215
00216
00217 TS_ASSERT( !p->set( 9 ) );
00218 TS_ASSERT( p->get() == 10 );
00219 TS_ASSERT( !p->set( 16 ) );
00220 TS_ASSERT( p->get() == 10 );
00221
00222
00223 p->setMin( 5 );
00224 p->setMax( 20 );
00225 p->m_constraints->getReadTicket()->get().size();
00226
00227
00228 TS_ASSERT( p->set( 9 ) );
00229 TS_ASSERT( p->get() == 9 );
00230 TS_ASSERT( p->set( 16 ) );
00231 TS_ASSERT( p->get() == 16 );
00232
00233
00234
00235
00236
00237 TS_ASSERT( p->ensureValidity( 10 ) );
00238 TS_ASSERT( p->get() == 16 );
00239
00240
00241 TS_ASSERT( p->isValid() );
00242 p->setMin( 17 );
00243 TS_ASSERT( !p->isValid() );
00244 TS_ASSERT( p->get() == 16 );
00245
00246
00247 TS_ASSERT( p->ensureValidity( 18 ) );
00248 TS_ASSERT( p->get() == 18 );
00249 TS_ASSERT( p->isValid() );
00250
00251
00252 p->setMin( 19 );
00253 TS_ASSERT( !p->ensureValidity( 16 ) );
00254 TS_ASSERT( !p->isValid() );
00255 TS_ASSERT( p->get() == 18 );
00256 }
00257
00258
00259
00260
00261 void testConstraintManagement( void )
00262 {
00263 WException::disableBacktrace();
00264
00265
00266 boost::shared_ptr< WPropertyVariable< int > > p =
00267 boost::shared_ptr< WPropertyVariable< int > >( new WPropertyVariable< int >( "hey", "you", false ) );
00268
00269
00270 p->getUpdateCondition()->subscribeSignal( boost::bind( &WPropertyVariableTest::setTemporary1, this ) );
00271
00272
00273
00274
00275
00276 m_testTemporary1 = false;
00277 WPropertyVariable< int >::PropertyConstraintMin cmin =
00278 boost::shared_ptr< WPropertyConstraintMin< int > >( new WPropertyConstraintMin< int >( 10 ) );
00279 p->addConstraint( cmin );
00280 TS_ASSERT( p->m_constraints->getReadTicket()->get().size() == 1 );
00281 TS_ASSERT( m_testTemporary1 );
00282
00283
00284
00285
00286
00287 m_testTemporary1 = false;
00288 TS_ASSERT( p->countConstraint( PC_MIN ) == 1 );
00289 TS_ASSERT( p->countConstraint( PC_MAX ) == 0 );
00290
00291
00292 TS_ASSERT( cmin == p->getFirstConstraint( PC_MIN ) );
00293 TS_ASSERT( !p->getFirstConstraint( PC_MAX ) );
00294 TS_ASSERT( !m_testTemporary1 );
00295
00296
00297
00298
00299
00300 m_testTemporary1 = false;
00301 WPropertyVariable< int >::PropertyConstraintMax cmax =
00302 boost::shared_ptr< WPropertyConstraintMax< int > >( new WPropertyConstraintMax< int >( 15 ) );
00303
00304
00305 TS_ASSERT_THROWS_NOTHING( p->replaceConstraint( cmax, PC_MAX ) );
00306 TS_ASSERT( m_testTemporary1 );
00307
00308
00309 m_testTemporary1 = false;
00310 WPropertyVariable< int >::PropertyConstraintMax cmax2 =
00311 boost::shared_ptr< WPropertyConstraintMax< int > >( new WPropertyConstraintMax< int >( 20 ) );
00312 p->replaceConstraint( cmax2, PC_MAX );
00313 TS_ASSERT( m_testTemporary1 );
00314 TS_ASSERT( cmax2 == p->getFirstConstraint( PC_MAX ) );
00315
00316
00317
00318
00319
00320 m_testTemporary1 = false;
00321 p->removeConstraint( PC_NOTEMPTY );
00322 TS_ASSERT( !m_testTemporary1 );
00323
00324
00325 m_testTemporary1 = false;
00326 TS_ASSERT( p->countConstraint( PC_MAX ) == 1 );
00327 p->removeConstraint( PC_MAX );
00328 TS_ASSERT( p->countConstraint( PC_MAX ) == 0 );
00329 TS_ASSERT( m_testTemporary1 );
00330
00331
00332 m_testTemporary1 = false;
00333 TS_ASSERT( p->countConstraint( PC_MIN ) == 1 );
00334 p->removeConstraint( cmin );
00335 TS_ASSERT( p->countConstraint( PC_MIN ) == 0 );
00336 TS_ASSERT( m_testTemporary1 );
00337 }
00338 };
00339
00340 #endif // WPROPERTYVARIABLE_TEST_H
00341