enum_string_type_test.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002   tag: The SourceWorks  Tue Sep 7 00:54:57 CEST 2010  enum_string_type_test.cpp
00003 
00004                         enum_string_type_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 <rtt-fwd.hpp>
00022 #include <internal/DataSources.hpp>
00023 #include <os/fosi.h>
00024 #include <boost/lambda/lambda.hpp>
00025 
00026 #include "datasource_fixture.hpp"
00027 #include "marsh/PropertyBagIntrospector.hpp"
00028 #include "types/EnumTypeInfo.hpp"
00029 #include "marsh/PropertyLoader.hpp"
00030 #include "TaskContext.hpp"
00031 
00032 using namespace boost::lambda;
00033 
00034 typedef enum
00035 {
00036     A, B, C
00037 } TheEnum;
00038 
00039 struct EnumStringTypeInfo: public EnumTypeInfo<TheEnum>
00040 {
00041     EnumStringTypeInfo() :
00042         EnumTypeInfo<TheEnum> ("TheEnum")
00043     {
00044         to_string[A] = "A";
00045         to_string[B] = "B";
00046         to_string[C] = "C";
00047     }
00048 };
00049 
00050 class EnumTypeTest
00051 {
00052 public:
00053     TheEnum enum1;
00054     TheEnum enum2;
00055 
00056     AssignableDataSource<TheEnum>::shared_ptr a; 
00057     AssignableDataSource<TheEnum>::shared_ptr b; 
00058     TypeInfo* ti; 
00059     TypeInfo* ts; 
00060 
00061     EnumTypeTest()
00062     {
00063 
00064         a = new ValueDataSource<TheEnum>( A );
00065         b = new ValueDataSource<TheEnum>( B );
00066 
00067         Types()->addType( new EnumStringTypeInfo() );
00068     }
00069     ~EnumTypeTest()
00070     {
00071     }
00072 };
00073 
00074 
00075 // Registers the fixture into the 'registry'
00076 BOOST_FIXTURE_TEST_SUITE( EnumTypeTestSuite, EnumTypeTest )
00077 
00078 // Tests enum to string conversions
00079 BOOST_AUTO_TEST_CASE( testEnumStringConversion )
00080 {
00081 
00082     ti = Types()->type("TheEnum");
00083     ts = Types()->type("string");
00084 
00085     BOOST_REQUIRE( ti );
00086     BOOST_REQUIRE( ts );
00087 
00088     // Test enum conversion
00089     BOOST_REQUIRE( ti->decomposeType( a ) );
00090     BOOST_CHECK_EQUAL( ti->decomposeType( a )->getTypeInfo(), ts );
00091 
00092     // converts a to "A" and then back to enum and stores it in B.
00093     BOOST_CHECK( ti->composeType(ti->decomposeType(a), b) );
00094 
00095     BOOST_CHECK_EQUAL( a->get(), A);
00096     BOOST_CHECK_EQUAL( b->get(), A);
00097 
00098     b->set(B);
00099 
00100     // Test composition of enum from string.
00101     PropertyBag result;
00102     Property<TheEnum> pa("a","", a );
00103     Property<TheEnum> pb("a","", b );
00104     PropertyBagIntrospector pbi(result);
00105     pbi.introspect( &pa);
00106     BOOST_REQUIRE_EQUAL( result.size(), 1);
00107     BOOST_CHECK( result.getItem(0)->getTypeInfo() == ts );
00108     DataSource<string>::shared_ptr dstring = DataSource<string>::narrow( result.getItem(0)->getDataSource().get() );
00109     BOOST_CHECK_EQUAL(dstring->get(), "A" );
00110 
00111     pbi.introspect( &pb);
00112     BOOST_REQUIRE_EQUAL( result.size(), 2);
00113     BOOST_CHECK( result.getItem(1)->getTypeInfo() == ts );
00114     dstring = DataSource<string>::narrow( result.getItem(1)->getDataSource().get() );
00115 
00116     BOOST_CHECK_EQUAL(dstring->get(), "B" );
00117 }
00118 
00119 // Tests enum to file and back
00120 BOOST_AUTO_TEST_CASE( testEnumSaveStringProperties )
00121 {
00122     TaskContext tc("TC");
00123     PropertyLoader pl(&tc);
00124     enum1 = A;
00125     enum2 = B;
00126     tc.addProperty("enum1", enum1 );
00127     tc.addProperty("enum2", enum2 );
00128 
00129     // saves A and B
00130     BOOST_CHECK( pl.save("enum_type_test_string_save.cpf", true) );
00131 
00132     enum1 = B;
00133     enum2 = A;
00134 
00135     // restores A and B
00136     BOOST_CHECK( pl.load("enum_type_test_string_save.cpf") );
00137 
00138     BOOST_CHECK_EQUAL( enum1, A);
00139     BOOST_CHECK_EQUAL( enum2, B);
00140 }
00141 
00142 BOOST_AUTO_TEST_SUITE_END()
00143 


rtt
Author(s): RTT Developers
autogenerated on Sat Jun 8 2019 18:46:09