Add test cases for operator[]

This commit is contained in:
Rapptz 2013-12-12 20:20:11 -05:00
parent af0097fb7f
commit 8db67834b2
2 changed files with 40 additions and 1 deletions

View File

@ -57,6 +57,6 @@ Sol makes use of C++11 features. GCC 4.7 and Clang 3.3 or higher should be able
## TODO ## TODO
- Support for `operator[]` based retrieval and modifying of tables. - Support for `operator[]` based retrieval and modifying of tables (mostly finished).
- Possibly document functions and classes via doxygen. - Possibly document functions and classes via doxygen.
- Provide more examples to showcase uses. - Provide more examples to showcase uses.

View File

@ -194,4 +194,43 @@ TEST_CASE("tables/functions_variables", "Check if tables and function calls work
auto rval = object(); auto rval = object();
lua.get<sol::table>("os").set_function("fun", &object::operator(), std::move(rval)); lua.get<sol::table>("os").set_function("fun", &object::operator(), std::move(rval));
REQUIRE_NOTHROW(run_script(lua)); REQUIRE_NOTHROW(run_script(lua));
}
TEST_CASE("tables/operator[]", "Check if operator[] retrieval and setting works properly") {
sol::state lua;
lua.open_libraries(sol::lib::base);
lua.script("foo = 20\nbar = \"hello world\"");
// basic retrieval
std::string bar = lua["bar"];
int foo = lua["foo"];
REQUIRE(bar == "hello world");
REQUIRE(foo == 20);
// basic setting
lua["bar"] = 20.4;
lua["foo"] = "goodbye";
// doesn't modify the actual values obviously.
REQUIRE(bar == "hello world");
REQUIRE(foo == 20);
// function setting
lua["test"] = plop_xyz;
REQUIRE_NOTHROW(lua.script("assert(test(10, 11, \"hello\") == 11)"));
// function retrieval
sol::function test = lua["test"];
REQUIRE(test.call<int>(10, 11, "hello") == 11);
// setting a lambda
lua["lamb"] = [](int x) {
return x * 2;
};
REQUIRE_NOTHROW(lua.script("assert(lamb(220) == 440)"));
// function retrieval of a lambda
sol::function lamb = lua["lamb"];
REQUIRE(lamb.call<int>(220) == 440);
} }