mirror of
https://github.com/pocoproject/poco.git
synced 2025-10-18 03:29:47 +02:00
initial import
This commit is contained in:
18
CppUnit/src/CppUnitException.cpp
Normal file
18
CppUnit/src/CppUnitException.cpp
Normal file
@@ -0,0 +1,18 @@
|
||||
//
|
||||
// CppUnitException.cpp
|
||||
//
|
||||
// $Id: //poco/1.1.0/CppUnit/src/CppUnitException.cpp#1 $
|
||||
//
|
||||
|
||||
|
||||
#include "CppUnit/CppUnitException.h"
|
||||
|
||||
|
||||
CppUnit_BEGIN
|
||||
|
||||
|
||||
const std::string CppUnitException::CPPUNIT_UNKNOWNFILENAME = "<unknown>";
|
||||
const int CppUnitException::CPPUNIT_UNKNOWNLINENUMBER = -1;
|
||||
|
||||
|
||||
CppUnit_END
|
168
CppUnit/src/TestCase.cpp
Normal file
168
CppUnit/src/TestCase.cpp
Normal file
@@ -0,0 +1,168 @@
|
||||
//
|
||||
// TestCase.cpp
|
||||
//
|
||||
// $Id: //poco/1.1.0/CppUnit/src/TestCase.cpp#1 $
|
||||
//
|
||||
|
||||
|
||||
#include <stdexcept>
|
||||
#include <math.h>
|
||||
#include "CppUnit/TestCase.h"
|
||||
#include "CppUnit/TestResult.h"
|
||||
#include "CppUnit/estring.h"
|
||||
#include <typeinfo>
|
||||
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
CppUnit_BEGIN
|
||||
|
||||
|
||||
// Create a default TestResult
|
||||
TestResult* TestCase::defaultResult()
|
||||
{
|
||||
return new TestResult;
|
||||
}
|
||||
|
||||
|
||||
// Check for a failed general assertion
|
||||
void TestCase::assertImplementation(bool condition, const std::string& conditionExpression, long lineNumber, const std::string& fileName)
|
||||
{
|
||||
if (!condition)
|
||||
throw CppUnitException(conditionExpression, lineNumber, fileName);
|
||||
}
|
||||
|
||||
|
||||
// Check for a failed equality assertion
|
||||
void TestCase::assertEquals(long expected, long actual, long lineNumber, const std::string& fileName)
|
||||
{
|
||||
if (expected != actual)
|
||||
assertImplementation(false, notEqualsMessage(expected, actual), lineNumber, fileName);
|
||||
}
|
||||
|
||||
|
||||
// Check for a failed equality assertion
|
||||
void TestCase::assertEquals(double expected, double actual, double delta, long lineNumber, const std::string& fileName)
|
||||
{
|
||||
if (fabs(expected - actual) > delta)
|
||||
assertImplementation(false, notEqualsMessage(expected, actual), lineNumber, fileName);
|
||||
}
|
||||
|
||||
|
||||
// Check for a failed equality assertion
|
||||
void TestCase::assertEquals(const void* expected, const void* actual, long lineNumber, const std::string& fileName)
|
||||
{
|
||||
if (expected != actual)
|
||||
assertImplementation(false, notEqualsMessage(expected, actual), lineNumber, fileName);
|
||||
}
|
||||
|
||||
|
||||
// Check for a failed equality assertion
|
||||
void TestCase::assertEquals(const std::string& expected, const std::string& actual, long lineNumber, const std::string& fileName)
|
||||
{
|
||||
if (expected != actual)
|
||||
assertImplementation(false, notEqualsMessage(expected, actual), lineNumber, fileName);
|
||||
}
|
||||
|
||||
|
||||
void TestCase::assertNotNull(const void* pointer, const std::string& pointerExpression, long lineNumber, const std::string& fileName)
|
||||
{
|
||||
if (pointer == NULL)
|
||||
throw CppUnitException(pointerExpression + " must not be NULL", lineNumber, fileName);
|
||||
}
|
||||
|
||||
|
||||
void TestCase::assertNull(const void* pointer, const std::string& pointerExpression, long lineNumber, const std::string& fileName)
|
||||
{
|
||||
if (pointer != NULL)
|
||||
throw CppUnitException(pointerExpression + " must be NULL", lineNumber, fileName);
|
||||
}
|
||||
|
||||
|
||||
void TestCase::fail (const std::string& message, long lineNumber, const std::string& fileName)
|
||||
{
|
||||
throw CppUnitException(std::string("fail: ") + message, lineNumber, fileName);
|
||||
}
|
||||
|
||||
|
||||
// Run the test and catch any exceptions that are triggered by it
|
||||
void TestCase::run(TestResult *result)
|
||||
{
|
||||
result->startTest(this);
|
||||
|
||||
setUp();
|
||||
try
|
||||
{
|
||||
runTest();
|
||||
}
|
||||
catch (CppUnitException& e)
|
||||
{
|
||||
CppUnitException* copy = new CppUnitException(e);
|
||||
result->addFailure(this, copy);
|
||||
}
|
||||
catch (std::exception& e)
|
||||
{
|
||||
std::string msg(typeid(e).name());
|
||||
msg.append(": ");
|
||||
msg.append(e.what());
|
||||
result->addError(this, new CppUnitException(msg));
|
||||
|
||||
}
|
||||
#if !defined(_WIN32)
|
||||
catch (...)
|
||||
{
|
||||
CppUnitException *e = new CppUnitException ("unknown exception");
|
||||
result->addError (this, e);
|
||||
}
|
||||
#endif
|
||||
tearDown ();
|
||||
result->endTest(this);
|
||||
}
|
||||
|
||||
|
||||
// A default run method
|
||||
TestResult* TestCase::run()
|
||||
{
|
||||
TestResult* result = defaultResult();
|
||||
|
||||
run(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
// All the work for runTest is deferred to subclasses
|
||||
void TestCase::runTest()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
// Build a message about a failed equality check
|
||||
std::string TestCase::notEqualsMessage(long expected, long actual)
|
||||
{
|
||||
return "expected: " + estring(expected) + " but was: " + estring(actual);
|
||||
}
|
||||
|
||||
|
||||
// Build a message about a failed equality check
|
||||
std::string TestCase::notEqualsMessage(double expected, double actual)
|
||||
{
|
||||
return "expected: " + estring(expected) + " but was: " + estring(actual);
|
||||
}
|
||||
|
||||
|
||||
// Build a message about a failed equality check
|
||||
std::string TestCase::notEqualsMessage(const void* expected, const void* actual)
|
||||
{
|
||||
return "expected: " + estring(expected) + " but was: " + estring(actual);
|
||||
}
|
||||
|
||||
|
||||
// Build a message about a failed equality check
|
||||
std::string TestCase::notEqualsMessage(const std::string& expected, const std::string& actual)
|
||||
{
|
||||
return "expected: \"" + expected + "\" but was: \"" + actual + "\"";
|
||||
}
|
||||
|
||||
|
||||
CppUnit_END
|
43
CppUnit/src/TestDecorator.cpp
Normal file
43
CppUnit/src/TestDecorator.cpp
Normal file
@@ -0,0 +1,43 @@
|
||||
//
|
||||
// TestDecorator.cpp
|
||||
//
|
||||
// $Id: //poco/1.1.0/CppUnit/src/TestDecorator.cpp#1 $
|
||||
//
|
||||
|
||||
|
||||
#include "CppUnit/TestDecorator.h"
|
||||
|
||||
|
||||
CppUnit_BEGIN
|
||||
|
||||
|
||||
TestDecorator::TestDecorator(Test* test)
|
||||
{
|
||||
_test = test;
|
||||
}
|
||||
|
||||
|
||||
TestDecorator::~TestDecorator()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
int TestDecorator::countTestCases()
|
||||
{
|
||||
return _test->countTestCases();
|
||||
}
|
||||
|
||||
|
||||
void TestDecorator::run(TestResult* result)
|
||||
{
|
||||
_test->run(result);
|
||||
}
|
||||
|
||||
|
||||
std::string TestDecorator::toString()
|
||||
{
|
||||
return _test->toString();
|
||||
}
|
||||
|
||||
|
||||
CppUnit_END
|
22
CppUnit/src/TestFailure.cpp
Normal file
22
CppUnit/src/TestFailure.cpp
Normal file
@@ -0,0 +1,22 @@
|
||||
//
|
||||
// TestFailure.cpp
|
||||
//
|
||||
// $Id: //poco/1.1.0/CppUnit/src/TestFailure.cpp#1 $
|
||||
//
|
||||
|
||||
|
||||
#include "CppUnit/TestFailure.h"
|
||||
#include "CppUnit/Test.h"
|
||||
|
||||
|
||||
CppUnit_BEGIN
|
||||
|
||||
|
||||
// Returns a short description of the failure.
|
||||
std::string TestFailure::toString()
|
||||
{
|
||||
return _failedTest->toString () + ": " + _thrownException->what();
|
||||
}
|
||||
|
||||
|
||||
CppUnit_END
|
29
CppUnit/src/TestResult.cpp
Normal file
29
CppUnit/src/TestResult.cpp
Normal file
@@ -0,0 +1,29 @@
|
||||
//
|
||||
// TestResult.cpp
|
||||
//
|
||||
// $Id: //poco/1.1.0/CppUnit/src/TestResult.cpp#1 $
|
||||
//
|
||||
|
||||
|
||||
#include "CppUnit/TestResult.h"
|
||||
|
||||
|
||||
CppUnit_BEGIN
|
||||
|
||||
|
||||
// Destroys a test result
|
||||
TestResult::~TestResult()
|
||||
{
|
||||
std::vector<TestFailure*>::iterator it;
|
||||
|
||||
for (it = _errors.begin(); it != _errors.end(); ++it)
|
||||
delete *it;
|
||||
|
||||
for (it = _failures.begin(); it != _failures.end(); ++it)
|
||||
delete *it;
|
||||
|
||||
delete _syncObject;
|
||||
}
|
||||
|
||||
|
||||
CppUnit_END
|
181
CppUnit/src/TestRunner.cpp
Normal file
181
CppUnit/src/TestRunner.cpp
Normal file
@@ -0,0 +1,181 @@
|
||||
//
|
||||
// TestRunner.cpp
|
||||
//
|
||||
// $Id: //poco/1.1.0/CppUnit/src/TestRunner.cpp#1 $
|
||||
//
|
||||
|
||||
|
||||
#include "CppUnit/TestRunner.h"
|
||||
#include "CppUnit/Test.h"
|
||||
#include "CppUnit/TestSuite.h"
|
||||
#include "CppUnit/TextTestResult.h"
|
||||
#include <iostream>
|
||||
|
||||
|
||||
CppUnit_BEGIN
|
||||
|
||||
|
||||
TestRunner::TestRunner()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
TestRunner::~TestRunner()
|
||||
{
|
||||
for (Mappings::iterator it = _mappings.begin(); it != _mappings.end(); ++it)
|
||||
delete it->second;
|
||||
}
|
||||
|
||||
|
||||
void TestRunner::printBanner()
|
||||
{
|
||||
std::cout
|
||||
<< "Usage: driver [-all] [-print] [-wait] [name] ..." << std::endl
|
||||
<< " where name is the name of a test case class" << std::endl;
|
||||
}
|
||||
|
||||
|
||||
bool TestRunner::run(const std::vector<std::string>& args)
|
||||
{
|
||||
std::string testCase;
|
||||
int numberOfTests = 0;
|
||||
bool success = true;
|
||||
bool all = false;
|
||||
bool wait = false;
|
||||
bool printed = false;
|
||||
|
||||
for (int i = 1; i < args.size(); i++)
|
||||
{
|
||||
const std::string& arg = args[i];
|
||||
if (arg == "-wait")
|
||||
{
|
||||
wait = true;
|
||||
continue;
|
||||
}
|
||||
else if (arg == "-all")
|
||||
{
|
||||
all = true;
|
||||
continue;
|
||||
}
|
||||
else if (arg == "-print")
|
||||
{
|
||||
for (Mappings::iterator it = _mappings.begin(); it != _mappings.end(); ++it)
|
||||
{
|
||||
print(it->first, it->second, 0);
|
||||
}
|
||||
printed = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!all)
|
||||
{
|
||||
testCase = arg;
|
||||
|
||||
if (testCase == "")
|
||||
{
|
||||
printBanner();
|
||||
return false;
|
||||
}
|
||||
|
||||
Test* testToRun = 0;
|
||||
for (Mappings::iterator it = _mappings.begin(); !testToRun && it != _mappings.end(); ++it)
|
||||
{
|
||||
testToRun = find(testCase, it->second, it->first);
|
||||
}
|
||||
if (testToRun)
|
||||
{
|
||||
if (!run(testToRun)) success = false;
|
||||
}
|
||||
numberOfTests++;
|
||||
|
||||
if (!testToRun)
|
||||
{
|
||||
std::cout << "Test " << testCase << " not found." << std::endl;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (all)
|
||||
{
|
||||
for (Mappings::iterator it = _mappings.begin(); it != _mappings.end(); ++it)
|
||||
{
|
||||
if (!run(it->second)) success = false;
|
||||
numberOfTests++;
|
||||
}
|
||||
}
|
||||
|
||||
if (numberOfTests == 0 && !printed)
|
||||
{
|
||||
printBanner();
|
||||
return false;
|
||||
}
|
||||
|
||||
if (wait)
|
||||
{
|
||||
std::cout << "<RETURN> to continue" << std::endl;
|
||||
std::cin.get();
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
|
||||
bool TestRunner::run(Test* test)
|
||||
{
|
||||
TextTestResult result;
|
||||
|
||||
test->run(&result);
|
||||
std::cout << result << std::endl;
|
||||
|
||||
return result.wasSuccessful();
|
||||
}
|
||||
|
||||
|
||||
void TestRunner::addTest(const std::string& name, Test* test)
|
||||
{
|
||||
_mappings.push_back(Mapping(name, test));
|
||||
}
|
||||
|
||||
|
||||
void TestRunner::print(const std::string& name, Test* pTest, int indent)
|
||||
{
|
||||
for (int i = 0; i < indent; ++i)
|
||||
std::cout << " ";
|
||||
std::cout << name << std::endl;
|
||||
TestSuite* pSuite = dynamic_cast<TestSuite*>(pTest);
|
||||
if (pSuite)
|
||||
{
|
||||
const std::vector<Test*>& tests = pSuite->tests();
|
||||
for (std::vector<Test*>::const_iterator it = tests.begin(); it != tests.end(); ++it)
|
||||
{
|
||||
print((*it)->toString(), *it, indent + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Test* TestRunner::find(const std::string& name, Test* pTest, const std::string& testName)
|
||||
{
|
||||
if (testName.find(name) != std::string::npos)
|
||||
{
|
||||
return pTest;
|
||||
}
|
||||
else
|
||||
{
|
||||
TestSuite* pSuite = dynamic_cast<TestSuite*>(pTest);
|
||||
if (pSuite)
|
||||
{
|
||||
const std::vector<Test*>& tests = pSuite->tests();
|
||||
for (std::vector<Test*>::const_iterator it = tests.begin(); it != tests.end(); ++it)
|
||||
{
|
||||
Test* result = find(name, *it, (*it)->toString());
|
||||
if (result) return result;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
CppUnit_END
|
49
CppUnit/src/TestSuite.cpp
Normal file
49
CppUnit/src/TestSuite.cpp
Normal file
@@ -0,0 +1,49 @@
|
||||
//
|
||||
// TestSuite.cpp
|
||||
//
|
||||
// $Id: //poco/1.1.0/CppUnit/src/TestSuite.cpp#1 $
|
||||
//
|
||||
|
||||
|
||||
#include "CppUnit/TestSuite.h"
|
||||
#include "CppUnit/TestResult.h"
|
||||
|
||||
|
||||
CppUnit_BEGIN
|
||||
|
||||
|
||||
// Deletes all tests in the suite.
|
||||
void TestSuite::deleteContents()
|
||||
{
|
||||
for (std::vector<Test*>::iterator it = _tests.begin(); it != _tests.end(); ++it)
|
||||
delete *it;
|
||||
}
|
||||
|
||||
|
||||
// Runs the tests and collects their result in a TestResult.
|
||||
void TestSuite::run(TestResult *result)
|
||||
{
|
||||
for (std::vector<Test*>::iterator it = _tests.begin(); it != _tests.end(); ++it)
|
||||
{
|
||||
if (result->shouldStop ())
|
||||
break;
|
||||
|
||||
Test *test = *it;
|
||||
test->run(result);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Counts the number of test cases that will be run by this test.
|
||||
int TestSuite::countTestCases()
|
||||
{
|
||||
int count = 0;
|
||||
|
||||
for (std::vector<Test*>::iterator it = _tests.begin (); it != _tests.end (); ++it)
|
||||
count += (*it)->countTestCases();
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
|
||||
CppUnit_END
|
133
CppUnit/src/TextTestResult.cpp
Normal file
133
CppUnit/src/TextTestResult.cpp
Normal file
@@ -0,0 +1,133 @@
|
||||
//
|
||||
// TextTestResult.cpp
|
||||
//
|
||||
// $Id: //poco/1.1.0/CppUnit/src/TextTestResult.cpp#1 $
|
||||
//
|
||||
|
||||
|
||||
#include "CppUnit/TextTestResult.h"
|
||||
#include "CppUnit/CppUnitException.h"
|
||||
#include "CppUnit/Test.h"
|
||||
#include "CppUnit/estring.h"
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
|
||||
|
||||
CppUnit_BEGIN
|
||||
|
||||
|
||||
void TextTestResult::addError(Test* test, CppUnitException* e)
|
||||
{
|
||||
TestResult::addError(test, e);
|
||||
std::cerr << "ERROR" << std::flush;
|
||||
}
|
||||
|
||||
|
||||
void TextTestResult::addFailure(Test* test, CppUnitException* e)
|
||||
{
|
||||
TestResult::addFailure(test, e);
|
||||
std::cerr << "FAILURE" << std::flush;
|
||||
}
|
||||
|
||||
|
||||
void TextTestResult::startTest(Test* test)
|
||||
{
|
||||
TestResult::startTest(test);
|
||||
std::cerr << "\n" << shortName(test->toString()) << ": ";
|
||||
}
|
||||
|
||||
|
||||
void TextTestResult::printErrors(std::ostream& stream)
|
||||
{
|
||||
if (testErrors() != 0)
|
||||
{
|
||||
stream << "\n";
|
||||
if (testErrors() == 1)
|
||||
stream << "There was " << testErrors() << " error: " << std::endl;
|
||||
else
|
||||
stream << "There were " << testErrors() << " errors: " << std::endl;
|
||||
|
||||
int i = 1;
|
||||
for (std::vector<TestFailure*>::iterator it = errors().begin(); it != errors().end(); ++it)
|
||||
{
|
||||
TestFailure* failure = *it;
|
||||
CppUnitException* e = failure->thrownException();
|
||||
|
||||
stream << std::setw(2) << i
|
||||
<< ": "
|
||||
<< failure->failedTest()->toString() << "\n"
|
||||
<< " \"" << (e ? e->what() : "") << "\"\n"
|
||||
<< " in \"" << (e ? e->fileName() : std::string()) << "\", line " << (e ? e->lineNumber() : 0) << "\n";
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void TextTestResult::printFailures(std::ostream& stream)
|
||||
{
|
||||
if (testFailures() != 0)
|
||||
{
|
||||
stream << "\n";
|
||||
if (testFailures() == 1)
|
||||
stream << "There was " << testFailures() << " failure: " << std::endl;
|
||||
else
|
||||
stream << "There were " << testFailures() << " failures: " << std::endl;
|
||||
|
||||
int i = 1;
|
||||
|
||||
for (std::vector<TestFailure*>::iterator it = failures().begin(); it != failures().end(); ++it)
|
||||
{
|
||||
TestFailure* failure = *it;
|
||||
CppUnitException* e = failure->thrownException();
|
||||
|
||||
stream << std::setw(2) << i
|
||||
<< ": "
|
||||
<< failure->failedTest()->toString() << "\n"
|
||||
<< " \"" << (e ? e->what() : "") << "\"\n"
|
||||
<< " in \"" << (e ? e->fileName() : std::string()) << "\", line " << (e ? e->lineNumber() : 0) << "\n";
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
void TextTestResult::print(std::ostream& stream)
|
||||
{
|
||||
printHeader(stream);
|
||||
printErrors(stream);
|
||||
printFailures(stream);
|
||||
}
|
||||
|
||||
|
||||
void TextTestResult::printHeader(std::ostream& stream)
|
||||
{
|
||||
std::cout << "\n\n";
|
||||
if (wasSuccessful())
|
||||
std::cout << "OK ("
|
||||
<< runTests() << " tests)"
|
||||
<< std::endl;
|
||||
else
|
||||
std::cout << "!!!FAILURES!!!" << std::endl
|
||||
<< "Runs: "
|
||||
<< runTests ()
|
||||
<< " Failures: "
|
||||
<< testFailures ()
|
||||
<< " Errors: "
|
||||
<< testErrors ()
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
|
||||
std::string TextTestResult::shortName(const std::string& testName)
|
||||
{
|
||||
std::string::size_type pos = testName.rfind('.');
|
||||
if (pos != std::string::npos)
|
||||
return std::string(testName, pos + 1);
|
||||
else
|
||||
return testName;
|
||||
}
|
||||
|
||||
|
||||
CppUnit_END
|
Reference in New Issue
Block a user