1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
|
// 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 <catch2/catch_all.hpp>
#include <sol/sol.hpp>
#include <stdexcept>
#include <string_view>
inline namespace sol2_tests_exceptions_functions {
inline constexpr const std::string_view special_string = "Whoopsie [expected]";
static void func_throw() {
throw std::runtime_error(special_string.data());
}
struct some_class {
void mem_func_throw() {
throw std::runtime_error(special_string.data());
}
};
} // namespace sol2_tests_exceptions_functions
TEST_CASE("exceptions/functions", "exercise the ability to throw exceptions many different function binding code paths") {
sol::state lua;
lua.open_libraries(sol::lib::base,
sol::lib::package,
sol::lib::coroutine,
sol::lib::string,
sol::lib::os,
sol::lib::math,
sol::lib::table,
sol::lib::io,
sol::lib::debug);
[[maybe_unused]] int a = 10;
some_class sc {};
auto throw_action = [](sol::state& lua, bool use_sc) {
if (use_sc) {
auto res = lua.safe_script("func_throw(sc)", sol::script_pass_on_error);
REQUIRE_FALSE(res.valid());
REQUIRE(res.status() == sol::call_status::runtime);
sol::error err = res.get<sol::error>();
std::string_view err_what(err.what());
REQUIRE(err_what.find(special_string) != std::string::npos);
}
else {
auto res = lua.safe_script("func_throw()", sol::script_pass_on_error);
REQUIRE_FALSE(res.valid());
REQUIRE(res.status() == sol::call_status::runtime);
sol::error err = res.get<sol::error>();
std::string_view err_what(err.what());
REQUIRE(err_what.find(special_string) != std::string::npos);
}
};
lua["sc"] = ≻
SECTION("proxy") {
lua["func_throw"] = sol::property(&func_throw, &some_class::mem_func_throw);
REQUIRE_NOTHROW(throw_action(lua, false));
REQUIRE_NOTHROW(throw_action(lua, true));
lua["func_throw"] = sol::property(&some_class::mem_func_throw);
REQUIRE_NOTHROW(throw_action(lua, true));
lua["func_throw"] = sol::readonly_property(func_throw);
REQUIRE_NOTHROW(throw_action(lua, false));
lua["func_throw"] = sol::writeonly_property(&some_class::mem_func_throw);
REQUIRE_NOTHROW(throw_action(lua, true));
lua["func_throw"] = [f = &func_throw] { return f(); };
REQUIRE_NOTHROW(throw_action(lua, false));
lua["func_throw"] = &func_throw;
REQUIRE_NOTHROW(throw_action(lua, false));
lua["func_throw"] = [] { return func_throw(); };
REQUIRE_NOTHROW(throw_action(lua, false));
lua["func_throw"] = [a]() { (void)a; return func_throw(); };
REQUIRE_NOTHROW(throw_action(lua, false));
lua["func_throw"] = [&sc]() { return sc.mem_func_throw(); };
REQUIRE_NOTHROW(throw_action(lua, false));
lua["func_throw"] = &some_class::mem_func_throw;
REQUIRE_NOTHROW(throw_action(lua, true));
lua["func_throw"] = std::function<void()>([f = &func_throw] { return f(); });
REQUIRE_NOTHROW(throw_action(lua, false));
lua["func_throw"] = sol::overload(std::function<void()>([f = &func_throw] { return f(); }));
REQUIRE_NOTHROW(throw_action(lua, false));
lua["func_throw"] = std::function<void()>([] { return func_throw(); });
REQUIRE_NOTHROW(throw_action(lua, false));
lua["func_throw"] = std::function<void()>(&func_throw);
REQUIRE_NOTHROW(throw_action(lua, false));
lua["func_throw"] = std::function<void(some_class&)>(&some_class::mem_func_throw);
REQUIRE_NOTHROW(throw_action(lua, true));
}
SECTION("set_function") {
lua.set_function("func_throw", sol::property(&func_throw, &some_class::mem_func_throw));
REQUIRE_NOTHROW(throw_action(lua, false));
REQUIRE_NOTHROW(throw_action(lua, true));
lua.set_function("func_throw", sol::property(&some_class::mem_func_throw));
REQUIRE_NOTHROW(throw_action(lua, true));
lua.set_function("func_throw", sol::readonly_property(func_throw));
REQUIRE_NOTHROW(throw_action(lua, false));
// TODO: better handling of no_prop in this case here
// lua.set_function("func_throw", sol::writeonly_property(&some_class::mem_func_throw));
// REQUIRE_NOTHROW(throw_action(lua, true));
lua.set_function("func_throw", [f = &func_throw] { return f(); });
REQUIRE_NOTHROW(throw_action(lua, false));
lua.set_function("func_throw", &func_throw);
REQUIRE_NOTHROW(throw_action(lua, false));
lua.set_function("func_throw", [] { return func_throw(); });
REQUIRE_NOTHROW(throw_action(lua, false));
lua.set_function("func_throw", [a]() { (void)a; return func_throw(); });
REQUIRE_NOTHROW(throw_action(lua, false));
// TODO: this should work at some point, yeah?
/*
lua.set_function(
"func_throw", [](some_class& sc) { return sc.mem_func_throw(); }, sc);
REQUIRE_NOTHROW(throw_action(lua, false));
*/
lua.set_function("func_throw", [&sc]() { return sc.mem_func_throw(); });
REQUIRE_NOTHROW(throw_action(lua, false));
lua.set_function("func_throw", &some_class::mem_func_throw);
REQUIRE_NOTHROW(throw_action(lua, true));
lua.set_function("func_throw", std::function<void()>([f = &func_throw] { return f(); }));
REQUIRE_NOTHROW(throw_action(lua, false));
lua.set_function("func_throw", sol::overload(std::function<void()>([f = &func_throw] { return f(); })));
REQUIRE_NOTHROW(throw_action(lua, false));
lua.set_function("func_throw", std::function<void()>([] { return func_throw(); }));
REQUIRE_NOTHROW(throw_action(lua, false));
lua.set_function("func_throw", std::function<void()>(&func_throw));
REQUIRE_NOTHROW(throw_action(lua, false));
lua.set_function("func_throw", &some_class::mem_func_throw, &sc);
REQUIRE_NOTHROW(throw_action(lua, false));
lua.set_function("func_throw", &some_class::mem_func_throw, sc);
REQUIRE_NOTHROW(throw_action(lua, false));
lua.set_function("func_throw", &some_class::mem_func_throw, std::ref(sc));
REQUIRE_NOTHROW(throw_action(lua, false));
lua.set_function("func_throw", std::function<void(some_class&)>(&some_class::mem_func_throw));
REQUIRE_NOTHROW(throw_action(lua, true));
}
}
|