From a18c2c39d9af0a417ed3ccd12231083d2f0f1cff Mon Sep 17 00:00:00 2001 From: krzys-h Date: Wed, 23 Dec 2015 21:08:30 +0100 Subject: [PATCH] Added ASSERT() to CBot unit tests --- test/unit/CBot/CBot.cpp | 64 +++++++++++++++++++++++++++++++---------- 1 file changed, 49 insertions(+), 15 deletions(-) diff --git a/test/unit/CBot/CBot.cpp b/test/unit/CBot/CBot.cpp index e908a78a..6ebbb138 100644 --- a/test/unit/CBot/CBot.cpp +++ b/test/unit/CBot/CBot.cpp @@ -28,6 +28,7 @@ public: { CBotProgram::Init(); CBotProgram::AddFunction("FAIL", rFail, cFail); + CBotProgram::AddFunction("ASSERT", rAssert, cAssert); } void TearDown() @@ -74,6 +75,24 @@ private: throw CBotTestFail(message); } + static CBotTypResult cAssert(CBotVar* &var, void* user) + { + if (var == nullptr) return CBotTypResult(CBotErrLowParam); + if (var->GetType() != CBotTypBoolean) return CBotTypResult(CBotErrBadString); + var = var->GetNext(); + return CBotTypResult(CBotTypVoid); + } + + static bool rAssert(CBotVar* var, CBotVar* result, int& exception, void* user) + { + bool status = var->GetValInt(); + if (!status) + { + throw CBotTestFail("CBot assertion failed"); + } + return true; + } + // 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) @@ -219,7 +238,7 @@ protected: } }; -TEST_F(CBotUT, Test) +TEST_F(CBotUT, EmptyTest) { ExecuteTest( "extern void EmptyTest()" @@ -228,20 +247,6 @@ TEST_F(CBotUT, Test) ); } -TEST_F(CBotUT, DISABLED_TestFail) -{ - ExecuteTest( - "extern void FailingTest()" - "{" - " FAIL();" - "}" - "extern void AnotherFailingTest()" - "{" - " FAIL(\"This is a message\");" - "}" - ); -} - TEST_F(CBotUT, DivideByZero) { ExecuteTest( @@ -273,4 +278,33 @@ TEST_F(CBotUT, UndefinedFunction) "}", CBotErrUndefCall ); +} + +TEST_F(CBotUT, BasicOperations) +{ + ExecuteTest( + "extern void Comparations()" + "{" + " ASSERT(true);" + " ASSERT(!false);" + " ASSERT(1 != 0);" + " ASSERT(1 == 1);" + " ASSERT(1 > 0);" + " ASSERT(1 >= 0);" + " ASSERT(1 >= 1);" + " ASSERT(0 < 1);" + " ASSERT(0 <= 1);" + " ASSERT(1 <= 1);" + "}" + "" + "extern void BasicMath()" + "{" + " ASSERT(2+2 == 4);" + " ASSERT(4-2 == 2);" + " ASSERT(2*2 == 4);" + " ASSERT(2/2 == 1);" + " ASSERT(5%2 == 1);" + " ASSERT(5**3 == 125);" + "}" + ); } \ No newline at end of file