#ifndef POKEMONSCRIPTTESTER_TEST_HPP #define POKEMONSCRIPTTESTER_TEST_HPP #include #include "TestEnvironment.hpp" #include "TestResult.hpp" class Test { public: Test(const std::string name, asIScriptFunction* function) : _name(name), _function(function), _env(new TestEnvironment()) {} void Run(asIScriptContext* ctx) { ctx->PushState(); ctx->Prepare(_function); ctx->SetUserData(_env.get(), 684); auto e = ctx->Execute(); if (e == asEXECUTION_SUSPENDED) { auto s = ctx->GetState(); while (s != asEXECUTION_FINISHED && s != asEXECUTION_EXCEPTION && s != asEXECUTION_ABORTED && s != asEXECUTION_ERROR) { s = ctx->GetState(); continue; } } _env->CollectGarbage(); if (e == asEXECUTION_EXCEPTION) { std::stringstream error; const char* exceptionSection = ""; int exceptionColumn = 0; auto exceptionLine = ctx->GetExceptionLineNumber(&exceptionColumn, &exceptionSection); error << "[" << exceptionSection << ":" << exceptionLine << "," << exceptionColumn << "] " << ctx->GetExceptionString(); _errorMessage = error.str(); _result = TestResult::Failed; ctx->PopState(); return; } ctx->PopState(); _result = TestResult::Success; } inline TestResult GetResult() const noexcept { return _result; } inline const std::string& GetErrorMessage() const noexcept { return _errorMessage; } inline size_t GetTotalRequirements() const noexcept { return _env->TotalRequirements; } inline size_t GetFailedRequirements() const noexcept { return _env->FailedRequirements; } private: std::string _name; asIScriptFunction* _function; std::unique_ptr _env; TestResult _result = TestResult::NotRan; std::string _errorMessage = ""; }; #endif // POKEMONSCRIPTTESTER_TEST_HPP