NetBurner 3.1
TestOutput.h
1 /*
2  * Copyright (c) 2007, Michael Feathers, James Grenning and Bas Vodde
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of the <organization> nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #ifndef D_TestOutput_h
29 #define D_TestOutput_h
30 
32 //
33 // This is a minimal printer interface.
34 // We kept streams out to keep footprint small, and so the test
35 // harness could be used with less capable compilers so more
36 // platforms could use this test harness
37 //
39 
40 #include "SimpleString.h"
41 
42 class UtestShell;
43 class TestFailure;
44 class TestResult;
45 
46 class TestOutput
47 {
48 public:
49  explicit TestOutput();
50  virtual ~TestOutput();
51 
52  virtual void printTestsStarted();
53  virtual void printTestsEnded(const TestResult& result);
54  virtual void printCurrentTestStarted(const UtestShell& test);
55  virtual void printCurrentTestEnded(const TestResult& res);
56  virtual void printCurrentGroupStarted(const UtestShell& test);
57  virtual void printCurrentGroupEnded(const TestResult& res);
58 
59  virtual void verbose();
60  virtual void color();
61  virtual void printBuffer(const char*)=0;
62  virtual void print(const char*);
63  virtual void print(long);
64  virtual void printDouble(double);
65  virtual void printFailure(const TestFailure& failure);
66  virtual void printTestRun(int number, int total);
67  virtual void setProgressIndicator(const char*);
68 
69  virtual void flush()=0;
70 
71  enum WorkingEnvironment {visualStudio, eclipse, detectEnvironment};
72 
73  static void setWorkingEnvironment(WorkingEnvironment workEnvironment);
74  static WorkingEnvironment getWorkingEnvironment();
75 
76 protected:
77 
78  virtual void printEclipseErrorInFileOnLine(SimpleString file, int lineNumber);
79  virtual void printVisualStudioErrorInFileOnLine(SimpleString file, int lineNumber);
80 
81  virtual void printProgressIndicator();
82  void printFileAndLineForTestAndFailure(const TestFailure& failure);
83  void printFileAndLineForFailure(const TestFailure& failure);
84  void printFailureInTest(SimpleString testName);
85  void printFailureMessage(SimpleString reason);
86  void printErrorInFileOnLineFormattedForWorkingEnvironment(SimpleString testFile, int lineNumber);
87 
88  TestOutput(const TestOutput&);
89  TestOutput& operator=(const TestOutput&);
90 
91  int dotCount_;
92  bool verbose_;
93  bool color_;
94  const char* progressIndication_;
95 
96  static WorkingEnvironment workingEnvironment_;
97 };
98 
99 TestOutput& operator<<(TestOutput&, const char*);
100 TestOutput& operator<<(TestOutput&, long);
101 
103 //
104 // ConsoleTestOutput.h
105 //
106 // Printf Based Solution
107 //
109 
110 class ConsoleTestOutput: public TestOutput
111 {
112 public:
113  explicit ConsoleTestOutput()
114  {
115  }
116  virtual ~ConsoleTestOutput()
117  {
118  }
119 
120  virtual void printBuffer(const char* s) _override;
121  virtual void flush() _override;
122 
123 private:
124  ConsoleTestOutput(const ConsoleTestOutput&);
125  ConsoleTestOutput& operator=(const ConsoleTestOutput&);
126 };
127 
129 //
130 // StringBufferTestOutput.h
131 //
132 // TestOutput for test purposes
133 //
135 
136 
137 class StringBufferTestOutput: public TestOutput
138 {
139 public:
140  explicit StringBufferTestOutput()
141  {
142  }
143 
144  virtual ~StringBufferTestOutput();
145 
146  void printBuffer(const char* s) _override
147  {
148  output += s;
149  }
150 
151  void flush() _override
152  {
153  output = "";
154  }
155 
156  const SimpleString& getOutput()
157  {
158  return output;
159  }
160 
161 protected:
162  SimpleString output;
163 
164 private:
165  StringBufferTestOutput(const StringBufferTestOutput&);
166  StringBufferTestOutput& operator=(const StringBufferTestOutput&);
167 
168 };
169 
170 class CompositeTestOutput : public TestOutput
171 {
172 public:
173  virtual void setOutputOne(TestOutput* output);
174  virtual void setOutputTwo(TestOutput* output);
175 
176  CompositeTestOutput();
177  virtual ~CompositeTestOutput();
178 
179  virtual void printTestsStarted();
180  virtual void printTestsEnded(const TestResult& result);
181 
182  virtual void printCurrentTestStarted(const UtestShell& test);
183  virtual void printCurrentTestEnded(const TestResult& res);
184  virtual void printCurrentGroupStarted(const UtestShell& test);
185  virtual void printCurrentGroupEnded(const TestResult& res);
186 
187  virtual void verbose();
188  virtual void color();
189  virtual void printBuffer(const char*);
190  virtual void print(const char*);
191  virtual void print(long);
192  virtual void printDouble(double);
193  virtual void printFailure(const TestFailure& failure);
194  virtual void setProgressIndicator(const char*);
195 
196  virtual void flush();
197 
198 protected:
199  CompositeTestOutput(const TestOutput&);
200  CompositeTestOutput& operator=(const TestOutput&);
201 
202 private:
203  TestOutput* outputOne_;
204  TestOutput* outputTwo_;
205 };
206 
207 #endif