summaryrefslogtreecommitdiff
path: root/fpcsrc/tests/test/units/fpcunit/tcfindnested.pp
blob: cd303c6be9160d5e62187172932c38206302e3da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
unit tcfindnested;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, fpcunit, testregistry;

type

  { TTestFindComponent }

  TTestFindComponent= class(TTestCase)
  Private
    R,A,B,AC,BC,D : TComponent;
    Function CreateNamed(AOwner : TComponent; AName : String) : TComponent;
    Procedure CheckFind(Root : TComponent; AName : String; Expected : TComponent);
  Protected
    procedure SetUp; override;
    procedure TearDown; override;
  published
    procedure TestFindA;
    procedure TestEmpty;
    procedure TestFindB;
    procedure TestFindACaseDiffer;
    procedure TestFindBCaseDiffer;
    procedure TestFindNonExist;
    procedure TestFindNonExistSub;
    procedure TestFindOwner;
    procedure TestFindOwnerNameOwner;
    procedure TestFindOwnerNamed;
    procedure TestFindOwnerSelf;
    procedure TestFindSubA;
    procedure TestFindSubB;
    procedure TestFindSubNoC;
  end;

implementation
{$DEFINE USENEW}
{$IFDEF USENEW}
Function FindNestedComponent(Root : TComponent; APath : String; CStyle : Boolean = True) : TComponent;

  Function GetNextName : String; inline;
  
  Var
    P : Integer;
    CM : Boolean;
    
  begin
    P:=Pos('.',APath);
    CM:=False;
    If (P=0) then
      begin
      If CStyle then
        begin
        P:=Pos('->',APath);
        CM:=P<>0;
        end;
      If (P=0) Then
        P:=Length(APath)+1;
      end;
    Result:=Copy(APath,1,P-1);
    Delete(APath,1,P+Ord(CM));
  end;

Var
  C : TComponent;
  S : String;
begin
  If (APath='') then
    Result:=Nil
  else
    begin
    Result:=Root;
    While (APath<>'') And (Result<>Nil) do
      begin
      C:=Result;
      S:=Uppercase(GetNextName);
      Result:=C.FindComponent(S);
      If (Result=Nil) And (S='OWNER') then
        Result:=C;
      end;
    end;
end;
{$ENDIF}

procedure TTestFindComponent.TestEmpty;

begin
  // Delphi crashes on this test, don't think we should copy that :-)
  CheckFind(R,'',Nil);
end;

procedure TTestFindComponent.TestFindA;

begin
  CheckFind(R,'AAAA',A);
end;

procedure TTestFindComponent.TestFindB;

begin
  CheckFind(R,'BBBB',B);
end;

procedure TTestFindComponent.TestFindACaseDiffer;
begin
  CheckFind(R,'aaaa',A);
end;

procedure TTestFindComponent.TestFindBCaseDiffer;
begin
  CheckFind(R,'bbbb',B);
end;

procedure TTestFindComponent.TestFindNonExistSub;
begin
  CheckFind(R,'aaaa.bbbb',Nil);
end;

procedure TTestFindComponent.TestFindNonExist;
begin
  CheckFind(R,'qqqq',Nil);
end;

procedure TTestFindComponent.TestFindSubA;
begin
  CheckFind(R,'aaaa.cccc',ac);
end;

procedure TTestFindComponent.TestFindSubB;
begin
  CheckFind(R,'bbbb.cccc',bc);
end;

procedure TTestFindComponent.TestFindSubNoC;
begin
  CheckFind(R,'cccc',nil);
end;

procedure TTestFindComponent.TestFindOwnerNamed;
begin
  CheckFind(R,'BBBB.OWNER',D);
end;

procedure TTestFindComponent.TestFindOwner;
begin
  CheckFind(B,'OWNER',D);
end;

procedure TTestFindComponent.TestFindOwnerSelf;
begin
  CheckFind(A,'OWNER',A);
end;

procedure TTestFindComponent.TestFindOwnerNameOwner;
begin
  CheckFind(B,'OWNER.OWNER',D);
end;

function TTestFindComponent.CreateNamed(AOwner: TComponent; AName: String
  ): TComponent;
begin
  Result:=TComponent.Create(AOwner);
  Result.Name:=AName;
end;

procedure TTestFindComponent.CheckFind(Root: TComponent; AName: String;
  Expected: TComponent);
  
  Function FN (C : TComponent): String;
  
  begin
    If (C=Nil) then
      Result:='<Nil>'
    else
      Result:=C.GetNamePath;
  end;

Var
  Res : TComponent;
  
begin
  Res:=FindNestedComponent(Root,AName);
  If Res<>Expected then
    Fail('Search for "'+AName+'" failed : Found "'+FN(Res)+'", expected : "'+Fn(Expected)+'"');
end;

procedure TTestFindComponent.SetUp;
begin
  R:=CreateNamed(Nil,'Root');
  A:=CreateNamed(R,'AAAA');
  B:=CreateNamed(R,'BBBB');
  AC:=CreateNamed(A,'CCCC');
  BC:=CreateNamed(B,'CCCC');
  D:=CreateNamed(B,'OWNER');
  inherited SetUp;
end;

procedure TTestFindComponent.TearDown;
begin
  FreeAndNil(R); // Will free the rest.
  A:=Nil;
  B:=Nil;
  AC:=Nil;
  BC:=Nil;
  D:=Nil;
end;


initialization

  RegisterTest(TTestFindComponent); 
end.