test: added test for lua class.get()

Dieser Commit ist enthalten in:
Reinder Feenstra 2022-08-27 11:42:52 +02:00
Ursprung cfdc203c2b
Commit 5bffbccce2
3 geänderte Dateien mit 175 neuen und 132 gelöschten Zeilen

Datei anzeigen

@ -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<Clock>(L, "CLOCK");
// hardware - interface:
registerValue<DCCPlusPlusInterface>(L, "DCCPLUSPLUS");
registerValue<ECoSInterface>(L, "DCCPLUSPLUS");
registerValue<HSI88Interface>(L, "HSI88");
registerValue<LocoNetInterface>(L, "LOCONET");
registerValue<TraintasticDIYInterface>(L, "TRAINTASTIC_DIY");
registerValue<XpressNetInterface>(L, "XPRESSNET");
registerValue<WlanMausInterface>(L, "WLANMAUS");
registerValue<Z21Interface>(L, "Z21");
registerValue<DecoderFunction>(L, "DECODER_FUNCTION");
registerValue<DecoderList>(L, "DECODER_LIST");
registerValue<Decoder>(L, "DECODER");

Datei anzeigen

@ -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 <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);
}

Datei anzeigen

@ -0,0 +1,157 @@
#include <catch2/catch.hpp>
#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<TestType>::value).append(".").append(toUpper(EnumValues<TestType>::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<TestType>).append(".").append(toUpper(set_values_v<TestType>.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);
}