From 732b4bd700080ca6b0fec0a765a34f10c13d1270 Mon Sep 17 00:00:00 2001 From: ThePhD Date: Thu, 28 Dec 2017 12:11:12 -0500 Subject: [PATCH] add new tests for unqualified returns through as_nested/as_table --- README.md | 4 +- appveyor.yml | 2 + examples/assert.hpp | 2 +- sol/types.hpp | 14 ++++++- tests/test_containers.cpp | 80 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 97 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index db135a5f..915cfa4b 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,7 @@ Lua Workshop 2016 - Mashape, San Francisco, CA "Wrapping Lua C in C++ - Efficiently, Nicely, and with a Touch of Magic" ThePhD -Boston C++ Meetup November 2017 - CiC, Boston, MA +Boston C++ Meetup November 2017 - CiC, Boston, MA [Deck](https://github.com/ThePhD/sol2/blob/develop/docs/presentations/ThePhD%20-%20Wrapping%20Lua%20C%20in%20C%2B%2B%20-%202017.11.8.pdf) ## Creating a single header @@ -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 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 diff --git a/appveyor.yml b/appveyor.yml index d28ee910..df680311 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -72,6 +72,8 @@ matrix: - platform: x86 LLVM_VERSION: 4.0.0 # Get rid of x86 builds + - platform: x86 + image: Visual Studio 2015 - platform: x86 LUA_VERSION: 5.2.4 - platform: x86 diff --git a/examples/assert.hpp b/examples/assert.hpp index 7f073ee8..bb3e6ce4 100644 --- a/examples/assert.hpp +++ b/examples/assert.hpp @@ -24,7 +24,7 @@ } while (false) #else # 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 // EXAMPLES_ASSERT_HPP diff --git a/sol/types.hpp b/sol/types.hpp index 4a394ed2..60bae750 100644 --- a/sol/types.hpp +++ b/sol/types.hpp @@ -508,15 +508,25 @@ namespace sol { }; template - as_table_t as_table(T&& container) { + as_table_t as_table_ref(T&& container) { return as_table_t(std::forward(container)); } template - nested as_nested(T&& container) { + as_table_t> as_table(T&& container) { + return as_table_t>(std::forward(container)); + } + + template + nested as_nested_ref(T&& container) { return nested(std::forward(container)); } + template + nested> as_nested(T&& container) { + return nested>(std::forward(container)); + } + struct this_state { lua_State* L; diff --git a/tests/test_containers.cpp b/tests/test_containers.cpp index 978a0047..7766c593 100644 --- a/tests/test_containers.cpp +++ b/tests/test_containers.cpp @@ -196,6 +196,86 @@ TEST_CASE("containers/returns", "make sure that even references to vectors are b 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 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 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 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") { sol::state lua; lua.open_libraries(sol::lib::base);