2010-12-30 09:46:44 +01:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
|
|
|
* Copyright (C) 2007-2010 Daniel Marjamäki and Cppcheck team.
|
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "testsuite.h"
|
|
|
|
#include "testutils.h"
|
|
|
|
#define private public
|
|
|
|
#include "symboldatabase.h"
|
|
|
|
|
|
|
|
class TestSymbolDatabase: public TestFixture
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
TestSymbolDatabase()
|
|
|
|
:TestFixture("TestSymbolDatabase")
|
|
|
|
,si(NULL, NULL, NULL)
|
|
|
|
,vartok(NULL)
|
2010-12-30 22:57:43 +01:00
|
|
|
,typetok(NULL)
|
2010-12-30 09:46:44 +01:00
|
|
|
{}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const SymbolDatabase::SpaceInfo si;
|
|
|
|
const Token* vartok;
|
2010-12-30 22:57:43 +01:00
|
|
|
const Token* typetok;
|
|
|
|
|
|
|
|
void reset()
|
|
|
|
{
|
|
|
|
vartok = NULL;
|
|
|
|
typetok = NULL;
|
|
|
|
}
|
2010-12-30 09:46:44 +01:00
|
|
|
|
|
|
|
void run()
|
|
|
|
{
|
|
|
|
TEST_CASE(test_isVariableDeclarationCanHandleNull);
|
|
|
|
TEST_CASE(test_isVariableDeclarationIdentifiesSimpleDeclaration);
|
|
|
|
TEST_CASE(test_isVariableDeclarationIdentifiesScopedDeclaration);
|
|
|
|
TEST_CASE(test_isVariableDeclarationIdentifiesStdDeclaration);
|
|
|
|
TEST_CASE(test_isVariableDeclarationIdentifiesScopedStdDeclaration);
|
|
|
|
TEST_CASE(test_isVariableDeclarationIdentifiesManyScopes);
|
2010-12-30 22:57:43 +01:00
|
|
|
TEST_CASE(test_isVariableDeclarationIdentifiesPointers);
|
2010-12-30 09:46:44 +01:00
|
|
|
TEST_CASE(test_isVariableDeclarationDoesNotIdentifyConstness);
|
|
|
|
TEST_CASE(test_isVariableDeclarationIdentifiesFirstOfManyVariables);
|
2010-12-30 22:57:43 +01:00
|
|
|
TEST_CASE(test_isVariableDeclarationIdentifiesScopedPointerDeclaration);
|
|
|
|
TEST_CASE(test_isVariableDeclarationIdentifiesDeclarationWithIndirection);
|
|
|
|
TEST_CASE(test_isVariableDeclarationIdentifiesDeclarationWithMultipleIndirection);
|
|
|
|
|
2010-12-30 09:46:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_isVariableDeclarationCanHandleNull()
|
|
|
|
{
|
2010-12-30 22:57:43 +01:00
|
|
|
reset();
|
|
|
|
bool result = si.isVariableDeclaration(NULL, vartok, typetok);
|
2010-12-30 09:46:44 +01:00
|
|
|
ASSERT_EQUALS(false, result);
|
|
|
|
ASSERT(NULL == vartok);
|
2010-12-30 22:57:43 +01:00
|
|
|
ASSERT(NULL == typetok);
|
2010-12-30 09:46:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_isVariableDeclarationIdentifiesSimpleDeclaration()
|
|
|
|
{
|
2010-12-30 22:57:43 +01:00
|
|
|
reset();
|
2010-12-30 09:46:44 +01:00
|
|
|
givenACodeSampleToTokenize simpleDeclaration("int x;");
|
2010-12-30 22:57:43 +01:00
|
|
|
bool result = si.isVariableDeclaration(simpleDeclaration.tokens(), vartok, typetok);
|
2010-12-30 09:46:44 +01:00
|
|
|
ASSERT_EQUALS(true, result);
|
|
|
|
ASSERT_EQUALS("x", vartok->str());
|
2010-12-30 22:57:43 +01:00
|
|
|
ASSERT_EQUALS("int", typetok->str());
|
2010-12-30 09:46:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_isVariableDeclarationIdentifiesScopedDeclaration()
|
|
|
|
{
|
2010-12-30 22:57:43 +01:00
|
|
|
reset();
|
2010-12-30 09:46:44 +01:00
|
|
|
givenACodeSampleToTokenize ScopedDeclaration("::int x;");
|
2010-12-30 22:57:43 +01:00
|
|
|
bool result = si.isVariableDeclaration(ScopedDeclaration.tokens(), vartok, typetok);
|
2010-12-30 09:46:44 +01:00
|
|
|
ASSERT_EQUALS(true, result);
|
|
|
|
ASSERT_EQUALS("x", vartok->str());
|
2010-12-30 22:57:43 +01:00
|
|
|
ASSERT_EQUALS("int", typetok->str());
|
2010-12-30 09:46:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_isVariableDeclarationIdentifiesStdDeclaration()
|
|
|
|
{
|
2010-12-30 22:57:43 +01:00
|
|
|
reset();
|
2010-12-30 09:46:44 +01:00
|
|
|
givenACodeSampleToTokenize StdDeclaration("std::string x;");
|
2010-12-30 22:57:43 +01:00
|
|
|
bool result = si.isVariableDeclaration(StdDeclaration.tokens(), vartok, typetok);
|
2010-12-30 09:46:44 +01:00
|
|
|
ASSERT_EQUALS(true, result);
|
|
|
|
ASSERT_EQUALS("x", vartok->str());
|
2010-12-30 22:57:43 +01:00
|
|
|
ASSERT_EQUALS("string", typetok->str());
|
2010-12-30 09:46:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_isVariableDeclarationIdentifiesScopedStdDeclaration()
|
|
|
|
{
|
2010-12-30 22:57:43 +01:00
|
|
|
reset();
|
2010-12-30 09:46:44 +01:00
|
|
|
givenACodeSampleToTokenize StdDeclaration("::std::string x;");
|
2010-12-30 22:57:43 +01:00
|
|
|
bool result = si.isVariableDeclaration(StdDeclaration.tokens(), vartok, typetok);
|
2010-12-30 09:46:44 +01:00
|
|
|
ASSERT_EQUALS(true, result);
|
|
|
|
ASSERT_EQUALS("x", vartok->str());
|
2010-12-30 22:57:43 +01:00
|
|
|
ASSERT_EQUALS("string", typetok->str());
|
2010-12-30 09:46:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_isVariableDeclarationIdentifiesManyScopes()
|
|
|
|
{
|
2010-12-30 22:57:43 +01:00
|
|
|
reset();
|
2010-12-30 09:46:44 +01:00
|
|
|
givenACodeSampleToTokenize manyScopes("AA::BB::CC::DD::EE x;");
|
2010-12-30 22:57:43 +01:00
|
|
|
bool result = si.isVariableDeclaration(manyScopes.tokens(), vartok, typetok);
|
2010-12-30 09:46:44 +01:00
|
|
|
ASSERT_EQUALS(true, result);
|
|
|
|
ASSERT_EQUALS("x", vartok->str());
|
2010-12-30 22:57:43 +01:00
|
|
|
ASSERT_EQUALS("EE", typetok->str());
|
2010-12-30 09:46:44 +01:00
|
|
|
}
|
|
|
|
|
2010-12-30 22:57:43 +01:00
|
|
|
void test_isVariableDeclarationIdentifiesPointers()
|
2010-12-30 09:46:44 +01:00
|
|
|
{
|
2010-12-30 22:57:43 +01:00
|
|
|
reset();
|
2010-12-30 09:46:44 +01:00
|
|
|
givenACodeSampleToTokenize pointer("int* p;");
|
2010-12-30 22:57:43 +01:00
|
|
|
bool result = si.isVariableDeclaration(pointer.tokens(), vartok, typetok);
|
|
|
|
ASSERT_EQUALS(true, result);
|
|
|
|
ASSERT_EQUALS("p", vartok->str());
|
|
|
|
ASSERT_EQUALS("int", typetok->str());
|
2010-12-30 09:46:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_isVariableDeclarationDoesNotIdentifyConstness()
|
|
|
|
{
|
2010-12-30 22:57:43 +01:00
|
|
|
reset();
|
2010-12-30 09:46:44 +01:00
|
|
|
givenACodeSampleToTokenize constness("const int* cp;");
|
2010-12-30 22:57:43 +01:00
|
|
|
bool result = si.isVariableDeclaration(constness.tokens(), vartok, typetok);
|
2010-12-30 09:46:44 +01:00
|
|
|
ASSERT_EQUALS(false, result);
|
|
|
|
ASSERT(NULL == vartok);
|
2010-12-30 22:57:43 +01:00
|
|
|
ASSERT(NULL == typetok);
|
2010-12-30 09:46:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_isVariableDeclarationIdentifiesFirstOfManyVariables()
|
|
|
|
{
|
2010-12-30 22:57:43 +01:00
|
|
|
reset();
|
2010-12-30 09:46:44 +01:00
|
|
|
givenACodeSampleToTokenize multipleDeclaration("int first, second;");
|
2010-12-30 22:57:43 +01:00
|
|
|
bool result = si.isVariableDeclaration(multipleDeclaration.tokens(), vartok, typetok);
|
2010-12-30 09:46:44 +01:00
|
|
|
ASSERT_EQUALS(true, result);
|
|
|
|
ASSERT_EQUALS("first", vartok->str());
|
2010-12-30 22:57:43 +01:00
|
|
|
ASSERT_EQUALS("int", typetok->str());
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_isVariableDeclarationIdentifiesScopedPointerDeclaration()
|
|
|
|
{
|
|
|
|
reset();
|
|
|
|
givenACodeSampleToTokenize manyScopes("AA::BB::CC::DD::EE* p;");
|
|
|
|
bool result = si.isVariableDeclaration(manyScopes.tokens(), vartok, typetok);
|
|
|
|
ASSERT_EQUALS(true, result);
|
|
|
|
ASSERT_EQUALS("p", vartok->str());
|
|
|
|
ASSERT_EQUALS("EE", typetok->str());
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_isVariableDeclarationIdentifiesDeclarationWithIndirection()
|
|
|
|
{
|
|
|
|
reset();
|
|
|
|
givenACodeSampleToTokenize pointerToPointer("int** pp;");
|
|
|
|
bool result = si.isVariableDeclaration(pointerToPointer.tokens(), vartok, typetok);
|
|
|
|
ASSERT_EQUALS(true, result);
|
|
|
|
ASSERT_EQUALS("pp", vartok->str());
|
|
|
|
ASSERT_EQUALS("int", typetok->str());
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_isVariableDeclarationIdentifiesDeclarationWithMultipleIndirection()
|
|
|
|
{
|
|
|
|
reset();
|
|
|
|
givenACodeSampleToTokenize pointerToPointer("int***** p;");
|
|
|
|
bool result = si.isVariableDeclaration(pointerToPointer.tokens(), vartok, typetok);
|
|
|
|
ASSERT_EQUALS(true, result);
|
|
|
|
ASSERT_EQUALS("p", vartok->str());
|
|
|
|
ASSERT_EQUALS("int", typetok->str());
|
2010-12-30 09:46:44 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
REGISTER_TEST(TestSymbolDatabase)
|
|
|
|
|