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