tests.rtti.attrtypes2.pp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. unit tests.rtti.attrtypes2;
  2. {$mode objfpc}{$H+}
  3. {$modeswitch advancedrecords}
  4. interface
  5. uses
  6. Classes,
  7. SysUtils,
  8. TypInfo,
  9. Rtti
  10. {$ifndef Windows},
  11. ffi.manager
  12. {$endif};
  13. {$RTTI EXPLICIT
  14. FIELDS([vcPublic])
  15. PROPERTIES([vcPublic,vcPublished])
  16. METHODS([vcPublic,vcPublished])}
  17. var
  18. ErrorCount: Integer;
  19. type
  20. TTestAttr2Record = record
  21. fa:integer;
  22. fa2:integer;
  23. fa3:integer;
  24. public
  25. function Offset(arg1, arg2: Integer): Integer;
  26. property TestIProp[i1, i2: Integer]: Integer read Offset;
  27. constructor Create(a1, a2: Integer); overload;
  28. constructor Create(rec: TTestAttr2Record); overload;
  29. class function StaticFunc(d: integer; p: TPoint; r: TRect): string; static;
  30. end;
  31. TTestAttr2Class = class
  32. private
  33. class var
  34. static_var: Integer;
  35. class function GetStaticProp: Integer; static;
  36. class procedure SetStaticProp(value: Integer); static;
  37. function GetIndProp(arg1, arg2: Integer): TObject;
  38. procedure SetIndProp(arg1, arg2: Integer; value: TObject);
  39. public
  40. fa, fa2:integer;
  41. property TestIProp[i: Integer; i2: Integer]: TObject read GetIndProp write SetIndProp;
  42. class property StaticProp: Integer read GetStaticProp write SetStaticProp;
  43. procedure MethodForNil(arg1, arg2: TObject);
  44. class function StaticMethod(str: string): Integer; static;
  45. constructor Create(a1, a2: Integer);
  46. class procedure ClassProc(var int: Integer; var str: string);
  47. end;
  48. TInherited2Class = class(TTestAttr2Class)
  49. end;
  50. implementation
  51. uses fpcunit;
  52. procedure Check(ACondition: boolean; const AMessage: string);
  53. begin
  54. TAssert.AssertTrue(AMessage,ACondition);
  55. end;
  56. function TTestAttr2Record.Offset(arg1, arg2: Integer): Integer;
  57. begin
  58. fa := fa + arg1;
  59. fa2 := fa2 + arg2;
  60. Result := fa + fa2;
  61. end;
  62. constructor TTestAttr2Record.Create(a1, a2: Integer);
  63. begin
  64. Check((fa = 60) and (fa2 = 80) and (fa3 = 90), 'Original TTestAttr2Record was delivered incorrectly');
  65. fa := a1;
  66. fa2 := a2;
  67. end;
  68. constructor TTestAttr2Record.Create(rec: TTestAttr2Record);
  69. begin
  70. fa := rec.fa;
  71. fa2 := rec.fa2;
  72. end;
  73. class function TTestAttr2Record.StaticFunc(d: integer; p: TPoint; r: TRect): string;
  74. begin
  75. Result := 'experiment_'+d.ToString+'_'+p.X.ToString+'_'+p.Y.ToString+'_'+r.Left.ToString+'_'+r.Top.ToString+'_'+r.Right.ToString+'_'+r.Bottom.ToString;
  76. end;
  77. class function TTestAttr2Class.GetStaticProp: Integer;
  78. begin
  79. Result := static_var;
  80. end;
  81. class procedure TTestAttr2Class.SetStaticProp(value: Integer);
  82. begin
  83. static_var := Value;
  84. end;
  85. function TTestAttr2Class.GetIndProp(arg1, arg2: Integer): TObject;
  86. begin
  87. fa := arg1;
  88. fa2 := arg2;
  89. Result := Self;
  90. end;
  91. procedure TTestAttr2Class.SetIndProp(arg1, arg2: Integer; value: TObject);
  92. begin
  93. fa := arg1;
  94. fa2 := arg2;
  95. Check((arg1 = 653) and (arg2 = 796) and ((value as TTestAttr2Class).fa2 = 796),
  96. 'The setter of an indexed property is incorrectly called');
  97. end;
  98. procedure TTestAttr2Class.MethodForNil(arg1, arg2: TObject);
  99. begin
  100. Check((arg1 = nil) and (arg2 = nil), 'MethodForNil did not get only nil');
  101. end;
  102. class function TTestAttr2Class.StaticMethod(str: string): Integer;
  103. begin
  104. Check(str = 'simple string', 'The static method argument is incorrect');
  105. Result := 7775;
  106. end;
  107. class procedure TTestAttr2Class.ClassProc(var int: Integer; var str: string);
  108. begin
  109. Check(Self.ClassName = 'TInherited2Class', 'Incorrect class transfer to Self');
  110. Inc(int, 12);
  111. str := str + '_addon';
  112. end;
  113. constructor TTestAttr2Class.Create(a1, a2: Integer);
  114. begin
  115. fa:=a1;
  116. fa2:=a2;
  117. end;
  118. end.