1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791
|
unit EMSSelfTest;
{$mode objfpc}{$H+}
interface
{$IFDEF darwin}
{$DEFINE NeedTPointFix }
{$ENDIF}
uses
Classes, SysUtils, SynEdit, LazLoggerBase,
IDECommands, EMScriptClasses, EMScriptMacro, Clipbrd, Dialogs, Controls,
uPSCompiler, uPSRuntime, uPSUtils;
type
TEMScriptSelfTestException = Exception;
{ TEMSTPSTestExec }
TEMSTPSTestExec = class(TEMSTPSExec)
protected
procedure AddFuncToExec; override;
public
end;
{ TEMSPSTestPascalCompiler }
TEMSPSTestPascalCompiler = class(TEMSPSPascalCompiler)
private
FNextOnUses: TPSOnUses;
public
constructor Create;
end;
{ TEMSelfTestEditorMacro }
TEMSelfTestEditorMacro = class(TEMSEditorMacro)
public
constructor Create(aOwner: TComponent); override;
end;
function DoSelfTest: Boolean;
var
SelfTestErrorMsg: String;
implementation
{$IFDEF NeedTPointFix}
type TPoint2 = record x,y,a,b,c: Longint; end;
{$ENDIF}
{%region RegisterSelfTests}
var
{%H-}TestResultA: integer;
TestResultInt1, TestResultInt2: integer;
TestInputInt1, TestInputInt2: integer;
TestResultBool1, TestResultBool2: boolean;
TestInputBool1, TestInputBool2: boolean;
TestResultStr1, TestResultStr2: String;
TestInputStr1, TestInputStr2: String;
function test_ord_mt(AType: TMsgDlgType): Integer;
begin
Result := ord(AType);
TestResultA := Result;
end;
function test_ord_mb(ABtn: TMsgDlgBtn): Integer;
begin
Result := ord(ABtn);
TestResultA := Result;
end;
procedure test_int1(AValue: Integer);
begin
TestResultInt1 := AValue;
end;
procedure test_int2(AValue: Integer);
begin
TestResultInt2 := AValue;
end;
function test_getint1: Integer;
begin
Result := TestInputInt1;
end;
function test_getint2: Integer;
begin
Result := TestInputInt2;
end;
procedure test_varint1(var AValue: Integer);
begin
TestResultInt1 := AValue;
AValue := TestInputInt1;
end;
procedure test_bool1(AValue: boolean);
begin
TestResultBool1 := AValue;
end;
procedure test_bool2(AValue: boolean);
begin
TestResultBool2 := AValue;
end;
function test_getbool1: Boolean;
begin
Result := TestInputBool1;
end;
function test_getbool2: Boolean;
begin
Result := TestInputBool2;
end;
procedure test_point(AValue: TPoint);
begin
TestResultInt1 := AValue.X;
TestResultInt2 := AValue.Y;
end;
function test_getpoint: {$IFDEF NeedTPointFix}TPoint2{$ELSE}TPoint{$ENDIF};
begin
Result.X := TestInputInt1;
Result.Y := TestInputInt2;
end;
procedure test_varpoint(var AValue: TPoint);
begin
TestResultInt1 := AValue.X;
TestResultInt2 := AValue.Y;
AValue.X := TestInputInt1;
AValue.Y := TestInputInt2;
end;
procedure test_str1(AValue: String);
begin
TestResultStr1 := AValue;
end;
procedure test_str2(AValue: String);
begin
TestResultStr2 := AValue;
end;
function test_getstr1: String;
begin
Result := TestInputStr1;
end;
function test_getstr2: String;
begin
Result := TestInputStr2;
end;
procedure test_varstr1(var AValue: String);
begin
TestResultStr1 := AValue;
AValue := TestInputStr1;
end;
const
Decltest_ord_mt = 'function test_ord_mt(AType: TMsgDlgType): Integer;';
Decltest_ord_mb = 'function test_ord_mb(ABtn: TMsgDlgBtn): Integer;';
Decltest_int1 = 'procedure test_int1(AValue: Integer);';
Decltest_int2 = 'procedure test_int2(AValue: Integer);';
Decltest_getint1 = 'function test_getint1: Integer;';
Decltest_getint2 = 'function test_getint2: Integer;';
Decltest_varint1 = 'procedure test_varint1(var AValue: Integer);';
Decltest_bool1 = 'procedure test_bool1(AValue: Boolean);';
Decltest_bool2 = 'procedure test_bool2(AValue: Boolean);';
Decltest_getbool1 = 'function test_getbool1: Boolean;';
Decltest_getbool2 = 'function test_getbool2: Boolean;';
Decltest_point = 'procedure test_point(AValue: TPoint);';
Decltest_getpoint = 'function test_getpoint: TPoint;';
Decltest_varpoint = 'procedure test_varpoint(var AValue: TPoint);';
Decltest_str1 = 'procedure test_str1(AValue: String);';
Decltest_str2 = 'procedure test_str2(AValue: String);';
Decltest_getstr1 = 'function test_getstr1: String;';
Decltest_getstr2 = 'function test_getstr2: String;';
Decltest_varstr1 = 'procedure test_varstr1(var AValue: String);';
Functest_ord_mt: function(AType: TMsgDlgType): Integer = @test_ord_mt;
Functest_ord_mb: function(ABtn: TMsgDlgBtn): Integer = @test_ord_mb;
Proctest_int1: procedure (AValue: Integer) = @test_int1;
Proctest_int2: procedure (AValue: Integer) = @test_int2;
Proctest_getint1: function: Integer = @test_getint1;
Proctest_getint2: function: Integer = @test_getint2;
Proctest_varint1: procedure (var AValue: Integer) = @test_varint1;
Proctest_bool1: procedure (AValue: Boolean) = @test_bool1;
Proctest_bool2: procedure (AValue: Boolean) = @test_bool2;
Proctest_getbool1: function: Boolean = @test_getbool1;
Proctest_getbool2: function: Boolean = @test_getbool2;
Proctest_point: procedure (AValue: TPoint) = @test_point;
Proctest_getpoint: function: {$IFDEF NeedTPointFix}TPoint2{$ELSE}TPoint{$ENDIF} = @test_getpoint;
Proctest_varpoint: procedure (var AValue: TPoint) = @test_varpoint;
Proctest_str1: procedure (AValue: String) = @test_str1;
Proctest_str2: procedure (AValue: String) = @test_str2;
Proctest_getstr1: function: String = @test_getstr1;
Proctest_getstr2: function: String = @test_getstr2;
Proctest_varstr1: procedure (var AValue: String) = @test_varstr1;
{$IFDEF PasMacroNoNativeCalls}
const
Id_test_ord_mb = 901;
Id_test_ord_mt = 902;
Id_test_int1 = 910;
Id_test_int2 = 911;
Id_test_getint1 = 912;
Id_test_getint2 = 913;
Id_test_varint1 = 914;
Id_test_bool1 = 920;
Id_test_bool2 = 921;
Id_test_getbool1 = 922;
Id_test_getbool2 = 923;
Id_test_point = 930;
Id_test_getpoint = 931;
Id_test_varpoint = 932;
Id_test_str1 = 940;
Id_test_str2 = 941;
Id_test_getstr1 = 942;
Id_test_getstr2 = 943;
Id_test_varstr1 = 944;
function ExecTestHandler({%H-}Caller: TPSExec; p: TPSExternalProcRec;
{%H-}Global, Stack: TPSStack): Boolean;
var
data: PPoint;
i: integer;
s: TbtString;
begin
Result := True;
case Longint(p.Ext1) of
Id_test_ord_mb: begin // test_ord_mb(ABtn: TMsgDlgBtn): Integer;
if Stack.Count < 2 then raise TEMScriptBadParamException.Create('Invalid param count for "test_ord_mb"');
Stack.SetInt(-1, test_ord_mb(TMsgDlgBtn(Stack.GetUInt(-2))) );
end;
Id_test_ord_mt: begin // test_ord_mt(AType: TMsgDlgType): Integer;
if Stack.Count < 2 then raise TEMScriptBadParamException.Create('Invalid param count for "test_ord_mt"');
// Stack[Stack.Count-2]^.FType.ExportName = 'TMSGDLGTYPE'
Stack.SetInt(-1, test_ord_mt(TMsgDlgType(Stack.GetUInt(-2))) );
end;
Id_test_int1: begin // test_int1(AValue: Integer);
if Stack.Count < 1 then raise TEMScriptBadParamException.Create('Invalid param count for "test_int1"');
test_int1(Stack.GetUInt(-1));
end;
Id_test_int2: begin // test_int2(AValue: Integer);
if Stack.Count < 1 then raise TEMScriptBadParamException.Create('Invalid param count for "test_int2"');
test_int2(Stack.GetUInt(-1));
end;
Id_test_getint1: begin
if Stack.Count < 1 then raise TEMScriptBadParamException.Create('Invalid param count for "test_getint1"');
Stack.SetInt(-1, test_getint1());
end;
Id_test_getint2: begin
if Stack.Count < 1 then raise TEMScriptBadParamException.Create('Invalid param count for "test_getint2"');
Stack.SetInt(-1, test_getint2());
end;
Id_test_varint1: begin
if Stack.Count < 1 then raise TEMScriptBadParamException.Create('Invalid param count for "test_int1"');
i := Stack.GetInt(-1);
test_varint1(i);
Stack.SetInt(-1, i);
end;
Id_test_bool1: begin
if Stack.Count < 1 then raise TEMScriptBadParamException.Create('Invalid param count for "test_bool1()"');
test_bool1(Stack.GetBool(-1));
end;
Id_test_bool2: begin
if Stack.Count < 1 then raise TEMScriptBadParamException.Create('Invalid param count for "test_bool2()"');
test_bool2(Stack.GetBool(-1));
end;
Id_test_getbool1: begin
if Stack.Count < 1 then raise TEMScriptBadParamException.Create('Invalid param count for "test_getbool1"');
Stack.SetBool(-1, test_getbool1());
end;
Id_test_getbool2: begin
if Stack.Count < 1 then raise TEMScriptBadParamException.Create('Invalid param count for "test_getbool2"');
Stack.SetBool(-1, test_getbool2());
end;
Id_test_point: begin
if Stack.Count < 1 then raise TEMScriptBadParamException.Create('Invalid param count for "test_point()"');
test_point(GetPointFromStack(Stack, -1));
end;
Id_test_getpoint: begin
if Stack.Count < 1 then raise TEMScriptBadParamException.Create('Invalid param count for "test_getpoint"');
data := GetVarPointFromStack(Stack, -1);
TPoint(data^) := test_getpoint;
end;
Id_test_varpoint: begin
if Stack.Count < 1 then raise TEMScriptBadParamException.Create('Invalid param count for "test_getpoint"');
data := GetVarPointFromStack(Stack, -1);
test_varpoint(TPoint(data^));
end;
Id_test_str1: begin
if Stack.Count < 1 then raise TEMScriptBadParamException.Create('Invalid param count for "test_str1()"');
test_str1(Stack.GetAnsiString(-1));
end;
Id_test_str2: begin
if Stack.Count < 1 then raise TEMScriptBadParamException.Create('Invalid param count for "test_str2()"');
test_str2(Stack.GetAnsiString(-1));
end;
Id_test_getstr1: begin
if Stack.Count < 1 then raise TEMScriptBadParamException.Create('Invalid param count for "test_getstr1"');
Stack.SetAnsiString(-1, test_getstr1());
end;
Id_test_getstr2: begin
if Stack.Count < 1 then raise TEMScriptBadParamException.Create('Invalid param count for "test_getstr2"');
Stack.SetAnsiString(-1, test_getstr2());
end;
Id_test_varstr1: begin
if Stack.Count < 1 then raise TEMScriptBadParamException.Create('Invalid param count for "test_str1()"');
s := Stack.GetAnsiString(-1);
test_varstr1(s);
Stack.SetAnsiString(-1, s);
end;
else
Result := False;
end;
end;
{$ENDIF}
procedure CompRegisterSelfTests(AComp: TPSPascalCompiler);
begin
// for tests
AComp.AddDelphiFunction(Decltest_ord_mb);
AComp.AddDelphiFunction(Decltest_ord_mt);
AComp.AddDelphiFunction(Decltest_int1);
AComp.AddDelphiFunction(Decltest_int2);
AComp.AddDelphiFunction(Decltest_getint1);
AComp.AddDelphiFunction(Decltest_getint2);
AComp.AddDelphiFunction(Decltest_varint1);
AComp.AddDelphiFunction(Decltest_bool1);
AComp.AddDelphiFunction(Decltest_bool2);
AComp.AddDelphiFunction(Decltest_getbool1);
AComp.AddDelphiFunction(Decltest_getbool2);
AComp.AddDelphiFunction(Decltest_point);
AComp.AddDelphiFunction(Decltest_getpoint);
AComp.AddDelphiFunction(Decltest_varpoint);
AComp.AddDelphiFunction(Decltest_str1);
AComp.AddDelphiFunction(Decltest_str2);
AComp.AddDelphiFunction(Decltest_getstr1);
AComp.AddDelphiFunction(Decltest_getstr2);
AComp.AddDelphiFunction(Decltest_varstr1);
end;
procedure ExecRegisterSelfTests(AExec: TEMSTPSExec);
begin
// for tests
{$IFnDEF PasMacroNoNativeCalls}
AExec.RegisterDelphiFunction(Functest_ord_mb, 'test_ord_mb', cdRegister);
AExec.RegisterDelphiFunction(Functest_ord_mt, 'test_ord_mt', cdRegister);
AExec.RegisterDelphiFunction(Proctest_int1, 'test_int1', cdRegister);
AExec.RegisterDelphiFunction(Proctest_int2, 'test_int2', cdRegister);
AExec.RegisterDelphiFunction(Proctest_getint1, 'test_getint1', cdRegister);
AExec.RegisterDelphiFunction(Proctest_getint2, 'test_getint2', cdRegister);
AExec.RegisterDelphiFunction(Proctest_varint1, 'test_varint1', cdRegister);
AExec.RegisterDelphiFunction(Proctest_bool1, 'test_bool1', cdRegister);
AExec.RegisterDelphiFunction(Proctest_bool2, 'test_bool2', cdRegister);
AExec.RegisterDelphiFunction(Proctest_getbool1, 'test_getbool1', cdRegister);
AExec.RegisterDelphiFunction(Proctest_getbool2, 'test_getbool2', cdRegister);
AExec.RegisterDelphiFunction(Proctest_point, 'test_point', cdRegister);
AExec.RegisterDelphiFunction(Proctest_getpoint, 'test_getpoint', cdRegister);
AExec.RegisterDelphiFunction(Proctest_varpoint, 'test_varpoint', cdRegister);
AExec.RegisterDelphiFunction(Proctest_str1, 'test_str1', cdRegister);
AExec.RegisterDelphiFunction(Proctest_str2, 'test_str2', cdRegister);
AExec.RegisterDelphiFunction(Proctest_getstr1, 'test_getstr1', cdRegister);
AExec.RegisterDelphiFunction(Proctest_getstr2, 'test_getstr2', cdRegister);
AExec.RegisterDelphiFunction(Proctest_varstr1, 'test_varstr1', cdRegister);
{$ELSE}
AExec.RegisterFunctionName('test_ord_mb', @ExecTestHandler, Pointer(Id_test_ord_mb), nil);
AExec.RegisterFunctionName('test_ord_mt', @ExecTestHandler, Pointer(Id_test_ord_mt), nil);
AExec.RegisterFunctionName('test_int1', @ExecTestHandler, Pointer(Id_test_int1), nil);
AExec.RegisterFunctionName('test_int2', @ExecTestHandler, Pointer(Id_test_int2), nil);
AExec.RegisterFunctionName('test_getint1', @ExecTestHandler, Pointer(Id_test_getint1), nil);
AExec.RegisterFunctionName('test_getint2', @ExecTestHandler, Pointer(Id_test_getint2), nil);
AExec.RegisterFunctionName('test_varint1', @ExecTestHandler, Pointer(Id_test_varint1), nil);
AExec.RegisterFunctionName('test_bool1', @ExecTestHandler, Pointer(Id_test_bool1), nil);
AExec.RegisterFunctionName('test_bool2', @ExecTestHandler, Pointer(Id_test_bool2), nil);
AExec.RegisterFunctionName('test_getbool1', @ExecTestHandler, Pointer(Id_test_getbool1), nil);
AExec.RegisterFunctionName('test_getbool2', @ExecTestHandler, Pointer(Id_test_getbool2), nil);
AExec.RegisterFunctionName('test_point', @ExecTestHandler, Pointer(Id_test_point), nil);
AExec.RegisterFunctionName('test_getpoint', @ExecTestHandler, Pointer(Id_test_getpoint), nil);
AExec.RegisterFunctionName('test_varpoint', @ExecTestHandler, Pointer(Id_test_varpoint), nil);
AExec.RegisterFunctionName('test_str1', @ExecTestHandler, Pointer(Id_test_str1), nil);
AExec.RegisterFunctionName('test_str2', @ExecTestHandler, Pointer(Id_test_str2), nil);
AExec.RegisterFunctionName('test_getstr1', @ExecTestHandler, Pointer(Id_test_getstr1), nil);
AExec.RegisterFunctionName('test_getstr2', @ExecTestHandler, Pointer(Id_test_getstr2), nil);
AExec.RegisterFunctionName('test_varstr1', @ExecTestHandler, Pointer(Id_test_varstr1), nil);
{$ENDIF}
end;
{%endregion RegisterSelfTests}
{ TEMSPSTestPascalCompiler }
function CompilerOnUses(Sender: TPSPascalCompiler; const Name: TbtString): Boolean;
var
S: TEMSPSTestPascalCompiler;
begin
S := (Sender as TEMSPSTestPascalCompiler);
Result := assigned(S.FNextOnUses) and S.FNextOnUses(Sender, Name);
if Result and (Name = 'SYSTEM') then
begin
CompRegisterSelfTests(S);
Result := True;
end;
end;
constructor TEMSPSTestPascalCompiler.Create;
begin
inherited Create;
FNextOnUses := OnUses;
OnUses := @CompilerOnUses;
end;
{ TEMSTPSTestExec }
procedure TEMSTPSTestExec.AddFuncToExec;
begin
inherited AddFuncToExec;
ExecRegisterSelfTests(Self);
end;
{ TEMSelfTestEditorMacro }
constructor TEMSelfTestEditorMacro.Create(aOwner: TComponent);
begin
inherited Create(aOwner);
Compiler := TEMSPSTestPascalCompiler.Create;
Exec := TEMSTPSTestExec.Create;
end;
type THackTEMSEditorMacro = class(TEMSEditorMacro) end;
function DoSelfTest: Boolean;
var
m: TEMSEditorMacro;
syn: TSynEdit;
procedure RunMacro(AText: String);
begin
m.SetFromSource(AText);
m.PlaybackMacro(syn);
end;
procedure AssertEQ(Msg: String; Exp, Got: String); overload;
begin
if not(Got = Exp) then
raise TEMScriptSelfTestException.Create(Format('%s [Exp: "%s" / Got: "%s" / Info: %s / SynTxt: %s]', [Msg, Exp, Got, dbgs(m.IsInvalid) + ' ' + THackTEMSEditorMacro(m).GetErrorMsg, syn.Text]));
end;
procedure AssertEQ(Msg: String; Exp, Got: Integer); overload;
begin
if not(Got = Exp) then
raise TEMScriptSelfTestException.Create(Format('%s [Exp: %d / Got: %d / Info: %s / SynTxt: %s]', [Msg, Exp, Got, dbgs(m.IsInvalid) + ' ' + THackTEMSEditorMacro(m).GetErrorMsg, syn.Text]));
end;
procedure AssertEQ(Msg: String; Exp, Got: Boolean); overload;
begin
if not(Got = Exp) then
raise TEMScriptSelfTestException.Create(Format('%s [Exp: %s / Got: %s / Info: %s / SynTxt: %s]', [Msg, dbgs(Exp), dbgs(Got), dbgs(m.IsInvalid) + ' ' + THackTEMSEditorMacro(m).GetErrorMsg, syn.Text]));
end;
procedure TestInt(Msg, AText: String; Exp: Integer);
begin
TestResultInt1 := 0;
RunMacro(AText);
AssertEQ(Msg + '(init: 0)', Exp, TestResultInt1);
TestResultInt1 := -1;
RunMacro(AText);
AssertEQ(Msg + '(init: -1)', Exp, TestResultInt1);
TestResultInt1 := 99919;
RunMacro(AText);
AssertEQ(Msg + '(init: 99919)', Exp, TestResultInt1);
end;
procedure TestBool(Msg, AText: String; Exp: Boolean);
begin
TestResultBool1 := False;
RunMacro(AText);
AssertEQ(Msg + '(init: F)', Exp, TestResultBool1);
TestResultBool1 := True;
RunMacro(AText);
AssertEQ(Msg + '(init: T)', Exp, TestResultBool1);
end;
procedure TestBool(Msg, AText: String; Exp, Exp2: Boolean);
begin
TestResultBool1 := False;
TestResultBool2 := False;
RunMacro(AText);
AssertEQ(Msg + '(init: F,F)', Exp, TestResultBool1);
AssertEQ(Msg + '(init: F,F)', Exp2, TestResultBool2);
TestResultBool1 := True;
TestResultBool2 := True;
RunMacro(AText);
AssertEQ(Msg + '(init: T,T)', Exp, TestResultBool1);
AssertEQ(Msg + '(init: T,T)', Exp2, TestResultBool2);
TestResultBool1 := True;
TestResultBool2 := False;
RunMacro(AText);
AssertEQ(Msg + '(init: T,F)', Exp, TestResultBool1);
AssertEQ(Msg + '(init: T,F)', Exp2, TestResultBool2);
TestResultBool1 := False;
TestResultBool2 := True;
RunMacro(AText);
AssertEQ(Msg + '(init: F,T)', Exp, TestResultBool1);
AssertEQ(Msg + '(init: F,T)', Exp2, TestResultBool2);
end;
procedure TestSyn(Msg, AText: String; Exp: String);
begin
syn.ClearAll;
RunMacro(AText);
AssertEQ(Msg , True, pos(Exp, syn.Text) > 0);
end;
procedure TestSyn(Msg, AInit, AText: String; Exp: String);
begin
syn.ClearAll;
syn.Text := AInit;
RunMacro(AText);
AssertEQ(Msg , True, pos(Exp, syn.Text) > 0);
end;
begin
Result := False;
SelfTestErrorMsg := '';
try
try
m := TEMSelfTestEditorMacro.Create(nil);
syn := TSynEdit.Create(nil);
{%region calling convention}
// test_int1
TestResultInt1 := 99;
TestInt('test_int1(42)',
'begin' +
' test_int1(42);' +
'end.',
42);
TestInt('test_int1(-3)',
'begin' +
' test_int1(-3);' +
'end.',
-3);
TestInputInt1 := 1001;
TestInputInt2 := 2002;
RunMacro('var i: Integer;' +
'begin' +
' i := test_getint1' +
' test_int1(i);' +
' test_int2(test_getint2);' +
'end.');
AssertEQ('Failed getint1', 1001, TestResultInt1);
AssertEQ('Failed getint2', 2002, TestResultInt2);
TestInputInt1 := 2001;
TestResultInt1 := -1;
TestResultInt2 := -1;
RunMacro('var i: Integer;' +
'begin' +
' i := 1002' +
' test_varint1(i);' +
' test_int2(i);' +
'end.');
AssertEQ('Failed varint a', 1002, TestResultInt1);
AssertEQ('Failed varint b', 2001, TestResultInt2);
// test_bool
TestBool('test_bool(F,F)',
'begin' +
' test_bool1(False);' +
' test_bool2(False);' +
'end.',
False, False);
TestBool('test_bool(T,T)',
'begin' +
' test_bool1(True);' +
' test_bool2(True);' +
'end.',
True, True);
TestBool('test_bool(T,F)',
'begin' +
' test_bool1(True);' +
' test_bool2(False);' +
'end.',
True, False);
TestInputBool1 := True;
TestInputBool2 := False;
RunMacro('var i: Boolean;' +
'begin' +
' i := test_getbool1' +
' test_bool1(i);' +
' test_bool2(test_getbool2);' +
'end.');
AssertEQ('Failed getbool1', True, TestResultBool1);
AssertEQ('Failed getbool2', False, TestResultBool2);
// size_of(point)
TestResultInt1 := -1;
RunMacro('var p: TPoint;' +
'begin' +
' test_int1(SizeOf(p));' +
'end.');
AssertEQ('Failed int param (SizeOf(TPoint))', SizeOf(TPoint), TestResultInt1);
// TPoint
TestResultInt1 := -1;
TestResultInt2 := -1;
RunMacro('var p: TPoint;' +
'begin' +
' p := point(1001, 2002);' +
' test_point(p);' +
'end.');
AssertEQ('Failed point param X', 1001, TestResultInt1);
AssertEQ('Failed point param Y', 2002, TestResultInt2);
TestInputInt1 := 3001;
TestInputInt2 := 4002;
RunMacro('var p: TPoint;' +
'begin' +
' p := test_getpoint' +
' test_point(p);' +
'end.');
AssertEQ('Failed getpoint param X', 3001, TestResultInt1);
AssertEQ('Failed getpoint param Y', 4002, TestResultInt2);
TestInputInt1 := 5001;
TestInputInt2 := 6002;
RunMacro('begin' +
' test_point(test_getpoint);' +
'end.');
AssertEQ('Failed getpoint(2) param X', 5001, TestResultInt1);
AssertEQ('Failed getpoint(2) param Y', 6002, TestResultInt2);
TestResultInt1 := -1;
TestResultInt2 := -1;
TestResultBool1 := False;
TestInputInt1 := 1005;
TestInputInt2 := 1006;
RunMacro('var p: TPoint;' +
'begin' +
' p := point(990, 991);' +
' test_varpoint(p);' +
' test_bool1((p.x = 1005) and (p.y = 1006));' +
'end.');
AssertEQ('Failed varpoint x', 990, TestResultInt1);
AssertEQ('Failed varpoint y', 991, TestResultInt2);
AssertEQ('Failed varpoint new', True, TestResultBool1);
// string
TestResultStr1 := 'no no';
TestResultStr2 := TestResultStr1;
RunMacro('var s: String;' +
'begin' +
' s := ''abc'';' +
' test_str1(''123'');' +
' test_str2(s);' +
'end.');
AssertEQ('Failed str1 param', '123', TestResultStr1);
AssertEQ('Failed str1 param', 'abc', TestResultStr2);
TestInputStr1 := '123';
TestInputStr2 := '456';
RunMacro('var s: String;' +
'begin' +
' s := test_getstr1' +
' test_str1(s);' +
' test_str2(test_getstr2);' +
'end.');
AssertEQ('Failed getstr1', '123', TestResultStr1);
AssertEQ('Failed getstr2', '456', TestResultStr2);
TestInputStr1 := '123';
TestResultStr1 := '';
TestResultStr2 := '';
TestResultBool1 := False;
RunMacro('var s: String;' +
'begin' +
' s := ''aaa''' +
' test_varstr1(s);' +
' test_bool1(s = ''123'');' +
' test_str2(s);' +
'end.');
AssertEQ('Failed varstr1', 'aaa', TestResultStr1);
AssertEQ('Failed varstr2', '123', TestResultStr2);
AssertEQ('Failed varstr3',True, TestResultBool1);
{%endregion calling convention}
{%region }
TestBool('mrNone',
'begin' +
' test_bool1(mrNone = ' +IntToStr(mrNone) + ');' +
'end.',
True
);
TestBool('mrOk',
'begin' +
' test_bool1(mrOk = ' +IntToStr(mrOk) + ');' +
'end.',
True
);
TestBool('mtWarning',
'begin' +
' test_bool1(test_ord_mt(mtWarning) = ' +IntToStr(ord(mtWarning)) + ');' +
'end.',
True
);
TestBool('mtConfirmation',
'begin' +
' test_bool1(test_ord_mt(mtConfirmation) = ' +IntToStr(ord(mtConfirmation)) + ');' +
'end.',
True
);
TestBool('mbYes',
'begin' +
' test_bool1(test_ord_mb(mbYes) = ' +IntToStr(ord(mbYes)) + ');' +
'end.',
True
);
TestBool('mbCancel',
'begin' +
' test_bool1(test_ord_mb(mbCancel) = ' +IntToStr(ord(mbCancel)) + ');' +
'end.',
True
);
{%endregion }
TestSyn('ecChar',
'begin ecChar(''C''); end.',
'C'
);
TestSyn('InsertTextAtCaret',
'begin Caller.InsertTextAtCaret(''Foo'', scamEnd); end.',
'Foo');
TestSyn('TextBetweenPoints',
'123456',
'begin Caller.TextBetweenPoints[Point(3,1), point(5,1)] := ''ng''; end.',
'12ng56');
TestSyn('Replace All', 'Test abc abcde 123',
'begin Caller.SearchReplace(''abc'', ''XYZ'', [ssoReplaceAll]); end.',
'Test XYZ XYZde 123'
);
Result := True;
finally
FreeAndNil(m);
FreeAndNil(syn);
end;
except
on E: Exception do begin
SelfTestErrorMsg := E.Message;
Result := False;
end;
end;
end;
end.
|