Testing: Updated the testing to the new test framework

This commit is contained in:
Daniel Marjamäki 2008-10-13 06:42:40 +00:00
parent 26549bf916
commit d330eb197a
9 changed files with 310 additions and 257 deletions

View File

@ -2,15 +2,19 @@
#include "tokenize.h" #include "tokenize.h"
#include "CommonCheck.h" #include "CommonCheck.h"
#include "CheckBufferOverrun.h" #include "CheckBufferOverrun.h"
#include "MiniCppUnit.h" #include "testsuite.h"
#include <sstream> #include <sstream>
extern std::ostringstream errout; extern std::ostringstream errout;
extern bool ShowAll; extern bool ShowAll;
class TestBufferOverrun : public TestFixture<TestBufferOverrun> class TestBufferOverrun : public TestFixture
{ {
public:
TestBufferOverrun() : TestFixture("TestBufferOverrun")
{ }
private: private:
void check( const char code[] ) void check( const char code[] )
{ {
@ -31,8 +35,7 @@ private:
CheckBufferOverrun(); CheckBufferOverrun();
} }
public: void run()
TEST_FIXTURE( TestBufferOverrun )
{ {
TEST_CASE( noerr1 ); TEST_CASE( noerr1 );
TEST_CASE( noerr2 ); TEST_CASE( noerr2 );
@ -316,6 +319,6 @@ public:
}; };
REGISTER_FIXTURE( TestBufferOverrun ) static TestBufferOverrun testbufferoverrun;

View File

@ -4,16 +4,26 @@
#include "tokenize.h" #include "tokenize.h"
#include "CommonCheck.h" #include "CommonCheck.h"
#include "CheckOther.h" #include "CheckOther.h"
#include "MiniCppUnit.h" #include "testsuite.h"
#include <sstream> #include <sstream>
extern std::ostringstream errout; extern std::ostringstream errout;
extern bool ShowAll; extern bool ShowAll;
class TestCharVar : public TestFixture<TestCharVar> class TestCharVar : public TestFixture
{ {
public:
TestCharVar() : TestFixture("TestCharVar")
{ }
private: private:
void run()
{
TEST_CASE( array_index );
TEST_CASE( bitop );
}
void check( const char code[] ) void check( const char code[] )
{ {
// Tokenize.. // Tokenize..
@ -32,14 +42,6 @@ private:
CheckCharVariable(); CheckCharVariable();
} }
public:
TEST_FIXTURE( TestCharVar )
{
TEST_CASE( array_index );
TEST_CASE( bitop );
}
void array_index() void array_index()
{ {
check( "void foo()\n" check( "void foo()\n"
@ -75,5 +77,5 @@ public:
} }
}; };
REGISTER_FIXTURE( TestCharVar ) static TestCharVar testcharvar;

View File

@ -1,14 +1,18 @@
#include "tokenize.h" #include "tokenize.h"
#include "CheckClass.h" #include "CheckClass.h"
#include "MiniCppUnit.h" #include "testsuite.h"
#include <sstream> #include <sstream>
extern std::ostringstream errout; extern std::ostringstream errout;
class TestConstructors : public TestFixture<TestConstructors> class TestConstructors : public TestFixture
{ {
public:
TestConstructors() : TestFixture("TestConstructors")
{ }
private: private:
void check( const char code[] ) void check( const char code[] )
{ {
@ -25,8 +29,7 @@ private:
CheckConstructors(); CheckConstructors();
} }
public: void run()
TEST_FIXTURE( TestConstructors )
{ {
TEST_CASE( simple1 ); TEST_CASE( simple1 );
TEST_CASE( simple2 ); TEST_CASE( simple2 );
@ -42,7 +45,9 @@ public:
"public:\n" "public:\n"
" int i;\n" " int i;\n"
"};\n" ); "};\n" );
ASSERT_EQUALS( std::string("[test.cpp:1] The class 'Fred' has no constructor\n"), errout.str() ); std::string actual( errout.str() );
std::string expected( "[test.cpp:1] The class 'Fred' has no constructor\n" );
ASSERT_EQUALS( expected, actual );
} }
@ -92,4 +97,4 @@ public:
}; };
REGISTER_FIXTURE( TestConstructors ) static TestConstructors testconstructors;

View File

@ -5,15 +5,19 @@
#include "tokenize.h" #include "tokenize.h"
#include "CheckOther.h" #include "CheckOther.h"
#include "MiniCppUnit.h" #include "testsuite.h"
#include <sstream> #include <sstream>
extern std::ostringstream errout; extern std::ostringstream errout;
extern bool ShowAll; extern bool ShowAll;
class TestDivision : public TestFixture<TestDivision> class TestDivision : public TestFixture
{ {
public:
TestDivision() : TestFixture("TestDivision")
{ }
private: private:
void check( const char code[] ) void check( const char code[] )
{ {
@ -31,8 +35,7 @@ private:
CheckUnsignedDivision(); CheckUnsignedDivision();
} }
public: void run()
TEST_FIXTURE( TestDivision )
{ {
TEST_CASE( division1 ); TEST_CASE( division1 );
TEST_CASE( division2 ); TEST_CASE( division2 );
@ -125,6 +128,6 @@ public:
} }
}; };
REGISTER_FIXTURE( TestDivision ) static TestDivision testdivision;

View File

@ -1,15 +1,19 @@
#include "tokenize.h" #include "tokenize.h"
#include "CheckMemoryLeak.h" #include "CheckMemoryLeak.h"
#include "MiniCppUnit.h" #include "testsuite.h"
#include <sstream> #include <sstream>
extern std::ostringstream errout; extern std::ostringstream errout;
extern bool ShowAll; extern bool ShowAll;
class TestMemleak : public TestFixture<TestMemleak> class TestMemleak : public TestFixture
{ {
public:
TestMemleak() : TestFixture("TestMemleak")
{ }
private: private:
void check( const char code[] ) void check( const char code[] )
{ {
@ -27,8 +31,7 @@ private:
CheckMemoryLeak(); CheckMemoryLeak();
} }
public: void run()
TEST_FIXTURE( TestMemleak )
{ {
TEST_CASE( simple1 ); TEST_CASE( simple1 );
TEST_CASE( simple2 ); TEST_CASE( simple2 );
@ -58,7 +61,6 @@ public:
TEST_CASE( func2 ); TEST_CASE( func2 );
TEST_CASE( class1 ); TEST_CASE( class1 );
} }
void simple1() void simple1()
@ -524,6 +526,6 @@ public:
}; };
REGISTER_FIXTURE( TestMemleak ) static TestMemleak testmemleak;

View File

@ -18,15 +18,15 @@
<CfgParent>Base</CfgParent> <CfgParent>Base</CfgParent>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Base)'!=''"> <PropertyGroup Condition="'$(Base)'!=''">
<OutputExt>exe</OutputExt>
<BCC_OptimizeForSpeed>true</BCC_OptimizeForSpeed> <BCC_OptimizeForSpeed>true</BCC_OptimizeForSpeed>
<DCC_CBuilderOutput>JPHNE</DCC_CBuilderOutput> <OutputExt>exe</OutputExt>
<Defines>NO_STRICT</Defines> <Defines>NO_STRICT</Defines>
<DCC_CBuilderOutput>JPHNE</DCC_CBuilderOutput>
<DynamicRTL>true</DynamicRTL> <DynamicRTL>true</DynamicRTL>
<ILINK_ObjectSearchPath>C:\cppcheck</ILINK_ObjectSearchPath>
<UsePackages>true</UsePackages> <UsePackages>true</UsePackages>
<NoVCL>true</NoVCL> <ILINK_ObjectSearchPath>C:\cppcheck</ILINK_ObjectSearchPath>
<ProjectType>CppConsoleApplication</ProjectType> <ProjectType>CppConsoleApplication</ProjectType>
<NoVCL>true</NoVCL>
<FinalOutputDir>.</FinalOutputDir> <FinalOutputDir>.</FinalOutputDir>
<PackageImports>vclx.bpi;vcl.bpi;rtl.bpi;vclactnband.bpi</PackageImports> <PackageImports>vclx.bpi;vcl.bpi;rtl.bpi;vclactnband.bpi</PackageImports>
<BCC_wpar>false</BCC_wpar> <BCC_wpar>false</BCC_wpar>
@ -34,10 +34,10 @@
<ILINK_LibraryPath>$(BDS)\lib;$(BDS)\lib\obj;$(BDS)\lib\psdk;C:\cppcheck</ILINK_LibraryPath> <ILINK_LibraryPath>$(BDS)\lib;$(BDS)\lib\obj;$(BDS)\lib\psdk;C:\cppcheck</ILINK_LibraryPath>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Cfg_1)'!=''"> <PropertyGroup Condition="'$(Cfg_1)'!=''">
<BCC_OptimizeForSpeed>false</BCC_OptimizeForSpeed>
<DCC_Optimize>false</DCC_Optimize> <DCC_Optimize>false</DCC_Optimize>
<DCC_DebugInfoInExe>true</DCC_DebugInfoInExe> <BCC_OptimizeForSpeed>false</BCC_OptimizeForSpeed>
<Defines>_DEBUG;$(Defines)</Defines> <Defines>_DEBUG;$(Defines)</Defines>
<DCC_DebugInfoInExe>true</DCC_DebugInfoInExe>
<ILINK_FullDebugInfo>true</ILINK_FullDebugInfo> <ILINK_FullDebugInfo>true</ILINK_FullDebugInfo>
<BCC_InlineFunctionExpansion>false</BCC_InlineFunctionExpansion> <BCC_InlineFunctionExpansion>false</BCC_InlineFunctionExpansion>
<ILINK_DisableIncrementalLinking>true</ILINK_DisableIncrementalLinking> <ILINK_DisableIncrementalLinking>true</ILINK_DisableIncrementalLinking>
@ -47,8 +47,8 @@
<IntermediateOutputDir>Debug</IntermediateOutputDir> <IntermediateOutputDir>Debug</IntermediateOutputDir>
<TASM_DisplaySourceLines>true</TASM_DisplaySourceLines> <TASM_DisplaySourceLines>true</TASM_DisplaySourceLines>
<BCC_StackFrames>true</BCC_StackFrames> <BCC_StackFrames>true</BCC_StackFrames>
<BCC_DisableOptimizations>true</BCC_DisableOptimizations>
<ILINK_LibraryPath>$(BDS)\lib\debug;$(ILINK_LibraryPath)</ILINK_LibraryPath> <ILINK_LibraryPath>$(BDS)\lib\debug;$(ILINK_LibraryPath)</ILINK_LibraryPath>
<BCC_DisableOptimizations>true</BCC_DisableOptimizations>
<TASM_Debugging>Full</TASM_Debugging> <TASM_Debugging>Full</TASM_Debugging>
<BCC_SourceDebuggingOn>true</BCC_SourceDebuggingOn> <BCC_SourceDebuggingOn>true</BCC_SourceDebuggingOn>
</PropertyGroup> </PropertyGroup>
@ -71,49 +71,49 @@
<ItemGroup> <ItemGroup>
<CppCompile Include="CheckBufferOverrun.cpp"> <CppCompile Include="CheckBufferOverrun.cpp">
<DependentOn>CheckBufferOverrun.h</DependentOn> <DependentOn>CheckBufferOverrun.h</DependentOn>
<BuildOrder>9</BuildOrder> <BuildOrder>7</BuildOrder>
</CppCompile> </CppCompile>
<CppCompile Include="CheckClass.cpp"> <CppCompile Include="CheckClass.cpp">
<DependentOn>CheckClass.h</DependentOn> <DependentOn>CheckClass.h</DependentOn>
<BuildOrder>10</BuildOrder> <BuildOrder>8</BuildOrder>
</CppCompile> </CppCompile>
<CppCompile Include="CheckMemoryLeak.cpp"> <CppCompile Include="CheckMemoryLeak.cpp">
<DependentOn>CheckMemoryLeak.h</DependentOn> <DependentOn>CheckMemoryLeak.h</DependentOn>
<BuildOrder>2</BuildOrder> <BuildOrder>1</BuildOrder>
</CppCompile> </CppCompile>
<CppCompile Include="CheckOther.cpp"> <CppCompile Include="CheckOther.cpp">
<DependentOn>CheckOther.h</DependentOn> <DependentOn>CheckOther.h</DependentOn>
<BuildOrder>11</BuildOrder> <BuildOrder>9</BuildOrder>
</CppCompile> </CppCompile>
<CppCompile Include="CommonCheck.cpp"> <CppCompile Include="CommonCheck.cpp">
<DependentOn>CommonCheck.h</DependentOn> <DependentOn>CommonCheck.h</DependentOn>
<BuildOrder>5</BuildOrder>
</CppCompile>
<CppCompile Include="MiniCppUnit.cpp">
<BuildOrder>3</BuildOrder> <BuildOrder>3</BuildOrder>
</CppCompile> </CppCompile>
<CppCompile Include="testbufferoverrun.cpp"> <CppCompile Include="testbufferoverrun.cpp">
<BuildOrder>7</BuildOrder> <BuildOrder>5</BuildOrder>
</CppCompile> </CppCompile>
<CppCompile Include="testcharvar.cpp"> <CppCompile Include="testcharvar.cpp">
<DependentOn>testcharvar.h</DependentOn> <DependentOn>testcharvar.h</DependentOn>
<BuildOrder>12</BuildOrder> <BuildOrder>10</BuildOrder>
</CppCompile> </CppCompile>
<CppCompile Include="testconstructors.cpp"> <CppCompile Include="testconstructors.cpp">
<BuildOrder>8</BuildOrder>
</CppCompile>
<CppCompile Include="testdivision.cpp">
<BuildOrder>6</BuildOrder> <BuildOrder>6</BuildOrder>
</CppCompile> </CppCompile>
<CppCompile Include="testmemleak.cpp"> <CppCompile Include="testdivision.cpp">
<BuildOrder>4</BuildOrder> <BuildOrder>4</BuildOrder>
</CppCompile> </CppCompile>
<CppCompile Include="TestsRunner.cpp"> <CppCompile Include="testmemleak.cpp">
<BuildOrder>0</BuildOrder> <BuildOrder>2</BuildOrder>
</CppCompile>
<CppCompile Include="testrunner.cpp">
<BuildOrder>11</BuildOrder>
</CppCompile>
<CppCompile Include="testsuite.cpp">
<BuildOrder>12</BuildOrder>
</CppCompile> </CppCompile>
<CppCompile Include="tokenize.cpp"> <CppCompile Include="tokenize.cpp">
<DependentOn>tokenize.h</DependentOn> <DependentOn>tokenize.h</DependentOn>
<BuildOrder>1</BuildOrder> <BuildOrder>0</BuildOrder>
</CppCompile> </CppCompile>
<BuildConfiguration Include="Debug"> <BuildConfiguration Include="Debug">
<Key>Cfg_1</Key> <Key>Cfg_1</Key>

View File

@ -1,10 +1,18 @@
#include "testsuite.h" #include "testsuite.h"
#include <string>
#include <vector>
bool ShowAll = false;
bool CheckCodingStyle = true;
extern std::vector<std::string> Files;
int main() int main()
{ {
TestSuite::runTests(); Files.push_back( "test.cpp" );
TestFixture::runTests();
return 0; return 0;
} }

View File

@ -13,7 +13,7 @@
class TestRegistry class TestRegistry
{ {
private: private:
std::list<TestSuite *> _tests; std::list<TestFixture *> _tests;
public: public:
static TestRegistry &theInstance() static TestRegistry &theInstance()
@ -22,54 +22,80 @@ public:
return testreg; return testreg;
} }
void addTest( TestSuite *t ) void addTest( TestFixture *t )
{ {
_tests.push_back( t ); _tests.push_back( t );
} }
void removeTest( TestSuite *t ) void removeTest( TestFixture *t )
{ {
_tests.remove( t ); _tests.remove( t );
} }
const std::list<TestSuite *> &tests() const const std::list<TestFixture *> &tests() const
{ return _tests; } {
return _tests;
}
}; };
/** /**
* TestSuite * TestFixture
**/ **/
TestSuite::TestSuite(const std::string &_name) : classname(_name) std::ostringstream TestFixture::errmsg;
unsigned int TestFixture::countTests;
TestFixture::TestFixture(const std::string &_name) : classname(_name)
{ {
TestRegistry::theInstance().addTest(this); TestRegistry::theInstance().addTest(this);
} }
TestSuite::~TestSuite() TestFixture::~TestFixture()
{ {
TestRegistry::theInstance().removeTest(this); TestRegistry::theInstance().removeTest(this);
} }
void TestSuite::printTests() bool TestFixture::runTest(const char testname[])
{ {
const std::list<TestSuite *> &tests = TestRegistry::theInstance().tests(); countTests++;
std::cout << classname << "::" << testname << "\n";
return true;
}
for ( std::list<TestSuite *>::const_iterator it = tests.begin(); it != tests.end(); ++it ) void TestFixture::assertFail(const char *filename, int linenr)
{
errmsg << "Assertion failed in " << filename << " at line " << linenr << std::endl;
}
void TestFixture::printTests()
{
const std::list<TestFixture *> &tests = TestRegistry::theInstance().tests();
for ( std::list<TestFixture *>::const_iterator it = tests.begin(); it != tests.end(); ++it )
{ {
std::cout << (*it)->classname << std::endl; std::cout << (*it)->classname << std::endl;
} }
} }
void TestSuite::runTests() void TestFixture::runTests()
{ {
const std::list<TestSuite *> &tests = TestRegistry::theInstance().tests(); countTests = 0;
errmsg.str("");
for ( std::list<TestSuite *>::const_iterator it = tests.begin(); it != tests.end(); ++it ) const std::list<TestFixture *> &tests = TestRegistry::theInstance().tests();
for ( std::list<TestFixture *>::const_iterator it = tests.begin(); it != tests.end(); ++it )
{ {
(*it)->run(); (*it)->run();
} }
std::cout << "\n\nTesting Complete\nNumber of tests: " << countTests << "\n";
std::cerr << errmsg.str();
} }

View File

@ -1,27 +1,31 @@
#include <iostream> #include <sstream>
#include <string>
class TestSuite class TestFixture
{ {
private:
static std::ostringstream errmsg;
static unsigned int countTests;
protected: protected:
std::string classname; std::string classname;
virtual void run() = 0; virtual void run()
{ }
bool runTest(const char testname[]);
void assertFail(const char *filename, int linenr);
public: public:
TestSuite(const std::string &_name); TestFixture(const std::string &_name);
~TestSuite(); ~TestFixture();
static void printTests(); static void printTests();
static void runTests(); static void runTests();
}; };
#define TEST_CASE( NAME ) std::cout << classname << "::" << #NAME << std::endl; NAME (); #define TEST_CASE( NAME ) if ( runTest(#NAME) ) NAME ();
#define ASSERT_EQUALS( EXPECTED , ACTUAL ) if (EXPECTED!=ACTUAL) assertFail(__FILE__, __LINE__);
/*
#define ASSERT_EQUALS( EXPECTED , ACTUAL )
*/