| 1 |
{ |
| 2 |
Copyright 2020-2022 Michalis Kamburelis. |
| 3 |
|
| 4 |
This file is part of "Castle Game Engine". |
| 5 |
|
| 6 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 7 |
included in this distribution, for details about the copyright. |
| 8 |
|
| 9 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 |
|
| 13 |
---------------------------------------------------------------------------- |
| 14 |
} |
| 15 |
|
| 16 |
{ (Partially-)automatic tests of CGE with Delphi. } |
| 17 |
program delphi_tests; |
| 18 |
|
| 19 |
{$APPTYPE CONSOLE} |
| 20 |
{$ASSERTIONS ON} |
| 21 |
|
| 22 |
{ For EXTENDED_EQUALS_DOUBLE symbol. } |
| 23 |
{$I castleconf.inc} |
| 24 |
|
| 25 |
uses |
| 26 |
{ standard units } |
| 27 |
SysUtils, Classes, |
| 28 |
{ CGE units taken from FPC, only for Delphi compatibility } |
| 29 |
URIParser, DOM, |
| 30 |
{ CGE units } |
| 31 |
CastleUtils, CastleVectors, CastleColors, CastleStringUtils, |
| 32 |
CastleLog, CastleClassUtils, CastleProjection, CastleTimeUtils, |
| 33 |
CastleRectangles, CastleFindFiles, CastleFilesUtils, |
| 34 |
CastleUriUtils, CastleXmlUtils, CastleImages, CastleInternalDataUri, |
| 35 |
CastleDownload, CastleInternalSoundFile, CastleSoundBase, |
| 36 |
{ units specific to this project } |
| 37 |
CastleAssertions, |
| 38 |
TestFpJsonRTTI, TestFpJson, TestCastleConfig, TestCastleSerialization; |
| 39 |
|
| 40 |
procedure TestLog; |
| 41 |
begin |
| 42 |
WritelnLog('MyLog', 'Something to log: %d', [123]); |
| 43 |
end; |
| 44 |
|
| 45 |
procedure TestRects; |
| 46 |
var |
| 47 |
R: TFloatRectangle; |
| 48 |
begin |
| 49 |
R := FloatRectangle(2, 3, 10, 10); |
| 50 |
R := R.Grow(100, 100); |
| 51 |
Writeln(R.ToString); |
| 52 |
AssertRectsEqual(FloatRectangle(-98, -97, 210, 210), R); |
| 53 |
end; |
| 54 |
|
| 55 |
procedure FoundFile(const FileInfo: TFileInfo; Data: Pointer; var StopSearch: boolean); |
| 56 |
begin |
| 57 |
Writeln(' Found file: ', FileInfo.Name, ', ', FileInfo.URL); |
| 58 |
end; |
| 59 |
|
| 60 |
procedure TestPaths; |
| 61 |
begin |
| 62 |
// useful stuff from FPC RTL |
| 63 |
Writeln('ApplicationName: ', ApplicationName); |
| 64 |
Assert('delphi_tests' = ApplicationName); |
| 65 |
Writeln('AppConfigDir (user): ', GetAppConfigDir(false)); |
| 66 |
Writeln('AppConfigDir (global): ', GetAppConfigDir(true)); |
| 67 |
|
| 68 |
// CGE data |
| 69 |
Writeln('castle-data:/: ', ResolveCastleDataURL('castle-data:/')); |
| 70 |
|
| 71 |
(* |
| 72 |
// Old code, when we needed to set ApplicationDataOverride. |
| 73 |
// Now, the castle-data:/ works with Delphi out-of-the-box. |
| 74 |
|
| 75 |
{$ifdef MSWINDOWS} |
| 76 |
// Go 2 parent dirs up |
| 77 |
ExePath := ParentPath(ParentPath(ParentPath(ParamStr(0), false), false), false); |
| 78 |
{$else} |
| 79 |
ExePath := InclPathDelim(GetCurrentDir); |
| 80 |
{$endif} |
| 81 |
Writeln('Detected ExePath: ', ExePath); |
| 82 |
ApplicationDataOverride := FilenameToUriSafe(ExePath + 'data/'); |
| 83 |
Writeln('castle-data:/ after ApplicationDataOverride: ', ResolveCastleDataURL('castle-data:/')); |
| 84 |
*) |
| 85 |
|
| 86 |
Writeln('castle-data:/image.png: ', ResolveCastleDataURL('castle-data:/image.png')); |
| 87 |
|
| 88 |
// CastleFilesUtils test |
| 89 |
Writeln('GetTempFileNameCheck: ', GetTempFileNameCheck); |
| 90 |
|
| 91 |
// FindFiles |
| 92 |
Writeln('Searching recursively for *.dpr*:'); |
| 93 |
FindFiles('', '*.dpr*', false, FoundFile, nil, [ffRecursive]); |
| 94 |
Writeln('Searching recursively for *.dpr* in GetCurrentDir:'); |
| 95 |
FindFiles(FilenameToUriSafe(GetCurrentDir), '*.dpr*', false, FoundFile, nil, [ffRecursive]); |
| 96 |
end; |
| 97 |
|
| 98 |
procedure TestURI; |
| 99 |
var |
| 100 |
URI: TURI; |
| 101 |
begin |
| 102 |
URI := ParseURI('https://example.com/mypath/myfile.html'); |
| 103 |
Assert('https' = URI.Protocol); |
| 104 |
Assert('example.com' = URI.Host); |
| 105 |
Assert('/mypath/' = URI.Path); |
| 106 |
Assert('myfile.html' = URI.Document); |
| 107 |
end; |
| 108 |
|
| 109 |
procedure TestPercentEncoding; |
| 110 |
{$ifdef MSWINDOWS} |
| 111 |
var |
| 112 |
FilenamePart: String; |
| 113 |
FilenamePartPercent: String; |
| 114 |
FilenamePartUnescaped: String; |
| 115 |
Filename: String; |
| 116 |
FilenameAsUri: String; |
| 117 |
FilenameFromUri: String; |
| 118 |
{$endif} |
| 119 |
begin |
| 120 |
{$ifdef MSWINDOWS} |
| 121 |
AssertEquals('file:///c:/foo%254d.txt', FilenameToUriSafe('c:\foo%4d.txt')); |
| 122 |
AssertEquals('C:\fooM.txt', UriToFilenameSafe('file:///C:/foo%4d.txt')); |
| 123 |
AssertEquals('C:\foo%.txt', UriToFilenameSafe('file:///C:/foo%25.txt')); |
| 124 |
{$endif} |
| 125 |
{$ifdef UNIX} |
| 126 |
AssertEquals('file:///foo%254d.txt', FilenameToUriSafe('/foo%4d.txt')); |
| 127 |
AssertEquals('/fooM.txt', UriToFilenameSafe('file:///foo%4d.txt')); |
| 128 |
AssertEquals('/foo%.txt', UriToFilenameSafe('file:///foo%25.txt')); |
| 129 |
{$endif} |
| 130 |
|
| 131 |
{ Always UriToFilenameSafe and FilenameToUriSafe should reverse each other. } |
| 132 |
{$ifdef MSWINDOWS} |
| 133 |
AssertEquals('c:\foo%4d.txt', UriToFilenameSafe(FilenameToUriSafe('c:\foo%4d.txt'))); |
| 134 |
{ Actually this would be valid too: |
| 135 |
AssertEquals('file:///C:/foo%4d.txt', FilenameToUriSafe(UriToFilenameSafe('file:///C:/foo%4d.txt'))); |
| 136 |
But it's Ok that %4d gets converted to M, as char "M" is safe inside URI. } |
| 137 |
AssertEquals('file:///C:/fooM.txt', FilenameToUriSafe(UriToFilenameSafe('file:///C:/foo%4d.txt'))); |
| 138 |
{$endif} |
| 139 |
{$ifdef UNIX} |
| 140 |
AssertEquals('/foo%4d.txt', UriToFilenameSafe(FilenameToUriSafe('/foo%4d.txt'))); |
| 141 |
{ Actually this would be valid too: |
| 142 |
AssertEquals('file:///foo%25.txt', FilenameToUriSafe(UriToFilenameSafe('file:///foo%25.txt'))); |
| 143 |
But it's Ok that %4d gets converted to M, as char "M" is safe inside URI. } |
| 144 |
AssertEquals('file:///fooM.txt', FilenameToUriSafe(UriToFilenameSafe('file:///foo%4d.txt'))); |
| 145 |
{$endif} |
| 146 |
|
| 147 |
{$ifdef MSWINDOWS} |
| 148 |
Filename := 'C:\Users\cge\AppData\Local\test_local_filename_chars\config with Polish chars ćma źrebak żmija wąż królik.txt'; |
| 149 |
FilenameAsUri := FilenameToUriSafe(Filename); |
| 150 |
Assert('file:///C:/Users/cge/AppData/Local/test_local_filename_chars/config%20with%20Polish%20chars%20%C4%87ma%20%C5%BArebak%20%C5%BCmija%20w%C4%85%C5%BC%20kr%C3%B3lik.txt' = FilenameAsUri); |
| 151 |
FilenameFromUri := UriToFilenameSafe(FilenameAsUri); |
| 152 |
Assert(Filename = FilenameFromUri); |
| 153 |
|
| 154 |
FilenamePart := 'C:/Users/cge/AppData/Local/test_local_filename_chars/config with Polish chars ćma źrebak żmija wąż królik.txt'; |
| 155 |
FilenamePartPercent := InternalUriEscape(FilenamePart); |
| 156 |
Assert('C:/Users/cge/AppData/Local/test_local_filename_chars/config%20with%20Polish%20chars%20%C4%87ma%20%C5%BArebak%20%C5%BCmija%20w%C4%85%C5%BC%20kr%C3%B3lik.txt' = FilenamePartPercent); |
| 157 |
FilenamePartUnescaped := InternalUriUnescape(FilenamePartPercent); |
| 158 |
Assert(FilenamePart = FilenamePartUnescaped); |
| 159 |
{$endif} |
| 160 |
end; |
| 161 |
|
| 162 |
|
| 163 |
procedure TestTextRead; |
| 164 |
begin |
| 165 |
Writeln('test txt file reading: ', FileToString('castle-data:/test-file.txt')); |
| 166 |
Assert( |
| 167 |
'one line' + NL + |
| 168 |
'2nd line' + NL = FileToString('castle-data:/test-file.txt')); |
| 169 |
end; |
| 170 |
|
| 171 |
procedure TestXmlRead; |
| 172 |
var |
| 173 |
XML: TXMLDocument; |
| 174 |
begin |
| 175 |
XML := URLReadXML('castle-data:/test.xml'); |
| 176 |
try |
| 177 |
Writeln('test xml file reading: ', |
| 178 |
XML.DocumentElement.TagName, ' ', |
| 179 |
XML.DocumentElement.AttributeString('name'), ' ', |
| 180 |
XML.DocumentElement.Child('some_element') <> nil, ' ', |
| 181 |
XML.DocumentElement.Child('some_element').AttributeVector3('attribute').ToString); |
| 182 |
Assert('project' = XML.DocumentElement.TagName); |
| 183 |
Assert('castle_spine' = XML.DocumentElement.AttributeString('name')); |
| 184 |
Assert(XML.DocumentElement.Child('some_element') <> nil); |
| 185 |
Assert(TVector3.Equals(XML.DocumentElement.Child('some_element').AttributeVector3('attribute'), |
| 186 |
Vector3(1, 2, 3))); |
| 187 |
finally |
| 188 |
FreeAndNil(Xml); |
| 189 |
end; |
| 190 |
end; |
| 191 |
|
| 192 |
var |
| 193 |
TimeStart: TTimerResult; |
| 194 |
begin |
| 195 |
TimeStart := Timer; |
| 196 |
InitializeLog; |
| 197 |
|
| 198 |
TestLog; |
| 199 |
TestRects; |
| 200 |
TestPaths; |
| 201 |
TestURI; |
| 202 |
TestPercentEncoding; |
| 203 |
TestTextRead; |
| 204 |
TestXmlRead; |
| 205 |
|
| 206 |
TestFpJson1; |
| 207 |
// simpledemo form fcl-json examples |
| 208 |
TestFpJson2; |
| 209 |
|
| 210 |
TestRTTI; |
| 211 |
TestRTTICastle; |
| 212 |
TestCastleConfig1; |
| 213 |
|
| 214 |
// timer test |
| 215 |
Writeln('This was done in ', TimerSeconds(Timer, TimeStart):1:2, ' seconds'); |
| 216 |
|
| 217 |
//Readln; |
| 218 |
end. |