diff --git a/include/sol/utility/is_integer.hpp b/include/sol/utility/is_integer.hpp new file mode 100644 index 00000000..9292bb63 --- /dev/null +++ b/include/sol/utility/is_integer.hpp @@ -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 + +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 diff --git a/include/sol/utility/to_string.hpp b/include/sol/utility/to_string.hpp new file mode 100644 index 00000000..51b85a4f --- /dev/null +++ b/include/sol/utility/to_string.hpp @@ -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 +#include + +#include +#include + +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 diff --git a/tests/inclusion/source/utility/is_integer.cpp b/tests/inclusion/source/utility/is_integer.cpp new file mode 100644 index 00000000..ed9bc3c8 --- /dev/null +++ b/tests/inclusion/source/utility/is_integer.cpp @@ -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 diff --git a/tests/inclusion/source/utility/to_string.cpp b/tests/inclusion/source/utility/to_string.cpp new file mode 100644 index 00000000..5313ec1f --- /dev/null +++ b/tests/inclusion/source/utility/to_string.cpp @@ -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 diff --git a/tests/run_time/CMakeLists.txt b/tests/run_time/CMakeLists.txt index c412a9de..159119b6 100644 --- a/tests/run_time/CMakeLists.txt +++ b/tests/run_time/CMakeLists.txt @@ -1,18 +1,18 @@ # # # # 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 @@ -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 diff --git a/tests/run_time/source/is_integer.cpp b/tests/run_time/source/is_integer.cpp new file mode 100644 index 00000000..78300fc8 --- /dev/null +++ b/tests/run_time/source/is_integer.cpp @@ -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 +#include +#include + +#include + +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))); + } +} diff --git a/tests/run_time/source/to_string.cpp b/tests/run_time/source/to_string.cpp new file mode 100644 index 00000000..5d8d4707 --- /dev/null +++ b/tests/run_time/source/to_string.cpp @@ -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 +#include +#include + +#include + +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")); + } +}