Skip to content

Commit 3ab725f

Browse files
committed
Add few tests for ErrorLogger.
1 parent b25e6ff commit 3ab725f

File tree

4 files changed

+153
-1
lines changed

4 files changed

+153
-1
lines changed

test/test.vcproj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="Windows-1252"?>
22
<VisualStudioProject
33
ProjectType="Visual C++"
4-
Version="9,00"
4+
Version="9.00"
55
Name="test"
66
ProjectGUID="{BA98E531-D8B3-39CB-8171-064854404B81}"
77
Keyword="Qt4VSv1.0"
@@ -494,6 +494,10 @@
494494
RelativePath=".\testdivision.cpp"
495495
>
496496
</File>
497+
<File
498+
RelativePath=".\testerrorlogger.cpp"
499+
>
500+
</File>
497501
<File
498502
RelativePath=".\testexceptionsafety.cpp"
499503
>

test/test.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@
262262
<ClCompile Include="testconstructors.cpp" />
263263
<ClCompile Include="testcppcheck.cpp" />
264264
<ClCompile Include="testdivision.cpp" />
265+
<ClCompile Include="testerrorlogger.cpp" />
265266
<ClCompile Include="testexceptionsafety.cpp" />
266267
<ClCompile Include="testincompletestatement.cpp" />
267268
<ClCompile Include="testmathlib.cpp" />

test/test.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,9 @@
200200
<ClCompile Include="..\lib\symboldatabase.cpp">
201201
<Filter>Source Files</Filter>
202202
</ClCompile>
203+
<ClCompile Include="testerrorlogger.cpp">
204+
<Filter>Source Files</Filter>
205+
</ClCompile>
203206
</ItemGroup>
204207
<ItemGroup>
205208
<ClInclude Include="testsuite.h">

test/testerrorlogger.cpp

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
/*
2+
* Cppcheck - A tool for static C/C++ code analysis
3+
* Copyright (C) 2007-2010 Daniel Marjamäki and Cppcheck team.
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
#include <list>
20+
#include "testsuite.h"
21+
#include "errorlogger.h"
22+
23+
class TestErrorLogger : public TestFixture
24+
{
25+
public:
26+
TestErrorLogger() : TestFixture("TestErrorLogger")
27+
{ }
28+
29+
private:
30+
31+
void run()
32+
{
33+
TEST_CASE(FileLocationDefaults);
34+
TEST_CASE(FileLocationSetFile);
35+
TEST_CASE(ErrorMessageConstruct);
36+
TEST_CASE(ErrorMessageVerbose);
37+
TEST_CASE(CustomFormat);
38+
TEST_CASE(CustomFormat2);
39+
TEST_CASE(ToXml);
40+
TEST_CASE(ToVerboseXml);
41+
}
42+
43+
void FileLocationDefaults()
44+
{
45+
ErrorLogger::ErrorMessage::FileLocation loc;
46+
ASSERT_EQUALS("", loc.getfile());
47+
ASSERT_EQUALS(0, loc.line);
48+
}
49+
50+
void FileLocationSetFile()
51+
{
52+
ErrorLogger::ErrorMessage::FileLocation loc;
53+
loc.setfile("foo.cpp");
54+
ASSERT_EQUALS("foo.cpp", loc.getfile());
55+
ASSERT_EQUALS(0, loc.line);
56+
}
57+
58+
void ErrorMessageConstruct()
59+
{
60+
ErrorLogger::ErrorMessage::FileLocation loc;
61+
loc.setfile("foo.cpp");
62+
loc.line = 5;
63+
std::list<ErrorLogger::ErrorMessage::FileLocation> locs;
64+
locs.push_back(loc);
65+
ErrorMessage msg(locs, Severity::error, "Programming error.", "errorId");
66+
ASSERT_EQUALS(1, msg._callStack.size());
67+
ASSERT_EQUALS("Programming error.", msg.shortMessage());
68+
ASSERT_EQUALS("Programming error.", msg.verboseMessage());
69+
ASSERT_EQUALS("[foo.cpp:5]: (error) Programming error.", msg.toString(false));
70+
ASSERT_EQUALS("[foo.cpp:5]: (error) Programming error.", msg.toString(true));
71+
}
72+
73+
void ErrorMessageVerbose()
74+
{
75+
ErrorLogger::ErrorMessage::FileLocation loc;
76+
loc.setfile("foo.cpp");
77+
loc.line = 5;
78+
std::list<ErrorLogger::ErrorMessage::FileLocation> locs;
79+
locs.push_back(loc);
80+
ErrorMessage msg(locs, Severity::error, "Programming error.\nVerbose error", "errorId");
81+
ASSERT_EQUALS(1, msg._callStack.size());
82+
ASSERT_EQUALS("Programming error.", msg.shortMessage());
83+
ASSERT_EQUALS("Verbose error", msg.verboseMessage());
84+
ASSERT_EQUALS("[foo.cpp:5]: (error) Programming error.", msg.toString(false));
85+
ASSERT_EQUALS("[foo.cpp:5]: (error) Verbose error", msg.toString(true));
86+
}
87+
88+
void CustomFormat()
89+
{
90+
ErrorLogger::ErrorMessage::FileLocation loc;
91+
loc.setfile("foo.cpp");
92+
loc.line = 5;
93+
std::list<ErrorLogger::ErrorMessage::FileLocation> locs;
94+
locs.push_back(loc);
95+
ErrorMessage msg(locs, Severity::error, "Programming error.\nVerbose error", "errorId");
96+
ASSERT_EQUALS(1, msg._callStack.size());
97+
ASSERT_EQUALS("Programming error.", msg.shortMessage());
98+
ASSERT_EQUALS("Verbose error", msg.verboseMessage());
99+
ASSERT_EQUALS("foo.cpp:5,error,errorId,Programming error.", msg.toString(false, "{file}:{line},{severity},{id},{message}"));
100+
ASSERT_EQUALS("foo.cpp:5,error,errorId,Verbose error", msg.toString(true, "{file}:{line},{severity},{id},{message}"));
101+
}
102+
103+
void CustomFormat2()
104+
{
105+
ErrorLogger::ErrorMessage::FileLocation loc;
106+
loc.setfile("foo.cpp");
107+
loc.line = 5;
108+
std::list<ErrorLogger::ErrorMessage::FileLocation> locs;
109+
locs.push_back(loc);
110+
ErrorMessage msg(locs, Severity::error, "Programming error.\nVerbose error", "errorId");
111+
ASSERT_EQUALS(1, msg._callStack.size());
112+
ASSERT_EQUALS("Programming error.", msg.shortMessage());
113+
ASSERT_EQUALS("Verbose error", msg.verboseMessage());
114+
ASSERT_EQUALS("Programming error. - foo.cpp(5):(error,errorId)", msg.toString(false, "{message} - {file}({line}):({severity},{id})"));
115+
ASSERT_EQUALS("Verbose error - foo.cpp(5):(error,errorId)", msg.toString(true, "{message} - {file}({line}):({severity},{id})"));
116+
}
117+
118+
void ToXml()
119+
{
120+
ErrorLogger::ErrorMessage::FileLocation loc;
121+
loc.setfile("foo.cpp");
122+
loc.line = 5;
123+
std::list<ErrorLogger::ErrorMessage::FileLocation> locs;
124+
locs.push_back(loc);
125+
ErrorMessage msg(locs, Severity::error, "Programming error.\nVerbose error", "errorId");
126+
ASSERT_EQUALS("<?xml version=\"1.0\"?>\n<results>", ErrorLogger::ErrorMessage::getXMLHeader(1));
127+
ASSERT_EQUALS("</results>", ErrorLogger::ErrorMessage::getXMLFooter());
128+
ASSERT_EQUALS("<error file=\"foo.cpp\" line=\"5\" id=\"errorId\" severity=\"error\" msg=\"Programming error.\"/>", msg.toXML(false,1));
129+
}
130+
131+
void ToVerboseXml()
132+
{
133+
ErrorLogger::ErrorMessage::FileLocation loc;
134+
loc.setfile("foo.cpp");
135+
loc.line = 5;
136+
std::list<ErrorLogger::ErrorMessage::FileLocation> locs;
137+
locs.push_back(loc);
138+
ErrorMessage msg(locs, Severity::error, "Programming error.\nVerbose error", "errorId");
139+
ASSERT_EQUALS("<?xml version=\"1.0\"?>\n<results>", ErrorLogger::ErrorMessage::getXMLHeader(1));
140+
ASSERT_EQUALS("</results>", ErrorLogger::ErrorMessage::getXMLFooter());
141+
ASSERT_EQUALS("<error file=\"foo.cpp\" line=\"5\" id=\"errorId\" severity=\"error\" msg=\"Verbose error\"/>", msg.toXML(true,1));
142+
}
143+
};
144+
REGISTER_TEST(TestErrorLogger)

0 commit comments

Comments
 (0)