Added ASSERT() to CBot unit tests

dev-time-step
krzys-h 2015-12-23 21:08:30 +01:00
parent b102f767d0
commit a18c2c39d9
1 changed files with 49 additions and 15 deletions

View File

@ -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);"
"}"
);
}