File: ErrorFormatter.h

package info (click to toggle)
0ad 0.0.17-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 51,248 kB
  • ctags: 46,933
  • sloc: cpp: 223,208; ansic: 31,240; python: 16,343; perl: 4,083; sh: 1,011; makefile: 915; xml: 733; java: 621; ruby: 229; erlang: 53; sql: 40
file content (347 lines) | stat: -rw-r--r-- 10,816 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
-------------------------------------------------------------------------
 CxxTest: A lightweight C++ unit testing library.
 Copyright (c) 2008 Sandia Corporation.
 This software is distributed under the LGPL License v3
 For more information, see the COPYING file in the top CxxTest directory.
 Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
 the U.S. Government retains certain rights in this software.
-------------------------------------------------------------------------
*/

#ifndef __cxxtest__ErrorFormatter_h__
#define __cxxtest__ErrorFormatter_h__

//
// The ErrorFormatter is a TestListener that
// prints reports of the errors to an output
// stream.  Since we cannot rely on the standard
// iostreams, this header defines a base class
// analogout to std::ostream.
//

#include <cxxtest/TestRunner.h>
#include <cxxtest/TestListener.h>
#include <cxxtest/TestTracker.h>
#include <cxxtest/ValueTraits.h>
#include <cstdio>

namespace CxxTest
{
class OutputStream
{
public:
    virtual ~OutputStream() {}
    virtual void flush() {};
    virtual OutputStream &operator<<(unsigned /*number*/) { return *this; }
    virtual OutputStream &operator<<(const char * /*string*/) { return *this; }

    typedef void (*Manipulator)(OutputStream &);

    virtual OutputStream &operator<<(Manipulator m) { m(*this); return *this; }
    static void endl(OutputStream &o) { (o << "\n").flush(); }
};

class ErrorFormatter : public TestListener
{
public:
    ErrorFormatter(OutputStream *o, const char *preLine = ":", const char *postLine = "",
                   const char *errorString = "Error",
                   const char *warningString = "Warning") :
        _dotting(true),
        _reported(false),
        _o(o),
        _preLine(preLine),
        _postLine(postLine),
        _errorString(errorString),
        _warningString(warningString)
    {
    }

    int run()
    {
        TestRunner::runAllTests(*this);
        return tracker().failedTests();
    }

    void enterWorld(const WorldDescription& desc)
    {
        (*_o) << "Running " << desc.worldName() << " tests (" << totalTests << ")";
        _o->flush();
        _dotting = true;
        _reported = false;
    }

    static void totalTests(OutputStream &o)
    {
        char s[WorldDescription::MAX_STRLEN_TOTAL_TESTS];
        const WorldDescription &wd = tracker().world();
        o << wd.strTotalTests(s) << (wd.numTotalTests() == 1 ? " test" : " tests");
    }

    void enterSuite(const SuiteDescription &)
    {
        _reported = false;
    }

    void enterTest(const TestDescription &)
    {
        _reported = false;
    }

    void leaveTest(const TestDescription &)
    {
        if (tracker().testSkipped())
        {
            (*_o) << "s";
            _o->flush();
            fflush(stdout);
            _dotting = true;
        }
        else if (!tracker().testFailed())
        {
            (*_o) << ".";
            _o->flush();
            fflush(stdout);
            _dotting = true;
        }
    }

    void leaveWorld(const WorldDescription &desc)
    {
        if (!tracker().failedTests())
        {
            (*_o) << "OK!" << endl;
            return;
        }
        newLine();
        (*_o) << "Failed " << tracker().failedTests() << " and Skipped " << tracker().skippedTests() << " of " << totalTests << endl;
        unsigned numPassed = desc.numTotalTests() - tracker().failedTests() - tracker().skippedTests();
        unsigned numTotal = desc.numTotalTests() - tracker().skippedTests();
        if (numTotal == 0)
        {
            (*_o) << "Success rate: 100%" << endl;
        }
        else
        {
            (*_o) << "Success rate: " << (unsigned)(numPassed * 100.0 / numTotal) << "%" << endl;
        }
    }

    void trace(const char *file, int line, const char *expression)
    {
        stop(file, line) << "Trace: " <<
                         expression << endl;
    }

    void warning(const char *file, int line, const char *expression)
    {
        stop(file, line) << _warningString << ": " <<
                         expression << endl;
    }

    void skippedTest(const char *file, int line, const char *expression)
    {
        if(expression && strlen(expression) > 0)
            stop(file, line) << _warningString << ": Test skipped: " <<
                             expression << endl;
    }

    void failedTest(const char *file, int line, const char *expression)
    {
        stop(file, line) << _errorString << ": Test failed: " <<
                         expression << endl;
    }

    void failedAssert(const char *file, int line, const char *expression)
    {
        stop(file, line) << _errorString << ": Assertion failed: " <<
                         expression << endl;
    }

    void failedAssertEquals(const char *file, int line,
                            const char *xStr, const char *yStr,
                            const char *x, const char *y)
    {
        stop(file, line) << _errorString << ": Expected (" <<
                         xStr << " == " << yStr << "), found (" <<
                         x << " != " << y << ")" << endl;
    }

    void failedAssertSameData(const char *file, int line,
                              const char *xStr, const char *yStr,
                              const char *sizeStr, const void *x,
                              const void *y, unsigned size)
    {
        stop(file, line) << _errorString << ": Expected " << sizeStr << " (" << size << ") bytes to be equal at (" <<
                         xStr << ") and (" << yStr << "), found:" << endl;
        dump(x, size);
        (*_o) << "     differs from" << endl;
        dump(y, size);
    }

    void failedAssertSameFiles(const char* file, int line,
                               const char*, const char*,
                               const char* explanation
                              )
    {
        stop(file, line) << _errorString << ": " << explanation << endl;
    }

    void failedAssertDelta(const char *file, int line,
                           const char *xStr, const char *yStr, const char *dStr,
                           const char *x, const char *y, const char *d)
    {
        stop(file, line) << _errorString << ": Expected (" <<
                         xStr << " == " << yStr << ") up to " << dStr << " (" << d << "), found (" <<
                         x << " != " << y << ")" << endl;
    }

    void failedAssertDiffers(const char *file, int line,
                             const char *xStr, const char *yStr,
                             const char *value)
    {
        stop(file, line) << _errorString << ": Expected (" <<
                         xStr << " != " << yStr << "), found (" <<
                         value << ")" << endl;
    }

    void failedAssertLessThan(const char *file, int line,
                              const char *xStr, const char *yStr,
                              const char *x, const char *y)
    {
        stop(file, line) << _errorString << ": Expected (" <<
                         xStr << " < " << yStr << "), found (" <<
                         x << " >= " << y << ")" << endl;
    }

    void failedAssertLessThanEquals(const char *file, int line,
                                    const char *xStr, const char *yStr,
                                    const char *x, const char *y)
    {
        stop(file, line) << _errorString << ": Expected (" <<
                         xStr << " <= " << yStr << "), found (" <<
                         x << " > " << y << ")" << endl;
    }

    void failedAssertRelation(const char *file, int line,
                              const char *relation, const char *xStr, const char *yStr,
                              const char *x, const char *y)
    {
        stop(file, line) << _errorString << ": Expected " << relation << "( " <<
                         xStr << ", " << yStr << " ), found !" << relation << "( " << x << ", " << y << " )" << endl;
    }

    void failedAssertPredicate(const char *file, int line,
                               const char *predicate, const char *xStr, const char *x)
    {
        stop(file, line) << _errorString << ": Expected " << predicate << "( " <<
                         xStr << " ), found !" << predicate << "( " << x << " )" << endl;
    }

    void failedAssertThrows(const char *file, int line,
                            const char *expression, const char *type,
                            bool otherThrown)
    {
        stop(file, line) << _errorString << ": Expected (" << expression << ") to throw (" <<
                         type << ") but it " << (otherThrown ? "threw something else" : "didn't throw") <<
                         endl;
    }

    void failedAssertThrowsNot(const char *file, int line, const char *expression)
    {
        stop(file, line) << _errorString << ": Expected (" << expression << ") not to throw, but it did" <<
                         endl;
    }

protected:
    OutputStream *outputStream() const
    {
        return _o;
    }

private:
    ErrorFormatter(const ErrorFormatter &);
    ErrorFormatter &operator=(const ErrorFormatter &);

    OutputStream &stop(const char *file, int line)
    {
        newLine();
        reportTest();
        return (*_o) << file << _preLine << line << _postLine << ": ";
    }

    void newLine(void)
    {
        if (_dotting)
        {
            (*_o) << endl;
            _dotting = false;
        }
    }

    void reportTest(void)
    {
        if (_reported)
        {
            return;
        }
        (*_o) << "In " << tracker().suite().suiteName() << "::" << tracker().test().testName() << ":" << endl;
        _reported = true;
    }

    void dump(const void *buffer, unsigned size)
    {
        if (!buffer)
        {
            dumpNull();
        }
        else
        {
            dumpBuffer(buffer, size);
        }
    }

    void dumpNull()
    {
        (*_o) << "   (null)" << endl;
    }

    void dumpBuffer(const void *buffer, unsigned size)
    {
        unsigned dumpSize = size;
        if (maxDumpSize() && dumpSize > maxDumpSize())
        {
            dumpSize = maxDumpSize();
        }

        const unsigned char *p = (const unsigned char *)buffer;
        (*_o) << "   { ";
        for (unsigned i = 0; i < dumpSize; ++ i)
        {
            (*_o) << byteToHex(*p++) << " ";
        }
        if (dumpSize < size)
        {
            (*_o) << "... ";
        }
        (*_o) << "}" << endl;
    }

    static void endl(OutputStream &o)
    {
        OutputStream::endl(o);
    }

    bool _dotting;
    bool _reported;
    OutputStream *_o;
    const char *_preLine;
    const char *_postLine;
    const char *_errorString;
    const char *_warningString;
};
}

#endif // __cxxtest__ErrorFormatter_h__