mirror of
https://github.com/ThePhD/sol2.git
synced 2024-03-22 13:10:44 +08:00
e69e7c79fa
added variadic_results, to return a variable number of arguments to Lua added variadic_results and as_results added improved function examples (for multiple results and split overloading out) added tests for variadics added tests for C++17 utilities added a forwarding header added a specific `unsafe_function` header added and improved documetation pages
69 lines
1.5 KiB
C++
69 lines
1.5 KiB
C++
#define SOL_CHECK_ARGUMENTS
|
|
|
|
#include <catch.hpp>
|
|
#include <sol.hpp>
|
|
|
|
#ifdef SOL_CXX17_FEATURES
|
|
#include <string_view>
|
|
#include <variant>
|
|
#endif
|
|
|
|
|
|
TEST_CASE("utility/variant", "test that variant can be round-tripped") {
|
|
#ifdef SOL_CXX17_FEATURES
|
|
SECTION("okay") {
|
|
sol::state lua;
|
|
lua.open_libraries(sol::lib::base);
|
|
|
|
lua.set_function("f", [](int v) {
|
|
return v == 2;
|
|
});
|
|
lua.set_function("g", [](std::variant<float, int, std::string> vv) {
|
|
int v = std::get<int>(vv);
|
|
return v == 2;
|
|
});
|
|
lua["v"] = std::variant<float, int, std::string>(2);
|
|
REQUIRE_NOTHROW([&]() {
|
|
lua.script("assert(f(v))");
|
|
lua.script("assert(g(v))");
|
|
}());
|
|
}
|
|
SECTION("throws") {
|
|
sol::state lua;
|
|
lua.open_libraries(sol::lib::base);
|
|
|
|
lua.set_function("f", [](int v) {
|
|
return v == 2;
|
|
});
|
|
lua.set_function("g", [](std::variant<float, int, std::string> vv) {
|
|
int v = std::get<int>(vv);
|
|
return v == 2;
|
|
});
|
|
lua["v"] = std::variant<float, int, std::string>(std::string("bark"));
|
|
REQUIRE_THROWS([&]() {
|
|
lua.script("assert(f(v))");
|
|
lua.script("assert(g(v))");
|
|
}());
|
|
}
|
|
#else
|
|
REQUIRE(true);
|
|
#endif // C++17
|
|
}
|
|
|
|
TEST_CASE("utility/string_view", "test that string_view can be taken as an argument") {
|
|
#ifdef SOL_CXX17_FEATURES
|
|
sol::state lua;
|
|
lua.open_libraries(sol::lib::base);
|
|
|
|
lua.set_function("f", [](std::string_view v) {
|
|
return v == "bark!";
|
|
});
|
|
lua["v"] = "bark!";
|
|
|
|
REQUIRE_NOTHROW([&]() {
|
|
lua.script("assert(f(v))");
|
|
}());
|
|
#else
|
|
REQUIRE(true);
|
|
#endif // C++17
|
|
} |