tcpersistent.pp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. unit tcpersistent;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils, fpcunit, testregistry;
  6. type
  7. { TTestTPersistent }
  8. TTestTPersistent= class(TTestCase)
  9. protected
  10. Instance : TPersistent;
  11. procedure SetUp; override;
  12. procedure TearDown; override;
  13. published
  14. procedure TestPropCount;
  15. procedure TestNamePath;
  16. end;
  17. { TMyPersistent }
  18. TMyPersistent = Class(TPersistent)
  19. private
  20. FMyProp: Integer;
  21. FOwner : TPersistent;
  22. protected
  23. function GetOwner: TPersistent; override;
  24. public
  25. procedure Assign(Source: TPersistent); override;
  26. published
  27. Property MyProp : Integer Read FMyProp Write FMyProp;
  28. end;
  29. { TTestPersistentDescendent }
  30. TTestPersistentDescendent = class(TTestCase)
  31. private
  32. procedure WrongAssign;
  33. Protected
  34. Instance : TMyPersistent;
  35. procedure SetUp; override;
  36. procedure TearDown; override;
  37. published
  38. procedure TestPropCount;
  39. procedure TestNamePath;
  40. procedure TestNamePathWithOwner;
  41. Procedure TestAssign;
  42. Procedure TestAssignFail;
  43. end;
  44. implementation
  45. uses typinfo;
  46. procedure TTestTPersistent.TestPropCount;
  47. Var
  48. ACOunt : Integer;
  49. P : Pointer;
  50. begin
  51. P:=Nil;
  52. ACOunt:=GetPropList(Instance,P);
  53. AssertEquals('Property count of TPersistence is zero',0,ACount);
  54. end;
  55. procedure TTestTPersistent.TestNamePath;
  56. begin
  57. AssertEquals('Namepath is class name if there is no owner','TPersistent',Instance.GetNamePath);
  58. end;
  59. procedure TTestTPersistent.SetUp;
  60. begin
  61. Instance:=TPersistent.Create;
  62. end;
  63. procedure TTestTPersistent.TearDown;
  64. begin
  65. FreeAndNil(Instance);
  66. end;
  67. { TTestPersistentDescendent }
  68. procedure TTestPersistentDescendent.SetUp;
  69. begin
  70. Instance:=TMyPersistent.Create;
  71. end;
  72. procedure TTestPersistentDescendent.TearDown;
  73. begin
  74. FreeAndNil(Instance);
  75. end;
  76. procedure TTestPersistentDescendent.TestPropCount;
  77. Var
  78. ACOunt : Integer;
  79. P : Pointer;
  80. begin
  81. P:=Nil;
  82. ACount:=GetPropList(Instance,P);
  83. AssertEquals('Property count of TPersistence is zero',1,ACount);
  84. Freemem(p);
  85. end;
  86. procedure TTestPersistentDescendent.TestNamePath;
  87. begin
  88. AssertEquals('Namepath is class name if there is no owner','TMyPersistent',Instance.GetNamePath);
  89. end;
  90. procedure TTestPersistentDescendent.TestNamePathWithOwner;
  91. Var
  92. AOwner : TMyPersistent;
  93. begin
  94. AOwner:=TMyPersistent.Create;
  95. try
  96. Instance.FOwner:=AOwner;
  97. AssertEquals('Namepath is owner namepath plus class name','TMyPersistent.TMyPersistent',Instance.GetNamePath);
  98. finally
  99. Aowner.Free;
  100. end;
  101. end;
  102. procedure TTestPersistentDescendent.TestAssign;
  103. Var
  104. I2 : TMyPersistent;
  105. begin
  106. I2:=TMyPersistent.Create;
  107. try
  108. I2.MyProp:=2;
  109. Instance.Assign(I2);
  110. AssertEquals('Property passed on during assign',2,Instance.MyProp);
  111. finally
  112. I2.Free;
  113. end;
  114. end;
  115. procedure TTestPersistentDescendent.TestAssignFail;
  116. begin
  117. AssertException('Assigning the wrong class',EConvertError,@WrongAssign);
  118. end;
  119. procedure TTestPersistentDescendent.WrongAssign;
  120. Var
  121. I2 : TPersistent;
  122. begin
  123. I2:=TPersistent.Create;
  124. try
  125. Instance.Assign(I2);
  126. finally
  127. I2.Free;
  128. end;
  129. end;
  130. { TMyPersistent }
  131. function TMyPersistent.GetOwner: TPersistent;
  132. begin
  133. Result:=FOwner;
  134. end;
  135. procedure TMyPersistent.Assign(Source: TPersistent);
  136. begin
  137. If (Source is TMyPersistent) then
  138. FMyProp:=TMyPersistent(Source).FMyProp
  139. else
  140. Inherited;
  141. end;
  142. initialization
  143. RegisterTests([TTestTPersistent,TTestPersistentDescendent]);
  144. end.