testutils.pp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. unit testutils;
  14. interface
  15. uses
  16. Classes, SysUtils;
  17. type
  18. {$M+}
  19. TNoRefCountObject = class(TObject, IInterface)
  20. protected
  21. { IInterface }
  22. function QueryInterface(constref IID: TGUID; out Obj): HResult; virtual; {$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  23. function _AddRef: Integer; {$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  24. function _Release: Integer; {$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  25. end;
  26. {$M-}
  27. procedure FreeObjects(List: TFPList);
  28. procedure GetMethodList( AObject: TObject; AList: TStrings ); overload;
  29. procedure GetMethodList( AClass: TClass; AList: TStrings ); overload;
  30. implementation
  31. function TNoRefCountObject.QueryInterface(constref IID: TGUID; out Obj): HResult; {$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  32. begin
  33. if GetInterface(IID, Obj) then Result := 0
  34. else Result := HRESULT($80004002);
  35. end;
  36. function TNoRefCountObject._AddRef: Integer;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  37. begin
  38. Result := -1;
  39. end;
  40. function TNoRefCountObject._Release: Integer;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  41. begin
  42. Result := -1;
  43. end;
  44. // been to the dentist and suffered a lot
  45. // Hack Alert! see objpas.inc
  46. // Get a list of published methods for a given class or object
  47. procedure GetMethodList( AObject: TObject; AList: TStrings );
  48. begin
  49. GetMethodList( AObject.ClassType, AList );
  50. end;
  51. procedure GetMethodList(AClass: TClass; AList: TStrings);
  52. type
  53. PMethodNameRec = ^TMethodNameRec;
  54. TMethodNameRec = packed record
  55. name : pshortstring;
  56. addr : codepointer;
  57. end;
  58. TMethodNameTable = packed record
  59. count : dword;
  60. entries : packed array[0..0] of TMethodNameRec;
  61. end;
  62. pMethodNameTable = ^TMethodNameTable;
  63. var
  64. methodTable : pMethodNameTable;
  65. i : dword;
  66. vmt: PVmt;
  67. idx: integer;
  68. pmr: PMethodNameRec;
  69. begin
  70. AList.Clear;
  71. vmt := PVmt(aClass);
  72. while assigned(vmt) do
  73. begin
  74. methodTable := pMethodNameTable(vmt^.vMethodTable);
  75. if assigned(MethodTable) then
  76. begin
  77. pmr := @methodTable^.entries[0];
  78. for i := 0 to MethodTable^.count - 1 do
  79. begin
  80. idx := aList.IndexOf(pmr^.name^);
  81. if (idx <> - 1) then
  82. //found overridden method so delete it
  83. aList.Delete(idx);
  84. aList.AddObject(pmr^.name^, TObject(pmr^.addr));
  85. Inc(pmr);
  86. end;
  87. end;
  88. vmt := vmt^.vParent;
  89. end;
  90. end;
  91. procedure FreeObjects(List: TFPList);
  92. var
  93. i: integer;
  94. begin
  95. for i:= 0 to List.Count - 1 do
  96. TObject(List.Items[i]).Free;
  97. end;
  98. end.