add new tests for unqualified returns through as_nested/as_table

This commit is contained in:
ThePhD 2017-12-28 12:11:12 -05:00
parent b544a43734
commit 732b4bd700
5 changed files with 97 additions and 5 deletions

View File

@ -93,7 +93,7 @@ Please make sure you use the `-std=c++1y`, `-std=c++14`, `-std=c++1z`, `-std=c++
Older compilers (GCC 4.9.x, Clang 3.4.x seem to be the lowest) can work with versions as late Older compilers (GCC 4.9.x, Clang 3.4.x seem to be the lowest) can work with versions as late
as [v2.17.5](https://github.com/ThePhD/sol2/releases/tag/v2.17.5), with the flag `-std=c++14` or `-std=c++1y`. as [v2.17.5](https://github.com/ThePhD/sol2/releases/tag/v2.17.5), with the flag `-std=c++14` or `-std=c++1y`.
Is checked by-hand for other platforms as well, including Android-based builds with GCC and iOS-based builds out of XCode with Apple-clang. It should work on both of these platforms, so long as you have the proper standards flags. sol2 is checked by-hand for other platforms as well, including Android-based builds with GCC and iOS-based builds out of XCode with Apple-clang. It should work on both of these platforms, so long as you have the proper standards flags.
## Running the Tests ## Running the Tests

View File

@ -72,6 +72,8 @@ matrix:
- platform: x86 - platform: x86
LLVM_VERSION: 4.0.0 LLVM_VERSION: 4.0.0
# Get rid of x86 builds # Get rid of x86 builds
- platform: x86
image: Visual Studio 2015
- platform: x86 - platform: x86
LUA_VERSION: 5.2.4 LUA_VERSION: 5.2.4
- platform: x86 - platform: x86

View File

@ -24,7 +24,7 @@
} while (false) } while (false)
#else #else
# define m_assert(condition, message) do { if (false) { (void)(condition); (void)sizeof(message); } } while (false) # define m_assert(condition, message) do { if (false) { (void)(condition); (void)sizeof(message); } } while (false)
# define c_assert(condition) do { (void)sizeof(condition); } while (false) # define c_assert(condition) do { if (false) { (void)(condition); } } while (false)
#endif #endif
#endif // EXAMPLES_ASSERT_HPP #endif // EXAMPLES_ASSERT_HPP

View File

@ -508,15 +508,25 @@ namespace sol {
}; };
template <typename T> template <typename T>
as_table_t<T> as_table(T&& container) { as_table_t<T> as_table_ref(T&& container) {
return as_table_t<T>(std::forward<T>(container)); return as_table_t<T>(std::forward<T>(container));
} }
template <typename T> template <typename T>
nested<T> as_nested(T&& container) { as_table_t<meta::unqualified_t<T>> as_table(T&& container) {
return as_table_t<meta::unqualified_t<T>>(std::forward<T>(container));
}
template <typename T>
nested<T> as_nested_ref(T&& container) {
return nested<T>(std::forward<T>(container)); return nested<T>(std::forward<T>(container));
} }
template <typename T>
nested<meta::unqualified_t<T>> as_nested(T&& container) {
return nested<meta::unqualified_t<T>>(std::forward<T>(container));
}
struct this_state { struct this_state {
lua_State* L; lua_State* L;

View File

@ -196,6 +196,86 @@ TEST_CASE("containers/returns", "make sure that even references to vectors are b
REQUIRE(matching); REQUIRE(matching);
} }
TEST_CASE("containers/table conversions (lvalue)", "test table conversions with as_table and nested, when not directly serializing a temporary / new value") {
sol::state lua;
auto f = []() {
std::vector<std::string> response_words;
response_words.push_back("a");
response_words.push_back("b");
response_words.push_back("c");
return sol::as_table(response_words);
};
auto g = []() {
std::vector<std::string> response_words;
response_words.push_back("a");
response_words.push_back("b");
response_words.push_back("c");
return sol::as_nested(response_words);
};
lua["f"] = std::ref(f);
lua["g"] = std::ref(g);
sol::safe_function sff = lua["f"];
sol::safe_function sfg = lua["g"];
sol::table tf = sff();
sol::table tg = sfg();
std::string af = tf[1];
std::string bf = tf[2];
std::string cf = tf[3];
std::string ag = tf[1];
std::string bg = tf[2];
std::string cg = tf[3];
REQUIRE(tf.size() == 3);
REQUIRE(af == "a");
REQUIRE(bf == "b");
REQUIRE(cf == "c");
REQUIRE(tg.size() == 3);
REQUIRE(ag == "a");
REQUIRE(bg == "b");
REQUIRE(cg == "c");
}
TEST_CASE("containers/table conversions (std::ref)", "test table conversions with as_table and nested, when not directly serializing a temporary / new value") {
sol::state lua;
std::vector<std::string> response_words;
response_words.push_back("a");
response_words.push_back("b");
response_words.push_back("c");
auto f = [&response_words]() {
return sol::as_table(std::ref(response_words));
};
auto g = [&response_words]() {
return sol::as_nested(std::ref(response_words));
};
lua["f"] = std::ref(f);
lua["g"] = std::ref(g);
sol::safe_function sff = lua["f"];
sol::safe_function sfg = lua["g"];
sol::table tf = sff();
sol::table tg = sfg();
std::string af = tf[1];
std::string bf = tf[2];
std::string cf = tf[3];
std::string ag = tf[1];
std::string bg = tf[2];
std::string cg = tf[3];
REQUIRE(tf.size() == 3);
REQUIRE(af == "a");
REQUIRE(bf == "b");
REQUIRE(cf == "c");
REQUIRE(tg.size() == 3);
REQUIRE(ag == "a");
REQUIRE(bg == "b");
REQUIRE(cg == "c");
}
TEST_CASE("containers/table conversion", "test table conversions with as_table and nested") { TEST_CASE("containers/table conversion", "test table conversions with as_table and nested") {
sol::state lua; sol::state lua;
lua.open_libraries(sol::lib::base); lua.open_libraries(sol::lib::base);