18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-19 22:44:59 +01:00

fix: a test run neither reads nor saves the player's odometer store

the autotests (--test, Global.test.enabled) run the vehicles of the player's game: their odometers count from the
legacy or made up states in a store of their own, and nothing is written to eu07_odometers.txt
This commit is contained in:
jerrrrycho
2026-11-09 16:01:16 +01:00
parent e8735f1564
commit 729f15e878
3 changed files with 42 additions and 2 deletions

View File

@@ -1,6 +1,7 @@
#include "stdafx.h"
#include "doctest.h"
#include "vehicle.h"
#include "utilities/Globals.h"
#include "vehicle/odometer.h"
#include <filesystem>
@@ -360,3 +361,25 @@ TEST_CASE("odometer: two simulations saving one file keep each other's states")
CHECK(load().size() == 1);
}
}
TEST_CASE("odometer: a test run neither reads nor saves the player's store") {
if (false == Global.test.enabled)
CHECK(odometer::persistent());
// the autotests (--test) run the vehicles of the player's game, their runs aren't the player's
struct test_mode {
bool const was{Global.test.enabled};
test_mode() { Global.test.enabled = true; }
test_mode(test_mode const &) = delete;
test_mode &operator=(test_mode const &) = delete;
~test_mode() { Global.test.enabled = was; }
} const testmode;
// stops before the player's file is touched, if the gate is gone
REQUIRE_FALSE(odometer::persistent());
auto &states{odometer::states()};
states.record("tested", run(1000.0, 5000.0));
CHECK(states.find("tested").value() == doctest::Approx(1005.0));
CHECK(odometer::save());
// a save to the file would have cleared it
CHECK(states.modified());
}

View File

@@ -10,6 +10,7 @@ http://mozilla.org/MPL/2.0/.
#include "stdafx.h"
#include "vehicle/odometer.h"
#include "utilities/Globals.h"
#include "utilities/Logs.h"
#include "utilities/utilities.h"
@@ -227,7 +228,17 @@ std::filesystem::path file() {
return path.empty() ? std::filesystem::path{"eu07_odometers.txt"} : path;
}
bool persistent() {
return false == Global.test.enabled;
}
store &states() {
if (false == persistent()) {
// the vehicles of a test run count their odometers as usual, from the guessed or legacy states, with nothing
// read from or saved to the player's file
static store scratch;
return scratch;
}
static store odometers = [] {
store result;
std::ifstream input{file(), std::ios::binary};
@@ -292,6 +303,8 @@ bool save(store &States, std::filesystem::path const &File) {
}
bool save() {
if (false == persistent())
return true;
return save(states(), file());
}

View File

@@ -101,14 +101,18 @@ reading starting_state(store const &States, std::string const &Vehicle, std::fil
// (the counter shows the odometer). nothing for a counter without an offset
std::optional<double> counter_seed(float Offset, float Scale);
// the odometer store of the simulation, read from the user's settings directory on the first use
// the store of the simulation is the player's file: false in a test run (--test, Global.test.enabled), whose vehicles
// neither start from the player's states nor change them
bool persistent();
// the odometer store of the simulation, read from the user's settings directory on the first use. in a test run an
// empty one, not read from anywhere
store &states();
std::filesystem::path file();
// writes the store to the file when anything was recorded since, merged with what other simulations saved in the
// file meanwhile. it's written to a file of this process aside first, then put in place. returns false when the
// write failed
bool save(store &States, std::filesystem::path const &File);
// saves the store of the simulation to its file
// saves the store of the simulation to its file. a test run doesn't save it
bool save();
} // namespace odometer