Permalink
Browse files

Improve failure messages

  • Loading branch information...
1 parent 67896b8 commit f7be9199d94444fb2bbcca7d2e87b5195a03395c @scoopr committed Feb 5, 2010
Showing with 21 additions and 9 deletions.
  1. +16 −6 spec/spec.cpp
  2. +5 −3 spec/spec.h
View
22 spec/spec.cpp
@@ -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 );
@@ -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;
@@ -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;
View
8 spec/spec.h
@@ -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;
};
@@ -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();

0 comments on commit f7be919

Please sign in to comment.