| 1 |
// -*- compile-command: "./test_single_testcase.sh TTestCastleFilesUtils" -*- |
| 2 |
{ |
| 3 |
Copyright 2007-2024 Michalis Kamburelis. |
| 4 |
|
| 5 |
This file is part of "Castle Game Engine". |
| 6 |
|
| 7 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 8 |
included in this distribution, for details about the copyright. |
| 9 |
|
| 10 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 |
|
| 14 |
---------------------------------------------------------------------------- |
| 15 |
} |
| 16 |
|
| 17 |
{ Test OS-specific utilities mostly in CastleFilesUtils unit. } |
| 18 |
unit TestCastleFilesUtils; |
| 19 |
|
| 20 |
interface |
| 21 |
|
| 22 |
uses |
| 23 |
Classes, SysUtils, CastleTester; |
| 24 |
|
| 25 |
type |
| 26 |
TTestCastleFilesUtils = class(TCastleTestCase) |
| 27 |
published |
| 28 |
procedure TestPathDelim; |
| 29 |
procedure TestExeName; |
| 30 |
procedure TestTimer; |
| 31 |
{$ifdef UNIX} procedure TestHomePath; {$endif} |
| 32 |
procedure TestGetTempDir; |
| 33 |
procedure TestApplicationData; |
| 34 |
procedure TestCombinePaths; |
| 35 |
{ Test CombinePaths with BasePath relative. |
| 36 |
CGE editor "new unit" depends on it when it does |
| 37 |
FinalUnitRelative := CombinePaths(EditUnitDir.Text, LowerCase(EditUnitName.Text) + '.pas'); |
| 38 |
FinalDesignRelative := CombinePaths(EditDesignDir.Text, LowerCase(EditUnitName.Text) + '.castle-user-interface') |
| 39 |
} |
| 40 |
procedure TestCombinePathsRelative; |
| 41 |
end; |
| 42 |
|
| 43 |
implementation |
| 44 |
|
| 45 |
uses CastleUtils, CastleFindFiles, CastleFilesUtils, CastleTimeUtils |
| 46 |
{$ifdef UNIX}, BaseUnix {$endif}{$ifndef FPC}, IOUtils{$endif}; |
| 47 |
|
| 48 |
procedure TTestCastleFilesUtils.TestPathDelim; |
| 49 |
begin |
| 50 |
{ slash should be added / stripped on both Windows and Unix } |
| 51 |
|
| 52 |
AssertEquals('/tmp', ExclPathDelim('/tmp/')); |
| 53 |
AssertEquals('', ExclPathDelim('/')); |
| 54 |
AssertEquals('/tmp', ExclPathDelim('/tmp')); |
| 55 |
AssertEquals('', ExclPathDelim('')); |
| 56 |
|
| 57 |
AssertEquals('/tmp/', InclPathDelim('/tmp/')); |
| 58 |
AssertEquals('/', InclPathDelim('/')); |
| 59 |
AssertEquals('/tmp' + PathDelim, InclPathDelim('/tmp')); |
| 60 |
AssertEquals(PathDelim, InclPathDelim('')); |
| 61 |
|
| 62 |
{ backslash should be likewise added / stripped on Windows } |
| 63 |
{$ifdef MSWINDOWS} |
| 64 |
AssertEquals('\tmp', ExclPathDelim('\tmp\')); |
| 65 |
AssertEquals('', ExclPathDelim('\')); |
| 66 |
AssertEquals('\tmp', ExclPathDelim('\tmp')); |
| 67 |
AssertEquals('', ExclPathDelim('')); |
| 68 |
|
| 69 |
AssertEquals('\tmp\', InclPathDelim('\tmp\')); |
| 70 |
AssertEquals('\', InclPathDelim('\')); |
| 71 |
AssertEquals('\tmp\', InclPathDelim('\tmp')); |
| 72 |
AssertEquals('\', InclPathDelim('')); |
| 73 |
{$endif} |
| 74 |
|
| 75 |
{ backslash should not be treated like dir separator on Unix } |
| 76 |
{$ifdef UNIX} |
| 77 |
AssertEquals('\tmp\', ExclPathDelim('\tmp\')); |
| 78 |
AssertEquals('\', ExclPathDelim('\')); |
| 79 |
AssertEquals('\tmp', ExclPathDelim('\tmp')); |
| 80 |
AssertEquals('', ExclPathDelim('')); |
| 81 |
|
| 82 |
AssertEquals('\tmp\/', InclPathDelim('\tmp\')); |
| 83 |
AssertEquals('\/', InclPathDelim('\')); |
| 84 |
AssertEquals('\tmp/', InclPathDelim('\tmp')); |
| 85 |
AssertEquals('/', InclPathDelim('')); |
| 86 |
{$endif} |
| 87 |
end; |
| 88 |
|
| 89 |
procedure TTestCastleFilesUtils.TestExeName; |
| 90 |
begin |
| 91 |
try |
| 92 |
{$warnings off} // knowingly using deprecated below, for test |
| 93 |
ExeName; |
| 94 |
{$warnings on} |
| 95 |
except |
| 96 |
on E: EExeNameNotAvailable do |
| 97 |
begin |
| 98 |
// Writeln('ExeName not available, allowed on non-Windows:' +nl+ ExceptMessage(E)); |
| 99 |
{ This is an error under Windows, where ExeName is guaranteed to be available } |
| 100 |
{$ifdef MSWINDOWS} raise; {$endif} |
| 101 |
end; |
| 102 |
end; |
| 103 |
end; |
| 104 |
|
| 105 |
procedure TTestCastleFilesUtils.TestTimer; |
| 106 |
{var |
| 107 |
TimerStart: TTimerResult;} |
| 108 |
begin |
| 109 |
{ Writeln('Waiting 5 seconds...'); |
| 110 |
|
| 111 |
ProcessTimerBegin; |
| 112 |
TimerStart := Timer; |
| 113 |
Sleep(5000); |
| 114 |
Writeln( Format( |
| 115 |
'Sleep(5 seconds) done,' +nl+ |
| 116 |
'ProcessTimer reports that %f seconds elapsed,'+nl+ |
| 117 |
'Timer reports that %f seconds elapsed.', |
| 118 |
[ ProcessTimerEnd, TimerSeconds(Timer, TimerStart) ])); } |
| 119 |
end; |
| 120 |
|
| 121 |
{$ifdef UNIX} |
| 122 |
procedure TTestCastleFilesUtils.TestHomePath; |
| 123 |
begin |
| 124 |
{ Writeln('HomePath is ', HomePath); |
| 125 |
Writeln(' ~/expanded is ', ExpandHomePath('~/expanded')); } |
| 126 |
AssertTrue(ExpandHomePath('/bin/') = '/bin/'); |
| 127 |
AssertTrue(ExpandHomePath('~') = ExclPathDelim(HomePath)); |
| 128 |
AssertTrue(ExpandHomePath('~/') = InclPathDelim(HomePath)); |
| 129 |
end; |
| 130 |
{$endif} |
| 131 |
|
| 132 |
procedure TTestCastleFilesUtils.TestGetTempDir; |
| 133 |
begin |
| 134 |
// Writeln('TempDir: ', GetTempDir); |
| 135 |
{$ifdef FPC} |
| 136 |
GetTempDir; // ignore result, just make sure it doesn't raise errors |
| 137 |
{$else} |
| 138 |
TPath.GetTempPath; |
| 139 |
{$endif} |
| 140 |
end; |
| 141 |
|
| 142 |
procedure TTestCastleFilesUtils.TestApplicationData; |
| 143 |
begin |
| 144 |
AssertEquals('castle-data:/', ApplicationData('')); |
| 145 |
AssertEquals('castle-data:/xxx/yyy', ApplicationData('xxx/yyy')); |
| 146 |
end; |
| 147 |
|
| 148 |
procedure TTestCastleFilesUtils.TestCombinePaths; |
| 149 |
begin |
| 150 |
{$ifdef UNIX} |
| 151 |
AssertEquals('/a/b', CombinePaths('/a', 'b')); |
| 152 |
AssertEquals('/a/b', CombinePaths('/a/', 'b')); |
| 153 |
AssertEquals('/b', CombinePaths('/a/', '/b')); |
| 154 |
|
| 155 |
AssertEquals('/a/b/c', CombinePaths('/a/b', 'c')); |
| 156 |
AssertEquals('/a/b/c', CombinePaths('/a', 'b/c')); |
| 157 |
AssertEquals('/a/b/c', CombinePaths('/a', './b/c')); |
| 158 |
|
| 159 |
// check ../ handling |
| 160 |
AssertEquals('/b/c', CombinePaths('/a', '../b/c')); |
| 161 |
{$endif} |
| 162 |
|
| 163 |
{$ifdef MSWINDOWS} |
| 164 |
AssertEquals('c:\a\b', CombinePaths('c:\a', 'b')); |
| 165 |
AssertEquals('c:\a\b', CombinePaths('c:\a\', 'b')); |
| 166 |
AssertEquals('c:\b', CombinePaths('c:\a\', 'c:\b')); |
| 167 |
|
| 168 |
AssertEquals('c:/a\b', CombinePaths('c:/a', 'b')); |
| 169 |
AssertEquals('c:/a/b', CombinePaths('c:/a/', 'b')); |
| 170 |
AssertEquals('c:/b', CombinePaths('c:/a/', 'c:/b')); |
| 171 |
{$endif} |
| 172 |
end; |
| 173 |
|
| 174 |
procedure TTestCastleFilesUtils.TestCombinePathsRelative; |
| 175 |
begin |
| 176 |
{$ifdef UNIX} |
| 177 |
AssertEquals('a/b', CombinePaths('a', 'b')); |
| 178 |
AssertEquals('a/b/c', CombinePaths('a/b', 'c')); |
| 179 |
AssertEquals('c', CombinePaths('', 'c')); |
| 180 |
{$endif} |
| 181 |
|
| 182 |
{$ifdef MSWINDOWS} |
| 183 |
AssertEquals('a\b', CombinePaths('a', 'b')); |
| 184 |
AssertEquals('a/b\c', CombinePaths('a/b', 'c')); |
| 185 |
AssertEquals('c', CombinePaths('', 'c')); |
| 186 |
|
| 187 |
// check special case when RelPath is absolute on drive, but without drive letter |
| 188 |
AssertEquals('x:/foo', CombinePaths('x:/bar', '/foo')); |
| 189 |
AssertEquals('x:\foo', CombinePaths('x:\bar', '\foo')); |
| 190 |
{$endif} |
| 191 |
end; |
| 192 |
|
| 193 |
initialization |
| 194 |
RegisterTest(TTestCastleFilesUtils); |
| 195 |
end. |