/** * server/test/lua/check_enum.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/check.hpp" #include "protect.hpp" #include // Enums: #include "../../src/enum/decoderprotocol.hpp" #include "../../src/enum/direction.hpp" #include "../../src/enum/worldevent.hpp" template struct other_enum_type { using type = WorldEvent; }; template<> struct other_enum_type { using type = DecoderProtocol; }; template static lua_State* createState() { lua_State* L = newStateWithProtect(); Lua::Enum::registerType(L); Lua::Enum::type>::registerType(L); return L; } TEMPLATE_TEST_CASE("Lua::check<>", "[lua][lua-check]", DecoderProtocol, Direction, WorldEvent) { using OtherEnumType = typename other_enum_type::type; const TestType lastValue = EnumValues::value.rbegin()->first; { INFO("nil") lua_State* L = createState(); lua_pushnil(L); TestType r; REQUIRE_FALSE(protect>(r, L, -1)); closeStateWithProtect(L); } { INFO("false") lua_State* L = createState(); lua_pushboolean(L, false); TestType r; REQUIRE_FALSE(protect>(r, L, -1)); closeStateWithProtect(L); } { INFO("true") lua_State* L = createState(); lua_pushboolean(L, true); TestType r; REQUIRE_FALSE(protect>(r, L, -1)); closeStateWithProtect(L); } { INFO("enum") lua_State* L = createState(); Lua::Enum::push(L, lastValue); TestType r; REQUIRE(protect>(r, L, -1)); REQUIRE(r == lastValue); closeStateWithProtect(L); } { INFO("other enum") lua_State* L = createState(); Lua::Enum::push(L, EnumValues::value.rbegin()->first); TestType r; REQUIRE_FALSE(protect>(r, L, -1)); closeStateWithProtect(L); } { INFO("123") lua_State* L = createState(); lua_pushinteger(L, 123); TestType r; REQUIRE_FALSE(protect>(r, L, -1)); closeStateWithProtect(L); } { INFO("0.5") lua_State* L = createState(); lua_pushnumber(L, 0.5); TestType r; REQUIRE_FALSE(protect>(r, L, -1)); closeStateWithProtect(L); } { INFO("\"test\"") lua_State* L = createState(); lua_pushliteral(L, "test"); TestType r; REQUIRE_FALSE(protect>(r, L, -1)); closeStateWithProtect(L); } { INFO("table") lua_State* L = createState(); lua_newtable(L); TestType r; REQUIRE_FALSE(protect>(r, L, -1)); closeStateWithProtect(L); } { INFO("userdata") lua_State* L = createState(); lua_newuserdata(L, 0); TestType r; REQUIRE_FALSE(protect>(r, L, -1)); closeStateWithProtect(L); } { INFO("lightuserdata") lua_State* L = createState(); lua_pushlightuserdata(L, nullptr); TestType r; REQUIRE_FALSE(protect>(r, L, -1)); closeStateWithProtect(L); } }