| 1 |
// -*- compile-command: "./test_single_testcase.sh TTestCastleScript" -*- |
| 2 |
{ |
| 3 |
Copyright 2007-2023 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 CastleScript unit. } |
| 18 |
unit TestCastleScript; |
| 19 |
|
| 20 |
{ Parts of CastleScript implementation right now use Pascal generics |
| 21 |
in ways that don't compile with Delphi. |
| 22 |
Symbol CASTLE_SCRIPT_FPC is in practice equal to just FPC, |
| 23 |
but having a dedicated symbol allows to easier grep for it and eventually fix. } |
| 24 |
{$ifdef FPC} |
| 25 |
{$define CASTLE_SCRIPT_FPC} |
| 26 |
{$endif} |
| 27 |
|
| 28 |
interface |
| 29 |
|
| 30 |
uses |
| 31 |
Classes, SysUtils, CastleTester; |
| 32 |
|
| 33 |
type |
| 34 |
TTestCastleScript = class(TCastleTestCase) |
| 35 |
published |
| 36 |
procedure Test1; |
| 37 |
procedure TestCodeCreatedExprs; |
| 38 |
procedure TestInheritsFrom; |
| 39 |
procedure TestFloatPrograms; |
| 40 |
procedure TestVariousTypesPrograms; |
| 41 |
{$ifdef CASTLE_SCRIPT_FPC} |
| 42 |
procedure TestArrays; |
| 43 |
{$endif CASTLE_SCRIPT_FPC} |
| 44 |
procedure TestBools; |
| 45 |
procedure TestParseConstantIntExpression; |
| 46 |
procedure TestInvalidOps; |
| 47 |
procedure TestTryExecuteMath; |
| 48 |
procedure TestCoalesce; |
| 49 |
procedure TestDivision; |
| 50 |
end; |
| 51 |
|
| 52 |
implementation |
| 53 |
|
| 54 |
uses CastleVectors, CastleScript, CastleScriptLexer, CastleScriptParser, |
| 55 |
CastleStringUtils, CastleScriptCoreFunctions, CastleClassUtils, |
| 56 |
CastleFilesUtils, CastleScriptArrays; |
| 57 |
|
| 58 |
procedure TTestCastleScript.Test1; |
| 59 |
|
| 60 |
procedure WritelnLexer(const s: string); |
| 61 |
var |
| 62 |
Lexer: TCasScriptLexer; |
| 63 |
begin |
| 64 |
Lexer := TCasScriptLexer.Create(s); |
| 65 |
repeat |
| 66 |
Writeln(Lexer.TokenDescription); |
| 67 |
Lexer.NextToken; |
| 68 |
until Lexer.token = tokEnd; |
| 69 |
Lexer.Free; |
| 70 |
end; |
| 71 |
|
| 72 |
begin |
| 73 |
{ Interactive test: |
| 74 |
WritelnLexer('-10 * Pi'); |
| 75 |
} |
| 76 |
AssertSameValue(-10 * Pi, ParseConstantFloatExpression('-10 * Pi')); |
| 77 |
end; |
| 78 |
|
| 79 |
procedure TTestCastleScript.TestCodeCreatedExprs; |
| 80 |
var |
| 81 |
Expr: TCasScriptExpression; |
| 82 |
MyVariable: TCasScriptFloat; |
| 83 |
begin |
| 84 |
Expr := TCasScriptAdd.Create([ |
| 85 |
TCasScriptSin.Create([TCasScriptFloat.Create(false, 3)]), |
| 86 |
TCasScriptFloat.Create(false, 10), |
| 87 |
TCasScriptFloat.Create(false, 1) |
| 88 |
]); |
| 89 |
try |
| 90 |
AssertTrue((Expr.Execute as TCasScriptFloat).Value = sin(3) + 10 + 1); |
| 91 |
finally FreeAndNil(Expr) end; |
| 92 |
|
| 93 |
{ BTW, this should not compile (the parameter-less constructor should be |
| 94 |
hidden by TCasScriptValue, so it's not a problem that descendants have |
| 95 |
overloaded constructors with "overload" keyword). } |
| 96 |
// MyVariable := TCasScriptFloat.Create; |
| 97 |
|
| 98 |
MyVariable := TCasScriptFloat.Create(false, 3); |
| 99 |
Expr := TCasScriptAdd.Create([ |
| 100 |
TCasScriptSin.Create([MyVariable]), |
| 101 |
TCasScriptFloat.Create(false, 10), |
| 102 |
TCasScriptFloat.Create(false, 1) |
| 103 |
]); |
| 104 |
try |
| 105 |
AssertTrue((Expr.Execute as TCasScriptFloat).Value = sin(3) + 10 + 1); |
| 106 |
|
| 107 |
MyVariable.Value := 4; |
| 108 |
AssertTrue((Expr.Execute as TCasScriptFloat).Value = sin(4) + 10 + 1); |
| 109 |
|
| 110 |
MyVariable.Value := 5; |
| 111 |
AssertTrue((Expr.Execute as TCasScriptFloat).Value = sin(5) + 10 + 1); |
| 112 |
finally FreeAndNil(Expr) end; |
| 113 |
end; |
| 114 |
|
| 115 |
procedure TTestCastleScript.TestInheritsFrom; |
| 116 |
begin |
| 117 |
AssertTrue(TCasScriptFloat.InheritsFrom(TCasScriptFloat)); |
| 118 |
AssertTrue(TCasScriptValue.InheritsFrom(TCasScriptValue)); |
| 119 |
AssertTrue(TCasScriptFloat.InheritsFrom(TCasScriptValue)); |
| 120 |
AssertTrue(not TCasScriptValue.InheritsFrom(TCasScriptFloat)); |
| 121 |
end; |
| 122 |
|
| 123 |
procedure TTestCastleScript.TestFloatPrograms; |
| 124 |
var |
| 125 |
Vars: array [0..3] of TCasScriptFloat; |
| 126 |
VarsAsValue: array [Low(Vars)..High(Vars)] of TCasScriptValue absolute Vars; |
| 127 |
Prog: TCasScriptProgram; |
| 128 |
I: Integer; |
| 129 |
begin |
| 130 |
for I := 0 to High(Vars) do |
| 131 |
begin |
| 132 |
Vars[I] := TCasScriptFloat.Create(true); |
| 133 |
Vars[I].Value := I; |
| 134 |
Vars[I].Name := 'x' + IntToStr(I); |
| 135 |
end; |
| 136 |
|
| 137 |
try |
| 138 |
Prog := ParseProgram(FileToString('castle-data:/test_script.kscript'), VarsAsValue); |
| 139 |
Prog.ExecuteFunction('main', []); |
| 140 |
FreeAndNil(Prog); |
| 141 |
|
| 142 |
AssertTrue(Vars[0].Value = 0); |
| 143 |
AssertTrue(Vars[1].Value = 3); |
| 144 |
AssertTrue(Vars[2].Value = 100 + 100 + 2 * 3); |
| 145 |
AssertTrue(Vars[3].Value = 666); |
| 146 |
finally |
| 147 |
for I := 0 to High(Vars) do FreeAndNil(Vars[I]); |
| 148 |
end; |
| 149 |
end; |
| 150 |
|
| 151 |
procedure TTestCastleScript.TestVariousTypesPrograms; |
| 152 |
const |
| 153 |
Epsilon = 0.01; |
| 154 |
|
| 155 |
var |
| 156 |
Prog: TCasScriptProgram; |
| 157 |
|
| 158 |
procedure ExecuteExpectError; |
| 159 |
begin |
| 160 |
try |
| 161 |
Prog.ExecuteFunction('main', []); |
| 162 |
Fail('should not get here'); |
| 163 |
except |
| 164 |
on ECasScriptError do ; |
| 165 |
end; |
| 166 |
end; |
| 167 |
|
| 168 |
var |
| 169 |
Vars: TCasScriptValueList; |
| 170 |
begin |
| 171 |
Vars := TCasScriptValueList.Create(true); |
| 172 |
try |
| 173 |
Vars.Add(TCasScriptInteger.Create(true, 23)); |
| 174 |
Vars.Add(TCasScriptFloat.Create(true, 3.14)); |
| 175 |
Vars.Add(TCasScriptBoolean.Create(true, false)); |
| 176 |
Vars.Add(TCasScriptString.Create(true, 'foo')); |
| 177 |
|
| 178 |
Vars[0].Name := 'my_int'; |
| 179 |
Vars[1].Name := 'my_float'; |
| 180 |
Vars[2].Name := 'my_bool'; |
| 181 |
Vars[3].Name := 'my_string'; |
| 182 |
|
| 183 |
Prog := ParseProgram('function main() 666', Vars); |
| 184 |
{ return any dummy value } |
| 185 |
Prog.ExecuteFunction('main', []); |
| 186 |
FreeAndNil(Prog); |
| 187 |
|
| 188 |
AssertTrue((Vars[0] as TCasScriptInteger).Value = 23); |
| 189 |
AssertTrue((Vars[1] as TCasScriptFloat).Value = 3.14); |
| 190 |
AssertTrue((Vars[2] as TCasScriptBoolean).Value = false); |
| 191 |
AssertTrue((Vars[3] as TCasScriptString).Value = 'foo'); |
| 192 |
|
| 193 |
Prog := ParseProgram(FileToString('castle-data:/test_script2.kscript'), Vars); |
| 194 |
Prog.ExecuteFunction('main', []); |
| 195 |
FreeAndNil(Prog); |
| 196 |
|
| 197 |
AssertTrue((Vars[0] as TCasScriptInteger).Value = 23 + 12); |
| 198 |
AssertSameValue(Sqrt(3.14 + 2.0), (Vars[1] as TCasScriptFloat).Value); |
| 199 |
AssertTrue((Vars[2] as TCasScriptBoolean).Value = true); |
| 200 |
AssertTrue((Vars[3] as TCasScriptString).Value = 'barfooxyz'); |
| 201 |
|
| 202 |
{ should raise ECasScriptError } |
| 203 |
|
| 204 |
Prog := ParseProgram('function main() my_int := 123.0', Vars); |
| 205 |
ExecuteExpectError; |
| 206 |
FreeAndNil(Prog); |
| 207 |
|
| 208 |
Prog := ParseProgram('function main() my_int := string(123.0)', Vars); |
| 209 |
ExecuteExpectError; |
| 210 |
FreeAndNil(Prog); |
| 211 |
|
| 212 |
{ test int() } |
| 213 |
|
| 214 |
Prog := ParseProgram('function main() my_int := int(3.14)', Vars); |
| 215 |
Prog.ExecuteFunction('main', []); |
| 216 |
FreeAndNil(Prog); |
| 217 |
AssertTrue((Vars[0] as TCasScriptInteger).Value = 3); |
| 218 |
|
| 219 |
Prog := ParseProgram('function main() my_int := int(-3.14)', Vars); |
| 220 |
Prog.ExecuteFunction('main', []); |
| 221 |
FreeAndNil(Prog); |
| 222 |
AssertTrue((Vars[0] as TCasScriptInteger).Value = -3); |
| 223 |
|
| 224 |
Prog := ParseProgram('function main() my_int := int(666)', Vars); |
| 225 |
Prog.ExecuteFunction('main', []); |
| 226 |
FreeAndNil(Prog); |
| 227 |
AssertTrue((Vars[0] as TCasScriptInteger).Value = 666); |
| 228 |
|
| 229 |
Prog := ParseProgram('function main() my_int := int(''44'')', Vars); |
| 230 |
Prog.ExecuteFunction('main', []); |
| 231 |
FreeAndNil(Prog); |
| 232 |
|
| 233 |
AssertTrue((Vars[0] as TCasScriptInteger).Value = 44); |
| 234 |
Prog := ParseProgram('function main() my_int := int(''blah'')', Vars); |
| 235 |
ExecuteExpectError; |
| 236 |
FreeAndNil(Prog); |
| 237 |
|
| 238 |
Prog := ParseProgram('function main() my_int := int(false)', Vars); |
| 239 |
Prog.ExecuteFunction('main', []); |
| 240 |
FreeAndNil(Prog); |
| 241 |
AssertTrue((Vars[0] as TCasScriptInteger).Value = 0); |
| 242 |
|
| 243 |
Prog := ParseProgram('function main() my_int := int(true)', Vars); |
| 244 |
Prog.ExecuteFunction('main', []); |
| 245 |
FreeAndNil(Prog); |
| 246 |
AssertTrue((Vars[0] as TCasScriptInteger).Value = 1); |
| 247 |
|
| 248 |
Prog := ParseProgram('function main() my_int := int(5 < 6)', Vars); |
| 249 |
Prog.ExecuteFunction('main', []); |
| 250 |
FreeAndNil(Prog); |
| 251 |
AssertTrue((Vars[0] as TCasScriptInteger).Value = 1); |
| 252 |
|
| 253 |
{ test float() } |
| 254 |
|
| 255 |
Prog := ParseProgram('function main() my_float := float(3.14)', Vars); |
| 256 |
Prog.ExecuteFunction('main', []); |
| 257 |
FreeAndNil(Prog); |
| 258 |
AssertSameValue((Vars[1] as TCasScriptFloat).Value, 3.14, Epsilon); |
| 259 |
|
| 260 |
Prog := ParseProgram('function main() my_float := float(-3.14)', Vars); |
| 261 |
Prog.ExecuteFunction('main', []); |
| 262 |
FreeAndNil(Prog); |
| 263 |
AssertSameValue((Vars[1] as TCasScriptFloat).Value, -3.14, Epsilon); |
| 264 |
|
| 265 |
Prog := ParseProgram('function main() my_float := float(666)', Vars); |
| 266 |
Prog.ExecuteFunction('main', []); |
| 267 |
FreeAndNil(Prog); |
| 268 |
AssertSameValue((Vars[1] as TCasScriptFloat).Value, 666, Epsilon); |
| 269 |
|
| 270 |
Prog := ParseProgram('function main() my_float := 123', Vars); |
| 271 |
Prog.ExecuteFunction('main', []); |
| 272 |
FreeAndNil(Prog); |
| 273 |
AssertSameValue((Vars[1] as TCasScriptFloat).Value, 123, Epsilon); |
| 274 |
|
| 275 |
Prog := ParseProgram('function main() my_float := float(''44.456'')', Vars); |
| 276 |
Prog.ExecuteFunction('main', []); |
| 277 |
FreeAndNil(Prog); |
| 278 |
AssertSameValue((Vars[1] as TCasScriptFloat).Value, 44.456, Epsilon); |
| 279 |
|
| 280 |
Prog := ParseProgram('function main() my_float := float(false)', Vars); |
| 281 |
Prog.ExecuteFunction('main', []); |
| 282 |
FreeAndNil(Prog); |
| 283 |
AssertSameValue((Vars[1] as TCasScriptFloat).Value, 0, Epsilon); |
| 284 |
|
| 285 |
Prog := ParseProgram('function main() my_float := float(true)', Vars); |
| 286 |
Prog.ExecuteFunction('main', []); |
| 287 |
FreeAndNil(Prog); |
| 288 |
AssertSameValue((Vars[1] as TCasScriptFloat).Value, 1, Epsilon); |
| 289 |
|
| 290 |
Prog := ParseProgram('function main() my_float := float(0 <> 0)', Vars); |
| 291 |
Prog.ExecuteFunction('main', []); |
| 292 |
FreeAndNil(Prog); |
| 293 |
AssertSameValue((Vars[1] as TCasScriptFloat).Value, 0, Epsilon); |
| 294 |
|
| 295 |
{ test bool() } |
| 296 |
|
| 297 |
Prog := ParseProgram('function main() my_bool := bool(3.14)', Vars); |
| 298 |
Prog.ExecuteFunction('main', []); |
| 299 |
FreeAndNil(Prog); |
| 300 |
AssertTrue((Vars[2] as TCasScriptBoolean).Value = true); |
| 301 |
|
| 302 |
Prog := ParseProgram('function main() my_bool := bool(0.0)', Vars); |
| 303 |
Prog.ExecuteFunction('main', []); |
| 304 |
FreeAndNil(Prog); |
| 305 |
AssertTrue((Vars[2] as TCasScriptBoolean).Value = false); |
| 306 |
|
| 307 |
Prog := ParseProgram('function main() my_bool := bool(0)', Vars); |
| 308 |
Prog.ExecuteFunction('main', []); |
| 309 |
FreeAndNil(Prog); |
| 310 |
AssertTrue((Vars[2] as TCasScriptBoolean).Value = false); |
| 311 |
|
| 312 |
Prog := ParseProgram('function main() my_bool := bool(''44.456'')', Vars); |
| 313 |
ExecuteExpectError; |
| 314 |
FreeAndNil(Prog); |
| 315 |
|
| 316 |
Prog := ParseProgram('function main() my_bool := bool(''faLSE'')', Vars); |
| 317 |
Prog.ExecuteFunction('main', []); |
| 318 |
FreeAndNil(Prog); |
| 319 |
AssertTrue((Vars[2] as TCasScriptBoolean).Value = false); |
| 320 |
|
| 321 |
Prog := ParseProgram('function main() my_bool := bool(''true'')', Vars); |
| 322 |
Prog.ExecuteFunction('main', []); |
| 323 |
FreeAndNil(Prog); |
| 324 |
AssertTrue((Vars[2] as TCasScriptBoolean).Value = true); |
| 325 |
|
| 326 |
Prog := ParseProgram('function main() my_bool := bool(false)', Vars); |
| 327 |
Prog.ExecuteFunction('main', []); |
| 328 |
FreeAndNil(Prog); |
| 329 |
AssertTrue((Vars[2] as TCasScriptBoolean).Value = false); |
| 330 |
|
| 331 |
Prog := ParseProgram('function main() my_bool := bool(true)', Vars); |
| 332 |
Prog.ExecuteFunction('main', []); |
| 333 |
FreeAndNil(Prog); |
| 334 |
AssertTrue((Vars[2] as TCasScriptBoolean).Value = true); |
| 335 |
|
| 336 |
Prog := ParseProgram('function main() my_bool := bool(0 <> 0)', Vars); |
| 337 |
Prog.ExecuteFunction('main', []); |
| 338 |
FreeAndNil(Prog); |
| 339 |
AssertTrue((Vars[2] as TCasScriptBoolean).Value = false); |
| 340 |
|
| 341 |
{ test string() } |
| 342 |
|
| 343 |
Prog := ParseProgram('function main() my_string := string(3.14)', Vars); |
| 344 |
Prog.ExecuteFunction('main', []); |
| 345 |
FreeAndNil(Prog); |
| 346 |
AssertTrue((Vars[3] as TCasScriptString).Value = '3.14'); |
| 347 |
|
| 348 |
Prog := ParseProgram('function main() my_string := string(0.0)', Vars); |
| 349 |
Prog.ExecuteFunction('main', []); |
| 350 |
FreeAndNil(Prog); |
| 351 |
AssertTrue((Vars[3] as TCasScriptString).Value = '0'); |
| 352 |
|
| 353 |
Prog := ParseProgram('function main() my_string := string(0)', Vars); |
| 354 |
Prog.ExecuteFunction('main', []); |
| 355 |
FreeAndNil(Prog); |
| 356 |
AssertTrue((Vars[3] as TCasScriptString).Value = '0'); |
| 357 |
|
| 358 |
Prog := ParseProgram('function main() my_string := string(''44.456hoho'')', Vars); |
| 359 |
Prog.ExecuteFunction('main', []); |
| 360 |
FreeAndNil(Prog); |
| 361 |
AssertTrue((Vars[3] as TCasScriptString).Value = '44.456hoho'); |
| 362 |
|
| 363 |
Prog := ParseProgram('function main() my_string := string(true)', Vars); |
| 364 |
Prog.ExecuteFunction('main', []); |
| 365 |
FreeAndNil(Prog); |
| 366 |
AssertTrue((Vars[3] as TCasScriptString).Value = 'true'); |
| 367 |
|
| 368 |
Prog := ParseProgram('function main() my_string := string(false)', Vars); |
| 369 |
Prog.ExecuteFunction('main', []); |
| 370 |
FreeAndNil(Prog); |
| 371 |
AssertTrue((Vars[3] as TCasScriptString).Value = 'false'); |
| 372 |
|
| 373 |
Prog := ParseProgram('function main() my_string := string(0 <> 0)', Vars); |
| 374 |
Prog.ExecuteFunction('main', []); |
| 375 |
FreeAndNil(Prog); |
| 376 |
AssertTrue((Vars[3] as TCasScriptString).Value = 'false'); |
| 377 |
|
| 378 |
{ test if() } |
| 379 |
|
| 380 |
Prog := ParseProgram(FileToString('castle-data:/test_script3.kscript'), Vars); |
| 381 |
Prog.ExecuteFunction('main', []); |
| 382 |
AssertTrue((Vars[0] as TCasScriptInteger).Value = 12); |
| 383 |
AssertTrue((Vars[1] as TCasScriptFloat).Value = 0); |
| 384 |
AssertTrue((Vars[2] as TCasScriptBoolean).Value = true); |
| 385 |
|
| 386 |
Prog.ExecuteFunction('main_alt', []); |
| 387 |
AssertTrue((Vars[0] as TCasScriptInteger).Value = 44); |
| 388 |
AssertTrue((Vars[1] as TCasScriptFloat).Value = 13); |
| 389 |
AssertTrue((Vars[2] as TCasScriptBoolean).Value = false); |
| 390 |
|
| 391 |
{ test while() } |
| 392 |
|
| 393 |
Prog.ExecuteFunction('main_alt_while', []); |
| 394 |
AssertTrue((Vars[0] as TCasScriptInteger).Value = 13); |
| 395 |
AssertTrue((Vars[3] as TCasScriptString).Value = 'foo 1 2 3 4 5 6 7 8 9 10 11 12'); |
| 396 |
|
| 397 |
{ test for() } |
| 398 |
|
| 399 |
Prog.ExecuteFunction('main_alt_for', []); |
| 400 |
AssertTrue((Vars[3] as TCasScriptString).Value = 'xxxxxxxxxxxfooxxxxxxxxxxx'); |
| 401 |
|
| 402 |
FreeAndNil(Prog); |
| 403 |
|
| 404 |
{ test not Writeable } |
| 405 |
Vars[0].Writeable := false; |
| 406 |
try |
| 407 |
Prog := ParseProgram('function main() my_int := 123', Vars); |
| 408 |
Fail('should not get here'); |
| 409 |
except |
| 410 |
on ECasScriptError do ; |
| 411 |
end; |
| 412 |
FreeAndNil(Prog); |
| 413 |
|
| 414 |
{ test "if" with missing else is caught correctly } |
| 415 |
Prog := ParseProgram('function main() if(true, 123)', []); |
| 416 |
ExecuteExpectError; |
| 417 |
FreeAndNil(Prog); |
| 418 |
finally |
| 419 |
FreeAndNil(Vars); |
| 420 |
end; |
| 421 |
end; |
| 422 |
|
| 423 |
{$ifdef CASTLE_SCRIPT_FPC} |
| 424 |
procedure TTestCastleScript.TestArrays; |
| 425 |
var |
| 426 |
Prog: TCasScriptProgram; |
| 427 |
|
| 428 |
procedure ExecuteExpectError(const FuncName: string); |
| 429 |
begin |
| 430 |
try |
| 431 |
Prog.ExecuteFunction(FuncName, []); |
| 432 |
Fail('should not get here'); |
| 433 |
except |
| 434 |
on ECasScriptError do ; |
| 435 |
end; |
| 436 |
end; |
| 437 |
|
| 438 |
var |
| 439 |
Vars: TCasScriptValueList; |
| 440 |
begin |
| 441 |
Vars := TCasScriptValueList.Create(true); |
| 442 |
try |
| 443 |
Vars.Add(TCasScriptInteger.Create(true, 23)); |
| 444 |
Vars.Add(TCasScriptFloat.Create(true, 3.14)); |
| 445 |
Vars.Add(TCasScriptBoolean.Create(true, false)); |
| 446 |
Vars.Add(TCasScriptString.Create(true, 'foo')); |
| 447 |
Vars.Add(TCasScriptInt32Array.Create(true)); |
| 448 |
|
| 449 |
Vars[0].Name := 'my_int'; |
| 450 |
Vars[1].Name := 'my_float'; |
| 451 |
Vars[2].Name := 'my_bool'; |
| 452 |
Vars[3].Name := 'my_string'; |
| 453 |
Vars[4].Name := 'a_int'; |
| 454 |
|
| 455 |
Prog := ParseProgram(FileToString('castle-data:/test_script_array.kscript'), Vars); |
| 456 |
|
| 457 |
Prog.ExecuteFunction('main', []); |
| 458 |
AssertTrue(TCasScriptInteger(Vars[0]).Value = 1 + 4 + 9 + 1 + 1 + 1); |
| 459 |
|
| 460 |
Prog.ExecuteFunction('main_array_d_test', []); |
| 461 |
AssertTrue(TCasScriptFloat(Vars[1]).Value = 3.0); |
| 462 |
|
| 463 |
ExecuteExpectError('main_test_invalid_index_get'); |
| 464 |
ExecuteExpectError('main_test_invalid_index_get_2'); |
| 465 |
ExecuteExpectError('main_test_invalid_index_set'); |
| 466 |
|
| 467 |
FreeAndNil(Prog); |
| 468 |
|
| 469 |
Prog := ParseProgram(FileToString('castle-data:/test_script_string_as_array.kscript'), Vars); |
| 470 |
Prog.ExecuteFunction('main', []); |
| 471 |
AssertTrue(TCasScriptString(Vars[3]).Value = 'bbbbbbbbbbbb' + #123 + '13'); |
| 472 |
|
| 473 |
ExecuteExpectError('error1'); |
| 474 |
ExecuteExpectError('error2'); |
| 475 |
ExecuteExpectError('error3'); |
| 476 |
|
| 477 |
FreeAndNil(Prog); |
| 478 |
finally |
| 479 |
FreeAndNil(Vars); |
| 480 |
end; |
| 481 |
end; |
| 482 |
{$endif CASTLE_SCRIPT_FPC} |
| 483 |
|
| 484 |
procedure TTestCastleScript.TestBools; |
| 485 |
begin |
| 486 |
AssertTrue(ParseConstantFloatExpression('or(false, false, false)') = 0); |
| 487 |
AssertTrue(ParseConstantFloatExpression('or(false, false, true)') = 1); |
| 488 |
AssertTrue(ParseConstantFloatExpression('or(false, true, false)') = 1); |
| 489 |
AssertTrue(ParseConstantFloatExpression('or(true, false, false)') = 1); |
| 490 |
AssertTrue(ParseConstantFloatExpression('or(true, true, false)') = 1); |
| 491 |
AssertTrue(ParseConstantFloatExpression('or(false)') = 0); |
| 492 |
AssertTrue(ParseConstantFloatExpression('or(false, false)') = 0); |
| 493 |
|
| 494 |
AssertTrue(ParseConstantFloatExpression('and(false, false, false)') = 0); |
| 495 |
AssertTrue(ParseConstantFloatExpression('and(false, false, true)') = 0); |
| 496 |
AssertTrue(ParseConstantFloatExpression('and(false, true, false)') = 0); |
| 497 |
AssertTrue(ParseConstantFloatExpression('and(true, false, false)') = 0); |
| 498 |
AssertTrue(ParseConstantFloatExpression('and(true, true, false)') = 0); |
| 499 |
AssertTrue(ParseConstantFloatExpression('and(false)') = 0); |
| 500 |
AssertTrue(ParseConstantFloatExpression('and(false, false)') = 0); |
| 501 |
AssertTrue(ParseConstantFloatExpression('and(true)') = 1); |
| 502 |
AssertTrue(ParseConstantFloatExpression('and(true, true)') = 1); |
| 503 |
|
| 504 |
AssertTrue(ParseConstantFloatExpression('not(false)') = 1); |
| 505 |
AssertTrue(ParseConstantFloatExpression('not(true)') = 0); |
| 506 |
end; |
| 507 |
|
| 508 |
procedure TTestCastleScript.TestParseConstantIntExpression; |
| 509 |
begin |
| 510 |
AssertTrue(ParseConstantIntExpression('123') = 123); |
| 511 |
AssertTrue(ParseConstantIntExpression('123 + 78 * 2') = 123 + 78 * 2); |
| 512 |
AssertTrue(ParseConstantIntExpression('false') = 0); |
| 513 |
AssertTrue(ParseConstantIntExpression('true') = 1); |
| 514 |
end; |
| 515 |
|
| 516 |
procedure TTestCastleScript.TestInvalidOps; |
| 517 |
|
| 518 |
{ Executing (but not parsing) of Expr should raise ECasScriptError } |
| 519 |
procedure ExpectMathErrors(const Expr: string); |
| 520 |
var |
| 521 |
Ex: TCasScriptExpression; |
| 522 |
begin |
| 523 |
Ex := ParseFloatExpression(Expr, []); |
| 524 |
try |
| 525 |
try |
| 526 |
Ex.Execute; |
| 527 |
Fail(Expr + ' should raise ECasScriptAnyMathError, but didn''t raise anything'); |
| 528 |
except |
| 529 |
on ECasScriptAnyMathError do ; |
| 530 |
end; |
| 531 |
finally FreeAndNil(Ex) end; |
| 532 |
end; |
| 533 |
|
| 534 |
{ Executing (but not parsing) of Expr should raise ECasScriptError, |
| 535 |
but not ECasScriptAnyMathError. } |
| 536 |
procedure ExpectNonMathErrors(const Expr: string); |
| 537 |
var |
| 538 |
Ex: TCasScriptExpression; |
| 539 |
begin |
| 540 |
Ex := ParseFloatExpression(Expr, []); |
| 541 |
try |
| 542 |
try |
| 543 |
Ex.Execute; |
| 544 |
Fail(Expr + ' should raise ECasScriptError, but didn''t raise anything'); |
| 545 |
except |
| 546 |
on E: ECasScriptError do |
| 547 |
begin |
| 548 |
AssertTrue(not (E is ECasScriptAnyMathError)); |
| 549 |
end; |
| 550 |
end; |
| 551 |
finally FreeAndNil(Ex) end; |
| 552 |
end; |
| 553 |
|
| 554 |
begin |
| 555 |
ExpectMathErrors('0.1 / 0.0'); |
| 556 |
ExpectMathErrors('float(1 / 0)'); |
| 557 |
ExpectMathErrors('ln(-3)'); |
| 558 |
ExpectMathErrors('sqrt(-3)'); |
| 559 |
|
| 560 |
ExpectNonMathErrors('false / true'); |
| 561 |
ExpectNonMathErrors('or(123)'); |
| 562 |
ExpectNonMathErrors('1 + true'); |
| 563 |
{$ifdef CASTLE_SCRIPT_FPC} |
| 564 |
ExpectNonMathErrors('image_load(''blah blah'')'); |
| 565 |
{$endif} |
| 566 |
end; |
| 567 |
|
| 568 |
procedure TTestCastleScript.TestTryExecuteMath; |
| 569 |
|
| 570 |
{ Executing (but not parsing) of Expr should raise ECasScriptError. |
| 571 |
TryExecute will return @nil then. } |
| 572 |
procedure ExpectMathErrors(const Expr: string); |
| 573 |
var |
| 574 |
Ex: TCasScriptExpression; |
| 575 |
begin |
| 576 |
Ex := ParseFloatExpression(Expr, []); |
| 577 |
try |
| 578 |
AssertTrue(Ex.TryExecuteMath = nil); |
| 579 |
finally FreeAndNil(Ex) end; |
| 580 |
end; |
| 581 |
|
| 582 |
{ Executing (but not parsing) of Expr should raise ECasScriptError, |
| 583 |
but not ECasScriptAnyMathError. TryExecute let's this error through then. } |
| 584 |
procedure ExpectNonMathErrors(const Expr: string); |
| 585 |
var |
| 586 |
Ex: TCasScriptExpression; |
| 587 |
begin |
| 588 |
Ex := ParseFloatExpression(Expr, []); |
| 589 |
try |
| 590 |
try |
| 591 |
Ex.TryExecuteMath; |
| 592 |
Fail(Expr + ' should raise ECasScriptError, but didn''t raise anything'); |
| 593 |
except |
| 594 |
on E: ECasScriptError do |
| 595 |
begin |
| 596 |
AssertTrue(not (E is ECasScriptAnyMathError)); |
| 597 |
end; |
| 598 |
end; |
| 599 |
finally FreeAndNil(Ex) end; |
| 600 |
end; |
| 601 |
|
| 602 |
begin |
| 603 |
ExpectMathErrors('0.1 / 0.0'); |
| 604 |
ExpectMathErrors('float(1 / 0)'); |
| 605 |
ExpectMathErrors('ln(-3)'); |
| 606 |
ExpectMathErrors('sqrt(-3)'); |
| 607 |
|
| 608 |
ExpectNonMathErrors('false / true'); |
| 609 |
ExpectNonMathErrors('or(123)'); |
| 610 |
ExpectNonMathErrors('or(sin(123))'); |
| 611 |
ExpectNonMathErrors('1 + true'); |
| 612 |
{$ifdef CASTLE_SCRIPT_FPC} |
| 613 |
ExpectNonMathErrors('image_load(''blah blah'')'); |
| 614 |
{$endif} |
| 615 |
end; |
| 616 |
|
| 617 |
procedure TTestCastleScript.TestCoalesce; |
| 618 |
|
| 619 |
function StringExpression(const S: String): String; |
| 620 |
var |
| 621 |
E: TCasScriptExpression; |
| 622 |
begin |
| 623 |
E := ParseStringExpression(S, []); |
| 624 |
try |
| 625 |
Result := E.AsString; |
| 626 |
finally FreeAndNil(E) end; |
| 627 |
end; |
| 628 |
|
| 629 |
begin |
| 630 |
AssertEquals('asd', StringExpression('coalesce(''asd'')')); |
| 631 |
AssertEquals('', StringExpression('coalesce('''')')); |
| 632 |
AssertEquals('asd', StringExpression('coalesce(''asd'', ''dfgds'')')); |
| 633 |
AssertEquals('4rfgdf', StringExpression('coalesce('''', ''4rfgdf'')')); |
| 634 |
AssertEquals('dfgds', StringExpression('coalesce(''dfgds'', ''asd'')')); |
| 635 |
AssertEquals('4rfgdf', StringExpression('coalesce(''4rfgdf'', '''')')); |
| 636 |
AssertEquals('poip', StringExpression('coalesce('''' + '''', ''poip'')')); |
| 637 |
end; |
| 638 |
|
| 639 |
procedure TTestCastleScript.TestDivision; |
| 640 |
begin |
| 641 |
AssertSameValue(0, ParseConstantFloatExpression('1 div 2')); |
| 642 |
AssertSameValue(1, ParseConstantFloatExpression('2 div 2')); |
| 643 |
AssertSameValue(1, ParseConstantFloatExpression('3 div 2')); |
| 644 |
AssertSameValue(2, ParseConstantFloatExpression('4 div 2')); |
| 645 |
|
| 646 |
AssertSameValue(0.5, ParseConstantFloatExpression('1 / 2')); |
| 647 |
AssertSameValue(1, ParseConstantFloatExpression('2 / 2')); |
| 648 |
AssertSameValue(1.5, ParseConstantFloatExpression('3 / 2')); |
| 649 |
AssertSameValue(2, ParseConstantFloatExpression('4 / 2')); |
| 650 |
end; |
| 651 |
|
| 652 |
initialization |
| 653 |
RegisterTest(TTestCastleScript); |
| 654 |
end. |