OpenWalnut  1.4.0
WTerminalColor_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 WTERMINALCOLOR_TEST_H
00026 #define WTERMINALCOLOR_TEST_H
00027 
00028 #include <string>
00029 #include <sstream>
00030 
00031 #include <boost/shared_ptr.hpp>
00032 #include <cxxtest/TestSuite.h>
00033 
00034 #include "../WTerminalColor.h"
00035 
00036 /**
00037  * Test WTerminalColor. Just some simple test to verify the control strings.
00038  */
00039 class WTerminalColorTest : public CxxTest::TestSuite
00040 {
00041 public:
00042     /**
00043      * An instantiation should never throw an exception.
00044      */
00045     void testInstantiation( void )
00046     {
00047         TS_ASSERT_THROWS_NOTHING( WTerminalColor c() );
00048         TS_ASSERT_THROWS_NOTHING( WTerminalColor( WTerminalColor::Bold, WTerminalColor::FGRed, WTerminalColor::BGNone ) );
00049     }
00050 
00051     /**
00052      * Test control string generated by class.
00053      */
00054     void testColorControlString( void )
00055     {
00056         WTerminalColor c;
00057         TS_ASSERT_THROWS_NOTHING( c = WTerminalColor( WTerminalColor::Bold, WTerminalColor::FGRed, WTerminalColor::BGGreen ) );
00058 
00059         // generate an own control string
00060         std::ostringstream ss;
00061 #if defined( __linux__ ) || defined( __APPLE__ )
00062         char cStart = 0x1B; // this is an indirect cast to char, otherwise 0x1B whould be interpreted as number 27
00063         ss << cStart << "[" << 1 << ";" << 31 << ";" << 42 << "m";
00064 #endif
00065         // compare them
00066         TS_ASSERT_EQUALS( ss.str(), c.m_colorString );
00067     }
00068 
00069     /**
00070      * Test control string (reset) generated by class.
00071      */
00072     void testColorResetControlString( void )
00073     {
00074         WTerminalColor c;
00075         TS_ASSERT_THROWS_NOTHING( c = WTerminalColor( WTerminalColor::Bold, WTerminalColor::FGRed, WTerminalColor::BGGreen ) );
00076 
00077         // generate an own control string
00078         std::ostringstream ss;
00079 #if defined( __linux__ ) || defined( __APPLE__ )
00080         char cStart = 0x1B;
00081         ss << cStart << "[0m";
00082 #endif
00083         // compare them
00084         TS_ASSERT( ss.str() == c.m_colorResetString );
00085     }
00086 
00087     /**
00088      * Test whether the class returns empty control strings when colors are disabled.
00089      */
00090     void testColorDisabled( void )
00091     {
00092         WTerminalColor c;
00093         TS_ASSERT_THROWS_NOTHING( c = WTerminalColor( WTerminalColor::Bold, WTerminalColor::FGRed, WTerminalColor::BGGreen ) );
00094 
00095         c.setEnabled( false );
00096         TS_ASSERT( c.m_colorResetString == "" );
00097         TS_ASSERT( c.m_colorString == "" );
00098     }
00099 };
00100 
00101 #endif  // WTERMINALCOLOR_TEST_H