From d577e7f41ba6c93448acf8ee224722d62f10c3d7 Mon Sep 17 00:00:00 2001 From: krzys-h Date: Wed, 23 Dec 2015 18:44:14 +0100 Subject: [PATCH] Tests for CBotToken --- src/CBot/CBotCall.cpp | 1 + src/CBot/CBotProgram.cpp | 2 +- test/unit/CBot/CBotToken_test.cpp | 121 ++++++++++++++++++++++++++++++ test/unit/CMakeLists.txt | 1 + 4 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 test/unit/CBot/CBotToken_test.cpp diff --git a/src/CBot/CBotCall.cpp b/src/CBot/CBotCall.cpp index 8784fabd..d1e7232c 100644 --- a/src/CBot/CBotCall.cpp +++ b/src/CBot/CBotCall.cpp @@ -62,6 +62,7 @@ CBotCall::~CBotCall() void CBotCall::Free() { delete CBotCall::m_ListCalls; + m_ListCalls = nullptr; } //////////////////////////////////////////////////////////////////////////////// diff --git a/src/CBot/CBotProgram.cpp b/src/CBot/CBotProgram.cpp index 65f304ed..8fffb671 100644 --- a/src/CBot/CBotProgram.cpp +++ b/src/CBot/CBotProgram.cpp @@ -429,7 +429,7 @@ void CBotProgram::Init() CBotProgram::DefineNum("CBotErrStackOver", CBotErrStackOver); // Stack overflow CBotProgram::DefineNum("CBotErrDeletedPtr", CBotErrDeletedPtr); // Attempted to use deleted object - CBotProgram::AddFunction("sizeof", rSizeOf, cSizeOf ); + CBotProgram::AddFunction("sizeof", rSizeOf, cSizeOf); InitStringFunctions(); InitMathFunctions(); diff --git a/test/unit/CBot/CBotToken_test.cpp b/test/unit/CBot/CBotToken_test.cpp new file mode 100644 index 00000000..49a3416d --- /dev/null +++ b/test/unit/CBot/CBotToken_test.cpp @@ -0,0 +1,121 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * 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://gnu.org/licenses + */ + +#include "CBot/CBotToken.h" +#include "CBot/CBotProgram.h" + +#include + +class CBotTokenUT : public testing::Test +{ +public: + void SetUp() + { + CBotProgram::Init(); + } + + void TearDown() + { + CBotProgram::Free(); + } + +protected: + struct TokenTest + { + std::string str; + int type; + }; + + void ExecuteTest(const std::string& code, std::vector data) + { + auto tokens = CBotToken::CompileTokens(code); + ASSERT_TRUE(tokens != nullptr); + CBotToken* token = tokens.get()->GetNext(); // TODO: why do we always have to skip the first one :/ + ASSERT_TRUE(token != nullptr); + unsigned int i = 0; + do { + ASSERT_LT(i, data.size()) << "too many tokens processed"; + + TokenTest correct = data[i]; + ASSERT_EQ(token->GetString(), correct.str) << "string mismatch at token #" << (i+1); + ASSERT_EQ(token->GetType(), correct.type) << "type mismatch at token #" << (i+1); + i++; + } + while((token = token->GetNext()) != nullptr); + ASSERT_EQ(i, data.size()) << "not enough tokens processed"; + } +}; + +TEST_F(CBotTokenUT, CodeExample) +{ + // this is the code example shown in the class documentation + ExecuteTest("\tint var = 3 * ( pos.y + x );\n\tstring test = \"Hello world\";", { + {"int", ID_INT}, + {"var", TokenTypVar}, + {"=", ID_ASS}, + {"3", TokenTypNum}, + {"*", ID_MUL}, + {"(", ID_OPENPAR}, + {"pos", TokenTypVar}, + {".", ID_DOT}, + {"y", TokenTypVar}, + {"+", ID_ADD}, + {"x", TokenTypVar}, + {")", ID_CLOSEPAR}, + {";", ID_SEP}, + {"string", ID_STRING}, + {"test", TokenTypVar}, + {"=", ID_ASS}, + {"\"Hello world\"", TokenTypString}, + {";", ID_SEP}, + }); +} + +TEST_F(CBotTokenUT, IgnoreComments) +{ + ExecuteTest("int /* comment*/x = 5; //comment", { + {"int", ID_INT}, + {"x", TokenTypVar}, + {"=", ID_ASS}, + {"5", TokenTypNum}, + {";", ID_SEP}, + }); +} + +TEST_F(CBotTokenUT, BasicProgram) +{ + ExecuteTest("extern void object::TestProgram()\n{\n\t\n\tmessage(\"test\"+2.0);\n\t\n}\n", { + {"extern", ID_EXTERN}, + {"void", ID_VOID}, + {"object", TokenTypVar}, + {"::", ID_DBLDOTS}, + {"TestProgram", TokenTypVar}, + {"(", ID_OPENPAR}, + {")", ID_CLOSEPAR}, + {"{", ID_OPBLK}, + {"message", TokenTypVar}, + {"(", ID_OPENPAR}, + {"\"test\"", TokenTypString}, + {"+", ID_ADD}, + {"2.0", TokenTypNum}, + {")", ID_CLOSEPAR}, + {";", ID_SEP}, + {"}", ID_CLBLK}, + }); +} \ No newline at end of file diff --git a/test/unit/CMakeLists.txt b/test/unit/CMakeLists.txt index f12c3a22..37eed553 100644 --- a/test/unit/CMakeLists.txt +++ b/test/unit/CMakeLists.txt @@ -9,6 +9,7 @@ endif() set(UT_SOURCES main.cpp app/app_test.cpp + CBot/CBotToken_test.cpp common/config_file_test.cpp graphics/engine/lightman_test.cpp math/func_test.cpp