[colobot] 182/377: Tests for CBotToken
Didier Raboud
odyx at moszumanska.debian.org
Wed Mar 30 13:34:13 UTC 2016
This is an automated email from the git hooks/post-receive script.
odyx pushed a commit to branch debian/master
in repository colobot.
commit d577e7f41ba6c93448acf8ee224722d62f10c3d7
Author: krzys-h <krzys_h at interia.pl>
Date: Wed Dec 23 18:44:14 2015 +0100
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(-)
diff --git a/src/CBot/CBotCall.cpp b/src/CBot/CBotCall.cpp
index 8784fab..d1e7232 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 65f304e..8fffb67 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 0000000..49a3416
--- /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 <gtest/gtest.h>
+
+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<TokenTest> 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 f12c3a2..37eed55 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
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-games/colobot.git
More information about the Pkg-games-commits
mailing list