#define SOL_CHECK_ARGUMENTS #include #include #include #include "test_stack_guard.hpp" TEST_CASE("environments/shadowing", "Environments can properly shadow and fallback on variables") { sol::state lua; lua["b"] = 2142; SECTION("no fallback") { sol::environment plain_env(lua, sol::create); lua.script("a = 24", plain_env); sol::optional maybe_env_a = plain_env["a"]; sol::optional maybe_global_a = lua["a"]; sol::optional maybe_env_b = plain_env["b"]; sol::optional maybe_global_b = lua["b"]; REQUIRE(maybe_env_a != sol::nullopt); REQUIRE(maybe_env_a.value() == 24); REQUIRE(maybe_env_b == sol::nullopt); REQUIRE(maybe_global_a == sol::nullopt); REQUIRE(maybe_global_b != sol::nullopt); REQUIRE(maybe_global_b.value() == 2142); } SECTION("fallback") { sol::environment env_with_fallback(lua, sol::create, lua.globals()); lua.script("a = 56", env_with_fallback, sol::default_on_error); sol::optional maybe_env_a = env_with_fallback["a"]; sol::optional maybe_global_a = lua["a"]; sol::optional maybe_env_b = env_with_fallback["b"]; sol::optional maybe_global_b = lua["b"]; REQUIRE(maybe_env_a != sol::nullopt); REQUIRE(maybe_env_a.value() == 56); REQUIRE(maybe_env_b != sol::nullopt); REQUIRE(maybe_env_b.value() == 2142); REQUIRE(maybe_global_a == sol::nullopt); REQUIRE(maybe_global_b != sol::nullopt); REQUIRE(maybe_global_b.value() == 2142); } }