testutils.pp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. {$mode objfpc}
  2. {$h+}
  3. {
  4. $Id$
  5. This file is part of the Free Component Library (FCL)
  6. Copyright (c) 2004 by Dean Zobec
  7. Port to Free Pascal of the JUnit framework.
  8. See the file COPYING.FPC, included in this distribution,
  9. for details about the copyright.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. **********************************************************************}
  14. unit testutils;
  15. interface
  16. uses
  17. Classes, SysUtils;
  18. type
  19. {$M+}
  20. TNoRefCountObject = class(TObject, IInterface)
  21. protected
  22. { IInterface }
  23. function QueryInterface(const IID: TGUID; out Obj): HResult; virtual; stdcall;
  24. function _AddRef: Integer; stdcall;
  25. function _Release: Integer; stdcall;
  26. end;
  27. {$M-}
  28. procedure FreeObjects(List: TList);
  29. procedure GetMethodList( AObject: TObject; AList: TStrings ); overload;
  30. procedure GetMethodList( AClass: TClass; AList: TStrings ); overload;
  31. implementation
  32. function TNoRefCountObject.QueryInterface(const IID: TGUID; out Obj): HResult; stdcall;
  33. begin
  34. if GetInterface(IID, Obj) then Result := 0
  35. else Result := HRESULT($80004002);
  36. end;
  37. function TNoRefCountObject._AddRef: Integer;stdcall;
  38. begin
  39. Result := -1;
  40. end;
  41. function TNoRefCountObject._Release: Integer;stdcall;
  42. begin
  43. Result := -1;
  44. end;
  45. // been to the dentist and suffered a lot
  46. // Hack Alert! see objpas.inc
  47. // Get a list of published methods for a given class or object
  48. procedure GetMethodList( AObject: TObject; AList: TStrings );
  49. begin
  50. GetMethodList( AObject.ClassType, AList );
  51. end;
  52. procedure GetMethodList(AClass: TClass; AList: TStrings);
  53. type
  54. TMethodNameRec = packed record
  55. name : pshortstring;
  56. addr : pointer;
  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: TClass;
  67. idx: integer;
  68. begin
  69. AList.Clear;
  70. vmt := aClass;
  71. while assigned(vmt) do
  72. begin
  73. methodTable := pMethodNameTable((Pointer(vmt) + vmtMethodTable)^);
  74. if assigned(MethodTable) then
  75. begin
  76. for i := 0 to MethodTable^.count - 1 do
  77. begin
  78. idx := aList.IndexOf(MethodTable^.entries[i].name^);
  79. if (idx <> - 1) then
  80. //found overridden method so delete it
  81. aList.Delete(idx);
  82. aList.AddObject(MethodTable^.entries[i].name^, TObject(MethodTable^.entries[i].addr));
  83. end;
  84. end;
  85. vmt := pClass(pointer(vmt) + vmtParent)^;
  86. end;
  87. end;
  88. procedure FreeObjects(List: TList);
  89. var
  90. i: integer;
  91. begin
  92. for i:= 0 to List.Count - 1 do
  93. TObject(List.Items[i]).Free;
  94. end;
  95. end.