Fixes several memory issues.

This commit is contained in:
Deukhoofd 2021-08-22 19:29:00 +02:00
parent a6013437b2
commit 3488339d51
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
4 changed files with 51 additions and 18 deletions

View File

@ -11,16 +11,17 @@ public:
: _name(name), _function(function), _env(new TestEnvironment()) {} : _name(name), _function(function), _env(new TestEnvironment()) {}
void Run(asIScriptContext* ctx) { void Run(asIScriptContext* ctx) {
ctx->PushState();
ctx->Prepare(_function); ctx->Prepare(_function);
ctx->SetUserData(_env); ctx->SetUserData(_env.get());
auto e = ctx->Execute(); auto e = ctx->Execute();
if (e == asEXECUTION_EXCEPTION) { if (e == asEXECUTION_EXCEPTION) {
_errorMessage = ctx->GetExceptionString(); _errorMessage = ctx->GetExceptionString();
_result = TestResult::Failed; _result = TestResult::Failed;
ctx->Release(); ctx->PopState();
return; return;
} }
ctx->Release(); ctx->PopState();
Ensure(e == asEXECUTION_FINISHED); Ensure(e == asEXECUTION_FINISHED);
_result = TestResult::Success; _result = TestResult::Success;
} }
@ -31,7 +32,7 @@ public:
private: private:
std::string _name; std::string _name;
asIScriptFunction* _function; asIScriptFunction* _function;
TestEnvironment* _env; std::unique_ptr<TestEnvironment> _env;
TestResult _result = TestResult::NotRan; TestResult _result = TestResult::NotRan;
std::string _errorMessage = ""; std::string _errorMessage = "";

View File

@ -40,7 +40,7 @@ private:
eqMsg = ss.str(); \ eqMsg = ss.str(); \
} }
static bool AssertEquals(i32 expected, i32 actual) { static bool AssertEqualsI32(i32 expected, i32 actual) {
if (expected != actual) { if (expected != actual) {
auto data = GetAssertionData(); auto data = GetAssertionData();
GetEqualityMessage(expected, actual); GetEqualityMessage(expected, actual);
@ -49,12 +49,28 @@ private:
return true; return true;
} }
static bool AssertEqualsString(const std::string& expected, const std::string& actual) {
if (expected != actual) {
auto data = GetAssertionData();
std::string eqMsg;
{
std::ostringstream ss;
ss << "Expected: '" << expected << "', but was: '" << actual << "'";
eqMsg = ss.str();
}
throw AssertionFailed(data.FileName, data.Line, eqMsg);
}
return true;
}
public: public:
static void Register(AngelScriptResolver* scriptResolver) { static void Register(AngelScriptResolver* scriptResolver) {
auto engine = scriptResolver->GetBuilder().GetEngine(); auto engine = scriptResolver->GetBuilder().GetEngine();
Ensure(engine->RegisterGlobalFunction("bool Assert(bool expression)", asFUNCTION(Assert), asCALL_CDECL) >= 0); Ensure(engine->RegisterGlobalFunction("bool Assert(bool expression)", asFUNCTION(Assert), asCALL_CDECL) >= 0);
Ensure(engine->RegisterGlobalFunction("bool AssertEquals(int expected, int actual)", asFUNCTION(AssertEquals), Ensure(engine->RegisterGlobalFunction("bool AssertEquals(int expected, int actual)",
asCALL_CDECL) >= 0); asFUNCTION(AssertEqualsI32), asCALL_CDECL) >= 0);
Ensure(engine->RegisterGlobalFunction("bool AssertEquals(const string &in expected, const string &in actual)",
asFUNCTION(AssertEqualsString), asCALL_CDECL) >= 0);
} }
}; };

View File

@ -5,7 +5,7 @@
#include "Test.hpp" #include "Test.hpp"
class TestRunner { class TestRunner {
ArbUt::Dictionary<std::string, Test> _tests; ArbUt::Dictionary<std::string, std::unique_ptr<Test>> _tests;
public: public:
TestRunner(AngelScriptResolver* scriptResolver) { TestRunner(AngelScriptResolver* scriptResolver) {
@ -18,24 +18,42 @@ public:
auto meta = AngelscriptMetadata(m); auto meta = AngelscriptMetadata(m);
if (meta.GetIdentifier() == "Test"_cnc) { if (meta.GetIdentifier() == "Test"_cnc) {
auto name = meta.GetParameter("name"_cnc); auto name = meta.GetParameter("name"_cnc);
_tests.Insert(name, Test(name, func)); _tests.GetStdMap().insert({name, std::make_unique<Test>(name, func)});
} }
} }
} }
} }
void RunAll(asIScriptEngine* engine) { i32 RunAll(asIScriptEngine* engine) {
auto ctx = engine->CreateContext(); auto ctx = engine->CreateContext();
for (auto& test : _tests) { for (auto& test : _tests) {
test.second.Run(ctx); test.second->Run(ctx);
} }
ctx->Release();
size_t notRunTests = 0;
size_t successfulTests = 0;
size_t failedTests = 0;
for (auto& test : _tests) { for (auto& test : _tests) {
if (test.second.GetResult() == TestResult::Failed) { auto result = test.second->GetResult();
std::cout << "Test '" << test.first << "' failed with message: " << std::endl; switch (result) {
std::cout << test.second.GetErrorMessage() << std::endl << std::endl; case TestResult::NotRan: notRunTests += 1; break;
case TestResult::Success: successfulTests += 1; break;
case TestResult::Failed: {
std::cout << "Test '" << test.first << "' failed with message: " << std::endl;
std::cout << test.second->GetErrorMessage() << std::endl << std::endl;
failedTests += 1;
break;
}
} }
} }
std::cout << "Ran " << successfulTests + failedTests << " tests. Passed tests: " << successfulTests
<< ". Failed tests: " << failedTests << "." << std::endl;
if (failedTests > 0) {
return 1;
}
return 0;
} }
}; };

View File

@ -49,8 +49,6 @@ int main(int argc, char** argv) {
} }
auto* scriptResolver = dynamic_cast<AngelScriptResolver*>(battleLib.GetValue()->GetScriptResolver().get()); auto* scriptResolver = dynamic_cast<AngelScriptResolver*>(battleLib.GetValue()->GetScriptResolver().get());
auto testRunner = TestRunner(scriptResolver); auto testRunner = TestRunner(scriptResolver);
auto engine = scriptResolver->GetBuilder().GetEngine(); auto* engine = scriptResolver->GetBuilder().GetEngine();
testRunner.RunAll(engine); return testRunner.RunAll(engine);
return 0;
} }