[colobot] 184/377: Compile error testing in CBot

Didier Raboud odyx at moszumanska.debian.org
Wed Mar 30 13:34:14 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 b102f767d0bdf61b61fc7e10a6282ceb5c26798c
Author: krzys-h <krzys_h at interia.pl>
Date:   Wed Dec 23 20:54:35 2015 +0100

    Compile error testing in CBot
---
 test/unit/CBot/CBot.cpp | 143 +++++++++++++++++++++++++++++++++---------------
 1 file changed, 99 insertions(+), 44 deletions(-)

diff --git a/test/unit/CBot/CBot.cpp b/test/unit/CBot/CBot.cpp
index ff5bc46..e908a78 100644
--- a/test/unit/CBot/CBot.cpp
+++ b/test/unit/CBot/CBot.cpp
@@ -21,59 +21,59 @@
 
 #include <gtest/gtest.h>
 
-class CBotTestFail : public std::runtime_error {
+class CBotUT : public testing::Test
+{
 public:
-    CBotTestFail(const std::string& message) : runtime_error(message)
+    void SetUp()
     {
+        CBotProgram::Init();
+        CBotProgram::AddFunction("FAIL", rFail, cFail);
     }
 
-    CBotTestFail(std::string message, int cursor1, int cursor2) : CBotTestFail(message)
+    void TearDown()
     {
-        this->cursor1 = cursor1;
-        this->cursor2 = cursor2;
+        CBotProgram::Free();
     }
 
-    int cursor1 = -1;
-    int cursor2 = -1;
-};
-
-CBotTypResult cFail(CBotVar* &var, void* user)
-{
-    if (var != nullptr)
-    {
-        if (var->GetType() != CBotTypString) return CBotTypResult(CBotErrBadString);
-        var = var->GetNext();
-    }
-    if (var != nullptr) return CBotTypResult(CBotErrOverParam);
-    return CBotTypResult(CBotTypVoid);
-}
+private:
+    class CBotTestFail : public std::runtime_error {
+    public:
+        CBotTestFail(const std::string& message) : runtime_error(message)
+        {
+        }
 
-bool rFail(CBotVar* var, CBotVar* result, int& exception, void* user)
-{
-    std::string message = "CBot test failed";
-    if (var != nullptr)
-    {
-        message = var->GetValString();
-    }
+        CBotTestFail(std::string message, int cursor1, int cursor2) : CBotTestFail(message)
+        {
+            this->cursor1 = cursor1;
+            this->cursor2 = cursor2;
+        }
 
-    throw CBotTestFail(message);
-}
+        int cursor1 = -1;
+        int cursor2 = -1;
+    };
 
-class CBotUT : public testing::Test
-{
-public:
-    void SetUp()
+    static CBotTypResult cFail(CBotVar* &var, void* user)
     {
-        CBotProgram::Init();
-        CBotProgram::AddFunction("FAIL", rFail, cFail);
+        if (var != nullptr)
+        {
+            if (var->GetType() != CBotTypString) return CBotTypResult(CBotErrBadString);
+            var = var->GetNext();
+        }
+        if (var != nullptr) return CBotTypResult(CBotErrOverParam);
+        return CBotTypResult(CBotTypVoid);
     }
 
-    void TearDown()
+    static bool rFail(CBotVar* var, CBotVar* result, int& exception, void* user)
     {
-        CBotProgram::Free();
+        std::string message = "CBot test failed";
+        if (var != nullptr)
+        {
+            message = var->GetValString();
+        }
+
+        throw CBotTestFail(message);
     }
 
-protected:
     // Modified version of PutList from src/script/script.cpp
     // Should be probably moved somewhere into the CBot library
     void PrintVars(std::stringstream& ss, CBotVar* var, const std::string& baseName = "", bool bArray = false)
@@ -136,18 +136,32 @@ protected:
         }
     }
 
+protected:
     void ExecuteTest(const std::string& code, CBotError expectedError = CBotNoErr)
     {
+        CBotError expectedCompileError = expectedError < 6000 ? expectedError : CBotNoErr;
+        CBotError expectedRuntimeError = expectedError >= 6000 ? expectedError : CBotNoErr;
+
         auto program = std::unique_ptr<CBotProgram>(new CBotProgram());
         std::vector<std::string> tests;
         program->Compile(code, tests);
 
         CBotError error;
         int cursor1, cursor2;
-        if (program->GetError(error, cursor1, cursor2))
+        program->GetError(error, cursor1, cursor2);
+        if (error != expectedCompileError)
         {
-            FAIL() << "Compile error - " << error << " (" << cursor1 << "-" << cursor2 << ")"; // TODO: Error messages are on Colobot side
+            std::stringstream ss;
+            if (error != CBotNoErr)
+            {
+                FAIL() << "Compile error - " << error << " (" << cursor1 << "-" << cursor2 << ")"; // TODO: Error messages are on Colobot side
+            }
+            else
+            {
+                FAIL() << "No compile error, expected " << expectedCompileError; // TODO: Error messages are on Colobot side
+            }
         }
+        if (expectedCompileError != CBotNoErr) return;
 
         for (const std::string& test : tests)
         {
@@ -156,7 +170,7 @@ protected:
                 program->Start(test);
                 while (!program->Run());
                 program->GetError(error, cursor1, cursor2);
-                if (error != expectedError)
+                if (error != expectedRuntimeError)
                 {
                     std::stringstream ss;
                     if (error != CBotNoErr)
@@ -165,7 +179,7 @@ protected:
                     }
                     else
                     {
-                        ss << "No runtime error, expected " << expectedError; // TODO: Error messages are on Colobot side
+                        ss << "No runtime error, expected " << expectedRuntimeError; // TODO: Error messages are on Colobot side
                         cursor1 = cursor2 = -1;
                     }
                     throw CBotTestFail(ss.str(), cursor1, cursor2);
@@ -207,15 +221,56 @@ protected:
 
 TEST_F(CBotUT, Test)
 {
-    ExecuteTest("extern void EmptyTest() { }");
+    ExecuteTest(
+        "extern void EmptyTest()"
+        "{"
+        "}"
+    );
 }
 
 TEST_F(CBotUT, DISABLED_TestFail)
 {
-    ExecuteTest("extern void FailingTest() { FAIL(); } extern void AnotherFailingTest() { FAIL(\"This is a message\"); }");
+    ExecuteTest(
+        "extern void FailingTest()"
+        "{"
+        "    FAIL();"
+        "}"
+        "extern void AnotherFailingTest()"
+        "{"
+        "    FAIL(\"This is a message\");"
+        "}"
+    );
 }
 
 TEST_F(CBotUT, DivideByZero)
 {
-    ExecuteTest("extern void DivideByZero() { float a = 5/0; }", CBotErrZeroDiv);
+    ExecuteTest(
+        "extern void DivideByZero()"
+        "{"
+        "    float a = 5/0;"
+        "}",
+        CBotErrZeroDiv
+    );
+}
+
+TEST_F(CBotUT, MissingSemicolon)
+{
+    ExecuteTest(
+        "extern void MissingSemicolon()"
+        "{"
+        "    string a = \"hello\""
+        "}",
+        CBotErrNoTerminator
+    );
 }
+
+TEST_F(CBotUT, UndefinedFunction)
+{
+    ExecuteTest(
+        "extern void UndefinedFunction()"
+        "{"
+        "    foo();"
+        "}",
+        CBotErrUndefCall
+    );
+}
\ No newline at end of file

-- 
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