diff --git a/README.md b/README.md index e2b5dc02..7d4e2709 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/tests.cpp b/tests.cpp index 3600df44..0ed2659a 100644 --- a/tests.cpp +++ b/tests.cpp @@ -194,4 +194,43 @@ TEST_CASE("tables/functions_variables", "Check if tables and function calls work auto rval = object(); lua.get("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(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(220) == 440); } \ No newline at end of file