root/library/tests/unittest-cpp/XmlTestReporter.cpp @ 434

Revision 418, 3.2 kB (checked in by vbarta, 15 years ago)

#27: added UnitTest?++ to bdm sources, changed test_user_data to use it

Line 
1#include "XmlTestReporter.h"
2#include "Config.h"
3
4#include <iostream>
5#include <sstream>
6#include <string>
7
8using std::string;
9using std::ostringstream;
10using std::ostream;
11
12namespace {
13
14void ReplaceChar(string& str, char c, string const& replacement)
15{
16    for (size_t pos = str.find(c); pos != string::npos; pos = str.find(c, pos + 1))
17        str.replace(pos, 1, replacement);
18}
19
20string XmlEscape(string const& value)
21{
22    string escaped = value;
23
24    ReplaceChar(escaped, '&', "&amp;");
25    ReplaceChar(escaped, '<', "&lt;");
26    ReplaceChar(escaped, '>', "&gt;");
27    ReplaceChar(escaped, '\'', "&apos;");
28    ReplaceChar(escaped, '\"', "&quot;");
29 
30    return escaped;
31}
32
33string BuildFailureMessage(string const& file, int line, string const& message)
34{
35    ostringstream failureMessage;
36    failureMessage << file << "(" << line << ") : " << message;
37    return failureMessage.str();
38}
39
40}
41
42namespace UnitTest {
43
44XmlTestReporter::XmlTestReporter(ostream& ostream)
45    : m_ostream(ostream)
46{
47}
48
49void XmlTestReporter::ReportSummary(int totalTestCount, int failedTestCount,
50                                    int failureCount, float secondsElapsed)
51{
52    AddXmlElement(m_ostream, NULL);
53
54    BeginResults(m_ostream, totalTestCount, failedTestCount, failureCount, secondsElapsed);
55
56    DeferredTestResultList const& results = GetResults();
57    for (DeferredTestResultList::const_iterator i = results.begin(); i != results.end(); ++i)
58    {
59        BeginTest(m_ostream, *i);
60
61        if (i->failed)
62            AddFailure(m_ostream, *i);
63
64        EndTest(m_ostream, *i);
65    }
66
67    EndResults(m_ostream);
68}
69
70void XmlTestReporter::AddXmlElement(ostream& os, char const* encoding)
71{
72    os << "<?xml version=\"1.0\"";
73
74    if (encoding != NULL)
75        os << " encoding=\"" << encoding << "\"";
76
77    os << "?>";
78}
79
80void XmlTestReporter::BeginResults(std::ostream& os, int totalTestCount, int failedTestCount, 
81                                   int failureCount, float secondsElapsed)
82{
83   os << "<unittest-results"
84       << " tests=\"" << totalTestCount << "\"" 
85       << " failedtests=\"" << failedTestCount << "\"" 
86       << " failures=\"" << failureCount << "\"" 
87       << " time=\"" << secondsElapsed << "\""
88       << ">";
89}
90
91void XmlTestReporter::EndResults(std::ostream& os)
92{
93    os << "</unittest-results>";
94}
95
96void XmlTestReporter::BeginTest(std::ostream& os, DeferredTestResult const& result)
97{
98    os << "<test"
99        << " suite=\"" << result.suiteName << "\"" 
100        << " name=\"" << result.testName << "\""
101        << " time=\"" << result.timeElapsed << "\"";
102}
103
104void XmlTestReporter::EndTest(std::ostream& os, DeferredTestResult const& result)
105{
106    if (result.failed)
107        os << "</test>";
108    else
109        os << "/>";
110}
111
112void XmlTestReporter::AddFailure(std::ostream& os, DeferredTestResult const& result)
113{
114    os << ">"; // close <test> element
115
116    for (DeferredTestResult::FailureVec::const_iterator it = result.failures.begin(); 
117         it != result.failures.end(); 
118         ++it)
119    {
120        string const escapedMessage = XmlEscape(it->second);
121        string const message = BuildFailureMessage(result.failureFile, it->first, escapedMessage);
122
123        os << "<failure" << " message=\"" << message << "\"" << "/>";
124    }
125}
126
127}
Note: See TracBrowser for help on using the browser.