WPropertyStruct_test.h 4.23 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------

#ifndef WPROPERTYSTRUCT_TEST_H
#define WPROPERTYSTRUCT_TEST_H

#include <string>
// NOTE: this is only included because the stylechecker wants this, although we never use std::vector. It thinks the mpl::vector is an
// std::vector and NOLINT does not work there (why?)
#include <vector>

#include <cxxtest/TestSuite.h>

#include "../WPropertyTypes.h"
#include "../WPropertyVariable.h"
#include "../WPropertyStruct.h"

/**
 * Test WPropertyStruct.
 */
class WPropertyStructTest : public CxxTest::TestSuite
{
public:

    /**
     * Test instantiation, also test name and description and type (from WPropertyBase)
     */
    void testInstantiation( void )
    {
        typedef WPropertyStruct< WPropInt, WPropBool > TestStruct;

        TestStruct* prop = new TestStruct( "Hallo", "Description Text" );
        TS_ASSERT( prop->size() == 2 );

        // although this is not a proper test, it fails compilation and therefore informs the programmer that he did something wrong
        BOOST_MPL_ASSERT( ( boost::is_same< TestStruct::TupleType, boost::tuple<  WPropInt, WPropBool > > ) );
        BOOST_MPL_ASSERT( ( boost::is_same< TestStruct::TypeVector,
                                            boost::mpl::vector<  WPropInt, WPropBool > > // NOLINT
                        ) );
    }

    /**
     * Test the set method
     */
    void testSet( void )
    {
        // do not test setting the properties here using one of the getProperty methods. Setting properties directly is tested in the appropriate
        // tests

        // we test getting/setting via string here

        // create the prop
74
        typedef WPropertyStruct< WPropInt, WPropBool >::SPtr TestStruct;
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
        TestStruct prop( new TestStruct::element_type( "Hallo", "Description Text" ) );

        // set some defaults
        prop->getProperty< 0 >()->set( 12 );
        prop->getProperty< 1 >()->set( true );

        // get as string
        std::string got = prop->getAsString();

        // change the value a little bit
        prop->getProperty< 0 >()->set( 111 );
        prop->getProperty< 1 >()->set( false );

        // set by string and check values
        prop->setAsString( got );

        TS_ASSERT( prop->getProperty< 0 >()->get() == 12 );
        TS_ASSERT( prop->getProperty< 1 >()->get() == true );

        // also test setting via property
        TestStruct prop2( new TestStruct::element_type( "Hallo2", "Description Text" ) );
        prop2->set( prop );
        TS_ASSERT( prop2->getProperty< 0 >()->get() == 12 );
        TS_ASSERT( prop2->getProperty< 1 >()->get() == true );
    }

    /**
     * Test getter
     */
    void testGet( void )
    {
        typedef WPropertyStruct< WPropInt, WPropBool > TestStruct;
        TestStruct prop( "Hallo", "Description Text" );

        // compile time test: this fails during compilation if something is wrong
        WPropInt i = prop.getProperty< 0 >();
        TS_ASSERT( i.get() );
        WPropBool b = prop.getProperty< 1 >();
        TS_ASSERT( b.get() );

        // get the second prop
        WPropertyBase::SPtr base = prop.getProperty( 1 );
        TS_ASSERT( base.get() );

        // this has to be a bool prop
        TS_ASSERT( base->toPropBool().get() );
    }
};

#endif  // WPROPERTYSTRUCT_TEST_H