1 #ifndef  __CXXTEST_GUI__H
2 #define  __CXXTEST_GUI__H
3 
4 //
5 // GuiListener is a simple base class for the differes GUIs
6 // GuiTuiRunner<GuiT, TuiT> combines a GUI with a text-mode error formatter
7 //
8 
9 #include <cxxtest/TeeListener.h>
10 
11 namespace CxxTest {
12   class GuiListener : public TestListener {
13     public:
GuiListener()14     GuiListener()
15         : _state( GREEN_BAR ) {}
~GuiListener()16     virtual ~GuiListener() {}
17 
runGui(int & argc,char ** argv,TestListener & listener)18     virtual void runGui( int& argc, char** argv, TestListener& listener ) {
19       enterGui( argc, argv );
20       TestRunner::runAllTests( listener );
21       leaveGui();
22     }
23 
enterGui(int &,char **)24     virtual void enterGui( int& /*argc*/, char** /*argv*/ ) {}
leaveGui()25     virtual void leaveGui() {}
26 
27     //
28     // The easy way is to implement these functions:
29     //
guiEnterWorld(unsigned)30     virtual void guiEnterWorld( unsigned /*numTotalTests*/ ) {}
guiEnterSuite(const char *)31     virtual void guiEnterSuite( const char* /*suiteName*/ ) {}
guiEnterTest(const char *,const char *)32     virtual void guiEnterTest( const char* /*suiteName*/,
33                                const char* /*testName*/ ) {}
yellowBar()34     virtual void yellowBar() {}
redBar()35     virtual void redBar() {}
36 
37     //
38     // The hard way is this:
39     //
enterWorld(const WorldDescription & d)40     void enterWorld( const WorldDescription& d ) {
41       guiEnterWorld( d.numTotalTests() );
42     }
enterSuite(const SuiteDescription & d)43     void enterSuite( const SuiteDescription& d ) {
44       guiEnterSuite( d.suiteName() );
45     }
enterTest(const TestDescription & d)46     void enterTest( const TestDescription& d ) {
47       guiEnterTest( d.suiteName(), d.testName() );
48     }
leaveTest(const TestDescription &)49     void leaveTest( const TestDescription& ) {}
leaveSuite(const SuiteDescription &)50     void leaveSuite( const SuiteDescription& ) {}
leaveWorld(const WorldDescription &)51     void leaveWorld( const WorldDescription& ) {}
52 
warning(const char *,unsigned,const char *)53     void warning( const char* /*file*/,
54                   unsigned /*line*/,
55                   const char* /*expression*/ ) {
56       yellowBarSafe();
57     }
58 
failedTest(const char *,unsigned,const char *)59     void failedTest( const char* /*file*/,
60                      unsigned /*line*/,
61                      const char* /*expression*/ ) {
62       redBarSafe();
63     }
64 
failedAssert(const char *,unsigned,const char *)65     void failedAssert( const char* /*file*/,
66                        unsigned /*line*/,
67                        const char* /*expression*/ ) {
68       redBarSafe();
69     }
70 
failedAssertEquals(const char *,unsigned,const char *,const char *,const char *,const char *)71     void failedAssertEquals( const char* /*file*/,
72                              unsigned /*line*/,
73                              const char* /*xStr*/,
74                              const char* /*yStr*/,
75                              const char* /*x*/,
76                              const char* /*y*/ ) {
77       redBarSafe();
78     }
79 
failedAssertSameData(const char *,unsigned,const char *,const char *,const char *,const void *,const void *,unsigned)80     void failedAssertSameData( const char* /*file*/,
81                                unsigned /*line*/,
82                                const char* /*xStr*/,
83                                const char* /*yStr*/,
84                                const char* /*sizeStr*/,
85                                const void* /*x*/,
86                                const void* /*y*/,
87                                unsigned /*size*/ ) {
88       redBarSafe();
89     }
90 
failedAssertDelta(const char *,unsigned,const char *,const char *,const char *,const char *,const char *,const char *)91     void failedAssertDelta( const char* /*file*/,
92                             unsigned /*line*/,
93                             const char* /*xStr*/,
94                             const char* /*yStr*/,
95                             const char* /*dStr*/,
96                             const char* /*x*/,
97                             const char* /*y*/,
98                             const char* /*d*/ ) {
99       redBarSafe();
100     }
101 
failedAssertDiffers(const char *,unsigned,const char *,const char *,const char *)102     void failedAssertDiffers( const char* /*file*/,
103                               unsigned /*line*/,
104                               const char* /*xStr*/,
105                               const char* /*yStr*/,
106                               const char* /*value*/ ) {
107       redBarSafe();
108     }
109 
failedAssertLessThan(const char *,unsigned,const char *,const char *,const char *,const char *)110     void failedAssertLessThan( const char* /*file*/,
111                                unsigned /*line*/,
112                                const char* /*xStr*/,
113                                const char* /*yStr*/,
114                                const char* /*x*/,
115                                const char* /*y*/ ) {
116       redBarSafe();
117     }
118 
failedAssertLessThanEquals(const char *,unsigned,const char *,const char *,const char *,const char *)119     void failedAssertLessThanEquals( const char* /*file*/,
120                                      unsigned /*line*/,
121                                      const char* /*xStr*/,
122                                      const char* /*yStr*/,
123                                      const char* /*x*/,
124                                      const char* /*y*/ ) {
125       redBarSafe();
126     }
127 
failedAssertPredicate(const char *,unsigned,const char *,const char *,const char *)128     void failedAssertPredicate( const char* /*file*/,
129                                 unsigned /*line*/,
130                                 const char* /*predicate*/,
131                                 const char* /*xStr*/,
132                                 const char* /*x*/ ) {
133       redBarSafe();
134     }
135 
failedAssertRelation(const char *,unsigned,const char *,const char *,const char *,const char *,const char *)136     void failedAssertRelation( const char* /*file*/,
137                                unsigned /*line*/,
138                                const char* /*relation*/,
139                                const char* /*xStr*/,
140                                const char* /*yStr*/,
141                                const char* /*x*/,
142                                const char* /*y*/ ) {
143       redBarSafe();
144     }
145 
failedAssertThrows(const char *,unsigned,const char *,const char *,bool)146     void failedAssertThrows( const char* /*file*/,
147                              unsigned /*line*/,
148                              const char* /*expression*/,
149                              const char* /*type*/,
150                              bool /*otherThrown*/ ) {
151       redBarSafe();
152     }
153 
failedAssertThrowsNot(const char *,unsigned,const char *)154     void failedAssertThrowsNot( const char* /*file*/,
155                                 unsigned /*line*/,
156                                 const char* /*expression*/ ) {
157       redBarSafe();
158     }
159 
160     protected:
yellowBarSafe()161     void yellowBarSafe() {
162       if ( _state < YELLOW_BAR ) {
163         yellowBar();
164         _state = YELLOW_BAR;
165       }
166     }
167 
redBarSafe()168     void redBarSafe() {
169       if ( _state < RED_BAR ) {
170         redBar();
171         _state = RED_BAR;
172       }
173     }
174 
175     private:
176     enum { GREEN_BAR, YELLOW_BAR, RED_BAR } _state;
177   };
178 
179   template <class GuiT, class TuiT>
180   class GuiTuiRunner : public TeeListener {
181     int& _argc;
182     char** _argv;
183     GuiT _gui;
184     TuiT _tui;
185 
186     public:
GuiTuiRunner(int & argc,char ** argv)187     GuiTuiRunner( int& argc, char** argv )
188         : _argc( argc )
189         , _argv( argv ) {
190       setFirst( _gui );
191       setSecond( _tui );
192     }
193 
run()194     int run() {
195       _gui.runGui( _argc, _argv, *this );
196       return tracker().failedTests();
197     }
198   };
199 };
200 
201 #endif  // __CXXTEST_GUI__H
202