diff --git a/server/src/lua/class.cpp b/server/src/lua/class.cpp index 21fc6f90..f4f8629e 100644 --- a/server/src/lua/class.cpp +++ b/server/src/lua/class.cpp @@ -55,6 +55,14 @@ #include "../clock/clock.hpp" +#include "../hardware/interface/dccplusplusinterface.hpp" +#include "../hardware/interface/ecosinterface.hpp" +#include "../hardware/interface/hsi88.hpp" +#include "../hardware/interface/loconetinterface.hpp" +#include "../hardware/interface/traintasticdiyinterface.hpp" +#include "../hardware/interface/wlanmausinterface.hpp" +#include "../hardware/interface/xpressnetinterface.hpp" +#include "../hardware/interface/z21interface.hpp" #include "../hardware/decoder/decoderfunction.hpp" #include "../hardware/decoder/list/decoderlist.hpp" @@ -159,6 +167,16 @@ void Class::registerValues(lua_State* L) registerValue(L, "CLOCK"); + // hardware - interface: + registerValue(L, "DCCPLUSPLUS"); + registerValue(L, "DCCPLUSPLUS"); + registerValue(L, "HSI88"); + registerValue(L, "LOCONET"); + registerValue(L, "TRAINTASTIC_DIY"); + registerValue(L, "XPRESSNET"); + registerValue(L, "WLANMAUS"); + registerValue(L, "Z21"); + registerValue(L, "DECODER_FUNCTION"); registerValue(L, "DECODER_LIST"); registerValue(L, "DECODER"); diff --git a/server/test/lua/getclass.cpp b/server/test/lua/getclass.cpp deleted file mode 100644 index ad8e981b..00000000 --- a/server/test/lua/getclass.cpp +++ /dev/null @@ -1,132 +0,0 @@ -/** - * 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 -#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); -} diff --git a/server/test/lua/script/class_get.cpp b/server/test/lua/script/class_get.cpp new file mode 100644 index 00000000..cbdd187d --- /dev/null +++ b/server/test/lua/script/class_get.cpp @@ -0,0 +1,157 @@ +#include +#include "../../interfaces.hpp" +#include "../../../src/world/world.hpp" +#include "../../../src/lua/enums.hpp" +#include "../../../src/lua/sets.hpp" +#include "../../../src/utils/toupper.hpp" + +TEST_CASE("Lua script: class.get() - nil", "[lua][lua-script][lua-script-class-get]") +{ + auto world = World::create(); + REQUIRE(world); + auto script = world->luaScripts->add(); + REQUIRE(script); + + script->code = "assert(class.get(nil) == nil)"; + script->start(); + INFO(script->error.value()); + REQUIRE(script->state.value() == LuaScriptState::Running); + script->stop(); + REQUIRE(script->state.value() == LuaScriptState::Stopped); +} + +TEST_CASE("Lua script: class.get() - false", "[lua][lua-script][lua-script-class-get]") +{ + auto world = World::create(); + REQUIRE(world); + auto script = world->luaScripts->add(); + REQUIRE(script); + + script->code = "assert(class.get(false) == nil)"; + script->start(); + REQUIRE(script->state.value() == LuaScriptState::Running); + script->stop(); + REQUIRE(script->state.value() == LuaScriptState::Stopped); +} + +TEST_CASE("Lua script: class.get() - true", "[lua][lua-script][lua-script-class-get]") +{ + auto world = World::create(); + REQUIRE(world); + auto script = world->luaScripts->add(); + REQUIRE(script); + + script->code = "assert(class.get(true) == nil)"; + script->start(); + REQUIRE(script->state.value() == LuaScriptState::Running); + script->stop(); + REQUIRE(script->state.value() == LuaScriptState::Stopped); +} + +TEST_CASE("Lua script: class.get() - integer", "[lua][lua-script][lua-script-class-get]") +{ + auto world = World::create(); + REQUIRE(world); + auto script = world->luaScripts->add(); + REQUIRE(script); + + script->code = "assert(class.get(42) == nil)"; + script->start(); + REQUIRE(script->state.value() == LuaScriptState::Running); + script->stop(); + REQUIRE(script->state.value() == LuaScriptState::Stopped); +} + +TEST_CASE("Lua script: class.get() - number", "[lua][lua-script][lua-script-class-get]") +{ + auto world = World::create(); + REQUIRE(world); + auto script = world->luaScripts->add(); + REQUIRE(script); + + script->code = "assert(class.get(3.14) == nil)"; + script->start(); + REQUIRE(script->state.value() == LuaScriptState::Running); + script->stop(); + REQUIRE(script->state.value() == LuaScriptState::Stopped); +} + +TEST_CASE("Lua script: class.get() - string", "[lua][lua-script][lua-script-class-get]") +{ + auto world = World::create(); + REQUIRE(world); + auto script = world->luaScripts->add(); + REQUIRE(script); + + script->code = "assert(class.get(\"traintastic\") == nil)"; + script->start(); + REQUIRE(script->state.value() == LuaScriptState::Running); + script->stop(); + REQUIRE(script->state.value() == LuaScriptState::Stopped); +} + +TEST_CASE("Lua script: class.get() - table", "[lua][lua-script][lua-script-class-get]") +{ + auto world = World::create(); + REQUIRE(world); + auto script = world->luaScripts->add(); + REQUIRE(script); + + script->code = "assert(class.get({}) == nil)"; + script->start(); + REQUIRE(script->state.value() == LuaScriptState::Running); + script->stop(); + REQUIRE(script->state.value() == LuaScriptState::Stopped); +} + +TEMPLATE_TEST_CASE("Lua script: class.get()", "[lua][lua-script][lua-script-class-get]", LUA_ENUMS) +{ + auto world = World::create(); + REQUIRE(world); + auto script = world->luaScripts->add(); + REQUIRE(script); + + script->code = std::string("assert(class.get(enum.").append(EnumName::value).append(".").append(toUpper(EnumValues::value.begin()->second)).append(") == nil)"); + script->start(); + INFO(script->code.value()); + INFO(script->error.value()); + REQUIRE(script->state.value() == LuaScriptState::Running); + script->stop(); + REQUIRE(script->state.value() == LuaScriptState::Stopped); +} + +TEMPLATE_TEST_CASE("Lua script: class.get()", "[lua][lua-script][lua-script-class-get]", LUA_SETS) +{ + auto world = World::create(); + REQUIRE(world); + auto script = world->luaScripts->add(); + REQUIRE(script); + + script->code = std::string("assert(class.get(set.").append(set_name_v).append(".").append(toUpper(set_values_v.begin()->second)).append(") == nil)"); + script->start(); + INFO(script->code.value()); + INFO(script->error.value()); + REQUIRE(script->state.value() == LuaScriptState::Running); + script->stop(); + REQUIRE(script->state.value() == LuaScriptState::Stopped); +} + +TEMPLATE_TEST_CASE("Lua script: class.get()", "[lua][lua-script][lua-script-class-get]", INTERFACES) +{ + auto world = World::create(); + REQUIRE(world); + auto interface = world->interfaces->add(TestType::classId); + REQUIRE(interface); + interface->id = "if"; + REQUIRE(interface->id.value() == "if"); + auto script = world->luaScripts->add(); + REQUIRE(script); + + script->code = "assert(class.get(world.get_object(\"if\")) ~= nil)"; + script->start(); + INFO(script->code.value()); + INFO(script->error.value()); + REQUIRE(script->state.value() == LuaScriptState::Running); + script->stop(); + REQUIRE(script->state.value() == LuaScriptState::Stopped); +}