More Item tests
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2020-01-22 11:29:14 +01:00
parent 22648e8617
commit 47db8464a2
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
2 changed files with 51 additions and 2 deletions

View File

@ -46,6 +46,13 @@ void RegisterItemTypes::RegisterBattleItemCategoryEnum(asIScriptEngine* engine)
assert(r >= 0); assert(r >= 0);
} }
// Hack to handle AngelScript not recognizing different sized enums on fields, and returning invalid values due to it.
#define ENUM__SIZE_WRAPPER(name, type, func) \
int32_t name(type* obj) { return static_cast<int32_t>(obj->func()); }
ENUM__SIZE_WRAPPER(Move_CategoryWrapper, PkmnLib::Library::Item, GetCategory)
ENUM__SIZE_WRAPPER(Move_BattleCategoryWrapper, PkmnLib::Library::Item, GetBattleCategory)
void RegisterItemTypes::RegisterItemType(asIScriptEngine* engine) { void RegisterItemTypes::RegisterItemType(asIScriptEngine* engine) {
[[maybe_unused]] int r = engine->RegisterObjectType("Item", 0, asOBJ_REF | asOBJ_NOCOUNT); [[maybe_unused]] int r = engine->RegisterObjectType("Item", 0, asOBJ_REF | asOBJ_NOCOUNT);
assert(r >= 0); assert(r >= 0);
@ -53,10 +60,10 @@ void RegisterItemTypes::RegisterItemType(asIScriptEngine* engine) {
asMETHOD(PkmnLib::Library::Item, GetName), asCALL_THISCALL); asMETHOD(PkmnLib::Library::Item, GetName), asCALL_THISCALL);
assert(r >= 0); assert(r >= 0);
r = engine->RegisterObjectMethod("Item", "ItemCategory get_Category() const property", r = engine->RegisterObjectMethod("Item", "ItemCategory get_Category() const property",
asMETHOD(PkmnLib::Library::Item, GetCategory), asCALL_THISCALL); asFUNCTION(Move_CategoryWrapper), asCALL_CDECL_OBJLAST);
assert(r >= 0); assert(r >= 0);
r = engine->RegisterObjectMethod("Item", "BattleItemCategory get_BattleCategory() const property", r = engine->RegisterObjectMethod("Item", "BattleItemCategory get_BattleCategory() const property",
asMETHOD(PkmnLib::Library::Item, GetBattleCategory), asCALL_THISCALL); asFUNCTION(Move_BattleCategoryWrapper), asCALL_CDECL_OBJLAST);
assert(r >= 0); assert(r >= 0);
r = engine->RegisterObjectMethod("Item", "int get_Price() const property", r = engine->RegisterObjectMethod("Item", "int get_Price() const property",
asMETHOD(PkmnLib::Library::Item, GetPrice), asCALL_THISCALL); asMETHOD(PkmnLib::Library::Item, GetPrice), asCALL_THISCALL);

View File

@ -7,6 +7,9 @@ static std::unordered_map<const char*, const char*> _scripts =
std::unordered_map<const char*, const char*>{{"testScript1", R"( std::unordered_map<const char*, const char*>{{"testScript1", R"(
class testScript1 { class testScript1 {
bool testName(const Item@ i, const string &in name){ return i.Name == name; } bool testName(const Item@ i, const string &in name){ return i.Name == name; }
bool testCategory(const Item@ i, ItemCategory category){ return i.Category == category; }
bool testBattleCategory(const Item@ i, BattleItemCategory category){ return i.BattleCategory == category; }
bool testPrice(const Item@ i, int price){ return i.Price == price; }
} }
)"}}; )"}};
@ -55,4 +58,43 @@ TEST_CASE("Validate Item Name in Script") {
REQUIRE((bool)data.Context->GetReturnWord()); REQUIRE((bool)data.Context->GetReturnWord());
} }
TEST_CASE("Validate Item Category in Script") {
auto mainLib = TestLibrary::GetLibrary();
auto data = GetScript(mainLib, "testCategory");
auto item = mainLib->GetItemLibrary()->GetItem("testItem");
data.Context->SetArgObject(0, const_cast<PkmnLib::Library::Item*>(item));
data.Context->SetArgDWord(1, static_cast<int32_t >(item->GetCategory()));
REQUIRE(data.Context->Execute() == asEXECUTION_FINISHED);
REQUIRE((bool)data.Context->GetReturnWord());
}
TEST_CASE("Validate Item Battle Category in Script") {
auto mainLib = TestLibrary::GetLibrary();
auto data = GetScript(mainLib, "testBattleCategory");
auto item = mainLib->GetItemLibrary()->GetItem("testItem");
data.Context->SetArgObject(0, const_cast<PkmnLib::Library::Item*>(item));
data.Context->SetArgDWord(1, static_cast<int32_t >(item->GetBattleCategory()));
REQUIRE(data.Context->Execute() == asEXECUTION_FINISHED);
REQUIRE((bool)data.Context->GetReturnWord());
}
TEST_CASE("Validate Item Price in Script") {
auto mainLib = TestLibrary::GetLibrary();
auto data = GetScript(mainLib, "testPrice");
auto item = mainLib->GetItemLibrary()->GetItem("testItem");
data.Context->SetArgObject(0, const_cast<PkmnLib::Library::Item*>(item));
data.Context->SetArgDWord(1, static_cast<int32_t >(item->GetPrice()));
REQUIRE(data.Context->Execute() == asEXECUTION_FINISHED);
REQUIRE((bool)data.Context->GetReturnWord());
}
#endif #endif