Files
CentrED/tests/UTestConfig.pas
T
aksdb 99749a5168 ✅ Add FPCUnit test suites for shared code and server
Introduces two headless FPCUnit runners under tests/ with 119 tests
covering the shared stream, cache, and packet layers, the MUL file
library, and the server config, region, account, and landscape
bounds logic. Each runner uses its own object output directory so
client and server PPUs with colliding unit names cannot pollute
each other's builds. Drone now builds and runs both suites before
packaging. Found on the way: two product defects fixed in the
previous commits.
2026-11-15 22:08:32 +01:00

144 lines
3.4 KiB
ObjectPascal

unit UTestConfig;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, fpcunit, testregistry, UConfig;
function BuildTestConfigXML: string;
function WriteTestConfigFile: string;
type
TTestConfig = class(TTestCase)
protected
procedure SetUp; override;
procedure TearDown; override;
private
FConfigPath: string;
procedure ExpectLoadFailure(const AXML, ATag: string);
published
procedure ValidConfigLoads;
procedure InvalidVersionRaises;
procedure MissingAccountsRaise;
procedure MissingMapRaises;
end;
implementation
function BuildTestConfigXML: string;
begin
Result :=
'<CEDConfig Version="3">' +
'<Port>2597</Port>' +
'<Map>' +
'<Map>map0.mul</Map>' +
'<StaIdx>staidx0.mul</StaIdx>' +
'<Statics>statics0.mul</Statics>' +
'<Width>10</Width>' +
'<Height>20</Height>' +
'</Map>' +
'<Tiledata>tiledata.mul</Tiledata>' +
'<Radarcol>radarcol.mul</Radarcol>' +
'<Regions>' +
'<Region><Name>britain</Name><Area><Rect x1="0" y1="0" x2="10" y2="10"/></Area></Region>' +
'<Region><Name>yew</Name><Area><Rect x1="5" y1="5" x2="15" y2="15"/></Area></Region>' +
'</Regions>' +
'<Accounts/>' +
'</CEDConfig>';
end;
function WriteTestConfigFile: string;
var
path: string;
stream: TStringStream;
begin
path := GetTempDir + 'centred-test-config.xml';
stream := TStringStream.Create(BuildTestConfigXML);
try
stream.SaveToFile(path);
finally
stream.Free;
end;
Result := path;
end;
procedure TTestConfig.SetUp;
begin
FConfigPath := '';
end;
procedure TTestConfig.TearDown;
begin
if FConfigPath <> '' then
DeleteFile(FConfigPath);
end;
procedure TTestConfig.ExpectLoadFailure(const AXML, ATag: string);
var
raised: Boolean;
begin
FConfigPath := GetTempDir + 'centred-test-config-' + ATag + '.xml';
with TStringStream.Create(AXML) do
try
SaveToFile(FConfigPath);
finally
Free;
end;
raised := False;
try
TConfig.Create(FConfigPath).Free;
except
on E: Exception do
raised := True;
end;
AssertTrue('loading must fail (' + ATag + ')', raised);
end;
procedure TTestConfig.ValidConfigLoads;
var
config: TConfig;
begin
FConfigPath := WriteTestConfigFile;
config := TConfig.Create(FConfigPath);
try
AssertEquals('port', 2597, config.Port);
AssertEquals('map file', 'map0.mul', config.Map.MapFile);
AssertEquals('width', 10, config.Map.Width);
AssertEquals('height', 20, config.Map.Height);
AssertEquals('tiledata', 'tiledata.mul', config.Tiledata);
AssertEquals('radarcol', 'radarcol.mul', config.Radarcol);
AssertEquals('regions', 2, config.Regions.Count);
AssertTrue('region by name', config.Regions.Find('britain') <> nil);
finally
config.Free;
end;
end;
procedure TTestConfig.InvalidVersionRaises;
var
xml: string;
begin
xml := StringReplace(BuildTestConfigXML, 'Version="3"', 'Version="2"', []);
ExpectLoadFailure(xml, 'badversion');
end;
procedure TTestConfig.MissingAccountsRaise;
begin
ExpectLoadFailure(StringReplace(BuildTestConfigXML, '<Accounts/>', '', []),
'noaccounts');
end;
procedure TTestConfig.MissingMapRaises;
begin
ExpectLoadFailure(StringReplace(BuildTestConfigXML,
'<Map><Map>map0.mul</Map><StaIdx>staidx0.mul</StaIdx><Statics>statics0.mul</Statics><Width>10</Width><Height>20</Height></Map>',
'', []), 'nomap');
end;
initialization
RegisterTests([TTestConfig]);
end.