From 8db67834b2bc55a4e0310eee3e2e35a143fd6821 Mon Sep 17 00:00:00 2001 From: Rapptz Date: Thu, 12 Dec 2013 20:20:11 -0500 Subject: [PATCH] Add test cases for operator[] --- README.md | 2 +- tests.cpp | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) 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