CppUTest
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 inteface.
34 // We kept streams out too 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 class UtestShell;
41 class TestFailure;
42 class TestResult;
43 
45 {
46 public:
47  explicit TestOutput();
48  virtual ~TestOutput();
49 
50  virtual void printTestsStarted();
51  virtual void printTestsEnded(const TestResult& result);
52  virtual void printCurrentTestStarted(const UtestShell& test);
53  virtual void printCurrentTestEnded(const TestResult& res);
54  virtual void printCurrentGroupStarted(const UtestShell& test);
55  virtual void printCurrentGroupEnded(const TestResult& res);
56 
57  virtual void verbose();
58  virtual void printBuffer(const char*)=0;
59  virtual void print(const char*);
60  virtual void print(long);
61  virtual void printDouble(double);
62  virtual void printHex(long);
63  virtual void print(const TestFailure& failure);
64  virtual void printTestRun(int number, int total);
65  virtual void setProgressIndicator(const char*);
66 
67  virtual void flush();
68 
69  enum WorkingEnvironment {vistualStudio, eclipse, detectEnvironment};
70 
71  static void setWorkingEnvironment(WorkingEnvironment workEnvironment);
72  static WorkingEnvironment getWorkingEnvironment();
73 
74 protected:
75 
76  virtual void printEclipseErrorInFileOnLine(SimpleString file, int lineNumber);
77  virtual void printVistualStudioErrorInFileOnLine(SimpleString file, int lineNumber);
78 
79  virtual void printProgressIndicator();
80  void printFileAndLineForTestAndFailure(const TestFailure& failure);
81  void printFileAndLineForFailure(const TestFailure& failure);
82  void printFailureInTest(SimpleString testName);
83  void printFailureMessage(SimpleString reason);
84  void printErrorInFileOnLineFormattedForWorkingEnvironment(SimpleString testFile, int lineNumber);
85 
86  TestOutput(const TestOutput&);
87  TestOutput& operator=(const TestOutput&);
88 
89  int dotCount_;
90  bool verbose_;
91  const char* progressIndication_;
92 
93  static WorkingEnvironment workingEnvironment_;
94 };
95 
96 TestOutput& operator<<(TestOutput&, const char*);
97 TestOutput& operator<<(TestOutput&, long);
98 
100 //
101 // ConsoleTestOutput.h
102 //
103 // Printf Based Solution
104 //
106 
108 {
109 public:
110  explicit ConsoleTestOutput()
111  {
112  }
113  virtual ~ConsoleTestOutput()
114  {
115  }
116 
117  virtual void printBuffer(const char* s);
118  virtual void flush();
119 
120 private:
122  ConsoleTestOutput& operator=(const ConsoleTestOutput&);
123 };
124 
126 //
127 // StringBufferTestOutput.h
128 //
129 // TestOutput for test purposes
130 //
132 
133 
135 {
136 public:
137  explicit StringBufferTestOutput()
138  {
139  }
140 
141  virtual ~StringBufferTestOutput();
142 
143  void printBuffer(const char* s)
144  {
145  output += s;
146  }
147 
148  void flush()
149  {
150  output = "";
151  }
152 
153  const SimpleString& getOutput()
154  {
155  return output;
156  }
157 
158 private:
159  SimpleString output;
160 
163 
164 };
165 
166 #endif
Definition: TestResult.h:41
Definition: TestOutput.h:107
Definition: TestOutput.h:134
Definition: SimpleString.h:46
Definition: TestFailure.h:44
Definition: Utest.h:61
Definition: TestOutput.h:44