mirror of
https://github.com/ThePhD/sol2.git
synced 2024-03-22 13:10:44 +08:00
Add test cases for operator[]
This commit is contained in:
parent
af0097fb7f
commit
8db67834b2
|
@ -57,6 +57,6 @@ Sol makes use of C++11 features. GCC 4.7 and Clang 3.3 or higher should be able
|
|||
|
||||
## 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.
|
||||
- Provide more examples to showcase uses.
|
||||
|
|
39
tests.cpp
39
tests.cpp
|
@ -194,4 +194,43 @@ TEST_CASE("tables/functions_variables", "Check if tables and function calls work
|
|||
auto rval = object();
|
||||
lua.get<sol::table>("os").set_function("fun", &object::operator(), std::move(rval));
|
||||
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);
|
||||
}
|
Loading…
Reference in New Issue
Block a user