Blame examples/cppunittest/XmlOutputterTest.cpp

Packit 8c9aa0
#include <cppunit/config/SourcePrefix.h>
Packit 8c9aa0
#include <cppunit/XmlOutputter.h>
Packit 8c9aa0
#include <cppunit/TestFailure.h>
Packit 8c9aa0
#include <cppunit/XmlOutputter.h>
Packit 8c9aa0
#include <cppunit/XmlOutputterHook.h>
Packit 8c9aa0
#include "OutputSuite.h"
Packit 8c9aa0
#include "XmlOutputterTest.h"
Packit 8c9aa0
#include "XmlUniformiser.h"
Packit 8c9aa0
Packit 8c9aa0
Packit 8c9aa0
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( XmlOutputterTest, 
Packit 8c9aa0
                                       outputSuiteName() );
Packit 8c9aa0
Packit 8c9aa0
Packit 8c9aa0
XmlOutputterTest::XmlOutputterTest()
Packit 8c9aa0
{
Packit 8c9aa0
}
Packit 8c9aa0
Packit 8c9aa0
Packit 8c9aa0
XmlOutputterTest::~XmlOutputterTest()
Packit 8c9aa0
{
Packit 8c9aa0
}
Packit 8c9aa0
Packit 8c9aa0
Packit 8c9aa0
void 
Packit 8c9aa0
XmlOutputterTest::setUp()
Packit 8c9aa0
{
Packit 8c9aa0
  m_dummyTests.clear();
Packit 8c9aa0
  m_result = new CPPUNIT_NS::TestResultCollector();
Packit 8c9aa0
}
Packit 8c9aa0
Packit 8c9aa0
Packit 8c9aa0
void 
Packit 8c9aa0
XmlOutputterTest::tearDown()
Packit 8c9aa0
{
Packit 8c9aa0
  delete m_result;
Packit 8c9aa0
  for ( unsigned int index =0; index < m_dummyTests.size(); ++index )
Packit 8c9aa0
    delete m_dummyTests[index];
Packit 8c9aa0
  m_dummyTests.clear();
Packit 8c9aa0
}
Packit 8c9aa0
Packit 8c9aa0
Packit 8c9aa0
void 
Packit 8c9aa0
XmlOutputterTest::testWriteXmlResultWithNoTest()
Packit 8c9aa0
{
Packit 8c9aa0
  CPPUNIT_NS::OStringStream stream;
Packit 8c9aa0
  CPPUNIT_NS::XmlOutputter outputter( m_result, stream );
Packit 8c9aa0
  outputter.write();
Packit 8c9aa0
Packit 8c9aa0
  std::string actualXml = stream.str();
Packit 8c9aa0
  std::string expectedXml = 
Packit 8c9aa0
    "<TestRun>"
Packit 8c9aa0
      "<FailedTests></FailedTests>"
Packit 8c9aa0
      "<SuccessfulTests></SuccessfulTests>"
Packit 8c9aa0
      "<Statistics>"
Packit 8c9aa0
        "<Tests>0</Tests>"
Packit 8c9aa0
        "<FailuresTotal>0</FailuresTotal>"
Packit 8c9aa0
        "<Errors>0</Errors>"
Packit 8c9aa0
        "<Failures>0</Failures>"
Packit 8c9aa0
      "</Statistics>"
Packit 8c9aa0
    "</TestRun>";
Packit 8c9aa0
  CPPUNITTEST_ASSERT_XML_EQUAL( expectedXml, actualXml );
Packit 8c9aa0
}
Packit 8c9aa0
Packit 8c9aa0
Packit 8c9aa0
void 
Packit 8c9aa0
XmlOutputterTest::testWriteXmlResultWithOneFailure()
Packit 8c9aa0
{
Packit 8c9aa0
  addTestFailure( "test1", "message failure1", CPPUNIT_NS::SourceLine( "test.cpp", 3 ) );
Packit 8c9aa0
Packit 8c9aa0
  CPPUNIT_NS::OStringStream stream;
Packit 8c9aa0
  CPPUNIT_NS::XmlOutputter outputter( m_result, stream );
Packit 8c9aa0
  outputter.write();
Packit 8c9aa0
Packit 8c9aa0
  std::string actualXml = stream.str();
Packit 8c9aa0
  std::string expectedXml = 
Packit 8c9aa0
    "<TestRun>"
Packit 8c9aa0
      "<FailedTests>"
Packit 8c9aa0
        "<FailedTest id=\"1\">"
Packit 8c9aa0
          "<Name>test1</Name>"
Packit 8c9aa0
          "<FailureType>Assertion</FailureType>"
Packit 8c9aa0
          "<Location>"
Packit 8c9aa0
            "<File>test.cpp</File>"
Packit 8c9aa0
            "<Line>3</Line>"
Packit 8c9aa0
          "</Location>"
Packit 8c9aa0
          "<Message>message failure1</Message>"
Packit 8c9aa0
        "</FailedTest>"
Packit 8c9aa0
      "</FailedTests>"
Packit 8c9aa0
      "<SuccessfulTests></SuccessfulTests>"
Packit 8c9aa0
      "<Statistics>"
Packit 8c9aa0
        "<Tests>1</Tests>"
Packit 8c9aa0
        "<FailuresTotal>1</FailuresTotal>"
Packit 8c9aa0
        "<Errors>0</Errors>"
Packit 8c9aa0
        "<Failures>1</Failures>"
Packit 8c9aa0
      "</Statistics>"
Packit 8c9aa0
    "</TestRun>";
Packit 8c9aa0
  CPPUNITTEST_ASSERT_XML_EQUAL( expectedXml, actualXml );
Packit 8c9aa0
}
Packit 8c9aa0
Packit 8c9aa0
Packit 8c9aa0
void 
Packit 8c9aa0
XmlOutputterTest::testWriteXmlResultWithOneError()
Packit 8c9aa0
{
Packit 8c9aa0
  addTestError( "test1", "message error1" );
Packit 8c9aa0
Packit 8c9aa0
  CPPUNIT_NS::OStringStream stream;
Packit 8c9aa0
  CPPUNIT_NS::XmlOutputter outputter( m_result, stream );
Packit 8c9aa0
  outputter.write();
Packit 8c9aa0
Packit 8c9aa0
  std::string actualXml = stream.str();
Packit 8c9aa0
  std::string expectedXml = 
Packit 8c9aa0
    "<TestRun>"
Packit 8c9aa0
      "<FailedTests>"
Packit 8c9aa0
        "<FailedTest id=\"1\">"
Packit 8c9aa0
          "<Name>test1</Name>"
Packit 8c9aa0
          "<FailureType>Error</FailureType>"
Packit 8c9aa0
          "<Message>message error1</Message>"
Packit 8c9aa0
        "</FailedTest>"
Packit 8c9aa0
      "</FailedTests>"
Packit 8c9aa0
      "<SuccessfulTests></SuccessfulTests>"
Packit 8c9aa0
      "<Statistics>"
Packit 8c9aa0
        "<Tests>1</Tests>"
Packit 8c9aa0
        "<FailuresTotal>1</FailuresTotal>"
Packit 8c9aa0
        "<Errors>1</Errors>"
Packit 8c9aa0
        "<Failures>0</Failures>"
Packit 8c9aa0
      "</Statistics>"
Packit 8c9aa0
    "</TestRun>";
Packit 8c9aa0
  CPPUNITTEST_ASSERT_XML_EQUAL( expectedXml, actualXml );
Packit 8c9aa0
}
Packit 8c9aa0
Packit 8c9aa0
Packit 8c9aa0
void 
Packit 8c9aa0
XmlOutputterTest::testWriteXmlResultWithOneSuccess()
Packit 8c9aa0
{
Packit 8c9aa0
  addTest( "test1" );
Packit 8c9aa0
Packit 8c9aa0
  CPPUNIT_NS::OStringStream stream;
Packit 8c9aa0
  CPPUNIT_NS::XmlOutputter outputter( m_result, stream );
Packit 8c9aa0
  outputter.write();
Packit 8c9aa0
Packit 8c9aa0
  std::string actualXml = stream.str();
Packit 8c9aa0
  std::string expectedXml = 
Packit 8c9aa0
    "<TestRun>"
Packit 8c9aa0
      "<FailedTests></FailedTests>"
Packit 8c9aa0
      "<SuccessfulTests>"
Packit 8c9aa0
        "<Test id=\"1\">"
Packit 8c9aa0
          "<Name>test1</Name>"
Packit 8c9aa0
        "</Test>"
Packit 8c9aa0
      "</SuccessfulTests>"
Packit 8c9aa0
      "<Statistics>"
Packit 8c9aa0
        "<Tests>1</Tests>"
Packit 8c9aa0
        "<FailuresTotal>0</FailuresTotal>"
Packit 8c9aa0
        "<Errors>0</Errors>"
Packit 8c9aa0
        "<Failures>0</Failures>"
Packit 8c9aa0
      "</Statistics>"
Packit 8c9aa0
    "</TestRun>";
Packit 8c9aa0
  CPPUNITTEST_ASSERT_XML_EQUAL( expectedXml, actualXml );
Packit 8c9aa0
}
Packit 8c9aa0
Packit 8c9aa0
Packit 8c9aa0
void 
Packit 8c9aa0
XmlOutputterTest::testWriteXmlResultWithThreeFailureTwoErrorsAndTwoSuccess()
Packit 8c9aa0
{
Packit 8c9aa0
  addTestFailure( "test1", "failure1" );
Packit 8c9aa0
  addTestError( "test2", "error1" );
Packit 8c9aa0
  addTestFailure( "test3", "failure2" );
Packit 8c9aa0
  addTestFailure( "test4", "failure3" );
Packit 8c9aa0
  addTest( "test5" );
Packit 8c9aa0
  addTestError( "test6", "error2" );
Packit 8c9aa0
  addTest( "test7" );
Packit 8c9aa0
Packit 8c9aa0
  CPPUNIT_NS::OStringStream stream;
Packit 8c9aa0
  CPPUNIT_NS::XmlOutputter outputter( m_result, stream );
Packit 8c9aa0
  outputter.write();
Packit 8c9aa0
Packit 8c9aa0
  std::string actualXml = stream.str();
Packit 8c9aa0
  std::string expectedXml = 
Packit 8c9aa0
    "<TestRun>"
Packit 8c9aa0
       "<FailedTests>"
Packit 8c9aa0
        "<FailedTest id=\"1\">"
Packit 8c9aa0
          "<Name>test1</Name>"
Packit 8c9aa0
          "<FailureType>Assertion</FailureType>"
Packit 8c9aa0
          "<Message>failure1</Message>"
Packit 8c9aa0
        "</FailedTest>"
Packit 8c9aa0
        "<FailedTest id=\"2\">"
Packit 8c9aa0
          "<Name>test2</Name>"
Packit 8c9aa0
          "<FailureType>Error</FailureType>"
Packit 8c9aa0
          "<Message>error1</Message>"
Packit 8c9aa0
        "</FailedTest>"
Packit 8c9aa0
        "<FailedTest id=\"3\">"
Packit 8c9aa0
          "<Name>test3</Name>"
Packit 8c9aa0
          "<FailureType>Assertion</FailureType>"
Packit 8c9aa0
          "<Message>failure2</Message>"
Packit 8c9aa0
        "</FailedTest>"
Packit 8c9aa0
        "<FailedTest id=\"4\">"
Packit 8c9aa0
          "<Name>test4</Name>"
Packit 8c9aa0
          "<FailureType>Assertion</FailureType>"
Packit 8c9aa0
          "<Message>failure3</Message>"
Packit 8c9aa0
        "</FailedTest>"
Packit 8c9aa0
        "<FailedTest id=\"6\">"
Packit 8c9aa0
          "<Name>test6</Name>"
Packit 8c9aa0
          "<FailureType>Error</FailureType>"
Packit 8c9aa0
          "<Message>error2</Message>"
Packit 8c9aa0
        "</FailedTest>"
Packit 8c9aa0
      "</FailedTests>"
Packit 8c9aa0
     "<SuccessfulTests>"
Packit 8c9aa0
        "<Test id=\"5\">"
Packit 8c9aa0
          "<Name>test5</Name>"
Packit 8c9aa0
        "</Test>"
Packit 8c9aa0
        "<Test id=\"7\">"
Packit 8c9aa0
          "<Name>test7</Name>"
Packit 8c9aa0
        "</Test>"
Packit 8c9aa0
      "</SuccessfulTests>"
Packit 8c9aa0
      "<Statistics>"
Packit 8c9aa0
        "<Tests>7</Tests>"
Packit 8c9aa0
        "<FailuresTotal>5</FailuresTotal>"
Packit 8c9aa0
        "<Errors>2</Errors>"
Packit 8c9aa0
        "<Failures>3</Failures>"
Packit 8c9aa0
      "</Statistics>"
Packit 8c9aa0
    "</TestRun>";
Packit 8c9aa0
  CPPUNITTEST_ASSERT_XML_EQUAL( expectedXml, actualXml );
Packit 8c9aa0
}
Packit 8c9aa0
Packit 8c9aa0
Packit 8c9aa0
class XmlOutputterTest::MockHook : public CPPUNIT_NS::XmlOutputterHook
Packit 8c9aa0
{
Packit 8c9aa0
public:
Packit 8c9aa0
  MockHook( int &beginCalls,
Packit 8c9aa0
            int &endCalls,
Packit 8c9aa0
            int &statisticsCalls,
Packit 8c9aa0
            int &successfulTestCalls,
Packit 8c9aa0
            int &failedTestCalls )
Packit 8c9aa0
      : m_beginCalls( beginCalls )
Packit 8c9aa0
      , m_endCalls( endCalls )
Packit 8c9aa0
      , m_statisticsCalls( statisticsCalls )
Packit 8c9aa0
      , m_successfulTestCalls( successfulTestCalls )
Packit 8c9aa0
      , m_failedTestCalls( failedTestCalls )
Packit 8c9aa0
  {
Packit 8c9aa0
  }
Packit 8c9aa0
Packit 8c9aa0
  void beginDocument( CPPUNIT_NS::XmlDocument * )
Packit 8c9aa0
  {
Packit 8c9aa0
    ++m_beginCalls;
Packit 8c9aa0
  }
Packit 8c9aa0
Packit 8c9aa0
  void endDocument( CPPUNIT_NS::XmlDocument * )
Packit 8c9aa0
  {
Packit 8c9aa0
    ++m_endCalls;
Packit 8c9aa0
  }
Packit 8c9aa0
Packit 8c9aa0
  void failTestAdded( CPPUNIT_NS::XmlDocument *,
Packit 8c9aa0
                      CPPUNIT_NS::XmlElement *,
Packit 8c9aa0
                      CPPUNIT_NS::Test *,
Packit 8c9aa0
                      CPPUNIT_NS::TestFailure * )
Packit 8c9aa0
  {
Packit 8c9aa0
    ++m_failedTestCalls;
Packit 8c9aa0
  }
Packit 8c9aa0
Packit 8c9aa0
  void successfulTestAdded( CPPUNIT_NS::XmlDocument *,
Packit 8c9aa0
                            CPPUNIT_NS::XmlElement *,
Packit 8c9aa0
                            CPPUNIT_NS::Test * )
Packit 8c9aa0
  {
Packit 8c9aa0
    ++m_successfulTestCalls;
Packit 8c9aa0
  }
Packit 8c9aa0
Packit 8c9aa0
  void statisticsAdded( CPPUNIT_NS::XmlDocument *,
Packit 8c9aa0
                        CPPUNIT_NS::XmlElement * )
Packit 8c9aa0
  {
Packit 8c9aa0
    ++m_statisticsCalls;
Packit 8c9aa0
  }
Packit 8c9aa0
Packit 8c9aa0
private:
Packit 8c9aa0
  int &m_beginCalls;
Packit 8c9aa0
  int &m_endCalls;
Packit 8c9aa0
  int &m_statisticsCalls;
Packit 8c9aa0
  int &m_successfulTestCalls;
Packit 8c9aa0
  int &m_failedTestCalls;
Packit 8c9aa0
};
Packit 8c9aa0
Packit 8c9aa0
Packit 8c9aa0
void 
Packit 8c9aa0
XmlOutputterTest::testHook()
Packit 8c9aa0
{
Packit 8c9aa0
  int begin =0, end =0, statistics =0, successful =0, failed =0;
Packit 8c9aa0
  MockHook hook( begin, end, statistics, successful, failed );
Packit 8c9aa0
Packit 8c9aa0
  addTest( "test1" );
Packit 8c9aa0
  addTest( "test2" );
Packit 8c9aa0
  addTest( "test3" );
Packit 8c9aa0
  addTestFailure( "testfail1", "assertion failed" );
Packit 8c9aa0
  addTestError( "testerror1", "exception" );
Packit 8c9aa0
Packit 8c9aa0
  CPPUNIT_NS::OStringStream stream;
Packit 8c9aa0
  CPPUNIT_NS::XmlOutputter outputter( m_result, stream );
Packit 8c9aa0
  outputter.addHook( &hook );
Packit 8c9aa0
  outputter.write();
Packit 8c9aa0
Packit 8c9aa0
  CPPUNIT_ASSERT_EQUAL( 1, begin );
Packit 8c9aa0
  CPPUNIT_ASSERT_EQUAL( 1, end );
Packit 8c9aa0
  CPPUNIT_ASSERT_EQUAL( 1, statistics );
Packit 8c9aa0
  CPPUNIT_ASSERT_EQUAL( 3, successful );
Packit 8c9aa0
  CPPUNIT_ASSERT_EQUAL( 2, failed );
Packit 8c9aa0
}
Packit 8c9aa0
Packit 8c9aa0
Packit 8c9aa0
void 
Packit 8c9aa0
XmlOutputterTest::addTest( std::string testName )
Packit 8c9aa0
{
Packit 8c9aa0
  CPPUNIT_NS::Test *test = makeDummyTest( testName );
Packit 8c9aa0
  m_result->startTest( test );
Packit 8c9aa0
  m_result->endTest( test );
Packit 8c9aa0
}
Packit 8c9aa0
Packit 8c9aa0
Packit 8c9aa0
void 
Packit 8c9aa0
XmlOutputterTest::addTestFailure( std::string testName,
Packit 8c9aa0
                                  std::string message,
Packit 8c9aa0
                                  CPPUNIT_NS::SourceLine sourceLine )
Packit 8c9aa0
{
Packit 8c9aa0
  addGenericTestFailure( testName, CPPUNIT_NS::Message(message), sourceLine, false );
Packit 8c9aa0
}
Packit 8c9aa0
Packit 8c9aa0
Packit 8c9aa0
void 
Packit 8c9aa0
XmlOutputterTest::addTestError( std::string testName,
Packit 8c9aa0
                                std::string message,
Packit 8c9aa0
                                CPPUNIT_NS::SourceLine sourceLine )
Packit 8c9aa0
{
Packit 8c9aa0
  addGenericTestFailure( testName, CPPUNIT_NS::Message(message), sourceLine, true );
Packit 8c9aa0
}
Packit 8c9aa0
Packit 8c9aa0
Packit 8c9aa0
void 
Packit 8c9aa0
XmlOutputterTest::addGenericTestFailure(  std::string testName,
Packit 8c9aa0
                                          CPPUNIT_NS::Message message,
Packit 8c9aa0
                                          CPPUNIT_NS::SourceLine sourceLine,
Packit 8c9aa0
                                          bool isError )
Packit 8c9aa0
{
Packit 8c9aa0
  CPPUNIT_NS::Test *test = makeDummyTest( testName );
Packit 8c9aa0
  m_result->startTest( test );
Packit 8c9aa0
  CPPUNIT_NS::TestFailure failure( test, 
Packit 8c9aa0
                                new CPPUNIT_NS::Exception( message, sourceLine ),
Packit 8c9aa0
                                isError );
Packit 8c9aa0
  m_result->addFailure( failure );
Packit 8c9aa0
  m_result->endTest( test );
Packit 8c9aa0
}
Packit 8c9aa0
Packit 8c9aa0
Packit 8c9aa0
CPPUNIT_NS::Test *
Packit 8c9aa0
XmlOutputterTest::makeDummyTest( std::string testName )
Packit 8c9aa0
{
Packit 8c9aa0
  CPPUNIT_NS::Test *test = new CPPUNIT_NS::TestCase( testName );
Packit 8c9aa0
  m_dummyTests.push_back( test );
Packit 8c9aa0
  return test;
Packit 8c9aa0
}
Packit 8c9aa0