Add couple of basic tests for CmdLineParser.

This commit is contained in:
Kimmo Varis 2010-09-06 22:31:06 +03:00
parent e800490b50
commit f36666572a
6 changed files with 160 additions and 5 deletions

View File

@ -370,8 +370,13 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
} }
if (argc <= 1 || _showHelp) if (argc <= 1)
_showHelp = true;
if (_showHelp)
{
PrintHelp(); PrintHelp();
}
else if (_pathnames.empty()) else if (_pathnames.empty())
{ {
std::cout << "cppcheck: No C or C++ source files found."; std::cout << "cppcheck: No C or C++ source files found.";

View File

@ -76,6 +76,14 @@ public:
return _pathnames; return _pathnames;
} }
/**
* Return if help is shown to user.
*/
bool GetShowHelp() const
{
return _showHelp;
}
protected: protected:
/** /**

View File

@ -44,7 +44,7 @@
<Tool <Tool
Name="VCCLCompilerTool" Name="VCCLCompilerTool"
Optimization="0" Optimization="0"
AdditionalIncludeDirectories="..\lib" AdditionalIncludeDirectories="..\lib;..\cli"
PreprocessorDefinitions="_DEBUG,WIN32,UNIT_TESTING;_CRT_SECURE_NO_WARNINGS" PreprocessorDefinitions="_DEBUG,WIN32,UNIT_TESTING;_CRT_SECURE_NO_WARNINGS"
BasicRuntimeChecks="3" BasicRuntimeChecks="3"
RuntimeLibrary="1" RuntimeLibrary="1"
@ -128,7 +128,7 @@
Optimization="2" Optimization="2"
EnableIntrinsicFunctions="true" EnableIntrinsicFunctions="true"
WholeProgramOptimization="true" WholeProgramOptimization="true"
AdditionalIncludeDirectories="..\lib" AdditionalIncludeDirectories="..\lib;..\cli"
PreprocessorDefinitions="NDEBUG,WIN32,UNIT_TESTING;_CRT_SECURE_NO_WARNINGS" PreprocessorDefinitions="NDEBUG,WIN32,UNIT_TESTING;_CRT_SECURE_NO_WARNINGS"
RuntimeLibrary="0" RuntimeLibrary="0"
TreatWChar_tAsBuiltInType="false" TreatWChar_tAsBuiltInType="false"
@ -222,6 +222,10 @@
RelativePath="..\lib\checkunusedfunctions.cpp" RelativePath="..\lib\checkunusedfunctions.cpp"
> >
</File> </File>
<File
RelativePath="..\cli\cmdlineparser.cpp"
>
</File>
<File <File
RelativePath="..\lib\cppcheck.cpp" RelativePath="..\lib\cppcheck.cpp"
> >
@ -274,6 +278,10 @@
RelativePath="testclass.cpp" RelativePath="testclass.cpp"
> >
</File> </File>
<File
RelativePath=".\testcmdlineparser.cpp"
>
</File>
<File <File
RelativePath="testconstructors.cpp" RelativePath="testconstructors.cpp"
> >
@ -428,6 +436,10 @@
RelativePath="..\lib\classinfo.h" RelativePath="..\lib\classinfo.h"
> >
</File> </File>
<File
RelativePath="..\cli\cmdlineparser.h"
>
</File>
<File <File
RelativePath="..\lib\cppcheck.h" RelativePath="..\lib\cppcheck.h"
> >

View File

@ -57,7 +57,7 @@
</Midl> </Midl>
<ClCompile> <ClCompile>
<Optimization>Disabled</Optimization> <Optimization>Disabled</Optimization>
<AdditionalIncludeDirectories>..\lib;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> <AdditionalIncludeDirectories>..\lib;..\cli;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<PreprocessorDefinitions>_DEBUG;WIN32;UNIT_TESTING;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions> <PreprocessorDefinitions>_DEBUG;WIN32;UNIT_TESTING;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<BasicRuntimeChecks>EnableFastChecks</BasicRuntimeChecks> <BasicRuntimeChecks>EnableFastChecks</BasicRuntimeChecks>
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary> <RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
@ -94,7 +94,7 @@
<Optimization>MaxSpeed</Optimization> <Optimization>MaxSpeed</Optimization>
<IntrinsicFunctions>true</IntrinsicFunctions> <IntrinsicFunctions>true</IntrinsicFunctions>
<WholeProgramOptimization>true</WholeProgramOptimization> <WholeProgramOptimization>true</WholeProgramOptimization>
<AdditionalIncludeDirectories>..\lib;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> <AdditionalIncludeDirectories>..\lib;..\cli;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<PreprocessorDefinitions>NDEBUG;WIN32;UNIT_TESTING;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions> <PreprocessorDefinitions>NDEBUG;WIN32;UNIT_TESTING;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary> <RuntimeLibrary>MultiThreaded</RuntimeLibrary>
<TreatWChar_tAsBuiltInType>false</TreatWChar_tAsBuiltInType> <TreatWChar_tAsBuiltInType>false</TreatWChar_tAsBuiltInType>
@ -120,6 +120,7 @@
</Bscmake> </Bscmake>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="..\cli\cmdlineparser.cpp" />
<ClCompile Include="..\lib\checkautovariables.cpp" /> <ClCompile Include="..\lib\checkautovariables.cpp" />
<ClCompile Include="..\lib\checkbufferoverrun.cpp" /> <ClCompile Include="..\lib\checkbufferoverrun.cpp" />
<ClCompile Include="..\lib\checkclass.cpp" /> <ClCompile Include="..\lib\checkclass.cpp" />
@ -144,6 +145,7 @@
<ClCompile Include="testbufferoverrun.cpp" /> <ClCompile Include="testbufferoverrun.cpp" />
<ClCompile Include="testcharvar.cpp" /> <ClCompile Include="testcharvar.cpp" />
<ClCompile Include="testclass.cpp" /> <ClCompile Include="testclass.cpp" />
<ClCompile Include="testcmdlineparser.cpp" />
<ClCompile Include="testconstructors.cpp" /> <ClCompile Include="testconstructors.cpp" />
<ClCompile Include="testcppcheck.cpp" /> <ClCompile Include="testcppcheck.cpp" />
<ClCompile Include="testdivision.cpp" /> <ClCompile Include="testdivision.cpp" />
@ -169,6 +171,7 @@
<ClCompile Include="tinyxml\tinyxmlparser.cpp" /> <ClCompile Include="tinyxml\tinyxmlparser.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="..\cli\cmdlineparser.h" />
<ClInclude Include="..\lib\check.h" /> <ClInclude Include="..\lib\check.h" />
<ClInclude Include="..\lib\checkautovariables.h" /> <ClInclude Include="..\lib\checkautovariables.h" />
<ClInclude Include="..\lib\checkbufferoverrun.h" /> <ClInclude Include="..\lib\checkbufferoverrun.h" />

View File

@ -152,6 +152,12 @@
<ClCompile Include="..\lib\timer.cpp"> <ClCompile Include="..\lib\timer.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\cli\cmdlineparser.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="testcmdlineparser.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="testsuite.h"> <ClInclude Include="testsuite.h">
@ -226,5 +232,8 @@
<ClInclude Include="..\lib\timer.h"> <ClInclude Include="..\lib\timer.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\cli\cmdlineparser.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
</Project> </Project>

118
test/testcmdlineparser.cpp Normal file
View File

@ -0,0 +1,118 @@
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2010 Daniel Marjamäki and Cppcheck team.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <iostream>
#include "testsuite.h"
#include "cmdlineparser.h"
#include "settings.h"
extern std::ostringstream errout;
extern std::ostringstream output;
class RedirectInputOutput
{
public:
RedirectInputOutput()
{
// flush all old output
std::cout.flush();
std::cerr.flush();
_oldCout = std::cout.rdbuf(); // back up cout's streambuf
_oldCerr = std::cerr.rdbuf(); // back up cerr's streambuf
std::cout.rdbuf(_out.rdbuf()); // assign streambuf to cout
std::cerr.rdbuf(_err.rdbuf()); // assign streambuf to cerr
}
~RedirectInputOutput()
{
std::cout.rdbuf(_oldCout); // restore cout's original streambuf
std::cerr.rdbuf(_oldCerr); // restore cerrs's original streambuf
errout << _err.str();
output << _out.str();
}
private:
std::stringstream _out;
std::stringstream _err;
std::streambuf* _oldCout;
std::streambuf *_oldCerr;
};
#define REDIRECT RedirectInputOutput redir;
class TestCmdlineParser : public TestFixture
{
public:
TestCmdlineParser() : TestFixture("TestCmdlineParser")
{ }
private:
void run()
{
TEST_CASE(nooptions);
TEST_CASE(helpshort);
TEST_CASE(helplong);
TEST_CASE(showversion);
}
void nooptions()
{
REDIRECT;
const char *argv[] = {"cppcheck"};
Settings settings;
CmdLineParser parser(&settings);
ASSERT(parser.ParseFromArgs(1, argv));
ASSERT_EQUALS(true, parser.GetShowHelp());
}
void helpshort()
{
REDIRECT;
const char *argv[] = {"cppcheck", "-h"};
Settings settings;
CmdLineParser parser(&settings);
ASSERT(parser.ParseFromArgs(2, argv));
ASSERT_EQUALS(true, parser.GetShowHelp());
}
void helplong()
{
REDIRECT;
const char *argv[] = {"cppcheck", "--help"};
Settings settings;
CmdLineParser parser(&settings);
ASSERT(parser.ParseFromArgs(2, argv));
ASSERT_EQUALS(true, parser.GetShowHelp());
}
void showversion()
{
REDIRECT;
const char *argv[] = {"cppcheck", "--version"};
Settings settings;
CmdLineParser parser(&settings);
ASSERT(parser.ParseFromArgs(2, argv));
ASSERT_EQUALS(true, parser.GetShowVersion());
}
};
REGISTER_TEST(TestCmdlineParser)