summaryrefslogtreecommitdiff
path: root/src/cppunit
diff options
context:
space:
mode:
authorBaptiste Lepilleur <gaiacrtn@free.fr>2002-05-23 17:38:39 +0000
committerBaptiste Lepilleur <gaiacrtn@free.fr>2002-05-23 17:38:39 +0000
commit5ad4640702a80078748b38ebaeda37e69dce1189 (patch)
tree9d1ecf8d28f0e9397c2f90565d96ccda6d08b98e /src/cppunit
parent7edd0684368ed3c43fe2707d1d34d6b7590d9fd6 (diff)
downloadcppunit-5ad4640702a80078748b38ebaeda37e69dce1189.tar.gz
Include/cppunit/XmlOutputter.
include/cppunit/XmlOutputter.h: * src/cppunit/XmlOutputter.cpp: extracted class XmlOutputter::Node to XmlElement. Extracted xml 'prolog' generation to XmlDocument. * include/cppunit/tools/XmlElement.h: * src/cppunit/tools/XmlElement.cpp: added, extracted from XmlOutputter::Node. * include/cppunit/tools/XmlDocument.h: * src/cppunit/tools/XmlDocument.cpp: added, extracted from XmlOutputter. Handle XML document prolog (encoding & style-sheet) and manage the root element. * src/DllPlugInTester/DllPlugInTester.cpp: bug fix, flag --xsl was ignored. * examples/cppunittest/XmlOutputterTest.h: * examples/cppunittest/XmlOutputterTest.cpp: updated for XmlOuputter changes. extracted tests for XmlOutputter::Node to XmlElementTest * examples/cppunittest/XmlElementTest.h: * examples/cppunittest/XmlElementTest.cpp: added, tests extracted from XmlOutputterTest.
Diffstat (limited to 'src/cppunit')
-rw-r--r--src/cppunit/Makefile.am4
-rw-r--r--src/cppunit/XmlDocument.cpp89
-rw-r--r--src/cppunit/XmlElement.cpp161
-rw-r--r--src/cppunit/XmlOutputter.cpp243
-rw-r--r--src/cppunit/cppunit.dsp20
-rw-r--r--src/cppunit/cppunit_dll.dsp20
6 files changed, 328 insertions, 209 deletions
diff --git a/src/cppunit/Makefile.am b/src/cppunit/Makefile.am
index ecd4887..965ad3c 100644
--- a/src/cppunit/Makefile.am
+++ b/src/cppunit/Makefile.am
@@ -1,5 +1,5 @@
#
-# $Id: Makefile.am,v 1.30 2002-04-20 21:51:33 blep Exp $
+# $Id: Makefile.am,v 1.31 2002-05-23 18:38:39 blep Exp $
#
EXTRA_DIST = cppunit.dsp cppunit_dll.dsp DllMain.cpp
@@ -41,6 +41,8 @@ libcppunit_la_SOURCES = \
TextTestRunner.cpp \
TypeInfoHelper.cpp \
UnixDynamicLibraryManager.cpp \
+ XmlDocument.cpp \
+ XmlElement.cpp \
XmlOutputter.cpp \
Win32DynamicLibraryManager.cpp
diff --git a/src/cppunit/XmlDocument.cpp b/src/cppunit/XmlDocument.cpp
new file mode 100644
index 0000000..b0bebab
--- /dev/null
+++ b/src/cppunit/XmlDocument.cpp
@@ -0,0 +1,89 @@
+#include <cppunit/tools/XmlDocument.h>
+#include <cppunit/tools/XmlElement.h>
+
+
+namespace CppUnit
+{
+
+
+
+
+XmlDocument::XmlDocument( const std::string &encoding,
+ const std::string &styleSheet )
+ : m_rootElement( new XmlElement( "DummyRoot" ) )
+ , m_styleSheet( styleSheet )
+{
+ setEncoding( encoding );
+}
+
+
+XmlDocument::~XmlDocument()
+{
+ delete m_rootElement;
+}
+
+
+
+std::string
+XmlDocument::encoding() const
+{
+ return m_encoding;
+}
+
+
+void
+XmlDocument::setEncoding( const std::string &encoding )
+{
+ m_encoding = encoding.empty() ? "ISO-8859-1" : encoding;
+}
+
+
+std::string
+XmlDocument::styleSheet() const
+{
+ return m_styleSheet;
+}
+
+
+void
+XmlDocument::setStyleSheet( const std::string &styleSheet )
+{
+ m_styleSheet = styleSheet;
+}
+
+
+void
+XmlDocument::setRootElement( XmlElement *rootElement )
+{
+ if ( rootElement == m_rootElement )
+ return;
+
+ delete m_rootElement;
+ m_rootElement = rootElement;
+}
+
+
+XmlElement &
+XmlDocument::rootElement() const
+{
+ return *m_rootElement;
+}
+
+
+std::string
+XmlDocument::toString() const
+{
+ std::string asString = "<?xml version=\"1.0\" "
+ "encoding='" + m_encoding + "' standalone='yes' ?>\n";
+
+ if ( !m_styleSheet.empty() )
+ asString += "<?xml-stylesheet type=\"text/xsl\" href=\"" + m_styleSheet + "\"?>\n";
+
+ asString += m_rootElement->toString();
+
+ return asString;
+}
+
+
+} // namespace CppUnit
+
diff --git a/src/cppunit/XmlElement.cpp b/src/cppunit/XmlElement.cpp
new file mode 100644
index 0000000..4a01405
--- /dev/null
+++ b/src/cppunit/XmlElement.cpp
@@ -0,0 +1,161 @@
+#include <cppunit/tools/XmlElement.h>
+
+
+namespace CppUnit
+{
+
+
+XmlElement::XmlElement( std::string elementName,
+ std::string content )
+ : m_name( elementName )
+ , m_content( content )
+{
+}
+
+
+XmlElement::XmlElement( std::string elementName,
+ int numericContent )
+ : m_name( elementName )
+{
+ m_content = asString( numericContent );
+}
+
+
+XmlElement::~XmlElement()
+{
+ Elements::iterator itNode = m_elements.begin();
+ while ( itNode != m_elements.end() )
+ delete *itNode++;
+}
+
+
+void
+XmlElement::addAttribute( std::string attributeName,
+ std::string value )
+{
+ m_attributes.push_back( Attribute( attributeName, value ) );
+}
+
+
+void
+XmlElement::addAttribute( std::string attributeName,
+ int numericValue )
+{
+ addAttribute( attributeName, asString( numericValue ) );
+}
+
+
+void
+XmlElement::addNode( XmlElement *node )
+{
+ m_elements.push_back( node );
+}
+
+
+std::string
+XmlElement::toString( const std::string &indent ) const
+{
+ std::string element( indent );
+ element += "<";
+ element += m_name;
+ if ( !m_attributes.empty() )
+ {
+ element += " ";
+ element += attributesAsString();
+ }
+ element += ">";
+
+ if ( !m_elements.empty() )
+ {
+ element += "\n";
+
+ std::string subNodeIndent( indent + " " );
+ Elements::const_iterator itNode = m_elements.begin();
+ while ( itNode != m_elements.end() )
+ {
+ const XmlElement *node = *itNode++;
+ element += node->toString( subNodeIndent );
+ }
+
+ element += indent;
+ }
+
+ if ( !m_content.empty() )
+ {
+ element += escape( m_content );
+ if ( !m_elements.empty() )
+ {
+ element += "\n";
+ element += indent;
+ }
+ }
+
+ element += "</";
+ element += m_name;
+ element += ">\n";
+
+ return element;
+}
+
+
+std::string
+XmlElement::attributesAsString() const
+{
+ std::string attributes;
+ Attributes::const_iterator itAttribute = m_attributes.begin();
+ while ( itAttribute != m_attributes.end() )
+ {
+ const Attribute &attribute = *itAttribute++;
+ attributes += attribute.first;
+ attributes += "=\"";
+ attributes += escape( attribute.second );
+ attributes += "\"";
+ }
+ return attributes;
+}
+
+
+std::string
+XmlElement::escape( std::string value ) const
+{
+ std::string escaped;
+ for ( int index =0; index < value.length(); ++index )
+ {
+ char c = value[index ];
+ switch ( c ) // escape all predefined XML entity (safe?)
+ {
+ case '<':
+ escaped += "&lt;";
+ break;
+ case '>':
+ escaped += "&gt;";
+ break;
+ case '&':
+ escaped += "&amp;";
+ break;
+ case '\'':
+ escaped += "&apos;";
+ break;
+ case '"':
+ escaped += "&quot;";
+ break;
+ default:
+ escaped += c;
+ }
+ }
+
+ return escaped;
+}
+
+// should be somewhere else... Future CppUnit::String ?
+std::string
+XmlElement::asString( int value )
+{
+ OStringStream stream;
+ stream << value;
+ return stream.str();
+}
+
+
+} // namespace CppUnit
+
diff --git a/src/cppunit/XmlOutputter.cpp b/src/cppunit/XmlOutputter.cpp
index 6a5c0d7..42e6b32 100644
--- a/src/cppunit/XmlOutputter.cpp
+++ b/src/cppunit/XmlOutputter.cpp
@@ -3,167 +3,18 @@
#include <cppunit/TestFailure.h>
#include <cppunit/TestResultCollector.h>
#include <cppunit/XmlOutputter.h>
-#include <map>
+#include <cppunit/tools/XmlDocument.h>
+#include <cppunit/tools/XmlElement.h>
#include <stdlib.h>
namespace CppUnit
{
-// XmlOutputter::Node
+// XmlElement
// //////////////////////////////////////////////////////////////////
-XmlOutputter::Node::Node( std::string elementName,
- std::string content ) :
- m_name( elementName ),
- m_content( content )
-{
-}
-
-
-XmlOutputter::Node::Node( std::string elementName,
- int numericContent ) :
- m_name( elementName )
-{
- m_content = asString( numericContent );
-}
-
-
-XmlOutputter::Node::~Node()
-{
- Nodes::iterator itNode = m_nodes.begin();
- while ( itNode != m_nodes.end() )
- delete *itNode++;
-}
-
-
-void
-XmlOutputter::Node::addAttribute( std::string attributeName,
- std::string value )
-{
- m_attributes.push_back( Attribute( attributeName, value ) );
-}
-
-
-void
-XmlOutputter::Node::addAttribute( std::string attributeName,
- int numericValue )
-{
- addAttribute( attributeName, asString( numericValue ) );
-}
-
-
-void
-XmlOutputter::Node::addNode( Node *node )
-{
- m_nodes.push_back( node );
-}
-
-
-std::string
-XmlOutputter::Node::toString( const std::string &indent ) const
-{
- std::string element( indent );
- element += "<";
- element += m_name;
- if ( !m_attributes.empty() )
- {
- element += " ";
- element += attributesAsString();
- }
- element += ">";
-
- if ( !m_nodes.empty() )
- {
- element += "\n";
-
- std::string subNodeIndent( indent + " " );
- Nodes::const_iterator itNode = m_nodes.begin();
- while ( itNode != m_nodes.end() )
- {
- const Node *node = *itNode++;
- element += node->toString( subNodeIndent );
- }
-
- element += indent;
- }
-
- if ( !m_content.empty() )
- {
- element += escape( m_content );
- if ( !m_nodes.empty() )
- {
- element += "\n";
- element += indent;
- }
- }
-
- element += "</";
- element += m_name;
- element += ">\n";
-
- return element;
-}
-
-
-std::string
-XmlOutputter::Node::attributesAsString() const
-{
- std::string attributes;
- Attributes::const_iterator itAttribute = m_attributes.begin();
- while ( itAttribute != m_attributes.end() )
- {
- const Attribute &attribute = *itAttribute++;
- attributes += attribute.first;
- attributes += "=\"";
- attributes += escape( attribute.second );
- attributes += "\"";
- }
- return attributes;
-}
-
-
-std::string
-XmlOutputter::Node::escape( std::string value ) const
-{
- std::string escaped;
- for ( int index =0; index < value.length(); ++index )
- {
- char c = value[index ];
- switch ( c ) // escape all predefined XML entity (safe?)
- {
- case '<':
- escaped += "&lt;";
- break;
- case '>':
- escaped += "&gt;";
- break;
- case '&':
- escaped += "&amp;";
- break;
- case '\'':
- escaped += "&apos;";
- break;
- case '"':
- escaped += "&quot;";
- break;
- default:
- escaped += c;
- }
- }
-
- return escaped;
-}
-
-// should be somewhere else... Future CppUnit::String ?
-std::string
-XmlOutputter::Node::asString( int value )
-{
- OStringStream stream;
- stream << value;
- return stream.str();
-}
@@ -173,63 +24,39 @@ XmlOutputter::Node::asString( int value )
XmlOutputter::XmlOutputter( TestResultCollector *result,
std::ostream &stream,
- std::string encoding ) :
- m_result( result ),
- m_stream( stream ),
- m_encoding( encoding == "" ? "ISO-8859-1" : encoding )
+ std::string encoding )
+ : m_result( result )
+ , m_stream( stream )
+ , m_xml( new XmlDocument( encoding ) )
{
}
XmlOutputter::~XmlOutputter()
{
+ delete m_xml;
}
void
XmlOutputter::write()
{
- writeProlog();
- writeTestsResult();
+ m_xml->setRootElement( makeRootNode() );
+ m_stream << m_xml->toString();
}
void
XmlOutputter::setStyleSheet( const std::string &styleSheet )
{
- m_styleSheet = styleSheet;
-}
-
-
-void
-XmlOutputter::writeProlog()
-{
- m_stream << "<?xml version=\"1.0\" "
- "encoding='" << m_encoding << "' standalone='yes' ?>"
- << std::endl;
-
- if ( !m_styleSheet.empty() )
- {
- m_stream << "<?xml-stylesheet type=\"text/xsl\" href=\""
- << m_styleSheet << "\"?>"
- << std::endl;
- }
-}
-
-
-void
-XmlOutputter::writeTestsResult()
-{
- Node *rootNode = makeRootNode();
- m_stream << rootNode->toString();
- delete rootNode;
+ m_xml->setStyleSheet( styleSheet );
}
-XmlOutputter::Node *
+XmlElement *
XmlOutputter::makeRootNode()
{
- Node *rootNode = new Node( "TestRun" );
+ XmlElement *rootNode = new XmlElement( "TestRun" );
FailedTests failedTests;
fillFailedTestsMap( failedTests );
@@ -257,9 +84,9 @@ XmlOutputter::fillFailedTestsMap( FailedTests &failedTests )
void
XmlOutputter::addFailedTests( FailedTests &failedTests,
- Node *rootNode )
+ XmlElement *rootNode )
{
- Node *testsNode = new Node( "FailedTests" );
+ XmlElement *testsNode = new XmlElement( "FailedTests" );
rootNode->addNode( testsNode );
const TestResultCollector::Tests &tests = m_result->tests();
@@ -274,9 +101,9 @@ XmlOutputter::addFailedTests( FailedTests &failedTests,
void
XmlOutputter::addSuccessfulTests( FailedTests &failedTests,
- Node *rootNode )
+ XmlElement *rootNode )
{
- Node *testsNode = new Node( "SuccessfulTests" );
+ XmlElement *testsNode = new XmlElement( "SuccessfulTests" );
rootNode->addNode( testsNode );
const TestResultCollector::Tests &tests = m_result->tests();
@@ -290,15 +117,15 @@ XmlOutputter::addSuccessfulTests( FailedTests &failedTests,
void
-XmlOutputter::addStatistics( Node *rootNode )
+XmlOutputter::addStatistics( XmlElement *rootNode )
{
- Node *statisticsNode = new Node( "Statistics" );
+ XmlElement *statisticsNode = new XmlElement( "Statistics" );
rootNode->addNode( statisticsNode );
- statisticsNode->addNode( new Node( "Tests", m_result->runTests() ) );
- statisticsNode->addNode( new Node( "FailuresTotal",
+ statisticsNode->addNode( new XmlElement( "Tests", m_result->runTests() ) );
+ statisticsNode->addNode( new XmlElement( "FailuresTotal",
m_result->testFailuresTotal() ) );
- statisticsNode->addNode( new Node( "Errors", m_result->testErrors() ) );
- statisticsNode->addNode( new Node( "Failures", m_result->testFailures() ) );
+ statisticsNode->addNode( new XmlElement( "Errors", m_result->testErrors() ) );
+ statisticsNode->addNode( new XmlElement( "Failures", m_result->testFailures() ) );
}
@@ -306,45 +133,45 @@ void
XmlOutputter::addFailedTest( Test *test,
TestFailure *failure,
int testNumber,
- Node *testsNode )
+ XmlElement *testsNode )
{
Exception *thrownException = failure->thrownException();
- Node *testNode = new Node( "FailedTest" );
+ XmlElement *testNode = new XmlElement( "FailedTest" );
testsNode->addNode( testNode );
testNode->addAttribute( "id", testNumber );
- testNode->addNode( new Node( "Name", test->getName() ) );
- testNode->addNode( new Node( "FailureType",
+ testNode->addNode( new XmlElement( "Name", test->getName() ) );
+ testNode->addNode( new XmlElement( "FailureType",
failure->isError() ? "Error" : "Assertion" ) );
if ( failure->sourceLine().isValid() )
addFailureLocation( failure, testNode );
- testNode->addNode( new Node( "Message", thrownException->what() ) );
+ testNode->addNode( new XmlElement( "Message", thrownException->what() ) );
}
void
XmlOutputter::addFailureLocation( TestFailure *failure,
- Node *testNode )
+ XmlElement *testNode )
{
- Node *locationNode = new Node( "Location" );
+ XmlElement *locationNode = new XmlElement( "Location" );
testNode->addNode( locationNode );
SourceLine sourceLine = failure->sourceLine();
- locationNode->addNode( new Node( "File", sourceLine.fileName() ) );
- locationNode->addNode( new Node( "Line", sourceLine.lineNumber() ) );
+ locationNode->addNode( new XmlElement( "File", sourceLine.fileName() ) );
+ locationNode->addNode( new XmlElement( "Line", sourceLine.lineNumber() ) );
}
void
XmlOutputter::addSuccessfulTest( Test *test,
int testNumber,
- Node *testsNode )
+ XmlElement *testsNode )
{
- Node *testNode = new Node( "Test" );
+ XmlElement *testNode = new XmlElement( "Test" );
testsNode->addNode( testNode );
testNode->addAttribute( "id", testNumber );
- testNode->addNode( new Node( "Name", test->getName() ) );
+ testNode->addNode( new XmlElement( "Name", test->getName() ) );
}
diff --git a/src/cppunit/cppunit.dsp b/src/cppunit/cppunit.dsp
index d33cc04..b8c5c97 100644
--- a/src/cppunit/cppunit.dsp
+++ b/src/cppunit/cppunit.dsp
@@ -492,6 +492,26 @@ SOURCE=.\UnixDynamicLibraryManager.cpp
SOURCE=.\Win32DynamicLibraryManager.cpp
# End Source File
# End Group
+# Begin Group "tools"
+
+# PROP Default_Filter ""
+# Begin Source File
+
+SOURCE=.\XmlDocument.cpp
+# End Source File
+# Begin Source File
+
+SOURCE=..\..\include\cppunit\tools\XmlDocument.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\XmlElement.cpp
+# End Source File
+# Begin Source File
+
+SOURCE=..\..\include\cppunit\tools\XmlElement.h
+# End Source File
+# End Group
# Begin Source File
SOURCE=..\..\configure.in
diff --git a/src/cppunit/cppunit_dll.dsp b/src/cppunit/cppunit_dll.dsp
index bc4ce19..9f6937b 100644
--- a/src/cppunit/cppunit_dll.dsp
+++ b/src/cppunit/cppunit_dll.dsp
@@ -494,6 +494,26 @@ SOURCE=.\UnixDynamicLibraryManager.cpp
SOURCE=.\Win32DynamicLibraryManager.cpp
# End Source File
# End Group
+# Begin Group "tools"
+
+# PROP Default_Filter ""
+# Begin Source File
+
+SOURCE=.\XmlDocument.cpp
+# End Source File
+# Begin Source File
+
+SOURCE=..\..\include\cppunit\tools\XmlDocument.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\XmlElement.cpp
+# End Source File
+# Begin Source File
+
+SOURCE=..\..\include\cppunit\tools\XmlElement.h
+# End Source File
+# End Group
# Begin Source File
SOURCE="..\..\INSTALL-WIN32.txt"