testclass : Added a new unit testing file

This commit is contained in:
Daniel Marjamäki 2008-12-06 18:31:26 +00:00
parent b71adda8b5
commit 1a5d694834
2 changed files with 87 additions and 1 deletions

View File

@ -1,6 +1,6 @@
SRCS=CheckBufferOverrun.cpp CheckClass.cpp CheckHeaders.cpp CheckMemoryLeak.cpp CheckFunctionUsage.cpp CheckOther.cpp FileLister.cpp preprocessor.cpp tokenize.cpp cppcheck.cpp settings.cpp token.cpp cppcheckexecutor.cpp SRCS=CheckBufferOverrun.cpp CheckClass.cpp CheckHeaders.cpp CheckMemoryLeak.cpp CheckFunctionUsage.cpp CheckOther.cpp FileLister.cpp preprocessor.cpp tokenize.cpp cppcheck.cpp settings.cpp token.cpp cppcheckexecutor.cpp
OBJS=$(SRCS:%.cpp=%.o) OBJS=$(SRCS:%.cpp=%.o)
TESTS=testbufferoverrun.o testcharvar.o testconstructors.o testdivision.o testfunctionusage.o testincompletestatement.o testmemleak.o testpreprocessor.o testsimplifytokens.o testtokenize.o testunusedprivfunc.o testunusedvar.o TESTS=testbufferoverrun.o testcharvar.o testclass.o testconstructors.o testdivision.o testfunctionusage.o testincompletestatement.o testmemleak.o testpreprocessor.o testsimplifytokens.o testtokenize.o testunusedprivfunc.o testunusedvar.o
BIN = ${DESTDIR}/usr/bin BIN = ${DESTDIR}/usr/bin
all: ${OBJS} main.o all: ${OBJS} main.o
@ -35,6 +35,8 @@ testbufferoverrun.o: testbufferoverrun.cpp tokenize.h settings.h errorlogger.h t
g++ -Wall -pedantic -g -I. -o $@ -c $*.cpp g++ -Wall -pedantic -g -I. -o $@ -c $*.cpp
testcharvar.o: testcharvar.cpp tokenize.h settings.h errorlogger.h token.h CheckOther.h testsuite.h testcharvar.o: testcharvar.cpp tokenize.h settings.h errorlogger.h token.h CheckOther.h testsuite.h
g++ -Wall -pedantic -g -I. -o $@ -c $*.cpp g++ -Wall -pedantic -g -I. -o $@ -c $*.cpp
testclass.o: testclass.cpp tokenize.h settings.h errorlogger.h token.h CheckClass.h testsuite.h
g++ -Wall -pedantic -g -I. -o $@ -c $*.cpp
testconstructors.o: testconstructors.cpp tokenize.h settings.h errorlogger.h token.h CheckClass.h testsuite.h testconstructors.o: testconstructors.cpp tokenize.h settings.h errorlogger.h token.h CheckClass.h testsuite.h
g++ -Wall -pedantic -g -I. -o $@ -c $*.cpp g++ -Wall -pedantic -g -I. -o $@ -c $*.cpp
testdivision.o: testdivision.cpp tokenize.h settings.h errorlogger.h token.h CheckOther.h testsuite.h testdivision.o: testdivision.cpp tokenize.h settings.h errorlogger.h token.h CheckOther.h testsuite.h

84
testclass.cpp Normal file
View File

@ -0,0 +1,84 @@
/*
* c++check - c/c++ syntax checking
* Copyright (C) 2007 Daniel Marjamäki
*
* 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/
*/
#define UNIT_TESTING
#include "tokenize.h"
#include "CheckClass.h"
#include "testsuite.h"
#include <sstream>
extern std::ostringstream errout;
class TestClass : public TestFixture
{
public:
TestClass() : TestFixture("TestClass")
{ }
private:
void run()
{
TEST_CASE( virtualDestructor1 );
TEST_CASE( virtualDestructor2 );
TEST_CASE( virtualDestructor3 );
}
// Check that base classes have virtual destructors
void checkVirtualDestructor(const char code[])
{
// Tokenize..
Tokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize( istr, "test.cpp" );
tokenizer.simplifyTokenList();
// Clear the error log
errout.str("");
// Check..
Settings settings;
CheckClass checkClass( &tokenizer, settings, this );
checkClass.virtualDestructor();
}
void virtualDestructor1()
{
checkVirtualDestructor("class Derived : public Base { };");
ASSERT_EQUALS( std::string(""), errout.str() );
}
void virtualDestructor2()
{
checkVirtualDestructor("class Base { };\n"
"class Derived : public Base { };");
ASSERT_EQUALS( std::string("[test.cpp:1]: Base class Base doesn't have a virtual destructor\n"), errout.str() );
}
void virtualDestructor3()
{
checkVirtualDestructor("class Base { public: ~Base(); };\n"
"class Derived : public Base { };");
ASSERT_EQUALS( std::string("[test.cpp:1]: The destructor for the base class Base is not virtual\n"), errout.str() );
}
};
REGISTER_TEST( TestClass )