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