Add more tests for ErrorLogger.

Add tests that have two items in error location stack.
This commit is contained in:
Kimmo Varis 2011-07-16 13:41:31 +03:00
parent 76960e54a1
commit 74d086d5a0
1 changed files with 124 additions and 1 deletions

View File

@ -34,12 +34,18 @@ private:
TEST_CASE(FileLocationDefaults);
TEST_CASE(FileLocationSetFile);
TEST_CASE(ErrorMessageConstruct);
TEST_CASE(ErrorMessageConstructLocations);
TEST_CASE(ErrorMessageVerbose);
TEST_CASE(ErrorMessageVerboseLocations);
TEST_CASE(CustomFormat);
TEST_CASE(CustomFormat2);
TEST_CASE(CustomFormatLocations);
TEST_CASE(ToXml);
TEST_CASE(ToXmlLocations);
TEST_CASE(ToVerboseXml);
TEST_CASE(ToVerboseXmlLocations);
TEST_CASE(ToXmlV2);
TEST_CASE(ToXmlV2Locations);
// Inconclusive results in xml reports..
TEST_CASE(InconclusiveXml);
@ -78,6 +84,25 @@ private:
ASSERT_EQUALS("[foo.cpp:5]: (error) Programming error.", msg.toString(true));
}
void ErrorMessageConstructLocations()
{
ErrorLogger::ErrorMessage::FileLocation loc;
loc.setfile("foo.cpp");
loc.line = 5;
ErrorLogger::ErrorMessage::FileLocation loc2;
loc2.setfile("bar.cpp");
loc2.line = 8;
std::list<ErrorLogger::ErrorMessage::FileLocation> locs;
locs.push_back(loc);
locs.push_back(loc2);
ErrorMessage msg(locs, Severity::error, "Programming error.", "errorId", false);
ASSERT_EQUALS(2, (int)msg._callStack.size());
ASSERT_EQUALS("Programming error.", msg.shortMessage());
ASSERT_EQUALS("Programming error.", msg.verboseMessage());
ASSERT_EQUALS("[foo.cpp:5] -> [bar.cpp:8]: (error) Programming error.", msg.toString(false));
ASSERT_EQUALS("[foo.cpp:5] -> [bar.cpp:8]: (error) Programming error.", msg.toString(true));
}
void ErrorMessageVerbose()
{
ErrorLogger::ErrorMessage::FileLocation loc;
@ -93,6 +118,25 @@ private:
ASSERT_EQUALS("[foo.cpp:5]: (error) Verbose error", msg.toString(true));
}
void ErrorMessageVerboseLocations()
{
ErrorLogger::ErrorMessage::FileLocation loc;
loc.setfile("foo.cpp");
loc.line = 5;
std::list<ErrorLogger::ErrorMessage::FileLocation> locs;
ErrorLogger::ErrorMessage::FileLocation loc2;
loc2.setfile("bar.cpp");
loc2.line = 8;
locs.push_back(loc);
locs.push_back(loc2);
ErrorMessage msg(locs, Severity::error, "Programming error.\nVerbose error", "errorId", false);
ASSERT_EQUALS(2, (int)msg._callStack.size());
ASSERT_EQUALS("Programming error.", msg.shortMessage());
ASSERT_EQUALS("Verbose error", msg.verboseMessage());
ASSERT_EQUALS("[foo.cpp:5] -> [bar.cpp:8]: (error) Programming error.", msg.toString(false));
ASSERT_EQUALS("[foo.cpp:5] -> [bar.cpp:8]: (error) Verbose error", msg.toString(true));
}
void CustomFormat()
{
ErrorLogger::ErrorMessage::FileLocation loc;
@ -123,6 +167,26 @@ private:
ASSERT_EQUALS("Verbose error - foo.cpp(5):(error,errorId)", msg.toString(true, "{message} - {file}({line}):({severity},{id})"));
}
void CustomFormatLocations()
{
// Check that first location from location stack is used in template
ErrorLogger::ErrorMessage::FileLocation loc;
loc.setfile("foo.cpp");
loc.line = 5;
ErrorLogger::ErrorMessage::FileLocation loc2;
loc2.setfile("bar.cpp");
loc2.line = 8;
std::list<ErrorLogger::ErrorMessage::FileLocation> locs;
locs.push_back(loc);
locs.push_back(loc2);
ErrorMessage msg(locs, Severity::error, "Programming error.\nVerbose error", "errorId", false);
ASSERT_EQUALS(2, (int)msg._callStack.size());
ASSERT_EQUALS("Programming error.", msg.shortMessage());
ASSERT_EQUALS("Verbose error", msg.verboseMessage());
ASSERT_EQUALS("Programming error. - bar.cpp(8):(error,errorId)", msg.toString(false, "{message} - {file}({line}):({severity},{id})"));
ASSERT_EQUALS("Verbose error - bar.cpp(8):(error,errorId)", msg.toString(true, "{message} - {file}({line}):({severity},{id})"));
}
void ToXml()
{
ErrorLogger::ErrorMessage::FileLocation loc;
@ -136,6 +200,23 @@ private:
ASSERT_EQUALS("<error file=\"foo.cpp\" line=\"5\" id=\"errorId\" severity=\"error\" msg=\"Programming error.\"/>", msg.toXML(false,1));
}
void ToXmlLocations()
{
ErrorLogger::ErrorMessage::FileLocation loc;
loc.setfile("foo.cpp");
loc.line = 5;
ErrorLogger::ErrorMessage::FileLocation loc2;
loc2.setfile("bar.cpp");
loc2.line = 8;
std::list<ErrorLogger::ErrorMessage::FileLocation> locs;
locs.push_back(loc);
locs.push_back(loc2);
ErrorMessage msg(locs, Severity::error, "Programming error.\nVerbose error", "errorId", false);
ASSERT_EQUALS("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<results>", ErrorLogger::ErrorMessage::getXMLHeader(1));
ASSERT_EQUALS("</results>", ErrorLogger::ErrorMessage::getXMLFooter(1));
ASSERT_EQUALS("<error file=\"bar.cpp\" line=\"8\" id=\"errorId\" severity=\"error\" msg=\"Programming error.\"/>", msg.toXML(false,1));
}
void ToVerboseXml()
{
ErrorLogger::ErrorMessage::FileLocation loc;
@ -149,6 +230,23 @@ private:
ASSERT_EQUALS("<error file=\"foo.cpp\" line=\"5\" id=\"errorId\" severity=\"error\" msg=\"Verbose error\"/>", msg.toXML(true,1));
}
void ToVerboseXmlLocations()
{
ErrorLogger::ErrorMessage::FileLocation loc;
loc.setfile("foo.cpp");
loc.line = 5;
ErrorLogger::ErrorMessage::FileLocation loc2;
loc2.setfile("bar.cpp");
loc2.line = 8;
std::list<ErrorLogger::ErrorMessage::FileLocation> locs;
locs.push_back(loc);
locs.push_back(loc2);
ErrorMessage msg(locs, Severity::error, "Programming error.\nVerbose error", "errorId", false);
ASSERT_EQUALS("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<results>", ErrorLogger::ErrorMessage::getXMLHeader(1));
ASSERT_EQUALS("</results>", ErrorLogger::ErrorMessage::getXMLFooter(1));
ASSERT_EQUALS("<error file=\"bar.cpp\" line=\"8\" id=\"errorId\" severity=\"error\" msg=\"Verbose error\"/>", msg.toXML(true,1));
}
void ToXmlV2()
{
ErrorLogger::ErrorMessage::FileLocation loc;
@ -166,7 +264,32 @@ private:
std::string message(" <error id=\"errorId\" severity=\"error\"");
message += " msg=\"Programming error.\" verbose=\"Verbose error\">\n";
message += " <location file=\"foo.cpp\" line=\"5\"/>\n </error>";
ASSERT_EQUALS(message, msg.toXML(false,2));
ASSERT_EQUALS(message, msg.toXML(false, 2));
}
void ToXmlV2Locations()
{
ErrorLogger::ErrorMessage::FileLocation loc;
loc.setfile("foo.cpp");
loc.line = 5;
ErrorLogger::ErrorMessage::FileLocation loc2;
loc2.setfile("bar.cpp");
loc2.line = 8;
std::list<ErrorLogger::ErrorMessage::FileLocation> locs;
locs.push_back(loc);
locs.push_back(loc2);
ErrorMessage msg(locs, Severity::error, "Programming error.\nVerbose error", "errorId", false);
std::string header("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<results version=\"2\">\n");
header += " <cppcheck version=\"";
header += CppCheck::version();
header += "\"/>\n <errors>";
ASSERT_EQUALS(header, ErrorLogger::ErrorMessage::getXMLHeader(2));
ASSERT_EQUALS(" </errors>\n</results>", ErrorLogger::ErrorMessage::getXMLFooter(2));
std::string message(" <error id=\"errorId\" severity=\"error\"");
message += " msg=\"Programming error.\" verbose=\"Verbose error\">\n";
message += " <location file=\"bar.cpp\" line=\"8\"/>\n";
message += " <location file=\"foo.cpp\" line=\"5\"/>\n </error>";
ASSERT_EQUALS(message, msg.toXML(false, 2));
}
void InconclusiveXml()