tccompstreaming.pp 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405
  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. ExpectBareString('Dice');
  582. ExpectIdent('one');
  583. ExpectEndOfList;
  584. ExpectEndOfList;
  585. Finally
  586. C.Free;
  587. end;
  588. end;
  589. Procedure TTestComponentStream.TestTEnumComponent3;
  590. Var
  591. C : TComponent;
  592. begin
  593. C:=TEnumComponent3.Create(Nil);
  594. Try
  595. SaveToStream(C);
  596. ExpectSignature;
  597. ExpectFlags([],0);
  598. ExpectBareString('TEnumComponent3');
  599. ExpectBareString('TestTEnumComponent3');
  600. ExpectBareString('Dice');
  601. ExpectIdent('three');
  602. ExpectEndOfList;
  603. ExpectEndOfList;
  604. Finally
  605. C.Free;
  606. end;
  607. end;
  608. Procedure TTestComponentStream.TestTEnumComponent4;
  609. Var
  610. C : TComponent;
  611. begin
  612. C:=TEnumComponent4.Create(Nil);
  613. Try
  614. SaveToStream(C);
  615. ExpectSignature;
  616. ExpectFlags([],0);
  617. ExpectBareString('TEnumComponent4');
  618. ExpectBareString('TestTEnumComponent4');
  619. ExpectEndOfList;
  620. ExpectEndOfList;
  621. Finally
  622. C.Free;
  623. end;
  624. end;
  625. Procedure TTestComponentStream.TestTEnumComponent5;
  626. Var
  627. C : TComponent;
  628. begin
  629. C:=TEnumComponent5.Create(Nil);
  630. Try
  631. SaveToStream(C);
  632. ExpectSignature;
  633. ExpectFlags([],0);
  634. ExpectBareString('TEnumComponent5');
  635. ExpectBareString('TestTEnumComponent5');
  636. ExpectEndOfList;
  637. ExpectEndOfList;
  638. Finally
  639. C.Free;
  640. end;
  641. end;
  642. Procedure TTestComponentStream.TestTSetComponent;
  643. Var
  644. C : TComponent;
  645. begin
  646. C:=TSetComponent.Create(Nil);
  647. Try
  648. SaveToStream(C);
  649. ExpectSignature;
  650. ExpectFlags([],0);
  651. ExpectBareString('TSetComponent');
  652. ExpectBareString('TestTSetComponent');
  653. ExpectBareString('Throw');
  654. ExpectValue(vaSet);
  655. ExpectBareString('two');
  656. ExpectBareString('five');
  657. ExpectBareString('');
  658. ExpectEndOfList;
  659. ExpectEndOfList;
  660. Finally
  661. C.Free;
  662. end;
  663. end;
  664. Procedure TTestComponentStream.TestTSetComponent2;
  665. Var
  666. C : TComponent;
  667. begin
  668. C:=TSetComponent2.Create(Nil);
  669. Try
  670. SaveToStream(C);
  671. ExpectSignature;
  672. ExpectFlags([],0);
  673. ExpectBareString('TSetComponent2');
  674. ExpectBareString('TestTSetComponent2');
  675. ExpectBareString('Throw');
  676. ExpectValue(vaSet);
  677. ExpectBareString('');
  678. ExpectEndOfList;
  679. ExpectEndOfList;
  680. Finally
  681. C.Free;
  682. end;
  683. end;
  684. Procedure TTestComponentStream.TestTSetComponent3;
  685. Var
  686. C : TComponent;
  687. begin
  688. C:=TSetComponent3.Create(Nil);
  689. Try
  690. SaveToStream(C);
  691. ExpectSignature;
  692. ExpectFlags([],0);
  693. ExpectBareString('TSetComponent3');
  694. ExpectBareString('TestTSetComponent3');
  695. ExpectBareString('Throw');
  696. ExpectValue(vaSet);
  697. ExpectBareString('one');
  698. ExpectBareString('four');
  699. ExpectBareString('');
  700. ExpectEndOfList;
  701. ExpectEndOfList;
  702. Finally
  703. C.Free;
  704. end;
  705. end;
  706. Procedure TTestComponentStream.TestTSetComponent4;
  707. Var
  708. C : TComponent;
  709. begin
  710. C:=TSetComponent4.Create(Nil);
  711. Try
  712. SaveToStream(C);
  713. ExpectSignature;
  714. ExpectFlags([],0);
  715. ExpectBareString('TSetComponent4');
  716. ExpectBareString('TestTSetComponent4');
  717. ExpectEndOfList;
  718. ExpectEndOfList;
  719. Finally
  720. C.Free;
  721. end;
  722. end;
  723. Procedure TTestComponentStream.TestTMultipleComponent;
  724. Var
  725. C : TComponent;
  726. begin
  727. C:=TMultipleComponent.Create(Nil);
  728. Try
  729. SaveToStream(C);
  730. ExpectSignature;
  731. ExpectFlags([],0);
  732. ExpectBareString('TMultipleComponent');
  733. ExpectBareString('TestTMultipleComponent');
  734. ExpectBareString('IntProp');
  735. ExpectInteger(1);
  736. ExpectBareString('StringProp');
  737. ExpectString('A String');
  738. ExpectBareString('CurrencyProp');
  739. ExpectExtended(2.30);
  740. ExpectBareString('Dice');
  741. ExpectIdent('two');
  742. ExpectBareString('Throw');
  743. ExpectValue(vaSet);
  744. ExpectBareString('three');
  745. ExpectBareString('four');
  746. ExpectBareString('');
  747. ExpectEndOfList;
  748. ExpectEndOfList;
  749. Finally
  750. C.Free;
  751. end;
  752. end;
  753. Procedure TTestComponentStream.TestTPersistentComponent;
  754. Var
  755. C : TComponent;
  756. begin
  757. C:=TPersistentComponent.Create(Nil);
  758. Try
  759. SaveToStream(C);
  760. ExpectSignature;
  761. ExpectFlags([],0);
  762. ExpectBareString('TPersistentComponent');
  763. ExpectBareString('TestTPersistentComponent');
  764. ExpectBareString('Persist.AInteger');
  765. ExpectInteger(3);
  766. ExpectBareString('Persist.AString');
  767. ExpectString('A persistent string');
  768. ExpectEndOfList;
  769. ExpectEndOfList;
  770. Finally
  771. C.Free;
  772. end;
  773. end;
  774. Procedure TTestComponentStream.TestTCollectionComponent;
  775. Var
  776. C : TComponent;
  777. begin
  778. C:=TCollectionComponent.Create(Nil);
  779. Try
  780. SaveToStream(C);
  781. ExpectSignature;
  782. ExpectFlags([],0);
  783. ExpectBareString('TCollectionComponent');
  784. ExpectBareString('TestTCollectionComponent');
  785. ExpectBareString('Coll');
  786. ExpectValue(vaCollection);
  787. ExpectEndOfList;
  788. ExpectEndOfList;
  789. ExpectEndOfList;
  790. Finally
  791. C.Free;
  792. end;
  793. end;
  794. Procedure TTestComponentStream.TestTCollectionComponent2;
  795. Var
  796. C : TComponent;
  797. begin
  798. C:=TCollectionComponent2.Create(Nil);
  799. Try
  800. SaveToStream(C);
  801. ExpectSignature;
  802. ExpectFlags([],0);
  803. ExpectBareString('TCollectionComponent2');
  804. ExpectBareString('TestTCollectionComponent2');
  805. ExpectBareString('Coll');
  806. ExpectValue(vaCollection);
  807. ExpectValue(vaList);
  808. ExpectBareString('StrProp');
  809. ExpectString('First');
  810. ExpectEndOfList;
  811. ExpectValue(vaList);
  812. ExpectBareString('StrProp');
  813. ExpectString('Second');
  814. ExpectEndOfList;
  815. ExpectValue(vaList);
  816. ExpectBareString('StrProp');
  817. ExpectString('Third');
  818. ExpectEndOfList;
  819. ExpectEndOfList;
  820. ExpectEndOfList;
  821. ExpectEndOfList;
  822. Finally
  823. C.Free;
  824. end;
  825. end;
  826. Procedure TTestComponentStream.TestTCollectionComponent3;
  827. Var
  828. C : TComponent;
  829. begin
  830. C:=TCollectionComponent3.Create(Nil);
  831. Try
  832. SaveToStream(C);
  833. ExpectSignature;
  834. ExpectFlags([],0);
  835. ExpectBareString('TCollectionComponent3');
  836. ExpectBareString('TestTCollectionComponent3');
  837. ExpectBareString('Coll');
  838. ExpectValue(vaCollection);
  839. ExpectValue(vaList);
  840. ExpectBareString('StrProp');
  841. ExpectString('First');
  842. ExpectEndOfList;
  843. ExpectValue(vaList);
  844. ExpectEndOfList;
  845. ExpectValue(vaList);
  846. ExpectBareString('StrProp');
  847. ExpectString('Third');
  848. ExpectEndOfList;
  849. ExpectEndOfList;
  850. ExpectEndOfList;
  851. ExpectEndOfList;
  852. Finally
  853. C.Free;
  854. end;
  855. end;
  856. Procedure TTestComponentStream.TestTCollectionComponent4;
  857. Var
  858. C : TComponent;
  859. begin
  860. C:=TCollectionComponent4.Create(Nil);
  861. Try
  862. SaveToStream(C);
  863. ExpectSignature;
  864. ExpectFlags([],0);
  865. ExpectBareString('TCollectionComponent4');
  866. ExpectBareString('TestTCollectionComponent4');
  867. ExpectBareString('Coll');
  868. ExpectValue(vaCollection);
  869. ExpectValue(vaList);
  870. ExpectBareString('StrProp');
  871. ExpectString('Something');
  872. ExpectEndOfList;
  873. ExpectEndOfList;
  874. ExpectEndOfList;
  875. ExpectEndOfList;
  876. Finally
  877. C.Free;
  878. end;
  879. end;
  880. Procedure TTestComponentStream.TestTCollectionComponent5;
  881. Var
  882. C : TComponent;
  883. begin
  884. C:=TCollectionComponent5.Create(Nil);
  885. Try
  886. SaveToStream(C);
  887. ExpectSignature;
  888. ExpectFlags([],0);
  889. ExpectBareString('TCollectionComponent5');
  890. ExpectBareString('TestTCollectionComponent5');
  891. ExpectBareString('Coll');
  892. ExpectValue(vaCollection);
  893. ExpectValue(vaList);
  894. ExpectBareString('StrProp1');
  895. ExpectString('Something');
  896. ExpectBareString('StrProp2');
  897. ExpectString('Otherthing');
  898. ExpectEndOfList;
  899. ExpectValue(vaList);
  900. ExpectBareString('StrProp1');
  901. ExpectString('Something 2');
  902. ExpectBareString('StrProp2');
  903. ExpectString('Otherthing 2');
  904. ExpectEndOfList;
  905. ExpectEndOfList;
  906. ExpectEndOfList;
  907. Finally
  908. C.Free;
  909. end;
  910. end;
  911. Procedure TTestComponentStream.TestTOwnedComponent;
  912. Var
  913. C : TComponent;
  914. begin
  915. C:=TOwnedComponent.Create(Nil);
  916. Try
  917. SaveToStream(C);
  918. ExpectSignature;
  919. ExpectFlags([],0);
  920. ExpectBareString('TOwnedComponent');
  921. ExpectBareString('TestTOwnedComponent');
  922. ExpectBareString('CompProp');
  923. ExpectIdent('SubComponent');
  924. ExpectEndOfList;
  925. ExpectEndOfList;
  926. Finally
  927. C.Free;
  928. end;
  929. end;
  930. Procedure TTestComponentStream.TestTStreamedOwnedComponent;
  931. Var
  932. C : TComponent;
  933. begin
  934. C:=TStreamedOwnedComponent.Create(Nil);
  935. Try
  936. SaveToStream(C);
  937. ExpectSignature;
  938. ExpectFlags([],0);
  939. ExpectBareString('TStreamedOwnedComponent');
  940. ExpectBareString('TestTStreamedOwnedComponent');
  941. ExpectEndOfList;
  942. ExpectFlags([],0);
  943. ExpectBareString('TIntegerComponent');
  944. ExpectBareString('Sub');
  945. ExpectBareString('IntProp');
  946. ExpectInteger(3);
  947. ExpectEndOfList;
  948. ExpectEndOfList;
  949. ExpectEndOfList;
  950. ExpectEndOfStream;
  951. Finally
  952. C.Free;
  953. end;
  954. end;
  955. Procedure TTestComponentStream.TestTStreamedOwnedComponents;
  956. Var
  957. C : TComponent;
  958. begin
  959. C:=TStreamedOwnedComponents.Create(Nil);
  960. Try
  961. SaveToStream(C);
  962. ExpectSignature;
  963. ExpectFlags([],0);
  964. ExpectBareString('TStreamedOwnedComponents');
  965. ExpectBareString('TestTStreamedOwnedComponents');
  966. ExpectEndOfList;
  967. ExpectFlags([],0);
  968. ExpectBareString('TIntegerComponent');
  969. ExpectBareString('SubA');
  970. ExpectBareString('IntProp');
  971. ExpectInteger(3);
  972. ExpectEndOfList;
  973. ExpectEndOfList;
  974. ExpectFlags([],0);
  975. ExpectBareString('TStringComponent');
  976. ExpectBareString('SubB');
  977. ExpectBareString('StringProp');
  978. ExpectString('A string');
  979. ExpectEndOfList;
  980. ExpectEndOfList;
  981. ExpectEndOfList;
  982. ExpectEndOfStream;
  983. Finally
  984. C.Free;
  985. end;
  986. end;
  987. Procedure TTestComponentStream.TestTMethodComponent;
  988. Var
  989. C : TComponent;
  990. begin
  991. C:=TMethodComponent.Create(Nil);
  992. Try
  993. SaveToStream(C);
  994. ExpectSignature;
  995. ExpectFlags([],0);
  996. ExpectBareString('TMethodComponent');
  997. ExpectBareString('TestTMethodComponent');
  998. ExpectBareString('MethodProp');
  999. ExpectIdent('MyMethod');
  1000. ExpectEndOfList;
  1001. ExpectEndOfList;
  1002. Finally
  1003. C.Free;
  1004. end;
  1005. end;
  1006. Procedure TTestComponentStream.TestTMethodComponent2;
  1007. Var
  1008. C : TComponent;
  1009. begin
  1010. C:=TMethodComponent2.Create(Nil);
  1011. Try
  1012. SaveToStream(C);
  1013. ExpectSignature;
  1014. ExpectFlags([],0);
  1015. ExpectBareString('TMethodComponent2');
  1016. ExpectBareString('TestTMethodComponent2');
  1017. ExpectEndOfList;
  1018. ExpectFlags([],0);
  1019. ExpectBareString('TMethodComponent');
  1020. ExpectBareString('AComponent');
  1021. ExpectBareString('MethodProp');
  1022. ExpectIdent('MyMethod2');
  1023. ExpectEndOfList;
  1024. ExpectEndOfList;
  1025. ExpectEndOfList;
  1026. Finally
  1027. C.Free;
  1028. end;
  1029. end;
  1030. { TMyColl }
  1031. function TMyColl.GetIt(index : Integer): TMyItem;
  1032. begin
  1033. Result:=Items[Index] as TMyItem;
  1034. end;
  1035. procedure TMyColl.SetIt(index : Integer; const AValue: TMyItem);
  1036. begin
  1037. Items[Index]:=AValue;
  1038. end;
  1039. { TCollComp }
  1040. procedure TCollComp.SetMyColl(const AValue: TMyColl);
  1041. begin
  1042. if (FMyColl=AValue) then
  1043. exit;
  1044. FMyColl.Assign(AValue);
  1045. end;
  1046. constructor TCollComp.Create(AOwner: TComponent);
  1047. begin
  1048. inherited Create(AOwner);
  1049. FMyColl:=TMyCOll.Create(TMyItem);
  1050. end;
  1051. destructor TCollComp.Destroy;
  1052. begin
  1053. FreeAndNil(FMyColl);
  1054. inherited Destroy;
  1055. end;
  1056. function TCollComp.ToStream: TStream;
  1057. begin
  1058. Result:=TMemoryStream.Create;
  1059. Result.WriteComponent(Self);
  1060. Result.Position:=0;
  1061. end;
  1062. procedure TCollComp.FromStream(AStream: TStream);
  1063. begin
  1064. AStream.ReadComponent(Self);
  1065. Astream.Free;
  1066. end;
  1067. procedure TMyItem.Assign(Source: TPersistent);
  1068. Var
  1069. I : TMyItem;
  1070. begin
  1071. If (Source is TMyItem) then
  1072. begin
  1073. I:=Source as TMyItem;
  1074. FNR:=I.NR;
  1075. FStr:=I.Str;
  1076. end
  1077. else
  1078. inherited Assign(Source);
  1079. end;
  1080. Procedure TTestCollectionStream.CompareColl(CA,CB : TMyColl);
  1081. Var
  1082. I : Integer;
  1083. begin
  1084. AssertEquals('Counts differ: %d %d',CA.Count,CB.Count);
  1085. For I:=0 to CA.Count-1 do
  1086. begin
  1087. AssertEquals(Format('Nr property of element %d equals',[I]),CA[i].Nr,CB[i].Nr);
  1088. AssertEquals(Format('Str property of element %d equals',[I]),CA[i].Str,CB[i].Str);
  1089. end;
  1090. end;
  1091. Function TTestCollectionStream.EmptyComp : TCollComp;
  1092. begin
  1093. Result:=TCollComp.Create(Nil);
  1094. end;
  1095. Function TTestCollectionStream.CreateColl(Anr : Integer) : TCollComp;
  1096. Var
  1097. I : Integer;
  1098. T : TMyItem;
  1099. begin
  1100. Result:=EmptyComp;
  1101. Result.Name:='C'+IntToStr(Anr);
  1102. For I:=0 to ANr-1 do
  1103. begin
  1104. T:=Result.MyColl.Add as TMyItem;
  1105. T.Nr:=I; // not I+1, so the default value gets tested too
  1106. T.Str:=IntToStr(I+1);
  1107. end;
  1108. end;
  1109. Procedure TTestCollectionStream.TestEmpty;
  1110. Var
  1111. CA,CB : TCollComp;
  1112. begin
  1113. CA:=CreateColl(0);
  1114. try
  1115. CB:=EmptyComp;
  1116. Try
  1117. CB.FromStream(CA.ToStream);
  1118. CompareColl(CA.MyColl,CB.MyColl);
  1119. Finally
  1120. CB.Free;
  1121. end;
  1122. Finally
  1123. CA.Free;
  1124. end;
  1125. end;
  1126. Procedure TTestCollectionStream.TestNr(ACount : Integer);
  1127. Var
  1128. CA,CB : TCollComp;
  1129. begin
  1130. CA:=CreateColl(ACount);
  1131. try
  1132. CB:=EmptyComp;
  1133. Try
  1134. CB.FromStream(CA.ToStream);
  1135. CompareColl(CA.MyColl,CB.MyColl);
  1136. Finally
  1137. CB.Free;
  1138. end;
  1139. Finally
  1140. CA.Free;
  1141. end;
  1142. end;
  1143. Procedure TTestCollectionStream.TestClear;
  1144. Var
  1145. CA,CB : TCollComp;
  1146. begin
  1147. CA:=CreateColl(3);
  1148. try
  1149. CB:=CreateColl(1);
  1150. CB.Name:='';
  1151. Try
  1152. // CB collection should be cleared before loading.
  1153. CB.FromStream(CA.ToStream);
  1154. CompareColl(CA.MyColl,CB.MyColl);
  1155. Finally
  1156. CB.Free;
  1157. end;
  1158. Finally
  1159. CA.Free;
  1160. end;
  1161. end;
  1162. Procedure TTestCollectionStream.Test1;
  1163. begin
  1164. TestNr(1);
  1165. end;
  1166. Procedure TTestCollectionStream.Test2;
  1167. begin
  1168. TestNr(2);
  1169. end;
  1170. Procedure TTestCollectionStream.Test3;
  1171. begin
  1172. TestNr(3);
  1173. end;
  1174. begin
  1175. RegisterTests([TTestComponentStream,TTestCollectionStream]);
  1176. end.