tccompstreaming.pp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413
  1. Unit tccompstreaming;
  2. interface
  3. Uses
  4. SysUtils,Classes,tcstreaming,fpcunit, testregistry;
  5. Type
  6. { TTestComponentStream }
  7. TTestComponentStream = Class(TTestStreaming)
  8. Published
  9. Procedure TestTEmptyComponent;
  10. Procedure TestTIntegerComponent;
  11. Procedure TestTIntegerComponent2;
  12. Procedure TestTIntegerComponent3;
  13. Procedure TestTIntegerComponent4;
  14. Procedure TestTIntegerComponent5;
  15. Procedure TestTInt64Component;
  16. Procedure TestTInt64Component2;
  17. Procedure TestTInt64Component3;
  18. Procedure TestTInt64Component4;
  19. Procedure TestTInt64Component5;
  20. Procedure TestTInt64Component6;
  21. Procedure TestTStringComponent;
  22. Procedure TestTStringComponent2;
  23. Procedure TestTWideStringComponent;
  24. Procedure TestTWideStringComponent2;
  25. Procedure TestTSingleComponent;
  26. Procedure TestTDoubleComponent;
  27. Procedure TestTExtendedComponent;
  28. Procedure TestTCompComponent;
  29. Procedure TestTCurrencyComponent;
  30. Procedure TestTDateTimeComponent;
  31. Procedure TestTDateTimeComponent2;
  32. Procedure TestTDateTimeComponent3;
  33. Procedure TestTEnumComponent;
  34. Procedure TestTEnumComponent2;
  35. Procedure TestTEnumComponent3;
  36. Procedure TestTEnumComponent4;
  37. Procedure TestTEnumComponent5;
  38. Procedure TestTSetComponent;
  39. Procedure TestTSetComponent2;
  40. Procedure TestTSetComponent3;
  41. Procedure TestTSetComponent4;
  42. Procedure TestTMultipleComponent;
  43. Procedure TestTPersistentComponent;
  44. Procedure TestTCollectionComponent;
  45. Procedure TestTCollectionComponent2;
  46. Procedure TestTCollectionComponent3;
  47. Procedure TestTCollectionComponent4;
  48. Procedure TestTCollectionComponent5;
  49. Procedure TestTOwnedComponent;
  50. Procedure TestTStreamedOwnedComponent;
  51. Procedure TestTStreamedOwnedComponents;
  52. Procedure TestTMethodComponent;
  53. Procedure TestTMethodComponent2;
  54. end;
  55. { TMyItem }
  56. TMyItem = Class(TCollectionItem)
  57. private
  58. FNR: Integer;
  59. FStr: String;
  60. Public
  61. Procedure Assign(Source : TPersistent); override;
  62. Published
  63. Property Nr : Integer Read FNR Write FNR;
  64. Property Str: String Read FStr Write FStr;
  65. end;
  66. { TMyColl }
  67. TMyColl = Class(TCollection)
  68. private
  69. function GetIt(index : Integer): TMyItem;
  70. procedure SetIt(index : Integer; const AValue: TMyItem);
  71. Public
  72. Property It[index : Integer] : TMyItem Read GetIt Write SetIt; default;
  73. end;
  74. { TCollComp }
  75. TCollComp = Class(TComponent)
  76. private
  77. FMyColl: TMyColl;
  78. procedure SetMyColl(const AValue: TMyColl);
  79. Public
  80. Constructor Create(AOwner : TComponent); override;
  81. Destructor Destroy; override;
  82. Function ToStream : TStream;
  83. Procedure FromStream(AStream : TStream);
  84. Published
  85. Property MyColl : TMyColl Read FMyColl Write SetMyColl;
  86. end;
  87. { TTestCollectionStream }
  88. TTestCollectionStream = Class(TTestCase)
  89. private
  90. procedure CompareColl(CA, CB: TMyColl);
  91. function CreateColl(Anr: Integer): TCollComp;
  92. function EmptyComp: TCollComp;
  93. procedure TestNr(ACount: Integer);
  94. Published
  95. procedure Test1;
  96. procedure Test2;
  97. procedure Test3;
  98. procedure TestClear;
  99. procedure TestEmpty;
  100. end;
  101. Implementation
  102. uses testcomps;
  103. Procedure TTestComponentStream.TestTEmptyComponent;
  104. Var
  105. C : TComponent;
  106. begin
  107. C:=TEmptyComponent.Create(Nil);
  108. Try
  109. SaveToStream(C);
  110. ExpectSignature;
  111. ExpectFlags([],0);
  112. ExpectBareString('TEmptyComponent');
  113. ExpectBareString('TestTEmptyComponent');
  114. ExpectEndOfList;
  115. ExpectEndOfList;
  116. Finally
  117. C.Free;
  118. end;
  119. end;
  120. Procedure TTestComponentStream.TestTIntegerComponent;
  121. Var
  122. C : TComponent;
  123. begin
  124. C:=TIntegerComponent.Create(Nil);
  125. Try
  126. SaveToStream(C);
  127. ExpectSignature;
  128. ExpectFlags([],0);
  129. ExpectBareString('TIntegerComponent');
  130. ExpectBareString('TestTIntegerComponent');
  131. ExpectBareString('IntProp');
  132. ExpectInteger(3);
  133. ExpectEndOfList;
  134. ExpectEndOfList;
  135. Finally
  136. C.Free;
  137. end;
  138. end;
  139. Procedure TTestComponentStream.TestTIntegerComponent2;
  140. Var
  141. C : TComponent;
  142. begin
  143. C:=TIntegerComponent2.Create(Nil);
  144. Try
  145. SaveToStream(C);
  146. ExpectSignature;
  147. ExpectFlags([],0);
  148. ExpectBareString('TIntegerComponent2');
  149. ExpectBareString('TestTIntegerComponent2');
  150. ExpectBareString('IntProp');
  151. ExpectInteger(1024);
  152. ExpectEndOfList;
  153. ExpectEndOfList;
  154. Finally
  155. C.Free;
  156. end;
  157. end;
  158. Procedure TTestComponentStream.TestTIntegerComponent3;
  159. Var
  160. C : TComponent;
  161. begin
  162. C:=TIntegerComponent3.Create(Nil);
  163. Try
  164. SaveToStream(C);
  165. ExpectSignature;
  166. ExpectFlags([],0);
  167. ExpectBareString('TIntegerComponent3');
  168. ExpectBareString('TestTIntegerComponent3');
  169. ExpectBareString('IntProp');
  170. ExpectInteger(262144);
  171. ExpectEndOfList;
  172. ExpectEndOfList;
  173. Finally
  174. C.Free;
  175. end;
  176. end;
  177. Procedure TTestComponentStream.TestTIntegerComponent4;
  178. Var
  179. C : TComponent;
  180. begin
  181. C:=TIntegerComponent4.Create(Nil);
  182. Try
  183. SaveToStream(C);
  184. ExpectSignature;
  185. ExpectFlags([],0);
  186. ExpectBareString('TIntegerComponent4');
  187. ExpectBareString('TestTIntegerComponent4');
  188. ExpectEndOfList;
  189. ExpectEndOfList;
  190. Finally
  191. C.Free;
  192. end;
  193. end;
  194. Procedure TTestComponentStream.TestTIntegerComponent5;
  195. Var
  196. C : TComponent;
  197. begin
  198. C:=TIntegerComponent5.Create(Nil);
  199. Try
  200. SaveToStream(C);
  201. ExpectSignature;
  202. ExpectFlags([],0);
  203. ExpectBareString('TIntegerComponent5');
  204. ExpectBareString('TestTIntegerComponent5');
  205. ExpectBareString('IntProp');
  206. ExpectInteger(5);
  207. ExpectEndOfList;
  208. ExpectEndOfList;
  209. Finally
  210. C.Free;
  211. end;
  212. end;
  213. Procedure TTestComponentStream.TestTInt64Component;
  214. Var
  215. C : TComponent;
  216. begin
  217. C:=TInt64Component.Create(Nil);
  218. Try
  219. SaveToStream(C);
  220. ExpectSignature;
  221. ExpectFlags([],0);
  222. ExpectBareString('TInt64Component');
  223. ExpectBareString('TestTInt64Component');
  224. ExpectBareString('Int64Prop');
  225. ExpectInteger(4);
  226. ExpectEndOfList;
  227. ExpectEndOfList;
  228. Finally
  229. C.Free;
  230. end;
  231. end;
  232. Procedure TTestComponentStream.TestTInt64Component2;
  233. Var
  234. C : TComponent;
  235. begin
  236. C:=TInt64Component2.Create(Nil);
  237. Try
  238. SaveToStream(C);
  239. ExpectSignature;
  240. ExpectFlags([],0);
  241. ExpectBareString('TInt64Component2');
  242. ExpectBareString('TestTInt64Component2');
  243. ExpectBareString('Int64Prop');
  244. ExpectInteger(1024);
  245. ExpectEndOfList;
  246. ExpectEndOfList;
  247. Finally
  248. C.Free;
  249. end;
  250. end;
  251. Procedure TTestComponentStream.TestTInt64Component3;
  252. Var
  253. C : TComponent;
  254. begin
  255. C:=TInt64Component3.Create(Nil);
  256. Try
  257. SaveToStream(C);
  258. ExpectSignature;
  259. ExpectFlags([],0);
  260. ExpectBareString('TInt64Component3');
  261. ExpectBareString('TestTInt64Component3');
  262. ExpectBareString('Int64Prop');
  263. ExpectInteger(262144);
  264. ExpectEndOfList;
  265. ExpectEndOfList;
  266. Finally
  267. C.Free;
  268. end;
  269. end;
  270. Procedure TTestComponentStream.TestTInt64Component4;
  271. Var
  272. C : TComponent;
  273. begin
  274. C:=TInt64Component4.Create(Nil);
  275. Try
  276. SaveToStream(C);
  277. ExpectSignature;
  278. ExpectFlags([],0);
  279. ExpectBareString('TInt64Component4');
  280. ExpectBareString('TestTInt64Component4');
  281. ExpectBareString('Int64Prop');
  282. ExpectInt64(2147745791);
  283. ExpectEndOfList;
  284. ExpectEndOfList;
  285. Finally
  286. C.Free;
  287. end;
  288. end;
  289. Procedure TTestComponentStream.TestTInt64Component5;
  290. Var
  291. C : TComponent;
  292. begin
  293. C:=TInt64Component5.Create(Nil);
  294. Try
  295. SaveToStream(C);
  296. ExpectSignature;
  297. ExpectFlags([],0);
  298. ExpectBareString('TInt64Component5');
  299. ExpectBareString('TestTInt64Component5');
  300. ExpectBareString('Int64Prop');
  301. ExpectInteger(7);
  302. ExpectEndOfList;
  303. ExpectEndOfList;
  304. Finally
  305. C.Free;
  306. end;
  307. end;
  308. Procedure TTestComponentStream.TestTInt64Component6;
  309. Var
  310. C : TComponent;
  311. begin
  312. C:=TInt64Component6.Create(Nil);
  313. Try
  314. SaveToStream(C);
  315. ExpectSignature;
  316. ExpectFlags([],0);
  317. ExpectBareString('TInt64Component6');
  318. ExpectBareString('TestTInt64Component6');
  319. ExpectBareString('Int64Prop');
  320. ExpectInteger(8);
  321. ExpectEndOfList;
  322. ExpectEndOfList;
  323. Finally
  324. C.Free;
  325. end;
  326. end;
  327. Procedure TTestComponentStream.TestTStringComponent;
  328. Var
  329. C : TComponent;
  330. begin
  331. C:=TStringComponent.Create(Nil);
  332. Try
  333. SaveToStream(C);
  334. ExpectSignature;
  335. ExpectFlags([],0);
  336. ExpectBareString('TStringComponent');
  337. ExpectBareString('TestTStringComponent');
  338. ExpectBareString('StringProp');
  339. ExpectString('A string');
  340. ExpectEndOfList;
  341. ExpectEndOfList;
  342. Finally
  343. C.Free;
  344. end;
  345. end;
  346. Procedure TTestComponentStream.TestTStringComponent2;
  347. Var
  348. C : TComponent;
  349. begin
  350. C:=TStringComponent2.Create(Nil);
  351. Try
  352. SaveToStream(C);
  353. ExpectSignature;
  354. ExpectFlags([],0);
  355. ExpectBareString('TStringComponent2');
  356. ExpectBareString('TestTStringComponent2');
  357. ExpectEndOfList;
  358. ExpectEndOfList;
  359. Finally
  360. C.Free;
  361. end;
  362. end;
  363. Procedure TTestComponentStream.TestTWideStringComponent;
  364. Var
  365. C : TComponent;
  366. begin
  367. C:=TWideStringComponent.Create(Nil);
  368. Try
  369. SaveToStream(C);
  370. ExpectSignature;
  371. ExpectFlags([],0);
  372. ExpectBareString('TWideStringComponent');
  373. ExpectBareString('TestTWideStringComponent');
  374. ExpectBareString('WideStringProp');
  375. ExpectString('Some WideString');
  376. ExpectEndOfList;
  377. ExpectEndOfList;
  378. Finally
  379. C.Free;
  380. end;
  381. end;
  382. Procedure TTestComponentStream.TestTWideStringComponent2;
  383. Var
  384. C : TComponent;
  385. begin
  386. C:=TWideStringComponent2.Create(Nil);
  387. Try
  388. SaveToStream(C);
  389. ExpectSignature;
  390. ExpectFlags([],0);
  391. ExpectBareString('TWideStringComponent2');
  392. ExpectBareString('TestTWideStringComponent2');
  393. ExpectEndOfList;
  394. ExpectEndOfList;
  395. Finally
  396. C.Free;
  397. end;
  398. end;
  399. Procedure TTestComponentStream.TestTSingleComponent;
  400. Var
  401. C : TComponent;
  402. begin
  403. C:=TSingleComponent.Create(Nil);
  404. Try
  405. SaveToStream(C);
  406. ExpectSignature;
  407. ExpectFlags([],0);
  408. ExpectBareString('TSingleComponent');
  409. ExpectBareString('TestTSingleComponent');
  410. ExpectBareString('SingleProp');
  411. ExpectExtended(1.23);
  412. ExpectEndOfList;
  413. ExpectEndOfList;
  414. Finally
  415. C.Free;
  416. end;
  417. end;
  418. Procedure TTestComponentStream.TestTDoubleComponent;
  419. Var
  420. C : TComponent;
  421. begin
  422. C:=TDoubleComponent.Create(Nil);
  423. Try
  424. SaveToStream(C);
  425. ExpectSignature;
  426. ExpectFlags([],0);
  427. ExpectBareString('TDoubleComponent');
  428. ExpectBareString('TestTDoubleComponent');
  429. ExpectBareString('DoubleProp');
  430. ExpectExtended(2.34);
  431. ExpectEndOfList;
  432. ExpectEndOfList;
  433. Finally
  434. C.Free;
  435. end;
  436. end;
  437. Procedure TTestComponentStream.TestTExtendedComponent;
  438. Var
  439. C : TComponent;
  440. begin
  441. C:=TExtendedComponent.Create(Nil);
  442. Try
  443. SaveToStream(C);
  444. ExpectSignature;
  445. ExpectFlags([],0);
  446. ExpectBareString('TExtendedComponent');
  447. ExpectBareString('TestTExtendedComponent');
  448. ExpectBareString('ExtendedProp');
  449. ExpectExtended(3.45);
  450. ExpectEndOfList;
  451. ExpectEndOfList;
  452. Finally
  453. C.Free;
  454. end;
  455. end;
  456. Procedure TTestComponentStream.TestTCompComponent;
  457. Var
  458. C : TComponent;
  459. begin
  460. C:=TCompComponent.Create(Nil);
  461. Try
  462. SaveToStream(C);
  463. ExpectSignature;
  464. ExpectFlags([],0);
  465. ExpectBareString('TCompComponent');
  466. ExpectBareString('TestTCompComponent');
  467. ExpectBareString('ExtendedProp');
  468. ExpectExtended(5.00);
  469. ExpectEndOfList;
  470. ExpectEndOfList;
  471. Finally
  472. C.Free;
  473. end;
  474. end;
  475. Procedure TTestComponentStream.TestTCurrencyComponent;
  476. Var
  477. C : TComponent;
  478. begin
  479. C:=TCurrencyComponent.Create(Nil);
  480. Try
  481. SaveToStream(C);
  482. ExpectSignature;
  483. ExpectFlags([],0);
  484. ExpectBareString('TCurrencyComponent');
  485. ExpectBareString('TestTCurrencyComponent');
  486. ExpectBareString('CurrencyProp');
  487. ExpectExtended(5.67);
  488. ExpectEndOfList;
  489. ExpectEndOfList;
  490. Finally
  491. C.Free;
  492. end;
  493. end;
  494. Procedure TTestComponentStream.TestTDateTimeComponent;
  495. Var
  496. C : TComponent;
  497. begin
  498. C:=TDateTimeComponent.Create(Nil);
  499. Try
  500. SaveToStream(C);
  501. ExpectSignature;
  502. ExpectFlags([],0);
  503. ExpectBareString('TDateTimeComponent');
  504. ExpectBareString('TestTDateTimeComponent');
  505. ExpectBareString('DateTimeProp');
  506. ExpectExtended(35278.00);
  507. ExpectEndOfList;
  508. ExpectEndOfList;
  509. Finally
  510. C.Free;
  511. end;
  512. end;
  513. Procedure TTestComponentStream.TestTDateTimeComponent2;
  514. Var
  515. C : TComponent;
  516. begin
  517. C:=TDateTimeComponent2.Create(Nil);
  518. Try
  519. SaveToStream(C);
  520. ExpectSignature;
  521. ExpectFlags([],0);
  522. ExpectBareString('TDateTimeComponent2');
  523. ExpectBareString('TestTDateTimeComponent2');
  524. ExpectBareString('DateTimeProp');
  525. ExpectExtended(0.97);
  526. ExpectEndOfList;
  527. ExpectEndOfList;
  528. Finally
  529. C.Free;
  530. end;
  531. end;
  532. Procedure TTestComponentStream.TestTDateTimeComponent3;
  533. Var
  534. C : TComponent;
  535. begin
  536. C:=TDateTimeComponent3.Create(Nil);
  537. Try
  538. SaveToStream(C);
  539. ExpectSignature;
  540. ExpectFlags([],0);
  541. ExpectBareString('TDateTimeComponent3');
  542. ExpectBareString('TestTDateTimeComponent3');
  543. ExpectBareString('DateTimeProp');
  544. ExpectExtended(35278.97);
  545. ExpectEndOfList;
  546. ExpectEndOfList;
  547. Finally
  548. C.Free;
  549. end;
  550. end;
  551. Procedure TTestComponentStream.TestTEnumComponent;
  552. Var
  553. C : TComponent;
  554. begin
  555. C:=TEnumComponent.Create(Nil);
  556. Try
  557. SaveToStream(C);
  558. ExpectSignature;
  559. ExpectFlags([],0);
  560. ExpectBareString('TEnumComponent');
  561. ExpectBareString('TestTEnumComponent');
  562. ExpectBareString('Dice');
  563. ExpectIdent('four');
  564. ExpectEndOfList;
  565. ExpectEndOfList;
  566. Finally
  567. C.Free;
  568. end;
  569. end;
  570. Procedure TTestComponentStream.TestTEnumComponent2;
  571. Var
  572. C : TComponent;
  573. begin
  574. C:=TEnumComponent2.Create(Nil);
  575. Try
  576. SaveToStream(C);
  577. ExpectSignature;
  578. ExpectFlags([],0);
  579. ExpectBareString('TEnumComponent2');
  580. ExpectBareString('TestTEnumComponent2');
  581. {$ifndef FPC}
  582. // FPC does not stream an undeclared default value, it assumes the
  583. // 0-the value is the default.
  584. ExpectBareString('Dice');
  585. ExpectIdent('one');
  586. {$endif FPC}
  587. ExpectEndOfList;
  588. ExpectEndOfList;
  589. Finally
  590. C.Free;
  591. end;
  592. end;
  593. Procedure TTestComponentStream.TestTEnumComponent3;
  594. Var
  595. C : TComponent;
  596. begin
  597. C:=TEnumComponent3.Create(Nil);
  598. Try
  599. SaveToStream(C);
  600. ExpectSignature;
  601. ExpectFlags([],0);
  602. ExpectBareString('TEnumComponent3');
  603. ExpectBareString('TestTEnumComponent3');
  604. ExpectBareString('Dice');
  605. ExpectIdent('three');
  606. ExpectEndOfList;
  607. ExpectEndOfList;
  608. Finally
  609. C.Free;
  610. end;
  611. end;
  612. Procedure TTestComponentStream.TestTEnumComponent4;
  613. Var
  614. C : TComponent;
  615. begin
  616. C:=TEnumComponent4.Create(Nil);
  617. Try
  618. SaveToStream(C);
  619. ExpectSignature;
  620. ExpectFlags([],0);
  621. ExpectBareString('TEnumComponent4');
  622. ExpectBareString('TestTEnumComponent4');
  623. ExpectEndOfList;
  624. ExpectEndOfList;
  625. Finally
  626. C.Free;
  627. end;
  628. end;
  629. Procedure TTestComponentStream.TestTEnumComponent5;
  630. Var
  631. C : TComponent;
  632. begin
  633. C:=TEnumComponent5.Create(Nil);
  634. Try
  635. SaveToStream(C);
  636. ExpectSignature;
  637. ExpectFlags([],0);
  638. ExpectBareString('TEnumComponent5');
  639. ExpectBareString('TestTEnumComponent5');
  640. ExpectEndOfList;
  641. ExpectEndOfList;
  642. Finally
  643. C.Free;
  644. end;
  645. end;
  646. Procedure TTestComponentStream.TestTSetComponent;
  647. Var
  648. C : TComponent;
  649. begin
  650. C:=TSetComponent.Create(Nil);
  651. Try
  652. SaveToStream(C);
  653. ExpectSignature;
  654. ExpectFlags([],0);
  655. ExpectBareString('TSetComponent');
  656. ExpectBareString('TestTSetComponent');
  657. ExpectBareString('Throw');
  658. ExpectValue(vaSet);
  659. ExpectBareString('two');
  660. ExpectBareString('five');
  661. ExpectBareString('');
  662. ExpectEndOfList;
  663. ExpectEndOfList;
  664. Finally
  665. C.Free;
  666. end;
  667. end;
  668. Procedure TTestComponentStream.TestTSetComponent2;
  669. Var
  670. C : TComponent;
  671. begin
  672. C:=TSetComponent2.Create(Nil);
  673. Try
  674. SaveToStream(C);
  675. ExpectSignature;
  676. ExpectFlags([],0);
  677. ExpectBareString('TSetComponent2');
  678. ExpectBareString('TestTSetComponent2');
  679. {$ifndef FPC}
  680. // Same as for sets: a set with undeclared default is regarded as
  681. // A set with default [], and is not streamed if it is empty.
  682. ExpectBareString('Throw');
  683. ExpectValue(vaSet);
  684. ExpectBareString('');
  685. {$endif FPC}
  686. ExpectEndOfList;
  687. ExpectEndOfList;
  688. Finally
  689. C.Free;
  690. end;
  691. end;
  692. Procedure TTestComponentStream.TestTSetComponent3;
  693. Var
  694. C : TComponent;
  695. begin
  696. C:=TSetComponent3.Create(Nil);
  697. Try
  698. SaveToStream(C);
  699. ExpectSignature;
  700. ExpectFlags([],0);
  701. ExpectBareString('TSetComponent3');
  702. ExpectBareString('TestTSetComponent3');
  703. ExpectBareString('Throw');
  704. ExpectValue(vaSet);
  705. ExpectBareString('one');
  706. ExpectBareString('four');
  707. ExpectBareString('');
  708. ExpectEndOfList;
  709. ExpectEndOfList;
  710. Finally
  711. C.Free;
  712. end;
  713. end;
  714. Procedure TTestComponentStream.TestTSetComponent4;
  715. Var
  716. C : TComponent;
  717. begin
  718. C:=TSetComponent4.Create(Nil);
  719. Try
  720. SaveToStream(C);
  721. ExpectSignature;
  722. ExpectFlags([],0);
  723. ExpectBareString('TSetComponent4');
  724. ExpectBareString('TestTSetComponent4');
  725. ExpectEndOfList;
  726. ExpectEndOfList;
  727. Finally
  728. C.Free;
  729. end;
  730. end;
  731. Procedure TTestComponentStream.TestTMultipleComponent;
  732. Var
  733. C : TComponent;
  734. begin
  735. C:=TMultipleComponent.Create(Nil);
  736. Try
  737. SaveToStream(C);
  738. ExpectSignature;
  739. ExpectFlags([],0);
  740. ExpectBareString('TMultipleComponent');
  741. ExpectBareString('TestTMultipleComponent');
  742. ExpectBareString('IntProp');
  743. ExpectInteger(1);
  744. ExpectBareString('StringProp');
  745. ExpectString('A String');
  746. ExpectBareString('CurrencyProp');
  747. ExpectExtended(2.30);
  748. ExpectBareString('Dice');
  749. ExpectIdent('two');
  750. ExpectBareString('Throw');
  751. ExpectValue(vaSet);
  752. ExpectBareString('three');
  753. ExpectBareString('four');
  754. ExpectBareString('');
  755. ExpectEndOfList;
  756. ExpectEndOfList;
  757. Finally
  758. C.Free;
  759. end;
  760. end;
  761. Procedure TTestComponentStream.TestTPersistentComponent;
  762. Var
  763. C : TComponent;
  764. begin
  765. C:=TPersistentComponent.Create(Nil);
  766. Try
  767. SaveToStream(C);
  768. ExpectSignature;
  769. ExpectFlags([],0);
  770. ExpectBareString('TPersistentComponent');
  771. ExpectBareString('TestTPersistentComponent');
  772. ExpectBareString('Persist.AInteger');
  773. ExpectInteger(3);
  774. ExpectBareString('Persist.AString');
  775. ExpectString('A persistent string');
  776. ExpectEndOfList;
  777. ExpectEndOfList;
  778. Finally
  779. C.Free;
  780. end;
  781. end;
  782. Procedure TTestComponentStream.TestTCollectionComponent;
  783. Var
  784. C : TComponent;
  785. begin
  786. C:=TCollectionComponent.Create(Nil);
  787. Try
  788. SaveToStream(C);
  789. ExpectSignature;
  790. ExpectFlags([],0);
  791. ExpectBareString('TCollectionComponent');
  792. ExpectBareString('TestTCollectionComponent');
  793. ExpectBareString('Coll');
  794. ExpectValue(vaCollection);
  795. ExpectEndOfList;
  796. ExpectEndOfList;
  797. ExpectEndOfList;
  798. Finally
  799. C.Free;
  800. end;
  801. end;
  802. Procedure TTestComponentStream.TestTCollectionComponent2;
  803. Var
  804. C : TComponent;
  805. begin
  806. C:=TCollectionComponent2.Create(Nil);
  807. Try
  808. SaveToStream(C);
  809. ExpectSignature;
  810. ExpectFlags([],0);
  811. ExpectBareString('TCollectionComponent2');
  812. ExpectBareString('TestTCollectionComponent2');
  813. ExpectBareString('Coll');
  814. ExpectValue(vaCollection);
  815. ExpectValue(vaList);
  816. ExpectBareString('StrProp');
  817. ExpectString('First');
  818. ExpectEndOfList;
  819. ExpectValue(vaList);
  820. ExpectBareString('StrProp');
  821. ExpectString('Second');
  822. ExpectEndOfList;
  823. ExpectValue(vaList);
  824. ExpectBareString('StrProp');
  825. ExpectString('Third');
  826. ExpectEndOfList;
  827. ExpectEndOfList;
  828. ExpectEndOfList;
  829. ExpectEndOfList;
  830. Finally
  831. C.Free;
  832. end;
  833. end;
  834. Procedure TTestComponentStream.TestTCollectionComponent3;
  835. Var
  836. C : TComponent;
  837. begin
  838. C:=TCollectionComponent3.Create(Nil);
  839. Try
  840. SaveToStream(C);
  841. ExpectSignature;
  842. ExpectFlags([],0);
  843. ExpectBareString('TCollectionComponent3');
  844. ExpectBareString('TestTCollectionComponent3');
  845. ExpectBareString('Coll');
  846. ExpectValue(vaCollection);
  847. ExpectValue(vaList);
  848. ExpectBareString('StrProp');
  849. ExpectString('First');
  850. ExpectEndOfList;
  851. ExpectValue(vaList);
  852. ExpectEndOfList;
  853. ExpectValue(vaList);
  854. ExpectBareString('StrProp');
  855. ExpectString('Third');
  856. ExpectEndOfList;
  857. ExpectEndOfList;
  858. ExpectEndOfList;
  859. ExpectEndOfList;
  860. Finally
  861. C.Free;
  862. end;
  863. end;
  864. Procedure TTestComponentStream.TestTCollectionComponent4;
  865. Var
  866. C : TComponent;
  867. begin
  868. C:=TCollectionComponent4.Create(Nil);
  869. Try
  870. SaveToStream(C);
  871. ExpectSignature;
  872. ExpectFlags([],0);
  873. ExpectBareString('TCollectionComponent4');
  874. ExpectBareString('TestTCollectionComponent4');
  875. ExpectBareString('Coll');
  876. ExpectValue(vaCollection);
  877. ExpectValue(vaList);
  878. ExpectBareString('StrProp');
  879. ExpectString('Something');
  880. ExpectEndOfList;
  881. ExpectEndOfList;
  882. ExpectEndOfList;
  883. ExpectEndOfList;
  884. Finally
  885. C.Free;
  886. end;
  887. end;
  888. Procedure TTestComponentStream.TestTCollectionComponent5;
  889. Var
  890. C : TComponent;
  891. begin
  892. C:=TCollectionComponent5.Create(Nil);
  893. Try
  894. SaveToStream(C);
  895. ExpectSignature;
  896. ExpectFlags([],0);
  897. ExpectBareString('TCollectionComponent5');
  898. ExpectBareString('TestTCollectionComponent5');
  899. ExpectBareString('Coll');
  900. ExpectValue(vaCollection);
  901. ExpectValue(vaList);
  902. ExpectBareString('StrProp1');
  903. ExpectString('Something');
  904. ExpectBareString('StrProp2');
  905. ExpectString('Otherthing');
  906. ExpectEndOfList;
  907. ExpectValue(vaList);
  908. ExpectBareString('StrProp1');
  909. ExpectString('Something 2');
  910. ExpectBareString('StrProp2');
  911. ExpectString('Otherthing 2');
  912. ExpectEndOfList;
  913. ExpectEndOfList;
  914. ExpectEndOfList;
  915. Finally
  916. C.Free;
  917. end;
  918. end;
  919. Procedure TTestComponentStream.TestTOwnedComponent;
  920. Var
  921. C : TComponent;
  922. begin
  923. C:=TOwnedComponent.Create(Nil);
  924. Try
  925. SaveToStream(C);
  926. ExpectSignature;
  927. ExpectFlags([],0);
  928. ExpectBareString('TOwnedComponent');
  929. ExpectBareString('TestTOwnedComponent');
  930. ExpectBareString('CompProp');
  931. ExpectIdent('SubComponent');
  932. ExpectEndOfList;
  933. ExpectEndOfList;
  934. Finally
  935. C.Free;
  936. end;
  937. end;
  938. Procedure TTestComponentStream.TestTStreamedOwnedComponent;
  939. Var
  940. C : TComponent;
  941. begin
  942. C:=TStreamedOwnedComponent.Create(Nil);
  943. Try
  944. SaveToStream(C);
  945. ExpectSignature;
  946. ExpectFlags([],0);
  947. ExpectBareString('TStreamedOwnedComponent');
  948. ExpectBareString('TestTStreamedOwnedComponent');
  949. ExpectEndOfList;
  950. ExpectFlags([],0);
  951. ExpectBareString('TIntegerComponent');
  952. ExpectBareString('Sub');
  953. ExpectBareString('IntProp');
  954. ExpectInteger(3);
  955. ExpectEndOfList;
  956. ExpectEndOfList;
  957. ExpectEndOfList;
  958. ExpectEndOfStream;
  959. Finally
  960. C.Free;
  961. end;
  962. end;
  963. Procedure TTestComponentStream.TestTStreamedOwnedComponents;
  964. Var
  965. C : TComponent;
  966. begin
  967. C:=TStreamedOwnedComponents.Create(Nil);
  968. Try
  969. SaveToStream(C);
  970. ExpectSignature;
  971. ExpectFlags([],0);
  972. ExpectBareString('TStreamedOwnedComponents');
  973. ExpectBareString('TestTStreamedOwnedComponents');
  974. ExpectEndOfList;
  975. ExpectFlags([],0);
  976. ExpectBareString('TIntegerComponent');
  977. ExpectBareString('SubA');
  978. ExpectBareString('IntProp');
  979. ExpectInteger(3);
  980. ExpectEndOfList;
  981. ExpectEndOfList;
  982. ExpectFlags([],0);
  983. ExpectBareString('TStringComponent');
  984. ExpectBareString('SubB');
  985. ExpectBareString('StringProp');
  986. ExpectString('A string');
  987. ExpectEndOfList;
  988. ExpectEndOfList;
  989. ExpectEndOfList;
  990. ExpectEndOfStream;
  991. Finally
  992. C.Free;
  993. end;
  994. end;
  995. Procedure TTestComponentStream.TestTMethodComponent;
  996. Var
  997. C : TComponent;
  998. begin
  999. C:=TMethodComponent.Create(Nil);
  1000. Try
  1001. SaveToStream(C);
  1002. ExpectSignature;
  1003. ExpectFlags([],0);
  1004. ExpectBareString('TMethodComponent');
  1005. ExpectBareString('TestTMethodComponent');
  1006. ExpectBareString('MethodProp');
  1007. ExpectIdent('MyMethod');
  1008. ExpectEndOfList;
  1009. ExpectEndOfList;
  1010. Finally
  1011. C.Free;
  1012. end;
  1013. end;
  1014. Procedure TTestComponentStream.TestTMethodComponent2;
  1015. Var
  1016. C : TComponent;
  1017. begin
  1018. C:=TMethodComponent2.Create(Nil);
  1019. Try
  1020. SaveToStream(C);
  1021. ExpectSignature;
  1022. ExpectFlags([],0);
  1023. ExpectBareString('TMethodComponent2');
  1024. ExpectBareString('TestTMethodComponent2');
  1025. ExpectEndOfList;
  1026. ExpectFlags([],0);
  1027. ExpectBareString('TMethodComponent');
  1028. ExpectBareString('AComponent');
  1029. ExpectBareString('MethodProp');
  1030. ExpectIdent('MyMethod2');
  1031. ExpectEndOfList;
  1032. ExpectEndOfList;
  1033. ExpectEndOfList;
  1034. Finally
  1035. C.Free;
  1036. end;
  1037. end;
  1038. { TMyColl }
  1039. function TMyColl.GetIt(index : Integer): TMyItem;
  1040. begin
  1041. Result:=Items[Index] as TMyItem;
  1042. end;
  1043. procedure TMyColl.SetIt(index : Integer; const AValue: TMyItem);
  1044. begin
  1045. Items[Index]:=AValue;
  1046. end;
  1047. { TCollComp }
  1048. procedure TCollComp.SetMyColl(const AValue: TMyColl);
  1049. begin
  1050. if (FMyColl=AValue) then
  1051. exit;
  1052. FMyColl.Assign(AValue);
  1053. end;
  1054. constructor TCollComp.Create(AOwner: TComponent);
  1055. begin
  1056. inherited Create(AOwner);
  1057. FMyColl:=TMyCOll.Create(TMyItem);
  1058. end;
  1059. destructor TCollComp.Destroy;
  1060. begin
  1061. FreeAndNil(FMyColl);
  1062. inherited Destroy;
  1063. end;
  1064. function TCollComp.ToStream: TStream;
  1065. begin
  1066. Result:=TMemoryStream.Create;
  1067. Result.WriteComponent(Self);
  1068. Result.Position:=0;
  1069. end;
  1070. procedure TCollComp.FromStream(AStream: TStream);
  1071. begin
  1072. AStream.ReadComponent(Self);
  1073. Astream.Free;
  1074. end;
  1075. procedure TMyItem.Assign(Source: TPersistent);
  1076. Var
  1077. I : TMyItem;
  1078. begin
  1079. If (Source is TMyItem) then
  1080. begin
  1081. I:=Source as TMyItem;
  1082. FNR:=I.NR;
  1083. FStr:=I.Str;
  1084. end
  1085. else
  1086. inherited Assign(Source);
  1087. end;
  1088. Procedure TTestCollectionStream.CompareColl(CA,CB : TMyColl);
  1089. Var
  1090. I : Integer;
  1091. begin
  1092. AssertEquals('Counts differ: %d %d',CA.Count,CB.Count);
  1093. For I:=0 to CA.Count-1 do
  1094. begin
  1095. AssertEquals(Format('Nr property of element %d equals',[I]),CA[i].Nr,CB[i].Nr);
  1096. AssertEquals(Format('Str property of element %d equals',[I]),CA[i].Str,CB[i].Str);
  1097. end;
  1098. end;
  1099. Function TTestCollectionStream.EmptyComp : TCollComp;
  1100. begin
  1101. Result:=TCollComp.Create(Nil);
  1102. end;
  1103. Function TTestCollectionStream.CreateColl(Anr : Integer) : TCollComp;
  1104. Var
  1105. I : Integer;
  1106. T : TMyItem;
  1107. begin
  1108. Result:=EmptyComp;
  1109. Result.Name:='C'+IntToStr(Anr);
  1110. For I:=0 to ANr-1 do
  1111. begin
  1112. T:=Result.MyColl.Add as TMyItem;
  1113. T.Nr:=I; // not I+1, so the default value gets tested too
  1114. T.Str:=IntToStr(I+1);
  1115. end;
  1116. end;
  1117. Procedure TTestCollectionStream.TestEmpty;
  1118. Var
  1119. CA,CB : TCollComp;
  1120. begin
  1121. CA:=CreateColl(0);
  1122. try
  1123. CB:=EmptyComp;
  1124. Try
  1125. CB.FromStream(CA.ToStream);
  1126. CompareColl(CA.MyColl,CB.MyColl);
  1127. Finally
  1128. CB.Free;
  1129. end;
  1130. Finally
  1131. CA.Free;
  1132. end;
  1133. end;
  1134. Procedure TTestCollectionStream.TestNr(ACount : Integer);
  1135. Var
  1136. CA,CB : TCollComp;
  1137. begin
  1138. CA:=CreateColl(ACount);
  1139. try
  1140. CB:=EmptyComp;
  1141. Try
  1142. CB.FromStream(CA.ToStream);
  1143. CompareColl(CA.MyColl,CB.MyColl);
  1144. Finally
  1145. CB.Free;
  1146. end;
  1147. Finally
  1148. CA.Free;
  1149. end;
  1150. end;
  1151. Procedure TTestCollectionStream.TestClear;
  1152. Var
  1153. CA,CB : TCollComp;
  1154. begin
  1155. CA:=CreateColl(3);
  1156. try
  1157. CB:=CreateColl(1);
  1158. CB.Name:='';
  1159. Try
  1160. // CB collection should be cleared before loading.
  1161. CB.FromStream(CA.ToStream);
  1162. CompareColl(CA.MyColl,CB.MyColl);
  1163. Finally
  1164. CB.Free;
  1165. end;
  1166. Finally
  1167. CA.Free;
  1168. end;
  1169. end;
  1170. Procedure TTestCollectionStream.Test1;
  1171. begin
  1172. TestNr(1);
  1173. end;
  1174. Procedure TTestCollectionStream.Test2;
  1175. begin
  1176. TestNr(2);
  1177. end;
  1178. Procedure TTestCollectionStream.Test3;
  1179. begin
  1180. TestNr(3);
  1181. end;
  1182. begin
  1183. RegisterTests([TTestComponentStream,TTestCollectionStream]);
  1184. end.