Refactorization: Reduced code duplication in testsymboldatabase.cpp

This commit is contained in:
PKEuS 2015-10-08 12:53:56 +02:00
parent 3645e3c16b
commit 856e4c414d
1 changed files with 40 additions and 51 deletions

View File

@ -23,18 +23,12 @@
#include <stdexcept>
#define GET_SYMBOL_DB(code) \
errout.str(""); \
Tokenizer tokenizer(&settings, this); \
std::istringstream istr(code); \
tokenizer.tokenize(istr, "test.cpp"); \
const SymbolDatabase *db = tokenizer.getSymbolDatabase();
const SymbolDatabase *db = getSymbolDB_inner(tokenizer, code, "test.cpp");
#define GET_SYMBOL_DB_C(code) \
errout.str(""); \
Tokenizer tokenizer(&settings, this); \
std::istringstream istr(code); \
tokenizer.tokenize(istr, "test.c"); \
const SymbolDatabase *db = tokenizer.getSymbolDatabase();
const SymbolDatabase *db = getSymbolDB_inner(tokenizer, code, "test.c");
class TestSymbolDatabase: public TestFixture {
public:
@ -62,6 +56,13 @@ private:
found = false;
}
const SymbolDatabase* getSymbolDB_inner(Tokenizer& tokenizer, const char* code, const char* filename) {
errout.str("");
std::istringstream istr(code);
tokenizer.tokenize(istr, filename);
return tokenizer.getSymbolDatabase();
}
static const Scope *findFunctionScopeByToken(const SymbolDatabase * db, const Token *tok) {
std::list<Scope>::const_iterator scope;
@ -726,58 +727,49 @@ private:
}
void arrayMemberVar1() {
const char code[] = "struct Foo {\n"
" int x;\n"
"};\n"
"void f() {\n"
" struct Foo foo[10];\n"
" foo[1].x = 123;\n" // <- x should get a variable() pointer
"}";
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
GET_SYMBOL_DB("struct Foo {\n"
" int x;\n"
"};\n"
"void f() {\n"
" struct Foo foo[10];\n"
" foo[1].x = 123;\n" // <- x should get a variable() pointer
"}");
const Token *tok = Token::findsimplematch(tokenizer.tokens(), ". x");
tok = tok ? tok->next() : nullptr;
ASSERT(db != nullptr);
ASSERT(tok && tok->variable() && Token::simpleMatch(tok->variable()->typeStartToken(), "int x ;"));
ASSERT(tok && tok->varId() == 0U); // It's possible to set a varId
}
void arrayMemberVar2() {
const char code[] = "struct Foo {\n"
" int x;\n"
"};\n"
"void f() {\n"
" struct Foo foo[10][10];\n"
" foo[1][2].x = 123;\n" // <- x should get a variable() pointer
"}";
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
GET_SYMBOL_DB("struct Foo {\n"
" int x;\n"
"};\n"
"void f() {\n"
" struct Foo foo[10][10];\n"
" foo[1][2].x = 123;\n" // <- x should get a variable() pointer
"}");
const Token *tok = Token::findsimplematch(tokenizer.tokens(), ". x");
tok = tok ? tok->next() : nullptr;
ASSERT(db != nullptr);
ASSERT(tok && tok->variable() && Token::simpleMatch(tok->variable()->typeStartToken(), "int x ;"));
ASSERT(tok && tok->varId() == 0U); // It's possible to set a varId
}
void arrayMemberVar3() {
const char code[] = "struct Foo {\n"
" int x;\n"
"};\n"
"void f() {\n"
" struct Foo foo[10];\n"
" (foo[1]).x = 123;\n" // <- x should get a variable() pointer
"}";
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
GET_SYMBOL_DB("struct Foo {\n"
" int x;\n"
"};\n"
"void f() {\n"
" struct Foo foo[10];\n"
" (foo[1]).x = 123;\n" // <- x should get a variable() pointer
"}");
const Token *tok = Token::findsimplematch(tokenizer.tokens(), ". x");
tok = tok ? tok->next() : nullptr;
ASSERT(db != nullptr);
ASSERT(tok && tok->variable() && Token::simpleMatch(tok->variable()->typeStartToken(), "int x ;"));
ASSERT(tok && tok->varId() == 0U); // It's possible to set a varId
}
@ -1516,7 +1508,6 @@ private:
" catch (X::Error4 x) { }\n"
"}";
GET_SYMBOL_DB(str);
check(str, false);
ASSERT_EQUALS("", errout.str());
ASSERT(db && db->getVariableListSize() == 5); // index 0 + 4 variables
ASSERT(db && db->scopeList.size() == 7); // global + function + try + 4 catch
@ -1747,14 +1738,14 @@ private:
ASSERT_EQUALS("", errout.str());
}
// #ticket 3437 (segmentation fault)
// ticket 3437 (segmentation fault)
void symboldatabase22() {
check("template <class C> struct A {};\n"
"A<int> a;\n");
ASSERT_EQUALS("", errout.str());
}
// #ticket 3435 (std::vector)
// ticket 3435 (std::vector)
void symboldatabase23() {
GET_SYMBOL_DB("class A { std::vector<int*> ints; };");
ASSERT_EQUALS(2U, db->scopeList.size());
@ -1765,7 +1756,7 @@ private:
ASSERT_EQUALS(true, var.isClass());
}
// #ticket 3508 (constructor, destructor)
// ticket 3508 (constructor, destructor)
void symboldatabase24() {
GET_SYMBOL_DB("struct Fred {\n"
" ~Fred();\n"
@ -1807,30 +1798,28 @@ private:
}
// #ticket #3561 (throw C++)
// ticket #3561 (throw C++)
void symboldatabase25() {
const char str[] = "int main() {\n"
" foo bar;\n"
" throw bar;\n"
"}";
GET_SYMBOL_DB(str);
check(str, false);
ASSERT_EQUALS("", errout.str());
ASSERT(db && db->getVariableListSize() == 2); // index 0 + 1 variable
}
// #ticket #3561 (throw C)
// ticket #3561 (throw C)
void symboldatabase26() {
const char str[] = "int main() {\n"
" throw bar;\n"
"}";
GET_SYMBOL_DB_C(str);
check(str, false);
ASSERT_EQUALS("", errout.str());
ASSERT(db && db->getVariableListSize() == 2); // index 0 + 1 variable
}
// #ticket #3543 (segmentation fault)
// ticket #3543 (segmentation fault)
void symboldatabase27() {
check("class C : public B1\n"
"{\n"
@ -1846,7 +1835,7 @@ private:
ASSERT(db && db->getVariableFromVarId(1) && db->getVariableFromVarId(1)->typeScope() && db->getVariableFromVarId(1)->typeScope()->className == "S");
}
// #ticket #4442 (segmentation fault)
// ticket #4442 (segmentation fault)
void symboldatabase29() {
check("struct B : A {\n"
" B() : A {}\n"