test: lua is_instance -> get_class, fixed implementation issues
Dieser Commit ist enthalten in:
Ursprung
21f2d6528f
Commit
a072bdef76
@ -133,6 +133,7 @@ inline static void registerValue(lua_State* L, std::string_view key)
|
||||
|
||||
// add to global class (used by Class::push)
|
||||
lua_getglobal(L, metaTableName);
|
||||
Lua::push(L, T::classId);
|
||||
*reinterpret_cast<IsInstance*>(lua_newuserdata(L, sizeof(IsInstance))) = isInstance<T>;
|
||||
if(luaL_newmetatable(L, metaTableName))
|
||||
{
|
||||
@ -140,7 +141,7 @@ inline static void registerValue(lua_State* L, std::string_view key)
|
||||
lua_setfield(L, -2, "__tostring");
|
||||
}
|
||||
lua_setmetatable(L, -2);
|
||||
lua_rawsetp(L, -2, T::classId.data());
|
||||
lua_rawset(L, -3);
|
||||
lua_pop(L, 1);
|
||||
|
||||
// add to sandbox class global:
|
||||
@ -229,7 +230,8 @@ void Class::push(lua_State* L, std::string_view classId)
|
||||
{
|
||||
lua_getglobal(L, metaTableName);
|
||||
assert(lua_istable(L, -1));
|
||||
lua_rawgetp(L, -1, classId.data());
|
||||
Lua::push(L, classId);
|
||||
lua_rawget(L, -2);
|
||||
assert(lua_isuserdata(L, -1));
|
||||
lua_insert(L, lua_gettop(L) - 1);
|
||||
lua_pop(L, 1);
|
||||
|
||||
132
server/test/lua/getclass.cpp
Normale Datei
132
server/test/lua/getclass.cpp
Normale Datei
@ -0,0 +1,132 @@
|
||||
/**
|
||||
* server/test/lua/getclass.cpp
|
||||
*
|
||||
* This file is part of the traintastic test suite.
|
||||
*
|
||||
* Copyright (C) 2021 Reinder Feenstra
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#include <catch2/catch.hpp>
|
||||
#include "../../src/lua/class.hpp"
|
||||
#include "../../src/lua/object.hpp"
|
||||
#include "../../src/world/world.hpp"
|
||||
|
||||
TEST_CASE("Lua get_class", "[lua][lua-get_class]")
|
||||
{
|
||||
static const char* get_class = "get_class";
|
||||
static const char* WORLD = "WORLD";
|
||||
static const char* DECODER = "DECODER";
|
||||
|
||||
lua_State* L = luaL_newstate();
|
||||
|
||||
// add in_instance function
|
||||
lua_pushcfunction(L, Lua::Class::getClass);
|
||||
lua_setglobal(L, get_class);
|
||||
|
||||
// add classes
|
||||
lua_newtable(L);
|
||||
Lua::Class::registerValues(L);
|
||||
|
||||
// register object type
|
||||
Lua::Object::registerType(L);
|
||||
|
||||
// nil
|
||||
lua_getglobal(L, get_class);
|
||||
lua_pushnil(L);
|
||||
REQUIRE(lua_pcall(L, 1, 1, 0) == LUA_ERRRUN);
|
||||
lua_pop(L, 1);
|
||||
|
||||
// boolean: false
|
||||
lua_getglobal(L, get_class);
|
||||
lua_pushboolean(L, 0);
|
||||
REQUIRE(lua_pcall(L, 1, 1, 0) == LUA_ERRRUN);
|
||||
lua_pop(L, 1);
|
||||
|
||||
// boolean: true
|
||||
lua_getglobal(L, get_class);
|
||||
lua_pushboolean(L, 1);
|
||||
REQUIRE(lua_pcall(L, 1, 1, 0) == LUA_ERRRUN);
|
||||
lua_pop(L, 1);
|
||||
|
||||
// integer: 42
|
||||
lua_getglobal(L, get_class);
|
||||
lua_pushinteger(L, 42);
|
||||
REQUIRE(lua_pcall(L, 1, 1, 0) == LUA_ERRRUN);
|
||||
lua_pop(L, 1);
|
||||
|
||||
// number: 3.14
|
||||
lua_getglobal(L, get_class);
|
||||
lua_pushnumber(L, 3.14);
|
||||
REQUIRE(lua_pcall(L, 1, 1, 0) == LUA_ERRRUN);
|
||||
lua_pop(L, 1);
|
||||
|
||||
// string
|
||||
lua_getglobal(L, get_class);
|
||||
lua_pushstring(L, "traintastic");
|
||||
REQUIRE(lua_pcall(L, 1, 1, 0) == LUA_ERRRUN);
|
||||
lua_pop(L, 1);
|
||||
|
||||
// table
|
||||
lua_getglobal(L, get_class);
|
||||
lua_newtable(L);
|
||||
REQUIRE(lua_pcall(L, 1, 1, 0) == LUA_ERRRUN);
|
||||
lua_pop(L, 1);
|
||||
|
||||
// userdata
|
||||
lua_getglobal(L, get_class);
|
||||
lua_newuserdata(L, sizeof(void*));
|
||||
REQUIRE(lua_pcall(L, 1, 1, 0) == LUA_ERRRUN);
|
||||
lua_pop(L, 1);
|
||||
|
||||
// World
|
||||
auto world = World::create();
|
||||
lua_getglobal(L, get_class);
|
||||
Lua::Object::push(L, world);
|
||||
REQUIRE(lua_pcall(L, 1, 1, 0) == LUA_OK);
|
||||
REQUIRE(lua_isuserdata(L, -1));
|
||||
lua_getfield(L, -2, WORLD);
|
||||
REQUIRE(lua_rawequal(L, -1, -2) == 1);
|
||||
lua_pop(L, 2);
|
||||
|
||||
lua_getglobal(L, get_class);
|
||||
Lua::Object::push(L, world);
|
||||
REQUIRE(lua_pcall(L, 1, 1, 0) == LUA_OK);
|
||||
REQUIRE(lua_isuserdata(L, -1));
|
||||
lua_getfield(L, -2, DECODER);
|
||||
REQUIRE(lua_rawequal(L, -1, -2) == 0);
|
||||
lua_pop(L, 2);
|
||||
|
||||
// Decoder
|
||||
auto decoder = world->decoders->add();
|
||||
lua_getglobal(L, get_class);
|
||||
Lua::Object::push(L, decoder);
|
||||
REQUIRE(lua_pcall(L, 1, 1, 0) == LUA_OK);
|
||||
REQUIRE(lua_isuserdata(L, -1));
|
||||
lua_getfield(L, -2, WORLD);
|
||||
REQUIRE(lua_rawequal(L, -1, -2) == 0);
|
||||
lua_pop(L, 2);
|
||||
|
||||
lua_getglobal(L, get_class);
|
||||
Lua::Object::push(L, decoder);
|
||||
REQUIRE(lua_pcall(L, 1, 1, 0) == LUA_OK);
|
||||
REQUIRE(lua_isuserdata(L, -1));
|
||||
lua_getfield(L, -2, DECODER);
|
||||
REQUIRE(lua_rawequal(L, -1, -2) == 1);
|
||||
lua_pop(L, 2);
|
||||
|
||||
lua_close(L);
|
||||
}
|
||||
@ -1,156 +0,0 @@
|
||||
/**
|
||||
* server/test/lua/isinstance.cpp
|
||||
*
|
||||
* This file is part of the traintastic test suite.
|
||||
*
|
||||
* Copyright (C) 2021 Reinder Feenstra
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#include <catch2/catch.hpp>
|
||||
#include "../../src/lua/class.hpp"
|
||||
#include "../../src/lua/object.hpp"
|
||||
#include "../../src/world/world.hpp"
|
||||
|
||||
TEST_CASE("Lua is_instance", "[lua][lua-isinstance]")
|
||||
{
|
||||
static const char* is_instance = "is_instance";
|
||||
static const char* WORLD = "WORLD";
|
||||
static const char* DECODER = "DECODER";
|
||||
|
||||
lua_State* L = luaL_newstate();
|
||||
|
||||
// add in_instance function
|
||||
lua_pushcfunction(L, Lua::Class::isInstance);
|
||||
lua_setglobal(L, is_instance);
|
||||
|
||||
// add classes
|
||||
lua_newtable(L);
|
||||
Lua::Class::registerValues(L);
|
||||
|
||||
// register object type
|
||||
Lua::Object::registerType(L);
|
||||
|
||||
// nil
|
||||
lua_getglobal(L, is_instance);
|
||||
lua_pushnil(L);
|
||||
lua_getfield(L, -3, WORLD);
|
||||
REQUIRE(lua_pcall(L, 2, 1, 0) == LUA_OK);
|
||||
REQUIRE(lua_isboolean(L, -1));
|
||||
REQUIRE_FALSE(lua_toboolean(L, -1));
|
||||
lua_pop(L, 1);
|
||||
|
||||
// boolean: false
|
||||
lua_getglobal(L, is_instance);
|
||||
lua_pushboolean(L, 0);
|
||||
lua_getfield(L, -3, WORLD);
|
||||
REQUIRE(lua_pcall(L, 2, 1, 0) == LUA_OK);
|
||||
REQUIRE(lua_isboolean(L, -1));
|
||||
REQUIRE_FALSE(lua_toboolean(L, -1));
|
||||
lua_pop(L, 1);
|
||||
|
||||
// boolean: true
|
||||
lua_getglobal(L, is_instance);
|
||||
lua_pushboolean(L, 1);
|
||||
lua_getfield(L, -3, WORLD);
|
||||
REQUIRE(lua_pcall(L, 2, 1, 0) == LUA_OK);
|
||||
REQUIRE(lua_isboolean(L, -1));
|
||||
REQUIRE_FALSE(lua_toboolean(L, -1));
|
||||
lua_pop(L, 1);
|
||||
|
||||
// integer: 42
|
||||
lua_getglobal(L, is_instance);
|
||||
lua_pushinteger(L, 42);
|
||||
lua_getfield(L, -3, WORLD);
|
||||
REQUIRE(lua_pcall(L, 2, 1, 0) == LUA_OK);
|
||||
REQUIRE(lua_isboolean(L, -1));
|
||||
REQUIRE_FALSE(lua_toboolean(L, -1));
|
||||
lua_pop(L, 1);
|
||||
|
||||
// number: 3.14
|
||||
lua_getglobal(L, is_instance);
|
||||
lua_pushnumber(L, 3.14);
|
||||
lua_getfield(L, -3, WORLD);
|
||||
REQUIRE(lua_pcall(L, 2, 1, 0) == LUA_OK);
|
||||
REQUIRE(lua_isboolean(L, -1));
|
||||
REQUIRE_FALSE(lua_toboolean(L, -1));
|
||||
lua_pop(L, 1);
|
||||
|
||||
// string
|
||||
lua_getglobal(L, is_instance);
|
||||
lua_pushstring(L, "traintastic");
|
||||
lua_getfield(L, -3, WORLD);
|
||||
REQUIRE(lua_pcall(L, 2, 1, 0) == LUA_OK);
|
||||
REQUIRE(lua_isboolean(L, -1));
|
||||
REQUIRE_FALSE(lua_toboolean(L, -1));
|
||||
lua_pop(L, 1);
|
||||
|
||||
// table
|
||||
lua_getglobal(L, is_instance);
|
||||
lua_newtable(L);
|
||||
lua_getfield(L, -3, WORLD);
|
||||
REQUIRE(lua_pcall(L, 2, 1, 0) == LUA_OK);
|
||||
REQUIRE(lua_isboolean(L, -1));
|
||||
REQUIRE_FALSE(lua_toboolean(L, -1));
|
||||
lua_pop(L, 1);
|
||||
|
||||
// userdata
|
||||
lua_getglobal(L, is_instance);
|
||||
lua_newuserdata(L, sizeof(void*));
|
||||
lua_getfield(L, -3, WORLD);
|
||||
REQUIRE(lua_pcall(L, 2, 1, 0) == LUA_OK);
|
||||
REQUIRE(lua_isboolean(L, -1));
|
||||
REQUIRE_FALSE(lua_toboolean(L, -1));
|
||||
lua_pop(L, 1);
|
||||
|
||||
// World
|
||||
auto world = World::create();
|
||||
lua_getglobal(L, is_instance);
|
||||
Lua::Object::push(L, world);
|
||||
lua_getfield(L, -3, WORLD);
|
||||
REQUIRE(lua_pcall(L, 2, 1, 0) == LUA_OK);
|
||||
REQUIRE(lua_isboolean(L, -1));
|
||||
REQUIRE(lua_toboolean(L, -1));
|
||||
lua_pop(L, 1);
|
||||
|
||||
lua_getglobal(L, is_instance);
|
||||
Lua::Object::push(L, world);
|
||||
lua_getfield(L, -3, DECODER);
|
||||
REQUIRE(lua_pcall(L, 2, 1, 0) == LUA_OK);
|
||||
REQUIRE(lua_isboolean(L, -1));
|
||||
REQUIRE_FALSE(lua_toboolean(L, -1));
|
||||
lua_pop(L, 1);
|
||||
|
||||
// Decoder
|
||||
auto decoder = world->decoders->add();
|
||||
lua_getglobal(L, is_instance);
|
||||
Lua::Object::push(L, decoder);
|
||||
lua_getfield(L, -3, WORLD);
|
||||
REQUIRE(lua_pcall(L, 2, 1, 0) == LUA_OK);
|
||||
REQUIRE(lua_isboolean(L, -1));
|
||||
REQUIRE_FALSE(lua_toboolean(L, -1));
|
||||
lua_pop(L, 1);
|
||||
|
||||
lua_getglobal(L, is_instance);
|
||||
Lua::Object::push(L, decoder);
|
||||
lua_getfield(L, -3, DECODER);
|
||||
REQUIRE(lua_pcall(L, 2, 1, 0) == LUA_OK);
|
||||
REQUIRE(lua_isboolean(L, -1));
|
||||
REQUIRE(lua_toboolean(L, -1));
|
||||
lua_pop(L, 1);
|
||||
|
||||
lua_close(L);
|
||||
}
|
||||
Laden…
x
In neuem Issue referenzieren
Einen Benutzer sperren