18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-01 08:17:09 +01:00
Files
maszyna/tests/unit/test_vehicle_command.cpp
T
Hirek e5639e9d48 refactor(physics): the commands between the vehicles are VehicleCommand, not text
The vehicles of a train passed their commands to each other as strings
(SendCtrlToNext("MainCtrl", ...)), and RunCommand() found out what to do by
comparing the text with 54 names. A misspelt name compiled and did nothing.

- physics/vehiclecommand.h/.cpp: enum class VehicleCommand, one value for
  each command a vehicle knows, plus none and unknown; ParseVehicleCommand()
  reads a text, VehicleCommandName() gives the name back
- SendCtrlToNext(), SetInternalCommand(), RunCommand() and its groups take
  VehicleCommand; Load= and UnLoad= carry the name of the load as the
  argument of RunCommand()
- the text is read once, where a command comes as text: PutCommand() (the
  events, Lua, the AI). TCommand keeps that text, as the AI reads the
  commands it knows itself (SetVelocity...) from it
- two commands sent but known to no vehicle stay as they were, passed on and
  not done: EpFuseSwitch and SandboxAutoAllow
- removed the unused GetExternalCommand(), CommandOut, ValueOut and
  CommandLast (written, never read)
- the names are the ones the sceneries and scripts have always used, compared
  exactly; new tests check that every command reads back from its name

No change in behaviour: 1144/1144 tests (2 new), golden traces exact against
staging 62f7a29b.
2026-11-27 20:40:13 +01:00

44 lines
1.9 KiB
C++

#include "stdafx.h"
#include "doctest.h"
#include <string>
#include "physics/vehiclecommand.h"
// the commands of the vehicles read from their text (events, scripts, the AI) and named for the log: every command
// has the name the scenery files have always used, and reads back as itself
TEST_CASE("vehicle commands read back from their names") {
using enum VehicleCommand;
for (auto command{static_cast<int>(mainController)}; command <= static_cast<int>(setDamage); ++command) {
auto const value{static_cast<VehicleCommand>(command)};
auto const name{std::string{VehicleCommandName(value)}};
CAPTURE(name);
REQUIRE_FALSE(name.empty());
if (value == load || value == unload) {
continue; // these carry the name of the load after the =
}
CHECK(ParseVehicleCommand(name).command == value);
CHECK(ParseVehicleCommand(name).argument.empty());
}
}
TEST_CASE("vehicle commands: the empty text, an unknown one, the load") {
CHECK(ParseVehicleCommand("").command == VehicleCommand::none);
CHECK(ParseVehicleCommand("SetVelocity").command == VehicleCommand::unknown); // the AI's, not a vehicle's
CHECK(ParseVehicleCommand("mainctrl").command == VehicleCommand::unknown); // the names are compared exactly
CHECK(ParseVehicleCommand("MainCtrl").command == VehicleCommand::mainController);
CHECK(ParseVehicleCommand("SpeedCtrlUnit.Parking").command == VehicleCommand::speedControlParking);
auto const load{ParseVehicleCommand("Load=Coal")};
CHECK(load.command == VehicleCommand::load);
CHECK(load.argument == "Coal");
auto const unload{ParseVehicleCommand("UnLoad=passengers")};
CHECK(unload.command == VehicleCommand::unload);
CHECK(unload.argument == "passengers");
CHECK(VehicleCommandName(VehicleCommand::none).empty());
CHECK(VehicleCommandName(VehicleCommand::unknown).empty());
CHECK(VehicleCommandName(VehicleCommand::integratedController) == "EIMIC");
}