/packages/fcl-passrc/tests/tcscanner.pas

https://github.com/slibre/freepascal · Pascal · 1390 lines · 1251 code · 127 blank · 12 comment · 9 complexity · 3e9bcc910adffc33525412c039e787de MD5 · raw file

  1. unit tcscanner;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils, typinfo, fpcunit, testregistry, pscanner;
  6. type
  7. { TTestTokenFinder }
  8. TTestTokenFinder = class(TTestCase)
  9. Published
  10. Procedure TestFind;
  11. end;
  12. { TTestStreamLineReader }
  13. TTestStreamLineReader = class(TTestCase)
  14. Private
  15. FReader: TStreamLineReader;
  16. Protected
  17. procedure NewSource(Const Source : string);
  18. Procedure TestLine(Const ALine : String; ExpectEOF : Boolean = True);
  19. procedure TearDown; override;
  20. Published
  21. Procedure TestCreate;
  22. Procedure TestEOF;
  23. Procedure TestEmptyLine;
  24. Procedure TestEmptyLineCR;
  25. Procedure TestEmptyLineLF;
  26. Procedure TestEmptyLineCRLF;
  27. Procedure TestEmptyLineLFCR;
  28. Procedure TestOneLine;
  29. Procedure TestTwoLines;
  30. end;
  31. { TTestingPascalScanner }
  32. TTestingPascalScanner = Class(TPascalScanner)
  33. private
  34. FDoSpecial: Boolean;
  35. protected
  36. function HandleMacro(AIndex: integer): TToken;override;
  37. Public
  38. Property DoSpecial : Boolean Read FDoSpecial Write FDoSpecial;
  39. end;
  40. { TTestScanner }
  41. TTestScanner= class(TTestCase)
  42. Private
  43. FLI: String;
  44. FScanner : TPascalScanner;
  45. FResolver : TStreamResolver;
  46. protected
  47. procedure SetUp; override;
  48. procedure TearDown; override;
  49. Function TokenToString(tk : TToken) : string;
  50. Procedure AssertEquals(Msg : String; Expected,Actual : TToken); overload;
  51. procedure NewSource(Const Source : string; DoClear : Boolean = True);
  52. Procedure DoTestToken(t : TToken; Const ASource : String; Const CheckEOF : Boolean = True);
  53. Procedure TestToken(t : TToken; Const ASource : String; Const CheckEOF : Boolean = True);
  54. Procedure TestTokens(t : array of TToken; Const ASource : String; Const CheckEOF : Boolean = True;Const DoClear : Boolean = True);
  55. Property LastIDentifier : String Read FLI Write FLi;
  56. published
  57. procedure TestEOF;
  58. procedure TestWhitespace;
  59. procedure TestComment1;
  60. procedure TestComment2;
  61. procedure TestComment3;
  62. procedure TestNestedComment1;
  63. procedure TestNestedComment2;
  64. procedure TestNestedComment3;
  65. procedure TestNestedComment4;
  66. procedure TestIdentifier;
  67. procedure TestString;
  68. procedure TestNumber;
  69. procedure TestChar;
  70. procedure TestBraceOpen;
  71. procedure TestBraceClose;
  72. procedure TestMul;
  73. procedure TestPlus;
  74. procedure TestComma;
  75. procedure TestMinus;
  76. procedure TestDot;
  77. procedure TestDivision;
  78. procedure TestColon;
  79. procedure TestSemicolon;
  80. procedure TestLessThan;
  81. procedure TestEqual;
  82. procedure TestGreaterThan;
  83. procedure TestAt;
  84. procedure TestSquaredBraceOpen;
  85. procedure TestSquaredBraceClose;
  86. procedure TestCaret;
  87. procedure TestBackslash;
  88. procedure TestDotDot;
  89. procedure TestAssign;
  90. procedure TestAssignPlus;
  91. procedure TestAssignMinus;
  92. procedure TestAssignMul;
  93. procedure TestAssignDivision;
  94. procedure TestNotEqual;
  95. procedure TestLessEqualThan;
  96. procedure TestGreaterEqualThan;
  97. procedure TestPower;
  98. procedure TestSymmetricalDifference;
  99. procedure TestAbsolute;
  100. procedure TestAnd;
  101. procedure TestArray;
  102. procedure TestAs;
  103. procedure TestAsm;
  104. procedure TestBegin;
  105. procedure TestBitpacked;
  106. procedure TestCase;
  107. procedure TestClass;
  108. procedure TestConst;
  109. procedure TestConstructor;
  110. procedure TestDestructor;
  111. procedure TestDiv;
  112. procedure TestDo;
  113. procedure TestDownto;
  114. procedure TestElse;
  115. procedure TestEnd;
  116. procedure TestExcept;
  117. procedure TestExports;
  118. procedure TestFalse;
  119. procedure TestFile;
  120. procedure TestFinalization;
  121. procedure TestFinally;
  122. procedure TestFor;
  123. procedure TestFunction;
  124. procedure TestGeneric;
  125. procedure TestGoto;
  126. Procedure TestHelper;
  127. procedure TestIf;
  128. procedure TestImplementation;
  129. procedure TestIn;
  130. procedure TestInherited;
  131. procedure TestInitialization;
  132. procedure TestInline;
  133. procedure TestInterface;
  134. procedure TestIs;
  135. procedure TestLabel;
  136. procedure TestLibrary;
  137. procedure TestMod;
  138. procedure TestNil;
  139. procedure TestNot;
  140. procedure TestObject;
  141. procedure TestOf;
  142. procedure TestOn;
  143. procedure TestOperator;
  144. procedure TestOr;
  145. procedure TestPacked;
  146. procedure TestProcedure;
  147. procedure TestProgram;
  148. procedure TestProperty;
  149. procedure TestRaise;
  150. procedure TestRecord;
  151. procedure TestRepeat;
  152. procedure TestResourceString;
  153. procedure TestSelf;
  154. procedure TestSet;
  155. procedure TestShl;
  156. procedure TestShr;
  157. procedure TestSpecialize;
  158. procedure TestThen;
  159. procedure TestThreadvar;
  160. procedure TestTo;
  161. procedure TestTrue;
  162. procedure TestTry;
  163. procedure TestType;
  164. procedure TestUnit;
  165. procedure TestUntil;
  166. procedure TestUses;
  167. procedure TestVar;
  168. procedure TestWhile;
  169. procedure TestWith;
  170. procedure TestXor;
  171. procedure TestLineEnding;
  172. procedure TestTab;
  173. Procedure TestTokenSeries;
  174. Procedure TestTokenSeriesNoWhiteSpace;
  175. Procedure TestTokenSeriesComments;
  176. Procedure TestTokenSeriesNoComments;
  177. Procedure TestDefine0;
  178. Procedure TestDefine1;
  179. Procedure TestDefine2;
  180. Procedure TestDefine3;
  181. Procedure TestDefine4;
  182. Procedure TestDefine5;
  183. Procedure TestDefine6;
  184. Procedure TestDefine7;
  185. Procedure TestDefine8;
  186. Procedure TestDefine9;
  187. Procedure TestDefine10;
  188. Procedure TestDefine11;
  189. Procedure TestDefine12;
  190. Procedure TestInclude;
  191. Procedure TestInclude2;
  192. Procedure TestUnDefine1;
  193. Procedure TestMacro1;
  194. procedure TestMacro2;
  195. procedure TestMacro3;
  196. procedure TestMacroHandling;
  197. end;
  198. implementation
  199. { TTestingPascalScanner }
  200. function TTestingPascalScanner.HandleMacro(AIndex: integer): TToken;
  201. begin
  202. if DoSpecial then
  203. begin
  204. Result:=tkIdentifier;
  205. SetCurTokenstring('somethingweird');
  206. end
  207. else
  208. Result:=inherited HandleMacro(AIndex);
  209. end;
  210. { TTestTokenFinder }
  211. procedure TTestTokenFinder.TestFind;
  212. Var
  213. tk,tkr : TToken;
  214. S : string;
  215. B : Boolean;
  216. begin
  217. For tk:=tkAbsolute to tkXor do
  218. begin
  219. S:=tokenInfos[tk];
  220. B:=IsNamedToken(S,tkr);
  221. AssertEquals('Token '+S+' is a token',true,B);
  222. AssertEquals('Token '+S+' returns correct token',Ord(tk),Ord(tkr));
  223. end;
  224. end;
  225. { TTestStreamLineReader }
  226. procedure TTestStreamLineReader.NewSource(Const Source: string);
  227. begin
  228. FReader:=TStringStreamLineReader.Create('afile',Source);
  229. end;
  230. procedure TTestStreamLineReader.TestLine(const ALine: String; ExpectEOF: Boolean);
  231. begin
  232. AssertNotNull('Have reader',FReader);
  233. AssertEquals('Reading source line',ALine,FReader.ReadLine);
  234. if ExpectEOF then
  235. AssertEquals('End of file reached',True,FReader.IsEOF);
  236. end;
  237. procedure TTestStreamLineReader.TearDown;
  238. begin
  239. inherited TearDown;
  240. If Assigned(FReader) then
  241. FreeAndNil(Freader);
  242. end;
  243. procedure TTestStreamLineReader.TestCreate;
  244. begin
  245. FReader:=TStreamLineReader.Create('afile');
  246. AssertEquals('Correct filename','afile',FReader.FileName);
  247. AssertEquals('Initially empty',True,FReader.isEOF);
  248. end;
  249. procedure TTestStreamLineReader.TestEOF;
  250. begin
  251. NewSource('');
  252. AssertEquals('Empty stream',True,FReader.IsEOF);
  253. end;
  254. procedure TTestStreamLineReader.TestEmptyLine;
  255. begin
  256. NewSource('');
  257. TestLine('');
  258. end;
  259. procedure TTestStreamLineReader.TestEmptyLineCR;
  260. begin
  261. NewSource(#13);
  262. TestLine('');
  263. end;
  264. procedure TTestStreamLineReader.TestEmptyLineLF;
  265. begin
  266. NewSource(#10);
  267. TestLine('');
  268. end;
  269. procedure TTestStreamLineReader.TestEmptyLineCRLF;
  270. begin
  271. NewSource(#13#10);
  272. TestLine('');
  273. end;
  274. procedure TTestStreamLineReader.TestEmptyLineLFCR;
  275. begin
  276. NewSource(#10#13);
  277. TestLine('',False);
  278. TestLine('');
  279. end;
  280. procedure TTestStreamLineReader.TestOneLine;
  281. Const
  282. S = 'a line with text';
  283. begin
  284. NewSource(S);
  285. TestLine(S);
  286. end;
  287. procedure TTestStreamLineReader.TestTwoLines;
  288. Const
  289. S = 'a line with text';
  290. begin
  291. NewSource(S+sLineBreak+S);
  292. TestLine(S,False);
  293. TestLine(S);
  294. end;
  295. { ---------------------------------------------------------------------
  296. TTestScanner
  297. ---------------------------------------------------------------------}
  298. procedure TTestScanner.SetUp;
  299. begin
  300. FResolver:=TStreamResolver.Create;
  301. FResolver.OwnsStreams:=True;
  302. FScanner:=TTestingPascalScanner.Create(FResolver);
  303. // Do nothing
  304. end;
  305. procedure TTestScanner.TearDown;
  306. begin
  307. FreeAndNil(FScanner);
  308. FreeAndNil(FResolver);
  309. end;
  310. function TTestScanner.TokenToString(tk: TToken): string;
  311. begin
  312. Result:=GetEnumName(TypeInfo(TToken),Ord(tk));
  313. end;
  314. procedure TTestScanner.AssertEquals(Msg: String; Expected, Actual: TToken);
  315. begin
  316. AssertEquals(Msg,TokenToString(Expected),TokenToString(Actual));
  317. end;
  318. procedure TTestScanner.NewSource(const Source: string; DoClear : Boolean = True);
  319. begin
  320. if DoClear then
  321. FResolver.Clear;
  322. FResolver.AddStream('afile.pp',TStringStream.Create(Source));
  323. FScanner.OpenFile('afile.pp');
  324. end;
  325. procedure TTestScanner.DoTestToken(t: TToken; const ASource: String;
  326. Const CheckEOF: Boolean);
  327. Var
  328. tk : ttoken;
  329. begin
  330. NewSource(ASource);
  331. tk:=FScanner.FetchToken;
  332. AssertEquals('Read token equals expected token.',t,tk);
  333. if CheckEOF then
  334. begin
  335. tk:=FScanner.FetchToken;
  336. if (tk=tkLineEnding) and not (t in [tkEOF,tkLineEnding]) then
  337. tk:=FScanner.FetchToken;
  338. AssertEquals('EOF reached.',tkEOF,FScanner.FetchToken);
  339. end;
  340. end;
  341. procedure TTestScanner.TestToken(t: TToken; const ASource: String; Const CheckEOF: Boolean);
  342. Var
  343. S : String;
  344. begin
  345. DoTestToken(t,ASource);
  346. if (ASource<>'') then
  347. begin
  348. S:=ASource;
  349. S[1]:=Upcase(S[1]);
  350. DoTestToken(t,S);
  351. end;
  352. DoTestToken(t,UpperCase(ASource));
  353. DoTestToken(t,LowerCase(ASource));
  354. end;
  355. procedure TTestScanner.TestTokens(t: array of TToken; const ASource: String;
  356. const CheckEOF: Boolean;Const DoClear : Boolean = True);
  357. Var
  358. tk : ttoken;
  359. i : integer;
  360. begin
  361. NewSource(ASource,DoClear);
  362. For I:=Low(t) to High(t) do
  363. begin
  364. tk:=FScanner.FetchToken;
  365. AssertEquals(Format('Read token %d equals expected token.',[i]),t[i],tk);
  366. if tk=tkIdentifier then
  367. LastIdentifier:=FScanner.CurtokenString;
  368. end;
  369. if CheckEOF then
  370. begin
  371. tk:=FScanner.FetchToken;
  372. if (tk=tkLineEnding) then
  373. tk:=FScanner.FetchToken;
  374. AssertEquals('EOF reached.',tkEOF,FScanner.FetchToken);
  375. end;
  376. end;
  377. procedure TTestScanner.TestEOF;
  378. begin
  379. TestToken(tkEOF,'')
  380. end;
  381. procedure TTestScanner.TestWhitespace;
  382. begin
  383. TestToken(tkWhitespace,' ');
  384. TestToken(tkWhitespace,' ');
  385. end;
  386. procedure TTestScanner.TestComment1;
  387. begin
  388. TestToken(tkComment,'{ comment }');
  389. end;
  390. procedure TTestScanner.TestComment2;
  391. begin
  392. TestToken(tkComment,'(* comment *)');
  393. end;
  394. procedure TTestScanner.TestComment3;
  395. begin
  396. TestToken(tkComment,'//');
  397. end;
  398. procedure TTestScanner.TestNestedComment1;
  399. begin
  400. TestToken(tkComment,'// { comment } ');
  401. end;
  402. procedure TTestScanner.TestNestedComment2;
  403. begin
  404. TestToken(tkComment,'(* { comment } *)');
  405. end;
  406. procedure TTestScanner.TestNestedComment3;
  407. begin
  408. TestToken(tkComment,'{ { comment } }');
  409. end;
  410. procedure TTestScanner.TestNestedComment4;
  411. begin
  412. TestToken(tkComment,'{ (* comment *) }');
  413. end;
  414. procedure TTestScanner.TestIdentifier;
  415. begin
  416. TestToken(tkIdentifier,'identifier');
  417. end;
  418. procedure TTestScanner.TestString;
  419. begin
  420. TestToken(pscanner.tkString,'''A string''');
  421. end;
  422. procedure TTestScanner.TestNumber;
  423. begin
  424. TestToken(tkNumber,'123');
  425. end;
  426. procedure TTestScanner.TestChar;
  427. begin
  428. TestToken(pscanner.tkChar,'#65 ', false);
  429. end;
  430. procedure TTestScanner.TestBraceOpen;
  431. begin
  432. TestToken(tkBraceOpen,'(');
  433. end;
  434. procedure TTestScanner.TestBraceClose;
  435. begin
  436. TestToken(tkBraceClose,')');
  437. end;
  438. procedure TTestScanner.TestMul;
  439. begin
  440. TestToken(tkMul,'*');
  441. end;
  442. procedure TTestScanner.TestPlus;
  443. begin
  444. TestToken(tkPlus,'+');
  445. end;
  446. procedure TTestScanner.TestComma;
  447. begin
  448. TestToken(tkComma,',');
  449. end;
  450. procedure TTestScanner.TestMinus;
  451. begin
  452. TestToken(tkMinus,'-');
  453. end;
  454. procedure TTestScanner.TestDot;
  455. begin
  456. TestToken(tkDot,'.');
  457. end;
  458. procedure TTestScanner.TestDivision;
  459. begin
  460. TestToken(tkDivision,'/');
  461. end;
  462. procedure TTestScanner.TestColon;
  463. begin
  464. TestToken(tkColon,':');
  465. end;
  466. procedure TTestScanner.TestSemicolon;
  467. begin
  468. TestToken(tkSemicolon,';');
  469. end;
  470. procedure TTestScanner.TestLessThan;
  471. begin
  472. TestToken(tkLessThan,'<');
  473. end;
  474. procedure TTestScanner.TestEqual;
  475. begin
  476. TestToken(tkEqual,'=');
  477. end;
  478. procedure TTestScanner.TestGreaterThan;
  479. begin
  480. TestToken(tkGreaterThan,'>');
  481. end;
  482. procedure TTestScanner.TestAt;
  483. begin
  484. TestToken(tkAt,'@');
  485. end;
  486. procedure TTestScanner.TestSquaredBraceOpen;
  487. begin
  488. TestToken(tkSquaredBraceOpen,'[');
  489. end;
  490. procedure TTestScanner.TestSquaredBraceClose;
  491. begin
  492. TestToken(tkSquaredBraceClose,']');
  493. end;
  494. procedure TTestScanner.TestCaret;
  495. begin
  496. TestToken(tkCaret,'^');
  497. end;
  498. procedure TTestScanner.TestBackslash;
  499. begin
  500. TestToken(tkBackslash,'\');
  501. end;
  502. procedure TTestScanner.TestDotDot;
  503. begin
  504. TestToken(tkDotDot,'..');
  505. end;
  506. procedure TTestScanner.TestAssign;
  507. begin
  508. TestToken(tkAssign,':=');
  509. end;
  510. procedure TTestScanner.TestAssignPlus;
  511. begin
  512. TestTokens([tkPlus,tkEqual],'+=');
  513. FScanner.Options:=[po_cassignments];
  514. TestToken(tkAssignPlus,'+=');
  515. end;
  516. procedure TTestScanner.TestAssignMinus;
  517. begin
  518. TestTokens([tkMinus,tkEqual],'-=');
  519. FScanner.Options:=[po_cassignments];
  520. TestToken(tkAssignMinus,'-=');
  521. end;
  522. procedure TTestScanner.TestAssignMul;
  523. begin
  524. TestTokens([tkMul,tkEqual],'*=');
  525. FScanner.Options:=[po_cassignments];
  526. TestToken(tkAssignMul,'*=');
  527. end;
  528. procedure TTestScanner.TestAssignDivision;
  529. begin
  530. TestTokens([tkDivision,tkEqual],'/=');
  531. FScanner.Options:=[po_cassignments];
  532. TestToken(tkAssignDivision,'/=');
  533. end;
  534. procedure TTestScanner.TestNotEqual;
  535. begin
  536. TestToken(tkNotEqual,'<>');
  537. end;
  538. procedure TTestScanner.TestLessEqualThan;
  539. begin
  540. TestToken(tkLessEqualThan,'<=');
  541. end;
  542. procedure TTestScanner.TestGreaterEqualThan;
  543. begin
  544. TestToken(tkGreaterEqualThan,'>=');
  545. end;
  546. procedure TTestScanner.TestPower;
  547. begin
  548. TestToken(tkPower,'**');
  549. end;
  550. procedure TTestScanner.TestSymmetricalDifference;
  551. begin
  552. TestToken(tkSymmetricalDifference,'><');
  553. end;
  554. procedure TTestScanner.TestAbsolute;
  555. begin
  556. TestToken(tkabsolute,'absolute');
  557. end;
  558. procedure TTestScanner.TestAnd;
  559. begin
  560. TestToken(tkand,'and');
  561. end;
  562. procedure TTestScanner.TestArray;
  563. begin
  564. TestToken(tkarray,'array');
  565. end;
  566. procedure TTestScanner.TestAs;
  567. begin
  568. TestToken(tkas,'as');
  569. end;
  570. procedure TTestScanner.TestAsm;
  571. begin
  572. TestToken(tkasm,'asm');
  573. end;
  574. procedure TTestScanner.TestBegin;
  575. begin
  576. TestToken(tkbegin,'begin');
  577. end;
  578. procedure TTestScanner.TestBitpacked;
  579. begin
  580. TestToken(tkbitpacked,'bitpacked');
  581. end;
  582. procedure TTestScanner.TestCase;
  583. begin
  584. TestToken(tkcase,'case');
  585. end;
  586. procedure TTestScanner.TestClass;
  587. begin
  588. TestToken(tkclass,'class');
  589. end;
  590. procedure TTestScanner.TestConst;
  591. begin
  592. TestToken(tkconst,'const');
  593. end;
  594. procedure TTestScanner.TestConstructor;
  595. begin
  596. TestToken(tkconstructor,'constructor');
  597. end;
  598. procedure TTestScanner.TestDestructor;
  599. begin
  600. TestToken(tkdestructor,'destructor');
  601. end;
  602. procedure TTestScanner.TestDiv;
  603. begin
  604. TestToken(tkdiv,'div');
  605. end;
  606. procedure TTestScanner.TestDo;
  607. begin
  608. TestToken(tkdo,'do');
  609. end;
  610. procedure TTestScanner.TestDownto;
  611. begin
  612. TestToken(tkdownto,'downto');
  613. end;
  614. procedure TTestScanner.TestElse;
  615. begin
  616. TestToken(tkelse,'else');
  617. end;
  618. procedure TTestScanner.TestEnd;
  619. begin
  620. TestToken(tkend,'end');
  621. end;
  622. procedure TTestScanner.TestExcept;
  623. begin
  624. TestToken(tkexcept,'except');
  625. end;
  626. procedure TTestScanner.TestExports;
  627. begin
  628. TestToken(tkexports,'exports');
  629. end;
  630. procedure TTestScanner.TestFalse;
  631. begin
  632. TestToken(tkfalse,'false');
  633. end;
  634. procedure TTestScanner.TestFile;
  635. begin
  636. TestToken(tkfile,'file');
  637. end;
  638. procedure TTestScanner.TestFinalization;
  639. begin
  640. TestToken(tkfinalization,'finalization');
  641. end;
  642. procedure TTestScanner.TestFinally;
  643. begin
  644. TestToken(tkfinally,'finally');
  645. end;
  646. procedure TTestScanner.TestFor;
  647. begin
  648. TestToken(tkfor,'for');
  649. end;
  650. procedure TTestScanner.TestFunction;
  651. begin
  652. TestToken(tkfunction,'function');
  653. end;
  654. procedure TTestScanner.TestGeneric;
  655. begin
  656. TestToken(tkgeneric,'generic');
  657. end;
  658. procedure TTestScanner.TestGoto;
  659. begin
  660. TestToken(tkgoto,'goto');
  661. end;
  662. procedure TTestScanner.TestHelper;
  663. begin
  664. TestToken(tkHelper,'helper');
  665. end;
  666. procedure TTestScanner.TestIf;
  667. begin
  668. TestToken(tkif,'if');
  669. end;
  670. procedure TTestScanner.TestImplementation;
  671. begin
  672. TestToken(tkimplementation,'implementation');
  673. end;
  674. procedure TTestScanner.TestIn;
  675. begin
  676. TestToken(tkin,'in');
  677. end;
  678. procedure TTestScanner.TestInherited;
  679. begin
  680. TestToken(tkinherited,'inherited');
  681. end;
  682. procedure TTestScanner.TestInitialization;
  683. begin
  684. TestToken(tkinitialization,'initialization');
  685. end;
  686. procedure TTestScanner.TestInline;
  687. begin
  688. TestToken(tkinline,'inline');
  689. end;
  690. procedure TTestScanner.TestInterface;
  691. begin
  692. TestToken(tkinterface,'interface');
  693. end;
  694. procedure TTestScanner.TestIs;
  695. begin
  696. TestToken(tkis,'is');
  697. end;
  698. procedure TTestScanner.TestLabel;
  699. begin
  700. TestToken(tklabel,'label');
  701. end;
  702. procedure TTestScanner.TestLibrary;
  703. begin
  704. TestToken(tklibrary,'library');
  705. end;
  706. procedure TTestScanner.TestMod;
  707. begin
  708. TestToken(tkmod,'mod');
  709. end;
  710. procedure TTestScanner.TestNil;
  711. begin
  712. TestToken(tknil,'nil');
  713. end;
  714. procedure TTestScanner.TestNot;
  715. begin
  716. TestToken(tknot,'not');
  717. end;
  718. procedure TTestScanner.TestObject;
  719. begin
  720. TestToken(tkobject,'object');
  721. end;
  722. procedure TTestScanner.TestOf;
  723. begin
  724. TestToken(tkof,'of');
  725. end;
  726. procedure TTestScanner.TestOn;
  727. begin
  728. TestToken(tkon,'on');
  729. end;
  730. procedure TTestScanner.TestOperator;
  731. begin
  732. TestToken(tkoperator,'operator');
  733. end;
  734. procedure TTestScanner.TestOr;
  735. begin
  736. TestToken(tkor,'or');
  737. end;
  738. procedure TTestScanner.TestPacked;
  739. begin
  740. TestToken(tkpacked,'packed');
  741. end;
  742. procedure TTestScanner.TestProcedure;
  743. begin
  744. TestToken(tkprocedure,'procedure');
  745. end;
  746. procedure TTestScanner.TestProgram;
  747. begin
  748. TestToken(tkprogram,'program');
  749. end;
  750. procedure TTestScanner.TestProperty;
  751. begin
  752. TestToken(tkproperty,'property');
  753. end;
  754. procedure TTestScanner.TestRaise;
  755. begin
  756. TestToken(tkraise,'raise');
  757. end;
  758. procedure TTestScanner.TestRecord;
  759. begin
  760. TestToken(tkrecord,'record');
  761. end;
  762. procedure TTestScanner.TestRepeat;
  763. begin
  764. TestToken(tkrepeat,'repeat');
  765. end;
  766. procedure TTestScanner.TestResourceString;
  767. begin
  768. TestToken(tkResourceString,'resourcestring');
  769. end;
  770. procedure TTestScanner.TestSelf;
  771. begin
  772. TestToken(tkself,'self');
  773. end;
  774. procedure TTestScanner.TestSet;
  775. begin
  776. TestToken(tkset,'set');
  777. end;
  778. procedure TTestScanner.TestShl;
  779. begin
  780. TestToken(tkshl,'shl');
  781. end;
  782. procedure TTestScanner.TestShr;
  783. begin
  784. TestToken(tkshr,'shr');
  785. end;
  786. procedure TTestScanner.TestSpecialize;
  787. begin
  788. TestToken(tkspecialize,'specialize');
  789. end;
  790. procedure TTestScanner.TestThen;
  791. begin
  792. TestToken(tkthen,'then');
  793. end;
  794. procedure TTestScanner.TestThreadvar;
  795. begin
  796. TestToken(tkthreadvar,'threadvar');
  797. end;
  798. procedure TTestScanner.TestTo;
  799. begin
  800. TestToken(tkto,'to');
  801. end;
  802. procedure TTestScanner.TestTrue;
  803. begin
  804. TestToken(tktrue,'true');
  805. end;
  806. procedure TTestScanner.TestTry;
  807. begin
  808. TestToken(tktry,'try');
  809. end;
  810. procedure TTestScanner.TestType;
  811. begin
  812. TestToken(tktype,'type');
  813. end;
  814. procedure TTestScanner.TestUnit;
  815. begin
  816. TestToken(tkunit,'unit');
  817. end;
  818. procedure TTestScanner.TestUntil;
  819. begin
  820. TestToken(tkuntil,'until');
  821. end;
  822. procedure TTestScanner.TestUses;
  823. begin
  824. TestToken(tkuses,'uses');
  825. end;
  826. procedure TTestScanner.TestVar;
  827. begin
  828. TestToken(tkvar,'var');
  829. end;
  830. procedure TTestScanner.TestWhile;
  831. begin
  832. TestToken(tkwhile,'while');
  833. end;
  834. procedure TTestScanner.TestWith;
  835. begin
  836. TestToken(tkwith,'with');
  837. end;
  838. procedure TTestScanner.TestXor;
  839. begin
  840. TestToken(tkxor,'xor');
  841. end;
  842. procedure TTestScanner.TestLineEnding;
  843. begin
  844. TestToken(tkLineEnding,#10);
  845. end;
  846. procedure TTestScanner.TestTab;
  847. begin
  848. TestToken(tkTab,#9);
  849. end;
  850. procedure TTestScanner.TestTokenSeries;
  851. begin
  852. TestTokens([tkin,tkWhitespace,tkOf,tkWhiteSpace,tkthen,tkWhiteSpace,tkIdentifier],'in of then aninteger')
  853. end;
  854. procedure TTestScanner.TestTokenSeriesNoWhiteSpace;
  855. begin
  856. FScanner.SkipWhiteSpace:=True;
  857. TestTokens([tkin,tkOf,tkthen,tkIdentifier],'in of then aninteger')
  858. end;
  859. procedure TTestScanner.TestTokenSeriesComments;
  860. begin
  861. TestTokens([tkin,tkWhitespace,tkOf,tkWhiteSpace,tkComment,tkWhiteSpace,tkIdentifier],'in of {then} aninteger')
  862. end;
  863. procedure TTestScanner.TestTokenSeriesNoComments;
  864. begin
  865. FScanner.SkipComments:=True;
  866. TestTokens([tkin,tkWhitespace,tkOf,tkWhiteSpace,tkWhiteSpace,tkIdentifier],'in of {then} aninteger')
  867. end;
  868. procedure TTestScanner.TestDefine0;
  869. begin
  870. TestTokens([tkComment],'{$DEFINE NEVER}');
  871. If FSCanner.Defines.IndexOf('NEVER')=-1 then
  872. Fail('Define not defined');
  873. end;
  874. procedure TTestScanner.TestDefine1;
  875. begin
  876. TestTokens([tkComment],'{$IFDEF NEVER} of {$ENDIF}');
  877. end;
  878. procedure TTestScanner.TestDefine2;
  879. begin
  880. FSCanner.Defines.Add('ALWAYS');
  881. TestTokens([tkComment,tkWhitespace,tkOf,tkWhitespace,tkcomment],'{$IFDEF ALWAYS} of {$ENDIF}');
  882. end;
  883. procedure TTestScanner.TestDefine3;
  884. begin
  885. FSCanner.Defines.Add('ALWAYS');
  886. TestTokens([tkComment,tkWhitespace,tkOf,tkWhitespace,tkcomment],'{$IFDEF ALWAYS} of {$ELSE} in {$ENDIF}');
  887. end;
  888. procedure TTestScanner.TestDefine4;
  889. begin
  890. TestTokens([tkComment,tkWhitespace,tkin,tkWhitespace,tkcomment],'{$IFDEF ALWAYS} of {$ELSE} in {$ENDIF}');
  891. end;
  892. procedure TTestScanner.TestDefine5;
  893. begin
  894. FScanner.SkipComments:=True;
  895. TestTokens([tkLineEnding],'{$IFDEF NEVER} of {$ENDIF}');
  896. end;
  897. procedure TTestScanner.TestDefine6;
  898. begin
  899. FSCanner.Defines.Add('ALWAYS');
  900. FScanner.SkipComments:=True;
  901. TestTokens([tkWhitespace,tkOf,tkWhitespace],'{$IFDEF ALWAYS} of {$ENDIF}');
  902. end;
  903. procedure TTestScanner.TestDefine7;
  904. begin
  905. FSCanner.Defines.Add('ALWAYS');
  906. FScanner.SkipComments:=True;
  907. TestTokens([tkWhitespace,tkOf,tkWhitespace],'{$IFDEF ALWAYS} of {$ELSE} in {$ENDIF}');
  908. end;
  909. procedure TTestScanner.TestDefine8;
  910. begin
  911. FScanner.SkipComments:=True;
  912. TestTokens([tkWhitespace,tkin,tkWhitespace],'{$IFDEF ALWAYS} of {$ELSE} in {$ENDIF}');
  913. end;
  914. procedure TTestScanner.TestDefine9;
  915. begin
  916. FScanner.SkipWhiteSpace:=True;
  917. TestTokens([],'{$IFDEF NEVER} of {$ENDIF}');
  918. end;
  919. procedure TTestScanner.TestDefine10;
  920. begin
  921. FSCanner.Defines.Add('ALWAYS');
  922. FScanner.SkipComments:=True;
  923. TestTokens([tkWhitespace,tkOf,tkWhitespace],'{$IFDEF ALWAYS} of {$ENDIF}');
  924. end;
  925. procedure TTestScanner.TestDefine11;
  926. begin
  927. FSCanner.Defines.Add('ALWAYS');
  928. FScanner.SkipComments:=True;
  929. FScanner.SkipWhiteSpace:=True;
  930. TestTokens([tkOf],'{$IFDEF ALWAYS} of {$ELSE} in {$ENDIF}');
  931. end;
  932. procedure TTestScanner.TestDefine12;
  933. begin
  934. FScanner.SkipComments:=True;
  935. FScanner.SkipWhiteSpace:=True;
  936. TestTokens([tkin],'{$IFDEF ALWAYS} of {$ELSE} in {$ENDIF}');
  937. end;
  938. procedure TTestScanner.TestInclude;
  939. begin
  940. FResolver.AddStream('myinclude.inc',TStringStream.Create('if true then'));
  941. FScanner.SkipWhiteSpace:=True;
  942. FScanner.SkipComments:=True;
  943. TestTokens([tkIf,tkTrue,tkThen],'{$I myinclude.inc}',True,False);
  944. end;
  945. procedure TTestScanner.TestInclude2;
  946. begin
  947. FResolver.AddStream('myinclude.inc',TStringStream.Create('if true then'));
  948. FScanner.SkipWhiteSpace:=True;
  949. FScanner.SkipComments:=True;
  950. TestTokens([tkIf,tkTrue,tkThen,tkElse],'{$I myinclude.inc} else',True,False);
  951. end;
  952. procedure TTestScanner.TestUnDefine1;
  953. begin
  954. FSCanner.Defines.Add('ALWAYS');
  955. TestTokens([tkComment],'{$UNDEF ALWAYS}');
  956. AssertEquals('No more define',-1,FScanner.Defines.INdexOf('ALWAYS'));
  957. end;
  958. procedure TTestScanner.TestMacro1;
  959. begin
  960. FScanner.SkipWhiteSpace:=True;
  961. FScanner.SkipComments:=True;
  962. TestTokens([tkbegin,tkend,tkDot],'{$DEFINE MM:=begin end.}'#13#10'MM',True,False);
  963. end;
  964. procedure TTestScanner.TestMacro2;
  965. begin
  966. FScanner.SkipWhiteSpace:=True;
  967. FScanner.SkipComments:=True;
  968. TestTokens([tkbegin,tkend,tkDot],'{$DEFINE MM:=begin end}'#13#10'MM .',True,False);
  969. end;
  970. procedure TTestScanner.TestMacro3;
  971. begin
  972. FScanner.SkipComments:=True;
  973. FScanner.SkipWhiteSpace:=True;
  974. TestTokens([tkof],'{$DEFINE MM:=begin end}'#13#10'{$IFDEF MM} of {$ELSE} in {$ENDIF}');
  975. end;
  976. procedure TTestScanner.TestMacroHandling;
  977. begin
  978. TTestingPascalScanner(FScanner).DoSpecial:=True;
  979. FScanner.SkipComments:=True;
  980. FScanner.SkipWhiteSpace:=True;
  981. TestTokens([tkIdentifier],'{$DEFINE MM:=begin end}'#13#10'MM');
  982. AssertEQuals('Correct identifier', 'somethingweird',LastIdentifier);
  983. end;
  984. initialization
  985. RegisterTests([TTestTokenFinder,TTestStreamLineReader,TTestScanner]);
  986. end.