Skip to content

Commit

Permalink
TestResult can determine if it has a line number
Browse files Browse the repository at this point in the history
  • Loading branch information
joakimkarlsson committed Jul 13, 2011
1 parent c4c0250 commit 7d777ee
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 17 deletions.
43 changes: 29 additions & 14 deletions igloo/core/assertionexception.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,38 @@
namespace igloo {
class AssertionException : public std::exception
{
public:
AssertionException(const std::string& message)
: m_message(message)
{
}
public:
AssertionException(const std::string& message)
: m_message(message), m_fileName(""), m_line(0)
{}

virtual ~AssertionException() throw()
{
}
AssertionException(const std::string& message, const std::string& fileName, unsigned int line)
: m_message(message), m_fileName(fileName), m_line(line)
{}

std::string GetMessage() const
{
return m_message;
}
virtual ~AssertionException() throw()
{
}

std::string GetMessage() const
{
return m_message;
}

std::string GetFilename() const
{
return m_fileName;
}

unsigned int GetLineNumber() const
{
return m_line;
}

private:
std::string m_message;
private:
std::string m_message;
std::string m_fileName;
unsigned int m_line;
};
}

Expand Down
10 changes: 8 additions & 2 deletions igloo/core/testresult.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@ namespace igloo {
class TestResult
{
public:
TestResult(const std::string& contextName, const std::string& specName, bool success, const std::string& errorMessage, const std::string& filename, unsigned int line)
: m_contextName(contextName), m_specName(specName), m_success(success), m_errorMessage(errorMessage), m_filename(filename), m_line(line)
{
}

TestResult(const std::string& contextName, const std::string& specName, bool success, const std::string& errorMessage)
: m_contextName(contextName), m_specName(specName), m_success(success), m_errorMessage(errorMessage)
: m_contextName(contextName), m_specName(specName), m_success(success), m_errorMessage(errorMessage), m_filename(""), m_line(0)
{
}

Expand All @@ -38,7 +43,7 @@ namespace igloo {

bool HasLineNumber() const
{
return false;
return m_line != 0;
}

unsigned int LineNumber() const
Expand Down Expand Up @@ -72,6 +77,7 @@ namespace igloo {
bool m_success;
std::string m_errorMessage;
std::string m_filename;
unsigned int m_line;
};

inline std::ostream& operator<<(std::ostream& stream, const TestResult& res)
Expand Down
2 changes: 1 addition & 1 deletion igloo/core/testresultfactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace igloo {

TestResult CreateFromException(const AssertionException& exception)
{
return TestResult(m_contextName, m_specName, false, exception.GetMessage());
return TestResult(m_contextName, m_specName, false, exception.GetMessage(), exception.GetFilename(), exception.GetLineNumber());
}

private:
Expand Down
20 changes: 20 additions & 0 deletions tests/testresultfactory_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,23 @@ Context(An_assertion_without_file_and_linenumber_information)
}

};

Context(An_assertion_with_file_and_linenumber_information)
{
AssertionException exception;
TestResultFactory factory;

An_assertion_with_file_and_linenumber_information()
: exception("An error message", "myfile.cpp", 432)
,factory("A context name", "A spec")
{}

Spec(Created_TestResult_should_have_a_linenumber)
{
TestResult result = factory.CreateFromException(exception);

Assert::That(result.HasLineNumber());

}

};

0 comments on commit 7d777ee

Please sign in to comment.