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

View File

@ -4,16 +4,26 @@
#include "tokenize.h"
#include "CommonCheck.h"
#include "CheckOther.h"
#include "MiniCppUnit.h"
#include "testsuite.h"
#include <sstream>
extern std::ostringstream errout;
extern bool ShowAll;
class TestCharVar : public TestFixture<TestCharVar>
class TestCharVar : public TestFixture
{
public:
TestCharVar() : TestFixture("TestCharVar")
{ }
private:
void run()
{
TEST_CASE( array_index );
TEST_CASE( bitop );
}
void check( const char code[] )
{
// Tokenize..
@ -32,14 +42,6 @@ private:
CheckCharVariable();
}
public:
TEST_FIXTURE( TestCharVar )
{
TEST_CASE( array_index );
TEST_CASE( bitop );
}
void array_index()
{
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 "CheckClass.h"
#include "MiniCppUnit.h"
#include "testsuite.h"
#include <sstream>
extern std::ostringstream errout;
class TestConstructors : public TestFixture<TestConstructors>
class TestConstructors : public TestFixture
{
public:
TestConstructors() : TestFixture("TestConstructors")
{ }
private:
void check( const char code[] )
{
@ -25,8 +29,7 @@ private:
CheckConstructors();
}
public:
TEST_FIXTURE( TestConstructors )
void run()
{
TEST_CASE( simple1 );
TEST_CASE( simple2 );
@ -42,7 +45,9 @@ public:
"public:\n"
" int i;\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 "CheckOther.h"
#include "MiniCppUnit.h"
#include "testsuite.h"
#include <sstream>
extern std::ostringstream errout;
extern bool ShowAll;
class TestDivision : public TestFixture<TestDivision>
class TestDivision : public TestFixture
{
public:
TestDivision() : TestFixture("TestDivision")
{ }
private:
void check( const char code[] )
{
@ -31,8 +35,7 @@ private:
CheckUnsignedDivision();
}
public:
TEST_FIXTURE( TestDivision )
void run()
{
TEST_CASE( division1 );
TEST_CASE( division2 );
@ -125,6 +128,6 @@ public:
}
};
REGISTER_FIXTURE( TestDivision )
static TestDivision testdivision;

View File

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

View File

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

View File

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

View File

@ -13,7 +13,7 @@
class TestRegistry
{
private:
std::list<TestSuite *> _tests;
std::list<TestFixture *> _tests;
public:
static TestRegistry &theInstance()
@ -22,54 +22,80 @@ public:
return testreg;
}
void addTest( TestSuite *t )
void addTest( TestFixture *t )
{
_tests.push_back( t );
}
void removeTest( TestSuite *t )
void removeTest( TestFixture *t )
{
_tests.remove( t );
}
const std::list<TestSuite *> &tests() const
{ return _tests; }
const std::list<TestFixture *> &tests() const
{
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);
}
TestSuite::~TestSuite()
TestFixture::~TestFixture()
{
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;
}
}
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();
}
std::cout << "\n\nTesting Complete\nNumber of tests: " << countTests << "\n";
std::cerr << errmsg.str();
}

View File

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