mirror of
https://github.com/ThePhD/sol2.git
synced 2024-03-22 13:10:44 +08:00
Add tests covering creation of tables from standard containers
This commit is contained in:
parent
675d42d281
commit
462802a1f5
44
tests.cpp
44
tests.cpp
@ -1,6 +1,8 @@
|
||||
#define CATCH_CONFIG_MAIN
|
||||
#include <catch.hpp>
|
||||
#include <sol.hpp>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
|
||||
void test_free_func(std::function<void()> f) {
|
||||
f();
|
||||
@ -17,6 +19,18 @@ std::string free_function() {
|
||||
return "test";
|
||||
}
|
||||
|
||||
std::vector<int> test_table_return_one() {
|
||||
return { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
|
||||
}
|
||||
|
||||
std::vector<std::pair<std::string, int>> test_table_return_two() {
|
||||
return {{ "one", 1 }, { "two", 2 }, { "three", 3 }};
|
||||
}
|
||||
|
||||
std::map<std::string, std::string> test_table_return_three() {
|
||||
return {{ "name", "Rapptz" }, { "friend", "ThePhD" }, { "project", "sol" }};
|
||||
}
|
||||
|
||||
struct self_test {
|
||||
int bark;
|
||||
|
||||
@ -557,3 +571,33 @@ TEST_CASE("tables/self-referential userdata", "userdata classes must play nice w
|
||||
"a:f(a)\n"
|
||||
);
|
||||
}
|
||||
|
||||
TEST_CASE("tables/arbitrary-creation", "tables should be created from standard containers") {
|
||||
sol::state lua;
|
||||
lua.open_libraries(sol::lib::base);
|
||||
lua.set_function("test_one", test_table_return_one);
|
||||
lua.set_function("test_two", test_table_return_two);
|
||||
lua.set_function("test_three", test_table_return_three);
|
||||
|
||||
REQUIRE_NOTHROW(lua.script("a = test_one()"));
|
||||
REQUIRE_NOTHROW(lua.script("b = test_two()"));
|
||||
REQUIRE_NOTHROW(lua.script("c = test_three()"));
|
||||
|
||||
REQUIRE_NOTHROW(lua.script("assert(#a == 10, 'error')"));
|
||||
REQUIRE_NOTHROW(lua.script("assert(a[3] == 3, 'error')"));
|
||||
REQUIRE_NOTHROW(lua.script("assert(b.one == 1, 'error')"));
|
||||
REQUIRE_NOTHROW(lua.script("assert(b.three == 3, 'error')"));
|
||||
REQUIRE_NOTHROW(lua.script("assert(c.name == 'Rapptz', 'error')"));
|
||||
REQUIRE_NOTHROW(lua.script("assert(c.project == 'sol', 'error')"));
|
||||
|
||||
auto&& a = lua.get<sol::table>("a");
|
||||
auto&& b = lua.get<sol::table>("b");
|
||||
auto&& c = lua.get<sol::table>("c");
|
||||
|
||||
REQUIRE(a.size() == 10ULL);
|
||||
REQUIRE(a.get<int>(3) == 3);
|
||||
REQUIRE(b.get<int>("one") == 1);
|
||||
REQUIRE(b.get<int>("three") == 3);
|
||||
REQUIRE(c.get<std::string>("name") == "Rapptz");
|
||||
REQUIRE(c.get<std::string>("project") == "sol");
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user