testutils.pp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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(const IID: TGUID; out Obj): HResult; virtual; stdcall;
  23. function _AddRef: Integer; stdcall;
  24. function _Release: Integer; stdcall;
  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(const IID: TGUID; out Obj): HResult; stdcall;
  32. begin
  33. if GetInterface(IID, Obj) then Result := 0
  34. else Result := HRESULT($80004002);
  35. end;
  36. function TNoRefCountObject._AddRef: Integer;stdcall;
  37. begin
  38. Result := -1;
  39. end;
  40. function TNoRefCountObject._Release: Integer;stdcall;
  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. TMethodNameRec = packed record
  54. name : pshortstring;
  55. addr : pointer;
  56. end;
  57. TMethodNameTable = packed record
  58. count : dword;
  59. entries : packed array[0..0] of TMethodNameRec;
  60. end;
  61. pMethodNameTable = ^TMethodNameTable;
  62. var
  63. methodTable : pMethodNameTable;
  64. i : dword;
  65. vmt: TClass;
  66. idx: integer;
  67. begin
  68. AList.Clear;
  69. vmt := aClass;
  70. while assigned(vmt) do
  71. begin
  72. methodTable := pMethodNameTable((Pointer(vmt) + vmtMethodTable)^);
  73. if assigned(MethodTable) then
  74. begin
  75. for i := 0 to MethodTable^.count - 1 do
  76. begin
  77. idx := aList.IndexOf(MethodTable^.entries[i].name^);
  78. if (idx <> - 1) then
  79. //found overridden method so delete it
  80. aList.Delete(idx);
  81. aList.AddObject(MethodTable^.entries[i].name^, TObject(MethodTable^.entries[i].addr));
  82. end;
  83. end;
  84. vmt := pClass(pointer(vmt) + vmtParent)^;
  85. end;
  86. end;
  87. procedure FreeObjects(List: TFPList);
  88. var
  89. i: integer;
  90. begin
  91. for i:= 0 to List.Count - 1 do
  92. TObject(List.Items[i]).Free;
  93. end;
  94. end.