Skip to content

Commit f366665

Browse files
committed
Add couple of basic tests for CmdLineParser.
1 parent e800490 commit f366665

6 files changed

Lines changed: 160 additions & 5 deletions

File tree

cli/cmdlineparser.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,8 +370,13 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
370370
}
371371

372372

373-
if (argc <= 1 || _showHelp)
373+
if (argc <= 1)
374+
_showHelp = true;
375+
376+
if (_showHelp)
377+
{
374378
PrintHelp();
379+
}
375380
else if (_pathnames.empty())
376381
{
377382
std::cout << "cppcheck: No C or C++ source files found.";

cli/cmdlineparser.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@ class CmdLineParser
7676
return _pathnames;
7777
}
7878

79+
/**
80+
* Return if help is shown to user.
81+
*/
82+
bool GetShowHelp() const
83+
{
84+
return _showHelp;
85+
}
86+
7987
protected:
8088

8189
/**

test/test.vcproj

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
<Tool
4545
Name="VCCLCompilerTool"
4646
Optimization="0"
47-
AdditionalIncludeDirectories="..\lib"
47+
AdditionalIncludeDirectories="..\lib;..\cli"
4848
PreprocessorDefinitions="_DEBUG,WIN32,UNIT_TESTING;_CRT_SECURE_NO_WARNINGS"
4949
BasicRuntimeChecks="3"
5050
RuntimeLibrary="1"
@@ -128,7 +128,7 @@
128128
Optimization="2"
129129
EnableIntrinsicFunctions="true"
130130
WholeProgramOptimization="true"
131-
AdditionalIncludeDirectories="..\lib"
131+
AdditionalIncludeDirectories="..\lib;..\cli"
132132
PreprocessorDefinitions="NDEBUG,WIN32,UNIT_TESTING;_CRT_SECURE_NO_WARNINGS"
133133
RuntimeLibrary="0"
134134
TreatWChar_tAsBuiltInType="false"
@@ -222,6 +222,10 @@
222222
RelativePath="..\lib\checkunusedfunctions.cpp"
223223
>
224224
</File>
225+
<File
226+
RelativePath="..\cli\cmdlineparser.cpp"
227+
>
228+
</File>
225229
<File
226230
RelativePath="..\lib\cppcheck.cpp"
227231
>
@@ -274,6 +278,10 @@
274278
RelativePath="testclass.cpp"
275279
>
276280
</File>
281+
<File
282+
RelativePath=".\testcmdlineparser.cpp"
283+
>
284+
</File>
277285
<File
278286
RelativePath="testconstructors.cpp"
279287
>
@@ -428,6 +436,10 @@
428436
RelativePath="..\lib\classinfo.h"
429437
>
430438
</File>
439+
<File
440+
RelativePath="..\cli\cmdlineparser.h"
441+
>
442+
</File>
431443
<File
432444
RelativePath="..\lib\cppcheck.h"
433445
>

test/test.vcxproj

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
</Midl>
5858
<ClCompile>
5959
<Optimization>Disabled</Optimization>
60-
<AdditionalIncludeDirectories>..\lib;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
60+
<AdditionalIncludeDirectories>..\lib;..\cli;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
6161
<PreprocessorDefinitions>_DEBUG;WIN32;UNIT_TESTING;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
6262
<BasicRuntimeChecks>EnableFastChecks</BasicRuntimeChecks>
6363
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
@@ -94,7 +94,7 @@
9494
<Optimization>MaxSpeed</Optimization>
9595
<IntrinsicFunctions>true</IntrinsicFunctions>
9696
<WholeProgramOptimization>true</WholeProgramOptimization>
97-
<AdditionalIncludeDirectories>..\lib;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
97+
<AdditionalIncludeDirectories>..\lib;..\cli;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
9898
<PreprocessorDefinitions>NDEBUG;WIN32;UNIT_TESTING;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
9999
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
100100
<TreatWChar_tAsBuiltInType>false</TreatWChar_tAsBuiltInType>
@@ -120,6 +120,7 @@
120120
</Bscmake>
121121
</ItemDefinitionGroup>
122122
<ItemGroup>
123+
<ClCompile Include="..\cli\cmdlineparser.cpp" />
123124
<ClCompile Include="..\lib\checkautovariables.cpp" />
124125
<ClCompile Include="..\lib\checkbufferoverrun.cpp" />
125126
<ClCompile Include="..\lib\checkclass.cpp" />
@@ -144,6 +145,7 @@
144145
<ClCompile Include="testbufferoverrun.cpp" />
145146
<ClCompile Include="testcharvar.cpp" />
146147
<ClCompile Include="testclass.cpp" />
148+
<ClCompile Include="testcmdlineparser.cpp" />
147149
<ClCompile Include="testconstructors.cpp" />
148150
<ClCompile Include="testcppcheck.cpp" />
149151
<ClCompile Include="testdivision.cpp" />
@@ -169,6 +171,7 @@
169171
<ClCompile Include="tinyxml\tinyxmlparser.cpp" />
170172
</ItemGroup>
171173
<ItemGroup>
174+
<ClInclude Include="..\cli\cmdlineparser.h" />
172175
<ClInclude Include="..\lib\check.h" />
173176
<ClInclude Include="..\lib\checkautovariables.h" />
174177
<ClInclude Include="..\lib\checkbufferoverrun.h" />

test/test.vcxproj.filters

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,12 @@
152152
<ClCompile Include="..\lib\timer.cpp">
153153
<Filter>Source Files</Filter>
154154
</ClCompile>
155+
<ClCompile Include="..\cli\cmdlineparser.cpp">
156+
<Filter>Source Files</Filter>
157+
</ClCompile>
158+
<ClCompile Include="testcmdlineparser.cpp">
159+
<Filter>Source Files</Filter>
160+
</ClCompile>
155161
</ItemGroup>
156162
<ItemGroup>
157163
<ClInclude Include="testsuite.h">
@@ -226,5 +232,8 @@
226232
<ClInclude Include="..\lib\timer.h">
227233
<Filter>Header Files</Filter>
228234
</ClInclude>
235+
<ClInclude Include="..\cli\cmdlineparser.h">
236+
<Filter>Header Files</Filter>
237+
</ClInclude>
229238
</ItemGroup>
230239
</Project>

test/testcmdlineparser.cpp

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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 <iostream>
20+
#include "testsuite.h"
21+
#include "cmdlineparser.h"
22+
#include "settings.h"
23+
24+
extern std::ostringstream errout;
25+
extern std::ostringstream output;
26+
27+
class RedirectInputOutput
28+
{
29+
public:
30+
RedirectInputOutput()
31+
{
32+
// flush all old output
33+
std::cout.flush();
34+
std::cerr.flush();
35+
36+
_oldCout = std::cout.rdbuf(); // back up cout's streambuf
37+
_oldCerr = std::cerr.rdbuf(); // back up cerr's streambuf
38+
39+
std::cout.rdbuf(_out.rdbuf()); // assign streambuf to cout
40+
std::cerr.rdbuf(_err.rdbuf()); // assign streambuf to cerr
41+
}
42+
43+
~RedirectInputOutput()
44+
{
45+
std::cout.rdbuf(_oldCout); // restore cout's original streambuf
46+
std::cerr.rdbuf(_oldCerr); // restore cerrs's original streambuf
47+
48+
errout << _err.str();
49+
output << _out.str();
50+
}
51+
52+
private:
53+
std::stringstream _out;
54+
std::stringstream _err;
55+
std::streambuf* _oldCout;
56+
std::streambuf *_oldCerr;
57+
};
58+
59+
#define REDIRECT RedirectInputOutput redir;
60+
61+
class TestCmdlineParser : public TestFixture
62+
{
63+
public:
64+
TestCmdlineParser() : TestFixture("TestCmdlineParser")
65+
{ }
66+
67+
private:
68+
69+
void run()
70+
{
71+
TEST_CASE(nooptions);
72+
TEST_CASE(helpshort);
73+
TEST_CASE(helplong);
74+
TEST_CASE(showversion);
75+
}
76+
77+
void nooptions()
78+
{
79+
REDIRECT;
80+
const char *argv[] = {"cppcheck"};
81+
Settings settings;
82+
CmdLineParser parser(&settings);
83+
ASSERT(parser.ParseFromArgs(1, argv));
84+
ASSERT_EQUALS(true, parser.GetShowHelp());
85+
}
86+
87+
void helpshort()
88+
{
89+
REDIRECT;
90+
const char *argv[] = {"cppcheck", "-h"};
91+
Settings settings;
92+
CmdLineParser parser(&settings);
93+
ASSERT(parser.ParseFromArgs(2, argv));
94+
ASSERT_EQUALS(true, parser.GetShowHelp());
95+
}
96+
97+
void helplong()
98+
{
99+
REDIRECT;
100+
const char *argv[] = {"cppcheck", "--help"};
101+
Settings settings;
102+
CmdLineParser parser(&settings);
103+
ASSERT(parser.ParseFromArgs(2, argv));
104+
ASSERT_EQUALS(true, parser.GetShowHelp());
105+
}
106+
107+
void showversion()
108+
{
109+
REDIRECT;
110+
const char *argv[] = {"cppcheck", "--version"};
111+
Settings settings;
112+
CmdLineParser parser(&settings);
113+
ASSERT(parser.ParseFromArgs(2, argv));
114+
ASSERT_EQUALS(true, parser.GetShowVersion());
115+
}
116+
};
117+
118+
REGISTER_TEST(TestCmdlineParser)

0 commit comments

Comments
 (0)