testutils.pp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. {$mode objfpc}
  2. {$h+}
  3. {
  4. This file is part of the Free Component Library (FCL)
  5. Copyright (c) 2004 by Dean Zobec
  6. Port to Free Pascal of the JUnit framework.
  7. See the file COPYING.FPC, included in this distribution,
  8. for details about the copyright.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. **********************************************************************}
  13. {$IFNDEF FPC_DOTTEDUNITS}
  14. unit testutils;
  15. {$ENDIF FPC_DOTTEDUNITS}
  16. interface
  17. {$IFDEF FPC_DOTTEDUNITS}
  18. uses
  19. System.Classes, System.SysUtils;
  20. {$ELSE FPC_DOTTEDUNITS}
  21. uses
  22. Classes, SysUtils;
  23. {$ENDIF FPC_DOTTEDUNITS}
  24. type
  25. {$M+}
  26. TNoRefCountObject = class(TObject, IInterface)
  27. protected
  28. { IInterface }
  29. function QueryInterface(constref IID: TGUID; out Obj): HResult; virtual; {$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  30. function _AddRef: Integer; {$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  31. function _Release: Integer; {$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  32. end;
  33. {$M-}
  34. procedure FreeObjects(List: TFPList);
  35. procedure GetMethodList( AObject: TObject; AList: TStrings ); overload;
  36. procedure GetMethodList( AClass: TClass; AList: TStrings ); overload;
  37. implementation
  38. function TNoRefCountObject.QueryInterface(constref IID: TGUID; out Obj): HResult; {$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  39. begin
  40. if GetInterface(IID, Obj) then Result := 0
  41. else Result := HRESULT($80004002);
  42. end;
  43. function TNoRefCountObject._AddRef: Integer;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  44. begin
  45. Result := -1;
  46. end;
  47. function TNoRefCountObject._Release: Integer;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  48. begin
  49. Result := -1;
  50. end;
  51. // been to the dentist and suffered a lot
  52. // Hack Alert! see objpas.inc
  53. // Get a list of published methods for a given class or object
  54. procedure GetMethodList( AObject: TObject; AList: TStrings );
  55. begin
  56. GetMethodList( AObject.ClassType, AList );
  57. end;
  58. procedure GetMethodList(AClass: TClass; AList: TStrings);
  59. type
  60. PMethodNameRec = ^TMethodNameRec;
  61. TMethodNameRec =
  62. {$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}
  63. packed
  64. {$endif}
  65. record
  66. name : pshortstring;
  67. addr : codepointer;
  68. end;
  69. TMethodNameTable =
  70. {$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}
  71. packed
  72. {$endif}
  73. record
  74. count : dword;
  75. entries : packed array[0..0] of TMethodNameRec;
  76. end;
  77. pMethodNameTable = ^TMethodNameTable;
  78. var
  79. methodTable : pMethodNameTable;
  80. i : integer;
  81. vmt: PVmt;
  82. idx: integer;
  83. pmr: PMethodNameRec;
  84. lName : shortstring;
  85. begin
  86. AList.Clear;
  87. vmt := PVmt(aClass);
  88. while assigned(vmt) do
  89. begin
  90. methodTable := pMethodNameTable(vmt^.vMethodTable);
  91. if assigned(MethodTable) then
  92. begin
  93. pmr := @methodTable^.entries[0];
  94. for i := 0 to MethodTable^.count - 1 do
  95. begin
  96. lName:=pmr^.name^;
  97. idx := aList.IndexOf(lName);
  98. if (idx <> - 1) then
  99. //found overridden method so delete it
  100. aList.Delete(idx);
  101. aList.AddObject(lName, TObject(pmr^.addr));
  102. Inc(pmr);
  103. end;
  104. end;
  105. vmt := vmt^.vParent;
  106. end;
  107. end;
  108. procedure FreeObjects(List: TFPList);
  109. var
  110. i: integer;
  111. begin
  112. for i:= 0 to List.Count - 1 do
  113. TObject(List.Items[i]).Free;
  114. end;
  115. end.