OpenWalnut  1.4.0
WTerminalColor_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 WTERMINALCOLOR_TEST_H
26 #define WTERMINALCOLOR_TEST_H
27 
28 #include <string>
29 #include <sstream>
30 
31 #ifndef Q_MOC_RUN
32 #include <boost/shared_ptr.hpp>
33 #endif
34 #include <cxxtest/TestSuite.h>
35 
36 #include "../WTerminalColor.h"
37 
38 /**
39  * Test WTerminalColor. Just some simple test to verify the control strings.
40  */
41 class WTerminalColorTest : public CxxTest::TestSuite
42 {
43 public:
44  /**
45  * An instantiation should never throw an exception.
46  */
47  void testInstantiation( void )
48  {
49  TS_ASSERT_THROWS_NOTHING( WTerminalColor c() );
50  TS_ASSERT_THROWS_NOTHING( WTerminalColor( WTerminalColor::Bold, WTerminalColor::FGRed, WTerminalColor::BGNone ) );
51  }
52 
53  /**
54  * Test control string generated by class.
55  */
57  {
59  TS_ASSERT_THROWS_NOTHING( c = WTerminalColor( WTerminalColor::Bold, WTerminalColor::FGRed, WTerminalColor::BGGreen ) );
60 
61  // generate an own control string
62  std::ostringstream ss;
63 #if defined( __linux__ ) || defined( __APPLE__ )
64  char cStart = 0x1B; // this is an indirect cast to char, otherwise 0x1B whould be interpreted as number 27
65  ss << cStart << "[" << 1 << ";" << 31 << ";" << 42 << "m";
66 #endif
67  // compare them
68  TS_ASSERT_EQUALS( ss.str(), c.m_colorString );
69  }
70 
71  /**
72  * Test control string (reset) generated by class.
73  */
75  {
77  TS_ASSERT_THROWS_NOTHING( c = WTerminalColor( WTerminalColor::Bold, WTerminalColor::FGRed, WTerminalColor::BGGreen ) );
78 
79  // generate an own control string
80  std::ostringstream ss;
81 #if defined( __linux__ ) || defined( __APPLE__ )
82  char cStart = 0x1B;
83  ss << cStart << "[0m";
84 #endif
85  // compare them
86  TS_ASSERT( ss.str() == c.m_colorResetString );
87  }
88 
89  /**
90  * Test whether the class returns empty control strings when colors are disabled.
91  */
92  void testColorDisabled( void )
93  {
95  TS_ASSERT_THROWS_NOTHING( c = WTerminalColor( WTerminalColor::Bold, WTerminalColor::FGRed, WTerminalColor::BGGreen ) );
96 
97  c.setEnabled( false );
98  TS_ASSERT( c.m_colorResetString == "" );
99  TS_ASSERT( c.m_colorString == "" );
100  }
101 };
102 
103 #endif // WTERMINALCOLOR_TEST_H
void testColorResetControlString(void)
Test control string (reset) generated by class.
void testColorDisabled(void)
Test whether the class returns empty control strings when colors are disabled.
void setEnabled(bool enabled)
With this you can easily trigger whether the color control string is used or if "" is returned...
void testInstantiation(void)
An instantiation should never throw an exception.
void testColorControlString(void)
Test control string generated by class.
Helper class to provide a convenient way to colorize output on the console.
std::string m_colorResetString
Control sequence to reset color.
Test WTerminalColor.
std::string m_colorString
The string actually containing the control sequence to enable colors on the console.