| 1 |
// -*- compile-command: "./test_single_testcase.sh TTestCastleXmlUtils" -*- |
| 2 |
{ |
| 3 |
Copyright 2014-2022 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 CastleXmlUtils unit. } |
| 18 |
unit TestCastleXmlUtils; |
| 19 |
|
| 20 |
interface |
| 21 |
|
| 22 |
uses |
| 23 |
Classes, SysUtils, CastleTester; |
| 24 |
|
| 25 |
type |
| 26 |
TTestCastleXmlUtils = class(TCastleTestCase) |
| 27 |
published |
| 28 |
procedure TestReadResult; |
| 29 |
procedure TestAttributeReading; |
| 30 |
end; |
| 31 |
|
| 32 |
implementation |
| 33 |
|
| 34 |
uses DOM, CastleXmlUtils, CastleFilesUtils, CastleDownload, CastleColors; |
| 35 |
|
| 36 |
procedure TTestCastleXmlUtils.TestReadResult; |
| 37 |
var |
| 38 |
Doc: TXMLDocument; |
| 39 |
begin |
| 40 |
{ FPC's ReadXMLFile, and (consistenly) URLReadXML, have a non-standard |
| 41 |
way of initializing the 1st Doc parameter: it is always initialized, |
| 42 |
initially to nil, |
| 43 |
and may be non-nil even in case of exceptions (check: are you sure |
| 44 |
this can actually happen with ReadXMLFile implementation?). |
| 45 |
So always free it, do not depend on it being freed in case of trouble. |
| 46 |
|
| 47 |
The previous Doc value should be always overridden. } |
| 48 |
try |
| 49 |
Doc := TXMLDocument(Pointer(123)); |
| 50 |
try |
| 51 |
URLReadXML(Doc, 'castle-data:/not-existing-test.xml'); |
| 52 |
Fail('Should not reach here, non-existing-test.xml should not exist'); |
| 53 |
finally AssertTrue(Doc = nil); end; |
| 54 |
except on EDownloadError do begin { this is Ok } end; end; |
| 55 |
end; |
| 56 |
|
| 57 |
procedure TTestCastleXmlUtils.TestAttributeReading; |
| 58 |
var |
| 59 |
Doc: TXMLDocument; |
| 60 |
S: string; |
| 61 |
I: Integer; |
| 62 |
begin |
| 63 |
try |
| 64 |
URLReadXML(Doc, 'castle-data:/test.xml'); |
| 65 |
|
| 66 |
AssertTrue(Doc.DocumentElement.AttributeStringDef('some_string', 'blah') = 'some_string_value'); |
| 67 |
//AssertTrue(Doc.DocumentElement.AttributeCardinalDef('some_int', 666) = 123); |
| 68 |
AssertTrue(Doc.DocumentElement.AttributeSingleDef('some_int', 666.0) = 123.0); |
| 69 |
AssertTrue(Doc.DocumentElement.AttributeFloatDef('some_int', 666.0) = 123.0); |
| 70 |
|
| 71 |
AssertTrue(Doc.DocumentElement.AttributeStringDef('some_string_not_existing', 'blah') = 'blah'); |
| 72 |
//AssertTrue(Doc.DocumentElement.AttributeCardinalDef('some_int_not_existing', 666) = 666); |
| 73 |
AssertTrue(Doc.DocumentElement.AttributeSingleDef('some_int_not_existing', 666.0) = 666.0); |
| 74 |
AssertTrue(Doc.DocumentElement.AttributeFloatDef('some_int_not_existing', 666.0) = 666.0); |
| 75 |
|
| 76 |
AssertTrue(Doc.DocumentElement.AttributeString('some_string') = 'some_string_value'); |
| 77 |
AssertTrue(Doc.DocumentElement.AttributeCardinal('some_int') = 123); |
| 78 |
AssertTrue(Doc.DocumentElement.AttributeSingle('some_int') = 123.0); |
| 79 |
AssertTrue(Doc.DocumentElement.AttributeFloat('some_int') = 123.0); |
| 80 |
|
| 81 |
AssertTrue(Doc.DocumentElement.AttributeColor('some_color_hex').X = White.X); |
| 82 |
AssertTrue(Doc.DocumentElement.AttributeColor('some_color_hex').Y = White.Y); |
| 83 |
AssertTrue(Doc.DocumentElement.AttributeColor('some_color_hex').Z = White.Z); |
| 84 |
AssertTrue(Doc.DocumentElement.AttributeColor('some_color_hex').W = White.W); |
| 85 |
AssertTrue(Doc.DocumentElement.AttributeColor('some_color_vector').X = White.X); |
| 86 |
AssertTrue(Doc.DocumentElement.AttributeColor('some_color_vector').Y = White.Y); |
| 87 |
AssertTrue(Doc.DocumentElement.AttributeColor('some_color_vector').Z = White.Z); |
| 88 |
AssertTrue(Doc.DocumentElement.AttributeColor('some_color_vector').W = White.W); |
| 89 |
|
| 90 |
AssertTrue(Doc.DocumentElement.AttributeColorRGB('some_colorrgb_hex').X = WhiteRgb.X); |
| 91 |
AssertTrue(Doc.DocumentElement.AttributeColorRGB('some_colorrgb_hex').Y = WhiteRgb.Y); |
| 92 |
AssertTrue(Doc.DocumentElement.AttributeColorRGB('some_colorrgb_hex').Z = WhiteRgb.Z); |
| 93 |
AssertTrue(Doc.DocumentElement.AttributeColorRGB('some_colorrgb_vector').X = WhiteRgb.X); |
| 94 |
AssertTrue(Doc.DocumentElement.AttributeColorRGB('some_colorrgb_vector').Y = WhiteRgb.Y); |
| 95 |
AssertTrue(Doc.DocumentElement.AttributeColorRGB('some_colorrgb_vector').Z = WhiteRgb.Z); |
| 96 |
|
| 97 |
Doc.DocumentElement.AttributeSet('color_white_vector', White); |
| 98 |
AssertTrue(Doc.DocumentElement.AttributeColor('color_white_vector').X = White.X); |
| 99 |
AssertTrue(Doc.DocumentElement.AttributeColor('color_white_vector').Y = White.Y); |
| 100 |
AssertTrue(Doc.DocumentElement.AttributeColor('color_white_vector').Z = White.Z); |
| 101 |
AssertTrue(Doc.DocumentElement.AttributeColor('color_white_vector').W = White.W); |
| 102 |
Doc.DocumentElement.AttributeColorSet('color_white_hex', White); |
| 103 |
AssertTrue(Doc.DocumentElement.AttributeColor('color_white_hex').X = White.X); |
| 104 |
AssertTrue(Doc.DocumentElement.AttributeColor('color_white_hex').Y = White.Y); |
| 105 |
AssertTrue(Doc.DocumentElement.AttributeColor('color_white_hex').Z = White.Z); |
| 106 |
AssertTrue(Doc.DocumentElement.AttributeColor('color_white_hex').W = White.W); |
| 107 |
|
| 108 |
Doc.DocumentElement.AttributeSet('color_whitergb_vector', WhiteRGB); |
| 109 |
AssertTrue(Doc.DocumentElement.AttributeColorRGB('color_whitergb_vector').X = WhiteRGB.X); |
| 110 |
AssertTrue(Doc.DocumentElement.AttributeColorRGB('color_whitergb_vector').Y = WhiteRGB.Y); |
| 111 |
AssertTrue(Doc.DocumentElement.AttributeColorRGB('color_whitergb_vector').Z = WhiteRGB.Z); |
| 112 |
Doc.DocumentElement.AttributeColorSet('color_whitergb_hex', WhiteRGB); |
| 113 |
AssertTrue(Doc.DocumentElement.AttributeColorRGB('color_whitergb_hex').X = WhiteRGB.X); |
| 114 |
AssertTrue(Doc.DocumentElement.AttributeColorRGB('color_whitergb_hex').Y = WhiteRGB.Y); |
| 115 |
AssertTrue(Doc.DocumentElement.AttributeColorRGB('color_whitergb_hex').Z = WhiteRGB.Z); |
| 116 |
|
| 117 |
try Doc.DocumentElement.AttributeString ('some_string_not_existing'); AssertTrue(false); except on EDOMAttributeMissing do begin { good } end; end; |
| 118 |
try Doc.DocumentElement.AttributeCardinal('some_int_not_existing' ); AssertTrue(false); except on EDOMAttributeMissing do begin { good } end; end; |
| 119 |
try Doc.DocumentElement.AttributeSingle ('some_int_not_existing' ); AssertTrue(false); except on EDOMAttributeMissing do begin { good } end; end; |
| 120 |
try Doc.DocumentElement.AttributeFloat ('some_int_not_existing' ); AssertTrue(false); except on EDOMAttributeMissing do begin { good } end; end; |
| 121 |
|
| 122 |
S := 'blah'; |
| 123 |
AssertTrue(Doc.DocumentElement.AttributeString('some_string', S)); |
| 124 |
AssertTrue(S = 'some_string_value'); |
| 125 |
|
| 126 |
I := 456; |
| 127 |
AssertTrue(Doc.DocumentElement.AttributeInteger('some_int', I)); |
| 128 |
AssertTrue(I = 123); |
| 129 |
|
| 130 |
S := 'good'; |
| 131 |
AssertTrue(not Doc.DocumentElement.AttributeString('some_string_not_existing', S)); |
| 132 |
AssertTrue(S = 'good'); |
| 133 |
|
| 134 |
I := 456; |
| 135 |
AssertTrue(not Doc.DocumentElement.AttributeInteger('some_int_not_existing', I)); |
| 136 |
AssertTrue(I = 456); |
| 137 |
finally FreeAndNil(Doc); end; |
| 138 |
end; |
| 139 |
|
| 140 |
initialization |
| 141 |
RegisterTest(TTestCastleXmlUtils); |
| 142 |
end. |