Refactoring the unit testing

This commit is contained in:
Daniel Marjamäki 2009-08-04 21:32:14 +02:00
parent 975e7778ab
commit 706ba34a6d
6 changed files with 37 additions and 103 deletions

View File

@ -118,7 +118,6 @@ public:
/** Check for post increment/decrement in for loop*/ /** Check for post increment/decrement in for loop*/
void postIncrement(); void postIncrement();
protected:
void lookupVar(const Token *tok1, const char varname[]); void lookupVar(const Token *tok1, const char varname[]);
// Redundant condition // Redundant condition
@ -126,7 +125,6 @@ protected:
// haystack.remove(needle); // haystack.remove(needle);
void redundantCondition2(); void redundantCondition2();
private:
// Error messages.. // Error messages..
void cstyleCastError(const Token *tok); void cstyleCastError(const Token *tok);
void redundantIfDelete0Error(const Token *tok); void redundantIfDelete0Error(const Token *tok);

View File

@ -79,9 +79,6 @@ public:
/** Simplify tokenlist */ /** Simplify tokenlist */
void simplifyTokenList(); void simplifyTokenList();
// Helper functions for handling the tokens list..
static void deleteTokens(Token *tok); static void deleteTokens(Token *tok);
static const char *getParameterName(const Token *ftok, int par); static const char *getParameterName(const Token *ftok, int par);
@ -90,8 +87,6 @@ public:
// Return size. // Return size.
int sizeOfType(const char type[]) const; int sizeOfType(const char type[]) const;
void initTokens();
const std::vector<std::string> *getFiles() const; const std::vector<std::string> *getFiles() const;
void fillFunctionList(); void fillFunctionList();
@ -110,6 +105,7 @@ public:
*/ */
static const Token *findClassFunction(const Token *tok, const char classname[], const char funcname[], int &indentlevel); static const Token *findClassFunction(const Token *tok, const char classname[], const char funcname[], int &indentlevel);
private:
/** /**
* Simplify variable declarations * Simplify variable declarations
@ -174,8 +170,6 @@ public:
*/ */
bool simplifyComma(); bool simplifyComma();
protected:
/** Add braces to an if-block /** Add braces to an if-block
* @return true if something is modified * @return true if something is modified
* false if nothing is done. * false if nothing is done.
@ -202,8 +196,6 @@ protected:
std::vector<const Token *> _functionList; std::vector<const Token *> _functionList;
private:
/** /**
* Finds matching "end" for "start". * Finds matching "end" for "start".
* @param tok The start tag * @param tok The start tag

View File

@ -18,8 +18,6 @@
#define UNIT_TESTING
#include "../src/tokenize.h" #include "../src/tokenize.h"
#include "../src/checkmemoryleak.h" #include "../src/checkmemoryleak.h"
#include "testsuite.h" #include "testsuite.h"

View File

@ -35,21 +35,6 @@ public:
TestRedundantIf() : TestFixture("TestRedundantIf") TestRedundantIf() : TestFixture("TestRedundantIf")
{ } { }
class OurCheckOther : public CheckOther
{
public:
OurCheckOther(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
: CheckOther(tokenizer, settings, errorLogger)
{
}
void redundantCondition2()
{
CheckOther::redundantCondition2();
}
};
private: private:
void check(const char code[]) void check(const char code[])
{ {
@ -63,7 +48,7 @@ private:
// Check for redundant condition.. // Check for redundant condition..
Settings settings; Settings settings;
OurCheckOther checkOther(&tokenizer, &settings, this); CheckOther checkOther(&tokenizer, &settings, this);
checkOther.redundantCondition2(); checkOther.redundantCondition2();
} }

View File

@ -19,33 +19,15 @@
#include "testsuite.h" #include "testsuite.h"
#define private public
#include "../src/tokenize.h" #include "../src/tokenize.h"
#undef private
#include "../src/token.h" #include "../src/token.h"
#include <sstream> #include <sstream>
extern std::ostringstream errout; extern std::ostringstream errout;
// A test tokenizer where protected functions are made public
class OpenTokenizer : public Tokenizer
{
public:
OpenTokenizer(const char code[]) : Tokenizer()
{
std::istringstream istr(code);
tokenize(istr, "test.cpp");
}
virtual ~OpenTokenizer()
{ }
bool elseif_()
{
return elseif();
}
};
class TestSimplifyTokens : public TestFixture class TestSimplifyTokens : public TestFixture
{ {
public: public:
@ -471,8 +453,9 @@ private:
{ {
std::istringstream istr(code); std::istringstream istr(code);
OpenTokenizer tokenizer(code); Tokenizer tokenizer;
tokenizer.elseif_(); tokenizer.createTokens(istr);
tokenizer.elseif();
return tokenizer.tokens()->stringifyList(false); return tokenizer.tokens()->stringifyList(false);
} }

View File

@ -23,7 +23,9 @@
#include <cstring> #include <cstring>
#include "testsuite.h" #include "testsuite.h"
#define private public
#include "../src/tokenize.h" #include "../src/tokenize.h"
#undef private
#include "../src/token.h" #include "../src/token.h"
extern std::ostringstream errout; extern std::ostringstream errout;
@ -33,30 +35,6 @@ public:
TestTokenizer() : TestFixture("TestTokenizer") TestTokenizer() : TestFixture("TestTokenizer")
{ } { }
class OurTokenizer : public Tokenizer
{
public:
void simplifyCasts()
{
Tokenizer::simplifyCasts();
}
bool simplifyIfAddBraces()
{
return Tokenizer::simplifyIfAddBraces();
}
bool simplifyKnownVariables()
{
return Tokenizer::simplifyKnownVariables();
}
std::vector<const Token *> &getFunctionList()
{
return _functionList;
}
};
private: private:
void run() void run()
@ -241,7 +219,7 @@ private:
const char code[] = "int *f(int *);"; const char code[] = "int *f(int *);";
// tokenize.. // tokenize..
OurTokenizer tokenizer; Tokenizer tokenizer;
std::istringstream istr(code); std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp"); tokenizer.tokenize(istr, "test.cpp");
@ -259,7 +237,7 @@ private:
const char code[] = "t = (static_cast<std::vector<int> *>(&p));\n"; const char code[] = "t = (static_cast<std::vector<int> *>(&p));\n";
// tokenize.. // tokenize..
OurTokenizer tokenizer; Tokenizer tokenizer;
std::istringstream istr(code); std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp"); tokenizer.tokenize(istr, "test.cpp");
@ -300,14 +278,14 @@ private:
"void b()\n" "void b()\n"
"{ }\n"; "{ }\n";
// tokenize.. // tokenize..
OurTokenizer tokenizer; Tokenizer tokenizer;
std::istringstream istr(code); std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp"); tokenizer.tokenize(istr, "test.cpp");
tokenizer.fillFunctionList(); tokenizer.fillFunctionList();
ASSERT_EQUALS(1, static_cast<unsigned int>(tokenizer.getFunctionList().size())); ASSERT_EQUALS(1, static_cast<unsigned int>(tokenizer._functionList.size()));
ASSERT_EQUALS("b", tokenizer.getFunctionList()[0]->str()); ASSERT_EQUALS("b", tokenizer._functionList[0]->str());
} }
void const_and_volatile_functions() void const_and_volatile_functions()
@ -331,18 +309,18 @@ private:
// tokenize.. // tokenize..
OurTokenizer tokenizer; Tokenizer tokenizer;
std::istringstream istr(code); std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp"); tokenizer.tokenize(istr, "test.cpp");
tokenizer.fillFunctionList(); tokenizer.fillFunctionList();
ASSERT_EQUALS(3, static_cast<unsigned int>(tokenizer.getFunctionList().size())); ASSERT_EQUALS(3, static_cast<unsigned int>(tokenizer._functionList.size()));
if (tokenizer.getFunctionList().size() == 3) if (tokenizer._functionList.size() == 3)
{ {
ASSERT_EQUALS("a", tokenizer.getFunctionList()[0]->str()); ASSERT_EQUALS("a", tokenizer._functionList[0]->str());
ASSERT_EQUALS("b", tokenizer.getFunctionList()[1]->str()); ASSERT_EQUALS("b", tokenizer._functionList[1]->str());
ASSERT_EQUALS("c", tokenizer.getFunctionList()[2]->str()); ASSERT_EQUALS("c", tokenizer._functionList[2]->str());
} }
} }
@ -543,7 +521,7 @@ private:
"}\n"; "}\n";
// tokenize.. // tokenize..
OurTokenizer tokenizer; Tokenizer tokenizer;
std::istringstream istr(code); std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp"); tokenizer.tokenize(istr, "test.cpp");
@ -564,7 +542,7 @@ private:
"}\n"; "}\n";
// tokenize.. // tokenize..
OurTokenizer tokenizer; Tokenizer tokenizer;
std::istringstream istr(code); std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp"); tokenizer.tokenize(istr, "test.cpp");
@ -588,7 +566,7 @@ private:
"}\n"; "}\n";
// tokenize.. // tokenize..
OurTokenizer tokenizer; Tokenizer tokenizer;
std::istringstream istr(code); std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp"); tokenizer.tokenize(istr, "test.cpp");
@ -614,7 +592,7 @@ private:
"}\n"; "}\n";
// tokenize.. // tokenize..
OurTokenizer tokenizer; Tokenizer tokenizer;
std::istringstream istr(code); std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp"); tokenizer.tokenize(istr, "test.cpp");
@ -636,7 +614,7 @@ private:
"}\n"; "}\n";
// tokenize.. // tokenize..
OurTokenizer tokenizer; Tokenizer tokenizer;
std::istringstream istr(code); std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp"); tokenizer.tokenize(istr, "test.cpp");
@ -658,7 +636,7 @@ private:
"}\n"; "}\n";
// tokenize.. // tokenize..
OurTokenizer tokenizer; Tokenizer tokenizer;
std::istringstream istr(code); std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp"); tokenizer.tokenize(istr, "test.cpp");
@ -681,7 +659,7 @@ private:
"}\n"; "}\n";
// tokenize.. // tokenize..
OurTokenizer tokenizer; Tokenizer tokenizer;
std::istringstream istr(code); std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp"); tokenizer.tokenize(istr, "test.cpp");
@ -703,7 +681,7 @@ private:
" abc[++i] = 2;\n" " abc[++i] = 2;\n"
"}\n"; "}\n";
// tokenize.. // tokenize..
OurTokenizer tokenizer; Tokenizer tokenizer;
std::istringstream istr(code); std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp"); tokenizer.tokenize(istr, "test.cpp");
@ -725,7 +703,7 @@ private:
" abc[i] = 0;\n" " abc[i] = 0;\n"
"}\n"; "}\n";
// tokenize.. // tokenize..
OurTokenizer tokenizer; Tokenizer tokenizer;
std::istringstream istr(code); std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp"); tokenizer.tokenize(istr, "test.cpp");
@ -747,7 +725,7 @@ private:
" ;\n" " ;\n"
"}\n"; "}\n";
// tokenize.. // tokenize..
OurTokenizer tokenizer; Tokenizer tokenizer;
std::istringstream istr(code); std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp"); tokenizer.tokenize(istr, "test.cpp");
@ -777,7 +755,7 @@ private:
" }\n" " }\n"
"}\n"; "}\n";
// tokenize.. // tokenize..
OurTokenizer tokenizer; Tokenizer tokenizer;
std::istringstream istr(code); std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp"); tokenizer.tokenize(istr, "test.cpp");
@ -805,7 +783,7 @@ private:
" }\n" " }\n"
"}\n"; "}\n";
// tokenize.. // tokenize..
OurTokenizer tokenizer; Tokenizer tokenizer;
std::istringstream istr(code); std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp"); tokenizer.tokenize(istr, "test.cpp");
@ -830,7 +808,7 @@ private:
" a(b);\n" " a(b);\n"
"}\n"; "}\n";
// tokenize.. // tokenize..
OurTokenizer tokenizer; Tokenizer tokenizer;
std::istringstream istr(code); std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp"); tokenizer.tokenize(istr, "test.cpp");
@ -852,7 +830,7 @@ private:
" int foo=0;\n" " int foo=0;\n"
"}\n"; "}\n";
// tokenize.. // tokenize..
OurTokenizer tokenizer; Tokenizer tokenizer;
std::istringstream istr(code); std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp"); tokenizer.tokenize(istr, "test.cpp");
@ -871,7 +849,7 @@ private:
"const double pi = 3.14;\n" "const double pi = 3.14;\n"
"int main(){}\n"; "int main(){}\n";
// tokenize.. // tokenize..
OurTokenizer tokenizer; Tokenizer tokenizer;
std::istringstream istr(code); std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp"); tokenizer.tokenize(istr, "test.cpp");
@ -2077,7 +2055,7 @@ private:
"}\n"; "}\n";
// tokenize.. // tokenize..
OurTokenizer tokenizer; Tokenizer tokenizer;
std::istringstream istr(code); std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp"); tokenizer.tokenize(istr, "test.cpp");
@ -2100,7 +2078,7 @@ private:
"}\n"; "}\n";
// tokenize.. // tokenize..
OurTokenizer tokenizer; Tokenizer tokenizer;
std::istringstream istr(code); std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp"); tokenizer.tokenize(istr, "test.cpp");
tokenizer.simplifyTokenList(); tokenizer.simplifyTokenList();