sol2/test_environments.cpp

47 lines
1.5 KiB
C++
Raw Normal View History

#define SOL_CHECK_ARGUMENTS
#include <catch.hpp>
#include <sol.hpp>
#include <iostream>
#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<int> maybe_env_a = plain_env["a"];
sol::optional<int> maybe_global_a = lua["a"];
sol::optional<int> maybe_env_b = plain_env["b"];
sol::optional<int> 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<int> maybe_env_a = env_with_fallback["a"];
sol::optional<int> maybe_global_a = lua["a"];
sol::optional<int> maybe_env_b = env_with_fallback["b"];
sol::optional<int> 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);
}
}