add to_string and is_integer utilities

This commit is contained in:
Marcel Gotsch 2022-08-26 15:21:02 +02:00
parent 4de99c5b41
commit 92cbc71c10
7 changed files with 296 additions and 12 deletions

View File

@ -0,0 +1,43 @@
// sol2
// The MIT License (MIT)
// Copyright (c) 2013-2022 Rapptz, ThePhD and contributors
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
// The above copyright notice and this Spermission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#ifndef SOL_IS_INTEGER_HPP
#define SOL_IS_INTEGER_HPP
#include <sol/object.hpp>
namespace sol::utility {
// Returns true if the object is represented by an integer,
// not a floating point number or any other type.
inline bool is_integer(const sol::object& object) {
auto pp = stack::push_pop(object);
return lua_isinteger(object.lua_state(), -1);
}
inline bool is_integer(const sol::stack_object& object) {
return lua_isinteger(object.lua_state(), object.stack_index());
}
} // namespace sol::utility
#endif // SOL_IS_INTEGER_HPP

View File

@ -0,0 +1,59 @@
// sol2
// The MIT License (MIT)
// Copyright (c) 2013-2022 Rapptz, ThePhD and contributors
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
// The above copyright notice and this Spermission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#ifndef SOL_TO_STRING_HPP
#define SOL_TO_STRING_HPP
#include <sol/forward.hpp>
#include <sol/object.hpp>
#include <cstddef>
#include <string>
namespace sol::utility {
// Converts any object into a string using luaL_tolstring.
//
// Note: Uses the metamethod __tostring if available.
inline std::string to_string(const sol::stack_object& object) {
std::size_t len;
const char* str = luaL_tolstring(object.lua_state(), object.stack_index(), &len);
auto result = std::string(str, len);
// luaL_tolstring pushes the string onto the stack, but since
// we have copied it into our std::string by now we should
// remove it from the stack.
lua_pop(object.lua_state(), 1);
return result;
}
inline std::string to_string(const sol::object& object) {
auto pp = sol::stack::push_pop(object);
return to_string(sol::stack_object(object.lua_state(), -1));
}
} // namespace sol::utility
#endif // SOL_IS_INTEGER_HPP

View File

@ -0,0 +1,24 @@
// sol2
// The MIT License (MIT)
// Copyright (c) 2013-2022 Rapptz, ThePhD and contributors
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include <sol/utility/is_integer.hpp>

View File

@ -0,0 +1,24 @@
// sol2
// The MIT License (MIT)
// Copyright (c) 2013-2022 Rapptz, ThePhD and contributors
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include <sol/utility/to_string.hpp>

View File

@ -22,12 +22,12 @@
# # # # sol2 tests - runtime tests
file(GLOB sources
LIST_DIRECTORIES FALSE
CONFIGURE_DEPENDS
source/*.cpp)
if (WAY_TOO_BUSTED_RIGHT_NOW)
# Many are broken, so we list the working ones manually for now.
set(sources
source/is_integer.cpp
source/main.cpp
source/to_string.cpp
)
sol2_create_basic_test(sol2.tests.run_time sol2::sol2 ${sources})
target_compile_definitions(sol2.tests.run_time PRIVATE
@ -37,5 +37,3 @@ if (SOL2_TESTS_SINGLE)
target_compile_definitions(sol2.single.tests.run_time PRIVATE
SOL_ALL_SAFETIES_ON=1)
endif()
endif() # currently borked

View File

@ -0,0 +1,53 @@
// sol2
// The MIT License (MIT)
// Copyright (c) 2013-2022 Rapptz, ThePhD and contributors
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include "sol_test.hpp"
#include <sol/forward.hpp>
#include <sol/reference.hpp>
#include <sol/utility/is_integer.hpp>
#include <catch2/catch_all.hpp>
TEST_CASE("is_integer") {
sol::state lua;
SECTION("non-numeric type") {
auto object = sol::make_object(lua, "Hello");
CHECK(!sol::utility::is_integer(object));
CHECK(!sol::utility::is_integer(sol::stack_object(lua, -1)));
}
SECTION("floating point") {
auto object = sol::make_object(lua, 5.5);
CHECK(!sol::utility::is_integer(object));
CHECK(!sol::utility::is_integer(sol::stack_object(lua, -1)));
}
SECTION("integer") {
auto object = sol::make_object(lua, 5);
CHECK(sol::utility::is_integer(object));
CHECK(sol::utility::is_integer(sol::stack_object(lua, -1)));
}
}

View File

@ -0,0 +1,83 @@
// sol2
// The MIT License (MIT)
// Copyright (c) 2013-2022 Rapptz, ThePhD and contributors
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include "sol_test.hpp"
#include <sol/forward.hpp>
#include <sol/reference.hpp>
#include <sol/utility/to_string.hpp>
#include <catch2/catch_all.hpp>
namespace {
int someFunc() {
return 5;
}
} // namespace
TEST_CASE("to_string") {
using Catch::Matchers::ContainsSubstring;
sol::state lua;
SECTION("boolean") {
auto object = sol::make_object(lua, true);
CHECK(sol::utility::to_string(object) == "true");
auto pp = sol::stack::push_pop(object);
CHECK(sol::utility::to_string(sol::stack_object(lua, -1)) == "true");
}
SECTION("string") {
auto object = sol::make_object(lua, "Hello");
CHECK(sol::utility::to_string(object) == "Hello");
auto pp = sol::stack::push_pop(object);
CHECK(sol::utility::to_string(sol::stack_object(lua, -1)) == "Hello");
}
SECTION("number") {
auto object = sol::make_object(lua, 5);
CHECK(sol::utility::to_string(object) == "5");
auto pp = sol::stack::push_pop(object);
CHECK(sol::utility::to_string(sol::stack_object(lua, -1)) == "5");
}
SECTION("table") {
auto object = lua.create_table();
CHECK_THAT(sol::utility::to_string(object), ContainsSubstring("table"));
auto pp = sol::stack::push_pop(object);
CHECK_THAT(sol::utility::to_string(sol::stack_object(lua, -1)), ContainsSubstring("table"));
}
SECTION("function") {
auto object = sol::make_object(lua, &someFunc);
CHECK_THAT(sol::utility::to_string(object), ContainsSubstring("function"));
auto pp = sol::stack::push_pop(object);
CHECK_THAT(sol::utility::to_string(sol::stack_object(lua, -1)), ContainsSubstring("function"));
}
}