From 1c4b3bb74a3fd70232b9eb2c3d40c2bbbd21f7bb Mon Sep 17 00:00:00 2001 From: Reinder Feenstra Date: Tue, 20 Apr 2021 22:22:23 +0200 Subject: [PATCH] Added test and support for Lua::check<> string/string_view --- server/src/lua/check.hpp | 8 +- server/test/lua/check_string.cpp | 141 +++++++++++++++++++++++++++++++ 2 files changed, 147 insertions(+), 2 deletions(-) create mode 100644 server/test/lua/check_string.cpp diff --git a/server/src/lua/check.hpp b/server/src/lua/check.hpp index d36f1a97..4fac4fd3 100644 --- a/server/src/lua/check.hpp +++ b/server/src/lua/check.hpp @@ -55,8 +55,12 @@ T check(lua_State* L, int index) } else if constexpr(std::is_floating_point_v) return luaL_checknumber(L, index); - else if constexpr(std::is_same_v) - return luaL_checkstring(L, index); + else if constexpr(std::is_same_v || std::is_same_v) + { + size_t l; + const char* s = luaL_checklstring(L, index, &l); + return T{s, l}; + } //else if constexpr(std::is_same_v) // Object::push(L, value); else diff --git a/server/test/lua/check_string.cpp b/server/test/lua/check_string.cpp new file mode 100644 index 00000000..1d14f94f --- /dev/null +++ b/server/test/lua/check_string.cpp @@ -0,0 +1,141 @@ +/** + * server/test/lua/check_string.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 "protect.hpp" +#include "../../src/lua/check.hpp" + +TEMPLATE_TEST_CASE("Lua::check<>", "[lua][lua-check]", std::string, std::string_view) +{ + { + INFO("nil") + + lua_State* L = newStateWithProtect(); + + lua_pushnil(L); + TestType r; + REQUIRE_FALSE(protect>(r, L, -1)); + + closeStateWithProtect(L); + } + + { + INFO("false") + + lua_State* L = newStateWithProtect(); + + lua_pushboolean(L, false); + TestType r; + REQUIRE_FALSE(protect>(r, L, -1)); + + closeStateWithProtect(L); + } + + { + INFO("true") + + lua_State* L = newStateWithProtect(); + + lua_pushboolean(L, true); + TestType r; + REQUIRE_FALSE(protect>(r, L, -1)); + + closeStateWithProtect(L); + } + + { + INFO("123") + + lua_State* L = newStateWithProtect(); + + lua_pushinteger(L, 123); + TestType r; + REQUIRE(protect>(r, L, -1)); + REQUIRE(r == "123"); + REQUIRE(lua_type(L, -1) == LUA_TSTRING); + + closeStateWithProtect(L); + } + + { + INFO("0.5") + + lua_State* L = newStateWithProtect(); + + lua_pushnumber(L, 0.5); + TestType r; + REQUIRE(protect>(r, L, -1)); + REQUIRE(r == "0.5"); + REQUIRE(lua_type(L, -1) == LUA_TSTRING); + + closeStateWithProtect(L); + } + + { + INFO("\"test\"") + + lua_State* L = newStateWithProtect(); + + lua_pushliteral(L, "test"); + TestType r; + REQUIRE(protect>(r, L, -1)); + REQUIRE(r == "test"); + + closeStateWithProtect(L); + } + + { + INFO("table") + + lua_State* L = newStateWithProtect(); + + lua_newtable(L); + TestType r; + REQUIRE_FALSE(protect>(r, L, -1)); + + closeStateWithProtect(L); + } + + { + INFO("userdata") + + lua_State* L = newStateWithProtect(); + + lua_newuserdata(L, 0); + TestType r; + REQUIRE_FALSE(protect>(r, L, -1)); + + closeStateWithProtect(L); + } + + { + INFO("lightuserdata") + + lua_State* L = newStateWithProtect(); + + lua_pushlightuserdata(L, nullptr); + TestType r; + REQUIRE_FALSE(protect>(r, L, -1)); + + closeStateWithProtect(L); + } +}