diff --git a/scripting/luaprogram.cpp b/scripting/luaprogram.cpp index 258929dc6..36d95d271 100644 --- a/scripting/luaprogram.cpp +++ b/scripting/luaprogram.cpp @@ -33,6 +33,8 @@ http://mozilla.org/MPL/2.0/. #include #include #include +#include +#include namespace script_screens { @@ -106,10 +108,13 @@ void count_hook(lua_State *L, lua_Debug * /*Debug*/) // objects of the api ------------------------------------------------------------------------------------------------- -template Type_ *push_object(lua_State *L, Type_ &&Object, char const *Metatable) +// puts the object in a new userdata with the metatable, left on the stack: an rvalue is moved in, an lvalue copied. +// what's stored is the plain type (no reference, no const), the one collect<> and check_object<> are given +template std::remove_cvref_t *push_object(lua_State *L, Type_ &&Object, char const *Metatable) { - auto *memory{lua_newuserdata(L, sizeof(Type_))}; - auto *object{new (memory) Type_(std::move(Object))}; + using object_type = std::remove_cvref_t; + auto *memory{lua_newuserdata(L, sizeof(object_type))}; + auto *object{new (memory) object_type(std::forward(Object))}; luaL_getmetatable(L, Metatable); lua_setmetatable(L, -2); return object; diff --git a/simulation/simulationenvironment.cpp b/simulation/simulationenvironment.cpp index 11e28b61f..10ef80f05 100644 --- a/simulation/simulationenvironment.cpp +++ b/simulation/simulationenvironment.cpp @@ -20,6 +20,22 @@ world_environment Environment; } // simulation +namespace { + +// turbidity of the sky under the given cloud cover +float +turbidity_of( float const Overcast ) { + + if( Overcast <= 0.20 ) { return 4.f; } + if( Overcast <= 0.40 ) { return 5.f; } + if( Overcast <= 0.60 ) { return 6.f; } + if( Overcast <= 0.70 ) { return 7.f; } + if( Overcast <= 0.80 ) { return 8.f; } + return 9.f; +} + +} // namespace + void world_environment::on_daylight_change() { @@ -68,16 +84,7 @@ world_environment::compute_weather() const { Global.AirTemperature > 1 ? "rain:" : "snow:"; - Global.fTurbidity = Global.Overcast <= 0.10 ? 4 : - Global.Overcast <= 0.20 ? 4 : - Global.Overcast <= 0.30 ? 5 : - Global.Overcast <= 0.40 ? 5 : - Global.Overcast <= 0.50 ? 6 : - Global.Overcast <= 0.60 ? 6 : - Global.Overcast <= 0.70 ? 7 : - Global.Overcast <= 0.80 ? 8 : - Global.Overcast <= 0.90 ? 9 : - 9; + Global.fTurbidity = turbidity_of( Global.Overcast ); } void diff --git a/tests/unit/CMakeLists.txt b/tests/unit/CMakeLists.txt index 82c3ffb9f..0f4e63d5c 100644 --- a/tests/unit/CMakeLists.txt +++ b/tests/unit/CMakeLists.txt @@ -37,6 +37,9 @@ endif() target_include_directories(${UNIT_TESTS_NAME} PRIVATE "${DEPS_DIR}/doctest") # the names the tests CAPTURE are often C strings, shown as text rather than pointers target_compile_definitions(${UNIT_TESTS_NAME} PRIVATE EU07_UNIT_TESTS_DATA="${CMAKE_CURRENT_SOURCE_DIR}/data" DOCTEST_CONFIG_TREAT_CHAR_STAR_AS_STRING) +# the folders of the tests that write files (the scripts of test_script_screen_host.cpp) are made in the build folder, +# and removed by the test that made them +target_compile_definitions(${UNIT_TESTS_NAME} PRIVATE EU07_UNIT_TESTS_SCRATCH="${CMAKE_CURRENT_BINARY_DIR}") if (MSVC) target_compile_options(${UNIT_TESTS_NAME} PRIVATE /bigobj) endif() diff --git a/tests/unit/test_script_screen_host.cpp b/tests/unit/test_script_screen_host.cpp index 705082dbd..355b9b9e8 100644 --- a/tests/unit/test_script_screen_host.cpp +++ b/tests/unit/test_script_screen_host.cpp @@ -77,17 +77,20 @@ struct recording_texture : ITexture { std::vector pixels; }; -// a folder of its own for the scripts of a test, removed with the object +// a folder of its own for the scripts of a test, removed with the object. it's made in the build folder of the tests +// (EU07_UNIT_TESTS_SCRATCH, see CMakeLists.txt), not in the system's temporary folder, which anyone can write to. the +// scripts are one level down in it, so a file a test puts next to their folder ("../name") is still inside the folder +// of the test, and is removed with it class script_folder { public: explicit script_folder(std::string const &Name) - : m_folder{std::filesystem::temp_directory_path() / std::format("eu07_script_screen_test_{}", Name)} { - std::filesystem::remove_all(m_folder); + : m_root{std::filesystem::path{EU07_UNIT_TESTS_SCRATCH} / std::format("eu07_script_screen_test_{}", Name)}, m_folder{m_root / "scripts"} { + std::filesystem::remove_all(m_root); std::filesystem::create_directories(m_folder); } ~script_folder() { std::error_code error; - std::filesystem::remove_all(m_folder, error); + std::filesystem::remove_all(m_root, error); } script_folder(script_folder const &) = delete; script_folder &operator=(script_folder const &) = delete; @@ -105,7 +108,8 @@ class script_folder { } private: - std::filesystem::path m_folder; + std::filesystem::path m_root; // the folder of the test + std::filesystem::path m_folder; // the folder of its scripts }; // runs frames until the condition holds, or gives up after a few seconds @@ -484,6 +488,7 @@ TEST_CASE("lua screens: the texture gets the bottom row first, as from the pytho TEST_CASE("lua screens: a script reaches no files outside its folder, nor the system, nor ffi") { script_folder const folder{"lua_sandbox"}; + // next to the folder of the scripts, where the script mustn't reach folder.write("../outside.txt", std::string_view{"outside"}); folder.write("data/inside.txt", std::string_view{"inside"}); auto const script{folder.write("sandbox.lua", sandbox_script)}; @@ -497,7 +502,6 @@ TEST_CASE("lua screens: a script reaches no files outside its folder, nor the sy REQUIRE(texture.pixels.size() == 4); CHECK(texture.pixels[0] == 255); queue.exit(); - std::filesystem::remove(std::filesystem::temp_directory_path() / "outside.txt"); } TEST_CASE("lua screens: precompiled code isn't loaded") {