Fixes several memory issues.
This commit is contained in:
parent
a6013437b2
commit
3488339d51
|
@ -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 = "";
|
||||||
|
|
|
@ -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);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -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) {
|
||||||
|
auto result = test.second->GetResult();
|
||||||
|
switch (result) {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (auto& test : _tests) {
|
std::cout << "Ran " << successfulTests + failedTests << " tests. Passed tests: " << successfulTests
|
||||||
if (test.second.GetResult() == TestResult::Failed) {
|
<< ". Failed tests: " << failedTests << "." << std::endl;
|
||||||
std::cout << "Test '" << test.first << "' failed with message: " << std::endl;
|
if (failedTests > 0) {
|
||||||
std::cout << test.second.GetErrorMessage() << std::endl << std::endl;
|
return 1;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -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;
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue