xml_reporter.hpp

Go to the documentation of this file.
00001 // vim:ts=2:sw=2:expandtab:autoindent:filetype=cpp:
00002 /*
00003   testsoon.hpp: "Test soon" testing framework.
00004 
00005   Copyright (C) 2006 Aristid Breitkreuz, Ronny Pfannschmidt and 
00006                      Benjamin Bykowski
00007 
00008   This software is provided 'as-is', without any express or implied
00009   warranty.  In no event will the authors be held liable for any damages
00010   arising from the use of this software.
00011 
00012   Permission is granted to anyone to use this software for any purpose,
00013   including commercial applications, and to alter it and redistribute it
00014   freely, subject to the following restrictions:
00015 
00016   1. The origin of this software must not be misrepresented; you must not
00017      claim that you wrote the original software. If you use this software
00018      in a product, an acknowledgment in the product documentation would be
00019      appreciated but is not required.
00020   2. Altered source versions must be plainly marked as such, and must not be
00021      misrepresented as being the original software.
00022   3. This notice may not be removed or altered from any source distribution.
00023 
00024   Aristid Breitkreuz aribrei@arcor.de
00025   Ronny Pfannschmidt Ronny.Pfannschmidt@gmx.de
00026   Benjamin Bykowski bennybyko@gmx.de
00027 */
00028 
00029 #ifndef TESTSOON_XML_REPORTER_HPP
00030 #define TESTSOON_XML_REPORTER_HPP
00031 
00032 #include "../testsoon.hpp"
00033 
00034 namespace testsoon {
00035 
00036 class xml_reporter : public test_reporter {
00037   public:
00038     typedef stream_class stream;
00039     xml_reporter(stream &out) : out(out), indent(1) {}
00040 
00041     void start() {
00042       out << "<?xml version=\"1.0\"?>\n";
00043       out << "<testsoon>\n";
00044     }
00045     void stop() {
00046       out << "</testsoon>\n";
00047     }
00048 
00049     void begin_group(test_group const &group) {
00050       if (group.parent) {
00051         print_ind();
00052         ++indent;
00053         if (group.parent->parent)
00054           out << "<group";
00055         else
00056           out << "<file";
00057         out << " name=\"" << group.name << "\">\n";
00058       }
00059     }
00060 
00061     void end_group(test_group const &group) {
00062       if (group.parent) {
00063         --indent;
00064         print_ind();
00065         if (group.parent->parent)
00066           out << "</group>\n";
00067         else
00068           out << "</file>\n";
00069       }
00070     }
00071 
00072     void success(test_info const &i, string const &k) {
00073       print_ind() << "<success line=\"" << i.line << "\"";      
00074       if (*i.name)
00075         out << " name=\"" << i.name << "\"";
00076       if (k.empty())
00077         out << " />\n";
00078       else {
00079         out << ">\n";
00080         ++indent;
00081         print_ind() << "<generator>" << k << "</generator>\n";
00082         --indent;
00083         print_ind() << "</success>\n";
00084       }
00085     }
00086     
00087     void failure(test_info const &i, test_failure const &x, string const &k) {
00088       print_ind() << "<failure line=\"" << i.line << "\"";
00089       if (*i.name)
00090         out << " name=\"" << i.name << "\"";
00091       out << ">\n";
00092       ++indent;
00093 
00094       print_ind() << "<problem>" << x.message << "</problem>\n";
00095       if (!x.data.empty()) {
00096         print_ind() << "<rawdata>\n";
00097         ++indent;
00098         for (string_vector::const_iterator it = x.data.begin(); 
00099             it != x.data.end();
00100             ++it) {
00101           print_ind() << "<item>" << *it << "</item>\n";
00102         } 
00103         --indent;
00104         print_ind() << "</rawdata>\n";
00105       }
00106 
00107       if (!k.empty()) {
00108         print_ind() << "<generator>" << k << "</generator>\n";
00109       }
00110 
00111       --indent;
00112       print_ind() << "</failure>\n";
00113     } 
00114 
00115   private:
00116     stream_class &print_ind() {
00117       for (unsigned i = 0; i < indent; ++i)
00118         out << "  ";
00119       return out;
00120     }
00121 
00122     stream &out;
00123     unsigned indent;
00124 };
00125 
00126 }
00127 
00128 #endif

Generated on Sat Dec 22 15:34:59 2007 for Test soon by  doxygen 1.5.3