tcpersistent.pp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. unit tcpersistent;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils, fpcunit, testutils, 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); virtual;
  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. end;
  85. procedure TTestPersistentDescendent.TestNamePath;
  86. begin
  87. AssertEquals('Namepath is class name if there is no owner','TMyPersistent',Instance.GetNamePath);
  88. end;
  89. procedure TTestPersistentDescendent.TestNamePathWithOwner;
  90. Var
  91. AOwner : TMyPersistent;
  92. begin
  93. AOwner:=TMyPersistent.Create;
  94. try
  95. Instance.FOwner:=AOwner;
  96. AssertEquals('Namepath is owner namepath plus class name','TMyPersistent.TMyPersistent',Instance.GetNamePath);
  97. finally
  98. Aowner.Free;
  99. end;
  100. end;
  101. procedure TTestPersistentDescendent.TestAssign;
  102. Var
  103. I2 : TMyPersistent;
  104. begin
  105. I2:=TMyPersistent.Create;
  106. try
  107. I2.MyProp:=2;
  108. Instance.Assign(I2);
  109. AssertEquals('Property passed on during assign',2,Instance.MyProp);
  110. finally
  111. I2.Free;
  112. end;
  113. end;
  114. procedure TTestPersistentDescendent.TestAssignFail;
  115. begin
  116. AssertException('Assigning the wrong class',EConvertError,@WrongAssign);
  117. end;
  118. procedure TTestPersistentDescendent.WrongAssign;
  119. Var
  120. I2 : TPersistent;
  121. begin
  122. I2:=TPersistent.Create;
  123. try
  124. Instance.Assign(I2);
  125. finally
  126. I2.Free;
  127. end;
  128. end;
  129. { TMyPersistent }
  130. function TMyPersistent.GetOwner: TPersistent;
  131. begin
  132. Result:=FOwner;
  133. end;
  134. procedure TMyPersistent.Assign(Source: TPersistent);
  135. begin
  136. If (Source is TMyPersistent) then
  137. FMyProp:=TMyPersistent(Source).FMyProp
  138. else
  139. Inherited;
  140. end;
  141. initialization
  142. RegisterTests([TTestTPersistent,TTestPersistentDescendent]);
  143. end.