Browse Source

* Completed tccollection tests

git-svn-id: branches/cleanroom@9310 -
michael 18 years ago
parent
commit
d3a6c9bd6e
2 changed files with 379 additions and 8 deletions
  1. 230 2
      rtl/tests/tccollection.pp
  2. 149 6
      rtl/tests/testclasses.lpi

+ 230 - 2
rtl/tests/tccollection.pp

@@ -21,12 +21,32 @@ type
     Property Nr : integer Read FNr Write FNr;
   end;
   
+  { TMyCollection }
+
+  TMyCollection = Class(TCollection)
+  Private
+    FOwner : TPersistent;
+    FUpdateCount : Integer;
+    FLastNotifyItem,
+    FLastUpdate : TCollectionItem;
+    FNotifyCount : Integer;
+    FLastNotify : TCollectionNotification;
+    Function GetOwner : TPersistent; override;
+  Public
+    procedure Update(Item: TCollectionItem); override;
+    procedure Notify(Item: TCollectionItem;Action: TCollectionNotification); override;
+    Procedure ResetUpdate;
+    Procedure ResetNotify;
+    property PropName;
+  end;
+  
+  
   { TTestTCollection }
 
   TTestTCollection= class(TTestCase)
   private
   protected
-    FColl : TCollection;
+    FColl : TMyCollection;
     Function MyItem(I : integer) : TMyItem;
     procedure AddItems(ACount : Integer);
     procedure SetUp; override; 
@@ -44,7 +64,18 @@ type
     Procedure TestID;
     Procedure TestItemOwner;
     Procedure TestDisplayName;
+    procedure TestOwnerNamePath;
     Procedure TestItemNamePath;
+    Procedure TestOwnerItemNamePath;
+    Procedure TestChangeCollection;
+    Procedure TestUpdateAdd;
+    Procedure TestUpdateDelete;
+    Procedure TestUpdateDisplayName;
+    Procedure TestUpdateCount;
+    Procedure TestUpdateCountNested;
+    Procedure TestUpdateMove;
+    Procedure TestNotifyAdd;
+    Procedure TestNotifyDelete;
   end;
 
 implementation
@@ -169,6 +200,168 @@ begin
   AssertEquals('Item namepath is collection namepath+index',FColl.GetNamePath+'[1]',MyItem(1).GetNamePath);
 end;
 
+procedure TTestTCollection.TestOwnerItemNamePath;
+
+Var
+  P : TPersistent;
+
+begin
+  P:=TPersistent.Create;
+  try
+    TMyCollection(FColl).FOwner:=P;
+    AddItems(2);
+    TMyCollection(FColl).PropName:='Something';
+    AssertEquals('Item namepath is collection namepath+index','TPersistent.Something[0]',MyItem(0).GetNamePath);
+  finally
+    P.Free;
+  end;
+end;
+
+procedure TTestTCollection.TestOwnerNamePath;
+
+Var
+  P : TPersistent;
+
+begin
+  P:=TPersistent.Create;
+  try
+    TMyCollection(FColl).FOwner:=P;
+    AddItems(2);
+    TMyCollection(FColl).PropName:='Something';
+    AssertEquals('Namepath is collection namepath+index','TPersistent.Something',FColl.GetNamePath);
+  finally
+    P.Free;
+  end;
+end;
+
+procedure TTestTCollection.TestChangeCollection;
+
+Var
+  FCol2 : TCollection;
+  I : TCollectionItem;
+  
+begin
+  AddItems(2);
+  FCol2:=TCollection.Create(TMyItem);
+  try
+    I:=FCol2.Add;
+    I.Collection:=FColl;
+    AssertEquals('Moved item, count of source is zero',0,FCol2.Count);
+    AssertEquals('Moved item, count of dest is 1',3,FColl.Count);
+    AssertEquals('Moved item, index is 2',2,I.Index);
+    If (FColl.Items[0].Collection<>FColl) then
+      Fail('Collection owner is not set correctly after move');
+    AssertEquals('Moved item, ID is 2',2,I.ID);
+  finally
+    FCol2.free;
+  end;
+end;
+
+procedure TTestTCollection.TestUpdateAdd;
+begin
+  AddItems(1);
+  If (FColl.FLastUpdate<>Nil) then
+    Fail('update item found !');
+  AssertEquals('Update count is 1',1,FColl.FUpdateCount);
+
+end;
+
+procedure TTestTCollection.TestUpdateDelete;
+begin
+  AddItems(1);
+  FColl.ResetUpdate;
+  FColl.Delete(0);
+  If (FColl.FLastUpdate<>Nil) then
+    Fail('update item found !');
+  AssertEquals('Update count is 1',1,FColl.FUpdateCount);
+
+end;
+
+procedure TTestTCollection.TestUpdateDisplayName;
+begin
+  AddItems(1);
+  FColl.ResetUpdate;
+  MyItem(0).DisplayName:='Something';
+  AssertEquals('Display name notification. Update count is 1',1,FColl.FUpdateCount);
+  If (FColl.FLastUpdate<>MyItem(0)) then
+    Fail('No displayname update');
+end;
+
+procedure TTestTCollection.TestUpdateCount;
+begin
+  FColl.BeginUpdate;
+  Try
+    AddItems(2);
+    
+    AssertEquals('Beginupdate; adds. Update count is 0',0,FColl.FUpdateCount);
+    If (FColl.FLastUpdate<>Nil) then
+      Fail('Beginupdate; FlastUpdate not nil');
+  finally
+    FColl.EndUpdate;
+  end;
+  AssertEquals('Endupdate; adds. Update count is 1',1,FColl.FUpdateCount);
+  If (FColl.FLastUpdate<>Nil) then
+    Fail('Endupdate; FlastUpdate not nil');
+end;
+
+procedure TTestTCollection.TestUpdateCountNested;
+begin
+  FColl.BeginUpdate;
+  Try
+    AddItems(2);
+    FColl.BeginUpdate;
+    Try
+      AddItems(2);
+      AssertEquals('Beginupdate 2; adds. Update count is 0',0,FColl.FUpdateCount);
+      If (FColl.FLastUpdate<>Nil) then
+        Fail('Beginupdate 2; FlastUpdate not nil');
+    finally
+      FColl.EndUpdate;
+    end;
+    AssertEquals('Endupdate 1; Update count is 0',0,FColl.FUpdateCount);
+    If (FColl.FLastUpdate<>Nil) then
+      Fail('EndUpdate 1; FlastUpdate not nil');
+  finally
+    FColl.EndUpdate;
+  end;
+  AssertEquals('Endupdate 2; adds. Update count is 1',1,FColl.FUpdateCount);
+  If (FColl.FLastUpdate<>Nil) then
+    Fail('Endupdate 2; FlastUpdate not nil');
+end;
+
+procedure TTestTCollection.TestUpdateMove;
+begin
+  AddItems(5);
+  FColl.ResetUpdate;
+  MyItem(4).Index:=2;
+  AssertEquals('Moved item. Update count is 1',1,FColl.FUpdateCount);
+  If (FColl.FLastUpdate<>Nil) then
+    Fail('Moved item notification - not all items updated');
+end;
+
+procedure TTestTCollection.TestNotifyAdd;
+begin
+  AddItems(1);
+  If (FColl.FLastNotifyItem<>MyItem(0)) then
+    Fail('No notify item found !');
+  AssertEquals('Notify count is 1',1,FColl.FNotifyCount);
+  AssertEquals('Notify action is cnAdded',Ord(cnAdded),Ord(FColl.FLastNotify));
+end;
+
+procedure TTestTCollection.TestNotifyDelete;
+
+Var
+  I : TMyItem;
+
+begin
+  AddItems(3);
+  FColl.ResetNotify;
+  FColl.Delete(1);
+  // cnDeleting/cnExtracing. Can't currently test for 2 events...
+  AssertEquals('Notify count is 2',2,FColl.FNotifyCount);
+  AssertEquals('Notify action is cnExtracted',Ord(cnExtracting),Ord(FColl.FLastNotify));
+end;
+
 function TTestTCollection.MyItem(I: integer): TMyItem;
 begin
   Result:=TMyItem(FColl.Items[i]);
@@ -186,7 +379,7 @@ end;
 
 procedure TTestTCollection.SetUp; 
 begin
-  FColl:=TCollection.Create(TMyItem);
+  FColl:=TMyCollection.Create(TMyItem);
 end; 
 
 procedure TTestTCollection.TearDown; 
@@ -201,6 +394,41 @@ begin
   Result:=inherited GetOwner;
 end;
 
+{ TMyCollection }
+
+function TMyCollection.GetOwner: TPersistent;
+begin
+  Result:=FOwner;
+  If (Result=Nil) then
+    Result:=Inherited GetOwner;
+end;
+
+procedure TMyCollection.Update(Item: TCollectionItem);
+begin
+  Inc(FUpdateCount);
+  FLastUpdate:=Item;
+end;
+
+procedure TMyCollection.Notify(Item: TCollectionItem;
+  Action: TCollectionNotification);
+begin
+  Inc(FNotifyCount);
+  FLastNotify:=Action;
+  FLastNotifyItem:=Item;
+end;
+
+procedure TMyCollection.ResetUpdate;
+begin
+  FUpdateCount:=0;
+  FLastUpdate:=Nil;
+end;
+
+procedure TMyCollection.ResetNotify;
+begin
+  FNotifyCount:=0;
+  FLastNotifyItem:=Nil;
+end;
+
 initialization
 
   RegisterTest(TTestTCollection); 

+ 149 - 6
rtl/tests/testclasses.lpi

@@ -8,7 +8,7 @@
       <IconPath Value="./"/>
       <TargetFileExt Value=""/>
       <Title Value="findnested"/>
-      <ActiveEditorIndexAtStart Value="1"/>
+      <ActiveEditorIndexAtStart Value="2"/>
     </General>
     <VersionInfo>
       <ProjectVersion Value=""/>
@@ -35,7 +35,7 @@
         <PackageName Value="FPCUnitConsoleRunner"/>
       </Item2>
     </RequiredPackages>
-    <Units Count="7">
+    <Units Count="9">
       <Unit0>
         <Filename Value="testclasses.lpr"/>
         <IsPartOfProject Value="True"/>
@@ -63,15 +63,21 @@
         <Filename Value="tccollection.pp"/>
         <IsPartOfProject Value="True"/>
         <UnitName Value="tccollection"/>
+        <CursorPos X="1" Y="360"/>
+        <TopLine Value="338"/>
+        <EditorIndex Value="2"/>
         <UsageCount Value="20"/>
-        <SyntaxHighlighter Value="Text"/>
+        <Loaded Value="True"/>
       </Unit3>
       <Unit4>
         <Filename Value="tclist.pp"/>
         <IsPartOfProject Value="True"/>
         <UnitName Value="tclist"/>
+        <CursorPos X="1" Y="336"/>
+        <TopLine Value="331"/>
+        <EditorIndex Value="4"/>
         <UsageCount Value="20"/>
-        <SyntaxHighlighter Value="Text"/>
+        <Loaded Value="True"/>
       </Unit4>
       <Unit5>
         <Filename Value="tcpersistent.pp"/>
@@ -84,14 +90,151 @@
         <Filename Value="tclinkedlist.pp"/>
         <IsPartOfProject Value="True"/>
         <UnitName Value="tclinkedlist"/>
-        <CursorPos X="18" Y="1"/>
+        <CursorPos X="14" Y="1"/>
         <TopLine Value="1"/>
         <EditorIndex Value="1"/>
         <UsageCount Value="20"/>
         <Loaded Value="True"/>
       </Unit6>
+      <Unit7>
+        <Filename Value="../../../../fpc/rtl/objpas/classes/classesh.inc"/>
+        <CursorPos X="14" Y="432"/>
+        <TopLine Value="401"/>
+        <EditorIndex Value="3"/>
+        <UsageCount Value="10"/>
+        <Loaded Value="True"/>
+      </Unit7>
+      <Unit8>
+        <Filename Value="../../../../fpc/rtl/objpas/classes/collect.inc"/>
+        <CursorPos X="51" Y="319"/>
+        <TopLine Value="293"/>
+        <EditorIndex Value="5"/>
+        <UsageCount Value="10"/>
+        <Loaded Value="True"/>
+      </Unit8>
     </Units>
-    <JumpHistory Count="0" HistoryIndex="-1"/>
+    <JumpHistory Count="30" HistoryIndex="29">
+      <Position1>
+        <Filename Value="../../../../fpc/rtl/objpas/classes/collect.inc"/>
+        <Caret Line="1" Column="1" TopLine="1"/>
+      </Position1>
+      <Position2>
+        <Filename Value="../../../../fpc/rtl/objpas/classes/collect.inc"/>
+        <Caret Line="51" Column="24" TopLine="22"/>
+      </Position2>
+      <Position3>
+        <Filename Value="../../../../fpc/rtl/objpas/classes/collect.inc"/>
+        <Caret Line="218" Column="12" TopLine="192"/>
+      </Position3>
+      <Position4>
+        <Filename Value="../../../../fpc/rtl/objpas/classes/collect.inc"/>
+        <Caret Line="240" Column="30" TopLine="214"/>
+      </Position4>
+      <Position5>
+        <Filename Value="../../../../fpc/rtl/objpas/classes/collect.inc"/>
+        <Caret Line="1" Column="1" TopLine="1"/>
+      </Position5>
+      <Position6>
+        <Filename Value="../../../../fpc/rtl/objpas/classes/collect.inc"/>
+        <Caret Line="42" Column="32" TopLine="22"/>
+      </Position6>
+      <Position7>
+        <Filename Value="../../../../fpc/rtl/objpas/classes/collect.inc"/>
+        <Caret Line="48" Column="24" TopLine="22"/>
+      </Position7>
+      <Position8>
+        <Filename Value="../../../../fpc/rtl/objpas/classes/collect.inc"/>
+        <Caret Line="50" Column="24" TopLine="24"/>
+      </Position8>
+      <Position9>
+        <Filename Value="../../../../fpc/rtl/objpas/classes/collect.inc"/>
+        <Caret Line="218" Column="12" TopLine="192"/>
+      </Position9>
+      <Position10>
+        <Filename Value="../../../../fpc/rtl/objpas/classes/collect.inc"/>
+        <Caret Line="240" Column="30" TopLine="214"/>
+      </Position10>
+      <Position11>
+        <Filename Value="../../../../fpc/rtl/objpas/classes/collect.inc"/>
+        <Caret Line="1" Column="1" TopLine="1"/>
+      </Position11>
+      <Position12>
+        <Filename Value="../../../../fpc/rtl/objpas/classes/collect.inc"/>
+        <Caret Line="43" Column="34" TopLine="16"/>
+      </Position12>
+      <Position13>
+        <Filename Value="../../../../fpc/rtl/objpas/classes/collect.inc"/>
+        <Caret Line="91" Column="12" TopLine="65"/>
+      </Position13>
+      <Position14>
+        <Filename Value="../../../../fpc/rtl/objpas/classes/collect.inc"/>
+        <Caret Line="99" Column="10" TopLine="73"/>
+      </Position14>
+      <Position15>
+        <Filename Value="../../../../fpc/rtl/objpas/classes/collect.inc"/>
+        <Caret Line="29" Column="27" TopLine="14"/>
+      </Position15>
+      <Position16>
+        <Filename Value="../../../../fpc/rtl/objpas/classes/collect.inc"/>
+        <Caret Line="1" Column="1" TopLine="1"/>
+      </Position16>
+      <Position17>
+        <Filename Value="tccollection.pp"/>
+        <Caret Line="289" Column="5" TopLine="232"/>
+      </Position17>
+      <Position18>
+        <Filename Value="tccollection.pp"/>
+        <Caret Line="299" Column="14" TopLine="271"/>
+      </Position18>
+      <Position19>
+        <Filename Value="tccollection.pp"/>
+        <Caret Line="71" Column="1" TopLine="49"/>
+      </Position19>
+      <Position20>
+        <Filename Value="tccollection.pp"/>
+        <Caret Line="297" Column="50" TopLine="278"/>
+      </Position20>
+      <Position21>
+        <Filename Value="tccollection.pp"/>
+        <Caret Line="333" Column="5" TopLine="276"/>
+      </Position21>
+      <Position22>
+        <Filename Value="tccollection.pp"/>
+        <Caret Line="350" Column="5" TopLine="288"/>
+      </Position22>
+      <Position23>
+        <Filename Value="tccollection.pp"/>
+        <Caret Line="348" Column="71" TopLine="316"/>
+      </Position23>
+      <Position24>
+        <Filename Value="tccollection.pp"/>
+        <Caret Line="345" Column="28" TopLine="319"/>
+      </Position24>
+      <Position25>
+        <Filename Value="tccollection.pp"/>
+        <Caret Line="355" Column="18" TopLine="322"/>
+      </Position25>
+      <Position26>
+        <Filename Value="tccollection.pp"/>
+        <Caret Line="361" Column="22" TopLine="329"/>
+      </Position26>
+      <Position27>
+        <Filename Value="tccollection.pp"/>
+        <Caret Line="364" Column="45" TopLine="334"/>
+      </Position27>
+      <Position28>
+        <Filename Value="../../../../fpc/rtl/objpas/classes/collect.inc"/>
+        <Caret Line="195" Column="3" TopLine="192"/>
+      </Position28>
+      <Position29>
+        <Filename Value="../../../../fpc/rtl/objpas/classes/collect.inc"/>
+        <Caret Line="1" Column="1" TopLine="1"/>
+      </Position29>
+      <Position30>
+        <Filename Value="../../../../fpc/rtl/objpas/classes/collect.inc"/>
+        <Caret Line="176" Column="33" TopLine="150"/>
+      </Position30>
+    </JumpHistory>
   </ProjectOptions>
   <CompilerOptions>
     <Version Value="5"/>