Skip to content

Commit

Permalink
Removed Failure class.
Browse files Browse the repository at this point in the history
  • Loading branch information
murrekatt committed Oct 5, 2014
1 parent c50c357 commit 04ce61c
Showing 1 changed file with 19 additions and 41 deletions.
60 changes: 19 additions & 41 deletions TestHarness.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,37 +66,20 @@
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <sstream>
#include <ctime>

namespace cpput
{

struct Failure
{
Failure(const std::string& filename, std::size_t line, const std::string& message)
: filename_(filename)
, line_(line)
, message_(message)
{
}

std::string filename_;
std::size_t line_;
std::string message_;
};

// ----------------------------------------------------------------------------

struct ResultWriter
{
virtual ~ResultWriter() {}

virtual void startTest(const std::string& className, const std::string& name) = 0;
virtual void endTest(bool success) = 0;

virtual void failure(const Failure& failure) = 0;
virtual void failure(const std::string& filename, std::size_t line, const std::string& message) = 0;
virtual int getNumberOfFailures() const = 0;
};

Expand All @@ -107,25 +90,21 @@ class TextResultWriter : public ResultWriter
public:
TextResultWriter()
: testCount_(0)
, failures_(0)
{
}

virtual ~TextResultWriter()
{
std::cout << "\n";
if (failures_.empty())
if (failures_ == 0)
{
std::cout << "All tests pass.\n";
std::cout << "\nAll tests pass.\n";
return;
}
// print details of all failures
std::cout << "\n";
for (std::size_t i=0; i<failures_.size(); ++i)
else
{
const Failure& f = failures_[i];
std::cout << f.filename_ << ", line " << f.line_ << ": " << f.message_ << '\n';
std::cout << "\n" << failures_ << " out of " << testCount_ << " tests failed.\n";
}
std::cout << "\n" << failures_.size() << " out of " << testCount_ << " tests failed.\n";
}

virtual void startTest(const std::string&, const std::string&)
Expand All @@ -141,16 +120,17 @@ class TextResultWriter : public ResultWriter
std::cout << 'F';
}

virtual void failure(const Failure& failure)
virtual void failure(const std::string& filename, std::size_t line, const std::string& message)
{
failures_.push_back(failure);
failures_++;
std::cout << "Failure: " << filename << ", line " << line << ": " << message << '\n';
}

virtual int getNumberOfFailures() const { return failures_.size(); }
virtual int getNumberOfFailures() const { return failures_; }

private:
int testCount_;
std::vector<Failure> failures_;
int testCount_;
int failures_;
};

// ----------------------------------------------------------------------------
Expand Down Expand Up @@ -191,16 +171,16 @@ class XmlResultWriter : public ResultWriter
std::cout << " </testcase>\n";
}

virtual void failure(const Failure& failure)
virtual void failure(const std::string& filename, std::size_t line, const std::string& message)
{
std::cout << static_cast<float>(std::clock()-startTime_)/CLOCKS_PER_SEC << "\"";
std::cout << ">\n"
<< " <failure>"
<< failure.message_
<< message
<< " in "
<< failure.filename_
<< filename
<< ", line "
<< failure.line_
<< line
<< "</failure>\n";
failureCount_++;
}
Expand Down Expand Up @@ -244,21 +224,19 @@ struct Result
ss << std::setprecision(20)
<< "failed comparison, expected " << expected
<< " got " << actual << "\n";
Failure f(filename, line, ss.str());
out_.failure(f);
out_.failure(filename, line, ss.str());
}

void addFailure(const char* filename,
std::size_t line,
const char* message)
{
pass_ = false;
Failure f(filename, line, message);
out_.failure(f);
out_.failure(filename, line, message);
}

ResultWriter& out_;
bool pass_;
bool pass_;
};

// ----------------------------------------------------------------------------
Expand Down

0 comments on commit 04ce61c

Please sign in to comment.