| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413 |
- Unit tccompstreaming;
- interface
- Uses
- SysUtils,Classes,tcstreaming,fpcunit, testregistry;
- Type
- { TTestComponentStream }
- TTestComponentStream = Class(TTestStreaming)
- Published
- Procedure TestTEmptyComponent;
- Procedure TestTIntegerComponent;
- Procedure TestTIntegerComponent2;
- Procedure TestTIntegerComponent3;
- Procedure TestTIntegerComponent4;
- Procedure TestTIntegerComponent5;
- Procedure TestTInt64Component;
- Procedure TestTInt64Component2;
- Procedure TestTInt64Component3;
- Procedure TestTInt64Component4;
- Procedure TestTInt64Component5;
- Procedure TestTInt64Component6;
- Procedure TestTStringComponent;
- Procedure TestTStringComponent2;
- Procedure TestTWideStringComponent;
- Procedure TestTWideStringComponent2;
- Procedure TestTSingleComponent;
- Procedure TestTDoubleComponent;
- Procedure TestTExtendedComponent;
- Procedure TestTCompComponent;
- Procedure TestTCurrencyComponent;
- Procedure TestTDateTimeComponent;
- Procedure TestTDateTimeComponent2;
- Procedure TestTDateTimeComponent3;
- Procedure TestTEnumComponent;
- Procedure TestTEnumComponent2;
- Procedure TestTEnumComponent3;
- Procedure TestTEnumComponent4;
- Procedure TestTEnumComponent5;
- Procedure TestTSetComponent;
- Procedure TestTSetComponent2;
- Procedure TestTSetComponent3;
- Procedure TestTSetComponent4;
- Procedure TestTMultipleComponent;
- Procedure TestTPersistentComponent;
- Procedure TestTCollectionComponent;
- Procedure TestTCollectionComponent2;
- Procedure TestTCollectionComponent3;
- Procedure TestTCollectionComponent4;
- Procedure TestTCollectionComponent5;
- Procedure TestTOwnedComponent;
- Procedure TestTStreamedOwnedComponent;
- Procedure TestTStreamedOwnedComponents;
- Procedure TestTMethodComponent;
- Procedure TestTMethodComponent2;
- end;
- { TMyItem }
- TMyItem = Class(TCollectionItem)
- private
- FNR: Integer;
- FStr: String;
- Public
- Procedure Assign(Source : TPersistent); override;
- Published
- Property Nr : Integer Read FNR Write FNR;
- Property Str: String Read FStr Write FStr;
- end;
- { TMyColl }
- TMyColl = Class(TCollection)
- private
- function GetIt(index : Integer): TMyItem;
- procedure SetIt(index : Integer; const AValue: TMyItem);
- Public
- Property It[index : Integer] : TMyItem Read GetIt Write SetIt; default;
- end;
- { TCollComp }
- TCollComp = Class(TComponent)
- private
- FMyColl: TMyColl;
- procedure SetMyColl(const AValue: TMyColl);
- Public
- Constructor Create(AOwner : TComponent); override;
- Destructor Destroy; override;
- Function ToStream : TStream;
- Procedure FromStream(AStream : TStream);
- Published
- Property MyColl : TMyColl Read FMyColl Write SetMyColl;
- end;
- { TTestCollectionStream }
- TTestCollectionStream = Class(TTestCase)
- private
- procedure CompareColl(CA, CB: TMyColl);
- function CreateColl(Anr: Integer): TCollComp;
- function EmptyComp: TCollComp;
- procedure TestNr(ACount: Integer);
- Published
- procedure Test1;
- procedure Test2;
- procedure Test3;
- procedure TestClear;
- procedure TestEmpty;
- end;
- Implementation
- uses testcomps;
- Procedure TTestComponentStream.TestTEmptyComponent;
- Var
- C : TComponent;
- begin
- C:=TEmptyComponent.Create(Nil);
- Try
- SaveToStream(C);
- ExpectSignature;
- ExpectFlags([],0);
- ExpectBareString('TEmptyComponent');
- ExpectBareString('TestTEmptyComponent');
- ExpectEndOfList;
- ExpectEndOfList;
- Finally
- C.Free;
- end;
- end;
- Procedure TTestComponentStream.TestTIntegerComponent;
- Var
- C : TComponent;
- begin
- C:=TIntegerComponent.Create(Nil);
- Try
- SaveToStream(C);
- ExpectSignature;
- ExpectFlags([],0);
- ExpectBareString('TIntegerComponent');
- ExpectBareString('TestTIntegerComponent');
- ExpectBareString('IntProp');
- ExpectInteger(3);
- ExpectEndOfList;
- ExpectEndOfList;
- Finally
- C.Free;
- end;
- end;
- Procedure TTestComponentStream.TestTIntegerComponent2;
- Var
- C : TComponent;
- begin
- C:=TIntegerComponent2.Create(Nil);
- Try
- SaveToStream(C);
- ExpectSignature;
- ExpectFlags([],0);
- ExpectBareString('TIntegerComponent2');
- ExpectBareString('TestTIntegerComponent2');
- ExpectBareString('IntProp');
- ExpectInteger(1024);
- ExpectEndOfList;
- ExpectEndOfList;
- Finally
- C.Free;
- end;
- end;
- Procedure TTestComponentStream.TestTIntegerComponent3;
- Var
- C : TComponent;
- begin
- C:=TIntegerComponent3.Create(Nil);
- Try
- SaveToStream(C);
- ExpectSignature;
- ExpectFlags([],0);
- ExpectBareString('TIntegerComponent3');
- ExpectBareString('TestTIntegerComponent3');
- ExpectBareString('IntProp');
- ExpectInteger(262144);
- ExpectEndOfList;
- ExpectEndOfList;
- Finally
- C.Free;
- end;
- end;
- Procedure TTestComponentStream.TestTIntegerComponent4;
- Var
- C : TComponent;
- begin
- C:=TIntegerComponent4.Create(Nil);
- Try
- SaveToStream(C);
- ExpectSignature;
- ExpectFlags([],0);
- ExpectBareString('TIntegerComponent4');
- ExpectBareString('TestTIntegerComponent4');
- ExpectEndOfList;
- ExpectEndOfList;
- Finally
- C.Free;
- end;
- end;
- Procedure TTestComponentStream.TestTIntegerComponent5;
- Var
- C : TComponent;
- begin
- C:=TIntegerComponent5.Create(Nil);
- Try
- SaveToStream(C);
- ExpectSignature;
- ExpectFlags([],0);
- ExpectBareString('TIntegerComponent5');
- ExpectBareString('TestTIntegerComponent5');
- ExpectBareString('IntProp');
- ExpectInteger(5);
- ExpectEndOfList;
- ExpectEndOfList;
- Finally
- C.Free;
- end;
- end;
- Procedure TTestComponentStream.TestTInt64Component;
- Var
- C : TComponent;
- begin
- C:=TInt64Component.Create(Nil);
- Try
- SaveToStream(C);
- ExpectSignature;
- ExpectFlags([],0);
- ExpectBareString('TInt64Component');
- ExpectBareString('TestTInt64Component');
- ExpectBareString('Int64Prop');
- ExpectInteger(4);
- ExpectEndOfList;
- ExpectEndOfList;
- Finally
- C.Free;
- end;
- end;
- Procedure TTestComponentStream.TestTInt64Component2;
- Var
- C : TComponent;
- begin
- C:=TInt64Component2.Create(Nil);
- Try
- SaveToStream(C);
- ExpectSignature;
- ExpectFlags([],0);
- ExpectBareString('TInt64Component2');
- ExpectBareString('TestTInt64Component2');
- ExpectBareString('Int64Prop');
- ExpectInteger(1024);
- ExpectEndOfList;
- ExpectEndOfList;
- Finally
- C.Free;
- end;
- end;
- Procedure TTestComponentStream.TestTInt64Component3;
- Var
- C : TComponent;
- begin
- C:=TInt64Component3.Create(Nil);
- Try
- SaveToStream(C);
- ExpectSignature;
- ExpectFlags([],0);
- ExpectBareString('TInt64Component3');
- ExpectBareString('TestTInt64Component3');
- ExpectBareString('Int64Prop');
- ExpectInteger(262144);
- ExpectEndOfList;
- ExpectEndOfList;
- Finally
- C.Free;
- end;
- end;
- Procedure TTestComponentStream.TestTInt64Component4;
- Var
- C : TComponent;
- begin
- C:=TInt64Component4.Create(Nil);
- Try
- SaveToStream(C);
- ExpectSignature;
- ExpectFlags([],0);
- ExpectBareString('TInt64Component4');
- ExpectBareString('TestTInt64Component4');
- ExpectBareString('Int64Prop');
- ExpectInt64(2147745791);
- ExpectEndOfList;
- ExpectEndOfList;
- Finally
- C.Free;
- end;
- end;
- Procedure TTestComponentStream.TestTInt64Component5;
- Var
- C : TComponent;
- begin
- C:=TInt64Component5.Create(Nil);
- Try
- SaveToStream(C);
- ExpectSignature;
- ExpectFlags([],0);
- ExpectBareString('TInt64Component5');
- ExpectBareString('TestTInt64Component5');
- ExpectBareString('Int64Prop');
- ExpectInteger(7);
- ExpectEndOfList;
- ExpectEndOfList;
- Finally
- C.Free;
- end;
- end;
- Procedure TTestComponentStream.TestTInt64Component6;
- Var
- C : TComponent;
- begin
- C:=TInt64Component6.Create(Nil);
- Try
- SaveToStream(C);
- ExpectSignature;
- ExpectFlags([],0);
- ExpectBareString('TInt64Component6');
- ExpectBareString('TestTInt64Component6');
- ExpectBareString('Int64Prop');
- ExpectInteger(8);
- ExpectEndOfList;
- ExpectEndOfList;
- Finally
- C.Free;
- end;
- end;
- Procedure TTestComponentStream.TestTStringComponent;
- Var
- C : TComponent;
- begin
- C:=TStringComponent.Create(Nil);
- Try
- SaveToStream(C);
- ExpectSignature;
- ExpectFlags([],0);
- ExpectBareString('TStringComponent');
- ExpectBareString('TestTStringComponent');
- ExpectBareString('StringProp');
- ExpectString('A string');
- ExpectEndOfList;
- ExpectEndOfList;
- Finally
- C.Free;
- end;
- end;
- Procedure TTestComponentStream.TestTStringComponent2;
- Var
- C : TComponent;
- begin
- C:=TStringComponent2.Create(Nil);
- Try
- SaveToStream(C);
- ExpectSignature;
- ExpectFlags([],0);
- ExpectBareString('TStringComponent2');
- ExpectBareString('TestTStringComponent2');
- ExpectEndOfList;
- ExpectEndOfList;
- Finally
- C.Free;
- end;
- end;
- Procedure TTestComponentStream.TestTWideStringComponent;
- Var
- C : TComponent;
- begin
- C:=TWideStringComponent.Create(Nil);
- Try
- SaveToStream(C);
- ExpectSignature;
- ExpectFlags([],0);
- ExpectBareString('TWideStringComponent');
- ExpectBareString('TestTWideStringComponent');
- ExpectBareString('WideStringProp');
- ExpectString('Some WideString');
- ExpectEndOfList;
- ExpectEndOfList;
- Finally
- C.Free;
- end;
- end;
- Procedure TTestComponentStream.TestTWideStringComponent2;
- Var
- C : TComponent;
- begin
- C:=TWideStringComponent2.Create(Nil);
- Try
- SaveToStream(C);
- ExpectSignature;
- ExpectFlags([],0);
- ExpectBareString('TWideStringComponent2');
- ExpectBareString('TestTWideStringComponent2');
- ExpectEndOfList;
- ExpectEndOfList;
- Finally
- C.Free;
- end;
- end;
- Procedure TTestComponentStream.TestTSingleComponent;
- Var
- C : TComponent;
- begin
- C:=TSingleComponent.Create(Nil);
- Try
- SaveToStream(C);
- ExpectSignature;
- ExpectFlags([],0);
- ExpectBareString('TSingleComponent');
- ExpectBareString('TestTSingleComponent');
- ExpectBareString('SingleProp');
- ExpectExtended(1.23);
- ExpectEndOfList;
- ExpectEndOfList;
- Finally
- C.Free;
- end;
- end;
- Procedure TTestComponentStream.TestTDoubleComponent;
- Var
- C : TComponent;
- begin
- C:=TDoubleComponent.Create(Nil);
- Try
- SaveToStream(C);
- ExpectSignature;
- ExpectFlags([],0);
- ExpectBareString('TDoubleComponent');
- ExpectBareString('TestTDoubleComponent');
- ExpectBareString('DoubleProp');
- ExpectExtended(2.34);
- ExpectEndOfList;
- ExpectEndOfList;
- Finally
- C.Free;
- end;
- end;
- Procedure TTestComponentStream.TestTExtendedComponent;
- Var
- C : TComponent;
- begin
- C:=TExtendedComponent.Create(Nil);
- Try
- SaveToStream(C);
- ExpectSignature;
- ExpectFlags([],0);
- ExpectBareString('TExtendedComponent');
- ExpectBareString('TestTExtendedComponent');
- ExpectBareString('ExtendedProp');
- ExpectExtended(3.45);
- ExpectEndOfList;
- ExpectEndOfList;
- Finally
- C.Free;
- end;
- end;
- Procedure TTestComponentStream.TestTCompComponent;
- Var
- C : TComponent;
- begin
- C:=TCompComponent.Create(Nil);
- Try
- SaveToStream(C);
- ExpectSignature;
- ExpectFlags([],0);
- ExpectBareString('TCompComponent');
- ExpectBareString('TestTCompComponent');
- ExpectBareString('ExtendedProp');
- ExpectExtended(5.00);
- ExpectEndOfList;
- ExpectEndOfList;
- Finally
- C.Free;
- end;
- end;
- Procedure TTestComponentStream.TestTCurrencyComponent;
- Var
- C : TComponent;
- begin
- C:=TCurrencyComponent.Create(Nil);
- Try
- SaveToStream(C);
- ExpectSignature;
- ExpectFlags([],0);
- ExpectBareString('TCurrencyComponent');
- ExpectBareString('TestTCurrencyComponent');
- ExpectBareString('CurrencyProp');
- ExpectExtended(5.67);
- ExpectEndOfList;
- ExpectEndOfList;
- Finally
- C.Free;
- end;
- end;
- Procedure TTestComponentStream.TestTDateTimeComponent;
- Var
- C : TComponent;
- begin
- C:=TDateTimeComponent.Create(Nil);
- Try
- SaveToStream(C);
- ExpectSignature;
- ExpectFlags([],0);
- ExpectBareString('TDateTimeComponent');
- ExpectBareString('TestTDateTimeComponent');
- ExpectBareString('DateTimeProp');
- ExpectExtended(35278.00);
- ExpectEndOfList;
- ExpectEndOfList;
- Finally
- C.Free;
- end;
- end;
- Procedure TTestComponentStream.TestTDateTimeComponent2;
- Var
- C : TComponent;
- begin
- C:=TDateTimeComponent2.Create(Nil);
- Try
- SaveToStream(C);
- ExpectSignature;
- ExpectFlags([],0);
- ExpectBareString('TDateTimeComponent2');
- ExpectBareString('TestTDateTimeComponent2');
- ExpectBareString('DateTimeProp');
- ExpectExtended(0.97);
- ExpectEndOfList;
- ExpectEndOfList;
- Finally
- C.Free;
- end;
- end;
- Procedure TTestComponentStream.TestTDateTimeComponent3;
- Var
- C : TComponent;
- begin
- C:=TDateTimeComponent3.Create(Nil);
- Try
- SaveToStream(C);
- ExpectSignature;
- ExpectFlags([],0);
- ExpectBareString('TDateTimeComponent3');
- ExpectBareString('TestTDateTimeComponent3');
- ExpectBareString('DateTimeProp');
- ExpectExtended(35278.97);
- ExpectEndOfList;
- ExpectEndOfList;
- Finally
- C.Free;
- end;
- end;
- Procedure TTestComponentStream.TestTEnumComponent;
- Var
- C : TComponent;
- begin
- C:=TEnumComponent.Create(Nil);
- Try
- SaveToStream(C);
- ExpectSignature;
- ExpectFlags([],0);
- ExpectBareString('TEnumComponent');
- ExpectBareString('TestTEnumComponent');
- ExpectBareString('Dice');
- ExpectIdent('four');
- ExpectEndOfList;
- ExpectEndOfList;
- Finally
- C.Free;
- end;
- end;
- Procedure TTestComponentStream.TestTEnumComponent2;
- Var
- C : TComponent;
- begin
- C:=TEnumComponent2.Create(Nil);
- Try
- SaveToStream(C);
- ExpectSignature;
- ExpectFlags([],0);
- ExpectBareString('TEnumComponent2');
- ExpectBareString('TestTEnumComponent2');
- {$ifndef FPC}
- // FPC does not stream an undeclared default value, it assumes the
- // 0-the value is the default.
- ExpectBareString('Dice');
- ExpectIdent('one');
- {$endif FPC}
- ExpectEndOfList;
- ExpectEndOfList;
- Finally
- C.Free;
- end;
- end;
- Procedure TTestComponentStream.TestTEnumComponent3;
- Var
- C : TComponent;
- begin
- C:=TEnumComponent3.Create(Nil);
- Try
- SaveToStream(C);
- ExpectSignature;
- ExpectFlags([],0);
- ExpectBareString('TEnumComponent3');
- ExpectBareString('TestTEnumComponent3');
- ExpectBareString('Dice');
- ExpectIdent('three');
- ExpectEndOfList;
- ExpectEndOfList;
- Finally
- C.Free;
- end;
- end;
- Procedure TTestComponentStream.TestTEnumComponent4;
- Var
- C : TComponent;
- begin
- C:=TEnumComponent4.Create(Nil);
- Try
- SaveToStream(C);
- ExpectSignature;
- ExpectFlags([],0);
- ExpectBareString('TEnumComponent4');
- ExpectBareString('TestTEnumComponent4');
- ExpectEndOfList;
- ExpectEndOfList;
- Finally
- C.Free;
- end;
- end;
- Procedure TTestComponentStream.TestTEnumComponent5;
- Var
- C : TComponent;
- begin
- C:=TEnumComponent5.Create(Nil);
- Try
- SaveToStream(C);
- ExpectSignature;
- ExpectFlags([],0);
- ExpectBareString('TEnumComponent5');
- ExpectBareString('TestTEnumComponent5');
- ExpectEndOfList;
- ExpectEndOfList;
- Finally
- C.Free;
- end;
- end;
- Procedure TTestComponentStream.TestTSetComponent;
- Var
- C : TComponent;
- begin
- C:=TSetComponent.Create(Nil);
- Try
- SaveToStream(C);
- ExpectSignature;
- ExpectFlags([],0);
- ExpectBareString('TSetComponent');
- ExpectBareString('TestTSetComponent');
- ExpectBareString('Throw');
- ExpectValue(vaSet);
- ExpectBareString('two');
- ExpectBareString('five');
- ExpectBareString('');
- ExpectEndOfList;
- ExpectEndOfList;
- Finally
- C.Free;
- end;
- end;
- Procedure TTestComponentStream.TestTSetComponent2;
- Var
- C : TComponent;
- begin
- C:=TSetComponent2.Create(Nil);
- Try
- SaveToStream(C);
- ExpectSignature;
- ExpectFlags([],0);
- ExpectBareString('TSetComponent2');
- ExpectBareString('TestTSetComponent2');
- {$ifndef FPC}
- // Same as for sets: a set with undeclared default is regarded as
- // A set with default [], and is not streamed if it is empty.
- ExpectBareString('Throw');
- ExpectValue(vaSet);
- ExpectBareString('');
- {$endif FPC}
- ExpectEndOfList;
- ExpectEndOfList;
- Finally
- C.Free;
- end;
- end;
- Procedure TTestComponentStream.TestTSetComponent3;
- Var
- C : TComponent;
- begin
- C:=TSetComponent3.Create(Nil);
- Try
- SaveToStream(C);
- ExpectSignature;
- ExpectFlags([],0);
- ExpectBareString('TSetComponent3');
- ExpectBareString('TestTSetComponent3');
- ExpectBareString('Throw');
- ExpectValue(vaSet);
- ExpectBareString('one');
- ExpectBareString('four');
- ExpectBareString('');
- ExpectEndOfList;
- ExpectEndOfList;
- Finally
- C.Free;
- end;
- end;
- Procedure TTestComponentStream.TestTSetComponent4;
- Var
- C : TComponent;
- begin
- C:=TSetComponent4.Create(Nil);
- Try
- SaveToStream(C);
- ExpectSignature;
- ExpectFlags([],0);
- ExpectBareString('TSetComponent4');
- ExpectBareString('TestTSetComponent4');
- ExpectEndOfList;
- ExpectEndOfList;
- Finally
- C.Free;
- end;
- end;
- Procedure TTestComponentStream.TestTMultipleComponent;
- Var
- C : TComponent;
- begin
- C:=TMultipleComponent.Create(Nil);
- Try
- SaveToStream(C);
- ExpectSignature;
- ExpectFlags([],0);
- ExpectBareString('TMultipleComponent');
- ExpectBareString('TestTMultipleComponent');
- ExpectBareString('IntProp');
- ExpectInteger(1);
- ExpectBareString('StringProp');
- ExpectString('A String');
- ExpectBareString('CurrencyProp');
- ExpectExtended(2.30);
- ExpectBareString('Dice');
- ExpectIdent('two');
- ExpectBareString('Throw');
- ExpectValue(vaSet);
- ExpectBareString('three');
- ExpectBareString('four');
- ExpectBareString('');
- ExpectEndOfList;
- ExpectEndOfList;
- Finally
- C.Free;
- end;
- end;
- Procedure TTestComponentStream.TestTPersistentComponent;
- Var
- C : TComponent;
- begin
- C:=TPersistentComponent.Create(Nil);
- Try
- SaveToStream(C);
- ExpectSignature;
- ExpectFlags([],0);
- ExpectBareString('TPersistentComponent');
- ExpectBareString('TestTPersistentComponent');
- ExpectBareString('Persist.AInteger');
- ExpectInteger(3);
- ExpectBareString('Persist.AString');
- ExpectString('A persistent string');
- ExpectEndOfList;
- ExpectEndOfList;
- Finally
- C.Free;
- end;
- end;
- Procedure TTestComponentStream.TestTCollectionComponent;
- Var
- C : TComponent;
- begin
- C:=TCollectionComponent.Create(Nil);
- Try
- SaveToStream(C);
- ExpectSignature;
- ExpectFlags([],0);
- ExpectBareString('TCollectionComponent');
- ExpectBareString('TestTCollectionComponent');
- ExpectBareString('Coll');
- ExpectValue(vaCollection);
- ExpectEndOfList;
- ExpectEndOfList;
- ExpectEndOfList;
- Finally
- C.Free;
- end;
- end;
- Procedure TTestComponentStream.TestTCollectionComponent2;
- Var
- C : TComponent;
- begin
- C:=TCollectionComponent2.Create(Nil);
- Try
- SaveToStream(C);
- ExpectSignature;
- ExpectFlags([],0);
- ExpectBareString('TCollectionComponent2');
- ExpectBareString('TestTCollectionComponent2');
- ExpectBareString('Coll');
- ExpectValue(vaCollection);
- ExpectValue(vaList);
- ExpectBareString('StrProp');
- ExpectString('First');
- ExpectEndOfList;
- ExpectValue(vaList);
- ExpectBareString('StrProp');
- ExpectString('Second');
- ExpectEndOfList;
- ExpectValue(vaList);
- ExpectBareString('StrProp');
- ExpectString('Third');
- ExpectEndOfList;
- ExpectEndOfList;
- ExpectEndOfList;
- ExpectEndOfList;
- Finally
- C.Free;
- end;
- end;
- Procedure TTestComponentStream.TestTCollectionComponent3;
- Var
- C : TComponent;
- begin
- C:=TCollectionComponent3.Create(Nil);
- Try
- SaveToStream(C);
- ExpectSignature;
- ExpectFlags([],0);
- ExpectBareString('TCollectionComponent3');
- ExpectBareString('TestTCollectionComponent3');
- ExpectBareString('Coll');
- ExpectValue(vaCollection);
- ExpectValue(vaList);
- ExpectBareString('StrProp');
- ExpectString('First');
- ExpectEndOfList;
- ExpectValue(vaList);
- ExpectEndOfList;
- ExpectValue(vaList);
- ExpectBareString('StrProp');
- ExpectString('Third');
- ExpectEndOfList;
- ExpectEndOfList;
- ExpectEndOfList;
- ExpectEndOfList;
- Finally
- C.Free;
- end;
- end;
- Procedure TTestComponentStream.TestTCollectionComponent4;
- Var
- C : TComponent;
- begin
- C:=TCollectionComponent4.Create(Nil);
- Try
- SaveToStream(C);
- ExpectSignature;
- ExpectFlags([],0);
- ExpectBareString('TCollectionComponent4');
- ExpectBareString('TestTCollectionComponent4');
- ExpectBareString('Coll');
- ExpectValue(vaCollection);
- ExpectValue(vaList);
- ExpectBareString('StrProp');
- ExpectString('Something');
- ExpectEndOfList;
- ExpectEndOfList;
- ExpectEndOfList;
- ExpectEndOfList;
- Finally
- C.Free;
- end;
- end;
- Procedure TTestComponentStream.TestTCollectionComponent5;
- Var
- C : TComponent;
- begin
- C:=TCollectionComponent5.Create(Nil);
- Try
- SaveToStream(C);
- ExpectSignature;
- ExpectFlags([],0);
- ExpectBareString('TCollectionComponent5');
- ExpectBareString('TestTCollectionComponent5');
- ExpectBareString('Coll');
- ExpectValue(vaCollection);
- ExpectValue(vaList);
- ExpectBareString('StrProp1');
- ExpectString('Something');
- ExpectBareString('StrProp2');
- ExpectString('Otherthing');
- ExpectEndOfList;
- ExpectValue(vaList);
- ExpectBareString('StrProp1');
- ExpectString('Something 2');
- ExpectBareString('StrProp2');
- ExpectString('Otherthing 2');
- ExpectEndOfList;
- ExpectEndOfList;
- ExpectEndOfList;
- Finally
- C.Free;
- end;
- end;
- Procedure TTestComponentStream.TestTOwnedComponent;
- Var
- C : TComponent;
- begin
- C:=TOwnedComponent.Create(Nil);
- Try
- SaveToStream(C);
- ExpectSignature;
- ExpectFlags([],0);
- ExpectBareString('TOwnedComponent');
- ExpectBareString('TestTOwnedComponent');
- ExpectBareString('CompProp');
- ExpectIdent('SubComponent');
- ExpectEndOfList;
- ExpectEndOfList;
- Finally
- C.Free;
- end;
- end;
- Procedure TTestComponentStream.TestTStreamedOwnedComponent;
- Var
- C : TComponent;
- begin
- C:=TStreamedOwnedComponent.Create(Nil);
- Try
- SaveToStream(C);
- ExpectSignature;
- ExpectFlags([],0);
- ExpectBareString('TStreamedOwnedComponent');
- ExpectBareString('TestTStreamedOwnedComponent');
- ExpectEndOfList;
- ExpectFlags([],0);
- ExpectBareString('TIntegerComponent');
- ExpectBareString('Sub');
- ExpectBareString('IntProp');
- ExpectInteger(3);
- ExpectEndOfList;
- ExpectEndOfList;
- ExpectEndOfList;
- ExpectEndOfStream;
- Finally
- C.Free;
- end;
- end;
- Procedure TTestComponentStream.TestTStreamedOwnedComponents;
- Var
- C : TComponent;
- begin
- C:=TStreamedOwnedComponents.Create(Nil);
- Try
- SaveToStream(C);
- ExpectSignature;
- ExpectFlags([],0);
- ExpectBareString('TStreamedOwnedComponents');
- ExpectBareString('TestTStreamedOwnedComponents');
- ExpectEndOfList;
- ExpectFlags([],0);
- ExpectBareString('TIntegerComponent');
- ExpectBareString('SubA');
- ExpectBareString('IntProp');
- ExpectInteger(3);
- ExpectEndOfList;
- ExpectEndOfList;
- ExpectFlags([],0);
- ExpectBareString('TStringComponent');
- ExpectBareString('SubB');
- ExpectBareString('StringProp');
- ExpectString('A string');
- ExpectEndOfList;
- ExpectEndOfList;
- ExpectEndOfList;
- ExpectEndOfStream;
- Finally
- C.Free;
- end;
- end;
- Procedure TTestComponentStream.TestTMethodComponent;
- Var
- C : TComponent;
- begin
- C:=TMethodComponent.Create(Nil);
- Try
- SaveToStream(C);
- ExpectSignature;
- ExpectFlags([],0);
- ExpectBareString('TMethodComponent');
- ExpectBareString('TestTMethodComponent');
- ExpectBareString('MethodProp');
- ExpectIdent('MyMethod');
- ExpectEndOfList;
- ExpectEndOfList;
- Finally
- C.Free;
- end;
- end;
- Procedure TTestComponentStream.TestTMethodComponent2;
- Var
- C : TComponent;
- begin
- C:=TMethodComponent2.Create(Nil);
- Try
- SaveToStream(C);
- ExpectSignature;
- ExpectFlags([],0);
- ExpectBareString('TMethodComponent2');
- ExpectBareString('TestTMethodComponent2');
- ExpectEndOfList;
- ExpectFlags([],0);
- ExpectBareString('TMethodComponent');
- ExpectBareString('AComponent');
- ExpectBareString('MethodProp');
- ExpectIdent('MyMethod2');
- ExpectEndOfList;
- ExpectEndOfList;
- ExpectEndOfList;
- Finally
- C.Free;
- end;
- end;
- { TMyColl }
- function TMyColl.GetIt(index : Integer): TMyItem;
- begin
- Result:=Items[Index] as TMyItem;
- end;
- procedure TMyColl.SetIt(index : Integer; const AValue: TMyItem);
- begin
- Items[Index]:=AValue;
- end;
- { TCollComp }
- procedure TCollComp.SetMyColl(const AValue: TMyColl);
- begin
- if (FMyColl=AValue) then
- exit;
- FMyColl.Assign(AValue);
- end;
- constructor TCollComp.Create(AOwner: TComponent);
- begin
- inherited Create(AOwner);
- FMyColl:=TMyCOll.Create(TMyItem);
- end;
- destructor TCollComp.Destroy;
- begin
- FreeAndNil(FMyColl);
- inherited Destroy;
- end;
- function TCollComp.ToStream: TStream;
- begin
- Result:=TMemoryStream.Create;
- Result.WriteComponent(Self);
- Result.Position:=0;
- end;
- procedure TCollComp.FromStream(AStream: TStream);
- begin
- AStream.ReadComponent(Self);
- Astream.Free;
- end;
- procedure TMyItem.Assign(Source: TPersistent);
- Var
- I : TMyItem;
- begin
- If (Source is TMyItem) then
- begin
- I:=Source as TMyItem;
- FNR:=I.NR;
- FStr:=I.Str;
- end
- else
- inherited Assign(Source);
- end;
- Procedure TTestCollectionStream.CompareColl(CA,CB : TMyColl);
- Var
- I : Integer;
- begin
- AssertEquals('Counts differ: %d %d',CA.Count,CB.Count);
- For I:=0 to CA.Count-1 do
- begin
- AssertEquals(Format('Nr property of element %d equals',[I]),CA[i].Nr,CB[i].Nr);
- AssertEquals(Format('Str property of element %d equals',[I]),CA[i].Str,CB[i].Str);
- end;
- end;
- Function TTestCollectionStream.EmptyComp : TCollComp;
- begin
- Result:=TCollComp.Create(Nil);
- end;
- Function TTestCollectionStream.CreateColl(Anr : Integer) : TCollComp;
- Var
- I : Integer;
- T : TMyItem;
- begin
- Result:=EmptyComp;
- Result.Name:='C'+IntToStr(Anr);
- For I:=0 to ANr-1 do
- begin
- T:=Result.MyColl.Add as TMyItem;
- T.Nr:=I; // not I+1, so the default value gets tested too
- T.Str:=IntToStr(I+1);
- end;
- end;
- Procedure TTestCollectionStream.TestEmpty;
- Var
- CA,CB : TCollComp;
- begin
- CA:=CreateColl(0);
- try
- CB:=EmptyComp;
- Try
- CB.FromStream(CA.ToStream);
- CompareColl(CA.MyColl,CB.MyColl);
- Finally
- CB.Free;
- end;
- Finally
- CA.Free;
- end;
- end;
- Procedure TTestCollectionStream.TestNr(ACount : Integer);
- Var
- CA,CB : TCollComp;
- begin
- CA:=CreateColl(ACount);
- try
- CB:=EmptyComp;
- Try
- CB.FromStream(CA.ToStream);
- CompareColl(CA.MyColl,CB.MyColl);
- Finally
- CB.Free;
- end;
- Finally
- CA.Free;
- end;
- end;
- Procedure TTestCollectionStream.TestClear;
- Var
- CA,CB : TCollComp;
- begin
- CA:=CreateColl(3);
- try
- CB:=CreateColl(1);
- CB.Name:='';
- Try
- // CB collection should be cleared before loading.
- CB.FromStream(CA.ToStream);
- CompareColl(CA.MyColl,CB.MyColl);
- Finally
- CB.Free;
- end;
- Finally
- CA.Free;
- end;
- end;
- Procedure TTestCollectionStream.Test1;
- begin
- TestNr(1);
- end;
- Procedure TTestCollectionStream.Test2;
- begin
- TestNr(2);
- end;
- Procedure TTestCollectionStream.Test3;
- begin
- TestNr(3);
- end;
- begin
- RegisterTests([TTestComponentStream,TTestCollectionStream]);
- end.
|