Skip to content

Commit

Permalink
Improve failure messages
Browse files Browse the repository at this point in the history
  • Loading branch information
scoopr committed Feb 5, 2010
1 parent 67896b8 commit f7be919
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
22 changes: 16 additions & 6 deletions spec/spec.cpp
Expand Up @@ -37,8 +37,8 @@ namespace specific {

void SpecWriter::startGroup(std::string /*group*/, std::string /*description*/) {}

void SpecWriter::addFailedAssertation(std::string msg, const char *file, int line) {
mFailures.push_back( SpecFailure(msg,file,line) );
void SpecWriter::addFailedAssertation(const std::string& msg, const char *file, int line, const std::string& group, const std::string& description) {
mFailures.push_back( SpecFailure(msg, file, line, group, description) );
}
void SpecWriter::addSpecResult(SpecResult r) {
mResults.push_back( r );
Expand All @@ -47,11 +47,21 @@ namespace specific {
void SpecWriter::stop() {
std::cout << std::endl;
size_t nth = 0;
std::string lastgroup = "";
std::string lastdescription = "";
for(std::vector<SpecFailure>::iterator i=mFailures.begin(); i != mFailures.end(); ++i, ++nth)
{
std::cout << std::endl;
std::cout << (nth+1) << ") Failed assertation at " << i->file << ":"
<< i->line << ":" << std::endl << " " << i->msg << std::endl;
if(lastgroup != i->group || lastdescription != i->description) {

lastgroup = i->group;
lastdescription = i->description;

std::cout << std::endl;
std::cout << "In " << i->group << ": " << i->description << ":" << std::endl << std::endl;
}
std::cout << " " << (nth+1) << ") Failed assertation at " << i->file << ":"
<< i->line << ":" << std::endl << " " << i->msg << std::endl;
}
std::cout << std::endl << mResults.size() << " examples, " << mFailures.size() << " failures" << std::endl;

Expand Down Expand Up @@ -152,15 +162,15 @@ namespace specific {
void SpecBase::should_test(bool value, const char* message, const char* file, int line) {
mLastFailed=false;
if(!value) {
mWriter->addFailedAssertation(message, file, line);
mWriter->addFailedAssertation(message, file, line, getGroup(), getDescription());
mLastFailed = mFailed = true;
throw spec_failure();
}
}


void SpecBase::error(std::string msg) {
mWriter->addFailedAssertation(msg, "exception", 0);
mWriter->addFailedAssertation(msg, "(exception)", 0, getGroup(), getDescription());
mLastFailed = true;
mFailed = true;
mError = true;
Expand Down
8 changes: 5 additions & 3 deletions spec/spec.h
Expand Up @@ -56,11 +56,13 @@ namespace specific {

class SpecFailure {
public:
SpecFailure(std::string amsg, const char* afile, int aline)
: msg(amsg), file(afile), line(aline) { }
SpecFailure(const std::string& amsg, const char* afile, int aline, const std::string& agroup, const std::string& adescription)
: msg(amsg), file(afile), line(aline), group(agroup), description(adescription) { }
std::string msg;
const char* file;
int line;
std::string group;
std::string description;
};


Expand All @@ -71,7 +73,7 @@ namespace specific {
SpecWriter() {}
virtual ~SpecWriter() {}
virtual void startGroup(std::string group, std::string description);
virtual void addFailedAssertation(std::string msg, const char *file, int line);
virtual void addFailedAssertation(const std::string& msg, const char *file, int line, const std::string& group, const std::string& description);
virtual void addSpecResult(SpecResult r);
virtual void start();
virtual void stop();
Expand Down

0 comments on commit f7be919

Please sign in to comment.