tcfindnested.pp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. unit tcfindnested;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils, fpcunit, testutils, testregistry;
  6. type
  7. { TTestFindComponent }
  8. TTestFindComponent= class(TTestCase)
  9. Private
  10. R,A,B,AC,BC,D : TComponent;
  11. Function CreateNamed(AOwner : TComponent; AName : String) : TComponent;
  12. Procedure CheckFind(Root : TComponent; AName : String; Expected : TComponent);
  13. Protected
  14. procedure SetUp; override;
  15. procedure TearDown; override;
  16. published
  17. procedure TestFindA;
  18. procedure TestEmpty;
  19. procedure TestFindB;
  20. procedure TestFindACaseDiffer;
  21. procedure TestFindBCaseDiffer;
  22. procedure TestFindNonExist;
  23. procedure TestFindNonExistSub;
  24. procedure TestFindOwner;
  25. procedure TestFindOwnerNameOwner;
  26. procedure TestFindOwnerNamed;
  27. procedure TestFindOwnerSelf;
  28. procedure TestFindSubA;
  29. procedure TestFindSubB;
  30. procedure TestFindSubNoC;
  31. end;
  32. implementation
  33. {$DEFINE USENEW}
  34. {$IFDEF USENEW}
  35. Function FindNestedComponent(Root : TComponent; APath : String; CStyle : Boolean = True) : TComponent;
  36. Function GetNextName : String; inline;
  37. Var
  38. P : Integer;
  39. CM : Boolean;
  40. begin
  41. P:=Pos('.',APath);
  42. CM:=False;
  43. If (P=0) then
  44. begin
  45. If CStyle then
  46. begin
  47. P:=Pos('->',APath);
  48. CM:=P<>0;
  49. end;
  50. If (P=0) Then
  51. P:=Length(APath)+1;
  52. end;
  53. Result:=Copy(APath,1,P-1);
  54. Delete(APath,1,P+Ord(CM));
  55. end;
  56. Var
  57. C : TComponent;
  58. S : String;
  59. begin
  60. If (APath='') then
  61. Result:=Nil
  62. else
  63. begin
  64. Result:=Root;
  65. While (APath<>'') And (Result<>Nil) do
  66. begin
  67. C:=Result;
  68. S:=Uppercase(GetNextName);
  69. Result:=C.FindComponent(S);
  70. If (Result=Nil) And (S='OWNER') then
  71. Result:=C;
  72. end;
  73. end;
  74. end;
  75. {$ENDIF}
  76. procedure TTestFindComponent.TestEmpty;
  77. begin
  78. // Delphi crashes on this test, don't think we should copy that :-)
  79. CheckFind(R,'',Nil);
  80. end;
  81. procedure TTestFindComponent.TestFindA;
  82. begin
  83. CheckFind(R,'AAAA',A);
  84. end;
  85. procedure TTestFindComponent.TestFindB;
  86. begin
  87. CheckFind(R,'BBBB',B);
  88. end;
  89. procedure TTestFindComponent.TestFindACaseDiffer;
  90. begin
  91. CheckFind(R,'aaaa',A);
  92. end;
  93. procedure TTestFindComponent.TestFindBCaseDiffer;
  94. begin
  95. CheckFind(R,'bbbb',B);
  96. end;
  97. procedure TTestFindComponent.TestFindNonExistSub;
  98. begin
  99. CheckFind(R,'aaaa.bbbb',Nil);
  100. end;
  101. procedure TTestFindComponent.TestFindNonExist;
  102. begin
  103. CheckFind(R,'qqqq',Nil);
  104. end;
  105. procedure TTestFindComponent.TestFindSubA;
  106. begin
  107. CheckFind(R,'aaaa.cccc',ac);
  108. end;
  109. procedure TTestFindComponent.TestFindSubB;
  110. begin
  111. CheckFind(R,'bbbb.cccc',bc);
  112. end;
  113. procedure TTestFindComponent.TestFindSubNoC;
  114. begin
  115. CheckFind(R,'cccc',nil);
  116. end;
  117. procedure TTestFindComponent.TestFindOwnerNamed;
  118. begin
  119. CheckFind(R,'BBBB.OWNER',D);
  120. end;
  121. procedure TTestFindComponent.TestFindOwner;
  122. begin
  123. CheckFind(B,'OWNER',D);
  124. end;
  125. procedure TTestFindComponent.TestFindOwnerSelf;
  126. begin
  127. CheckFind(A,'OWNER',A);
  128. end;
  129. procedure TTestFindComponent.TestFindOwnerNameOwner;
  130. begin
  131. CheckFind(B,'OWNER.OWNER',D);
  132. end;
  133. function TTestFindComponent.CreateNamed(AOwner: TComponent; AName: String
  134. ): TComponent;
  135. begin
  136. Result:=TComponent.Create(AOwner);
  137. Result.Name:=AName;
  138. end;
  139. procedure TTestFindComponent.CheckFind(Root: TComponent; AName: String;
  140. Expected: TComponent);
  141. Function FN (C : TComponent): String;
  142. begin
  143. If (C=Nil) then
  144. Result:='<Nil>'
  145. else
  146. Result:=C.GetNamePath;
  147. end;
  148. Var
  149. Res : TComponent;
  150. begin
  151. Res:=FindNestedComponent(Root,AName);
  152. If Res<>Expected then
  153. Fail('Search for "'+AName+'" failed : Found "'+FN(Res)+'", expected : "'+Fn(Expected)+'"');
  154. end;
  155. procedure TTestFindComponent.SetUp;
  156. begin
  157. R:=CreateNamed(Nil,'Root');
  158. A:=CreateNamed(R,'AAAA');
  159. B:=CreateNamed(R,'BBBB');
  160. AC:=CreateNamed(A,'CCCC');
  161. BC:=CreateNamed(B,'CCCC');
  162. D:=CreateNamed(B,'OWNER');
  163. inherited SetUp;
  164. end;
  165. procedure TTestFindComponent.TearDown;
  166. begin
  167. FreeAndNil(R); // Will free the rest.
  168. A:=Nil;
  169. B:=Nil;
  170. AC:=Nil;
  171. BC:=Nil;
  172. D:=Nil;
  173. end;
  174. initialization
  175. RegisterTest(TTestFindComponent);
  176. end.