Test_Logger.dpr 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  1. (* _ _
  2. * | |__ _ __ ___ ___ | | __
  3. * | '_ \| '__/ _ \ / _ \| |/ /
  4. * | |_) | | | (_) | (_) | <
  5. * |_.__/|_| \___/ \___/|_|\_\
  6. *
  7. * Microframework which helps to develop web Pascal applications.
  8. *
  9. * Copyright (c) 2012-2021 Silvio Clecio <[email protected]>
  10. *
  11. * Brook framework is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation; either
  14. * version 2.1 of the License, or (at your option) any later version.
  15. *
  16. * Brook framework is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with Brook framework; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. *)
  25. program Test_Logger;
  26. {$I Tests.inc}
  27. uses
  28. RTLConsts,
  29. SysUtils,
  30. Classes,
  31. {$IFNDEF FPC}
  32. IOUtils,
  33. {$ENDIF}
  34. BrookLibraryLoader,
  35. BrookLogger,
  36. Test;
  37. const
  38. FAKE_LOG_FILE_NAME = 'FakeOutput.log';
  39. FAKE_ERR_LOG_FILE_NAME = 'FakeErrOutput.log';
  40. type
  41. TFakeLoggerOutput = class(TBrookLoggerOutput)
  42. public
  43. FakeLevel: string;
  44. FakeMessage: string;
  45. class function GetName: string; override;
  46. procedure Log(const ALevel, AMessage: string); override;
  47. procedure Fail(const ALevel: string; AException: Exception); override;
  48. end;
  49. TFakeLoggerOutputFile = class(TBrookLoggerOutputFile)
  50. public
  51. property LastDate;
  52. end;
  53. class function TFakeLoggerOutput.GetName: string;
  54. begin
  55. Result := 'FakeOutput';
  56. end;
  57. procedure TFakeLoggerOutput.Log(const ALevel, AMessage: string);
  58. begin
  59. Assert(not ALevel.IsEmpty);
  60. Assert(not AMessage.IsEmpty);
  61. FakeLevel := ALevel;
  62. FakeMessage := AMessage;
  63. end;
  64. procedure TFakeLoggerOutput.Fail(const ALevel: string; AException: Exception);
  65. begin
  66. Assert(not ALevel.IsEmpty);
  67. Assert(Assigned(AException));
  68. FakeLevel := ALevel;
  69. FakeMessage := AException.Message;
  70. end;
  71. var
  72. FakeComponent: TComponent;
  73. FakeFilters, FakeOptions: TStringList;
  74. procedure DoLoggerOutputCreateParamIsNilFilters;
  75. begin
  76. TFakeLoggerOutput.Create(nil, FakeOptions);
  77. end;
  78. procedure DoLoggerOutputCreateParamIsNilOptions;
  79. begin
  80. TFakeLoggerOutput.Create(FakeFilters, nil);
  81. end;
  82. procedure Test_LoggerOutputCreate;
  83. var
  84. O: TBrookLoggerOutput;
  85. begin
  86. FakeFilters.Clear;
  87. FakeOptions.Clear;
  88. O := TFakeLoggerOutput.Create(FakeFilters, FakeOptions);
  89. try
  90. Assert(O.Filters = FakeFilters);
  91. Assert(O.Options = FakeOptions);
  92. AssertExcept(DoLoggerOutputCreateParamIsNilFilters, EArgumentNilException,
  93. Format(SParamIsNil, ['AFilters']));
  94. AssertExcept(DoLoggerOutputCreateParamIsNilOptions, EArgumentNilException,
  95. Format(SParamIsNil, ['AOptions']));
  96. finally
  97. O.Free;
  98. end;
  99. end;
  100. procedure Test_LoggerOutputGetRegisterAlias;
  101. begin
  102. Assert(TFakeLoggerOutput.GetRegisterAlias = 'BrookLogger_FakeOutput');
  103. end;
  104. procedure Test_LoggerOutputGetName;
  105. begin
  106. Assert(TFakeLoggerOutput.GetName = 'FakeOutput');
  107. end;
  108. procedure Test_LoggerOutputIsFiltered;
  109. var
  110. O: TBrookLoggerOutput;
  111. begin
  112. FakeFilters.Clear;
  113. FakeOptions.Clear;
  114. O := TFakeLoggerOutput.Create(FakeFilters, FakeOptions);
  115. try
  116. Assert(not O.IsFiltered(''));
  117. Assert(not O.IsFiltered('abc'));
  118. FakeFilters.Add('');
  119. FakeFilters.Add('abc');
  120. FakeFilters.Add('def');
  121. FakeFilters.Add('ghi');
  122. Assert(O.IsFiltered(''));
  123. Assert(O.IsFiltered('def'));
  124. finally
  125. O.Free;
  126. end;
  127. end;
  128. procedure Test_LoggerOutputLog;
  129. var
  130. O: TFakeLoggerOutput;
  131. begin
  132. FakeFilters.Clear;
  133. FakeOptions.Clear;
  134. O := TFakeLoggerOutput.Create(FakeFilters, FakeOptions);
  135. try
  136. O.FakeLevel := '';
  137. O.FakeMessage := '';
  138. Assert(O.FakeLevel = '');
  139. Assert(O.FakeMessage = '');
  140. O.Log('foo', 'bar');
  141. Assert(O.FakeLevel = 'foo');
  142. Assert(O.FakeMessage = 'bar');
  143. finally
  144. O.Free;
  145. end;
  146. end;
  147. procedure Test_LoggerOutputFail;
  148. var
  149. O: TFakeLoggerOutput;
  150. E: Exception;
  151. begin
  152. FakeFilters.Clear;
  153. FakeOptions.Clear;
  154. O := TFakeLoggerOutput.Create(FakeFilters, FakeOptions);
  155. E := Exception.Create('bar');
  156. try
  157. O.FakeLevel := '';
  158. O.FakeMessage := '';
  159. Assert(O.FakeLevel = '');
  160. Assert(O.FakeMessage = '');
  161. O.Fail('foo', E);
  162. Assert(O.FakeLevel = 'foo');
  163. Assert(O.FakeMessage = 'bar');
  164. finally
  165. E.Free;
  166. O.Free;
  167. end;
  168. end;
  169. procedure Test_LoggerOutputFilters;
  170. var
  171. O: TBrookLoggerOutput;
  172. begin
  173. O := TFakeLoggerOutput.Create(FakeFilters, FakeOptions);
  174. try
  175. Assert(O.Filters = FakeFilters);
  176. finally
  177. O.Free;
  178. end;
  179. end;
  180. procedure Test_LoggerOutputOptions;
  181. var
  182. O: TBrookLoggerOutput;
  183. begin
  184. O := TFakeLoggerOutput.Create(FakeFilters, FakeOptions);
  185. try
  186. Assert(O.Options = FakeOptions);
  187. finally
  188. O.Free;
  189. end;
  190. end;
  191. procedure Test_LoggerOutputConsoleGetName;
  192. begin
  193. Assert(TBrookLoggerOutputConsole.GetName = 'Console');
  194. end;
  195. procedure Test_LoggerOutputConsoleLog;
  196. var
  197. O: TBrookLoggerOutput;
  198. S: string;
  199. begin
  200. FakeFilters.Clear;
  201. FakeOptions.Clear;
  202. FakeFilters.Add('test');
  203. O := TBrookLoggerOutputConsole.Create(FakeFilters, FakeOptions);
  204. try
  205. DeleteFile(FAKE_LOG_FILE_NAME);
  206. AssignFile(Output, FAKE_LOG_FILE_NAME);
  207. Rewrite(Output);
  208. O.Log('foo', 'bar');
  209. O.Log('bar', 'foo');
  210. O.Log('test', 'ok');
  211. Flush(Output);
  212. CloseFile(Output);
  213. Reset(Output);
  214. S := '';
  215. ReadLn(Output, S);
  216. Assert(S.SubString(24) = 'foo: bar');
  217. S := '';
  218. ReadLn(Output, S);
  219. Assert(S.SubString(24) = 'bar: foo');
  220. S := '';
  221. ReadLn(Output, S);
  222. Assert(S = '');
  223. CloseFile(Output);
  224. finally
  225. O.Free;
  226. end;
  227. end;
  228. procedure Test_LoggerOutputConsoleFail;
  229. var
  230. O: TBrookLoggerOutput;
  231. E: Exception;
  232. S: string;
  233. begin
  234. FakeFilters.Clear;
  235. FakeOptions.Clear;
  236. O := TBrookLoggerOutputConsole.Create(FakeFilters, FakeOptions);
  237. E := Exception.Create('bar');
  238. try
  239. DeleteFile(FAKE_ERR_LOG_FILE_NAME);
  240. AssignFile(ErrOutput, FAKE_ERR_LOG_FILE_NAME);
  241. Rewrite(ErrOutput);
  242. E.Message := 'bar';
  243. O.Fail('foo', E);
  244. E.Message := 'foo';
  245. O.Fail('bar', E);
  246. Flush(ErrOutput);
  247. CloseFile(ErrOutput);
  248. Reset(ErrOutput);
  249. S := '';
  250. ReadLn(ErrOutput, S);
  251. Assert(S.SubString(24) = 'foo: Exception: bar');
  252. S := '';
  253. ReadLn(ErrOutput, S);
  254. Assert(S.SubString(24) = 'bar: Exception: foo');
  255. S := '';
  256. ReadLn(ErrOutput, S);
  257. Assert(S = '');
  258. CloseFile(ErrOutput);
  259. finally
  260. E.Free;
  261. O.Free;
  262. end;
  263. end;
  264. procedure Test_LoggerOutputFileAfterConstruction;
  265. var
  266. O: TBrookLoggerOutputFile;
  267. begin
  268. FakeOptions.Clear;
  269. FakeOptions.Clear;
  270. O := TBrookLoggerOutputFile.Create(FakeFilters, FakeOptions);
  271. try
  272. Assert(O.Directory = {$IFDEF FPC}GetUserDir{$ELSE}TPath.GetHomePath{$ENDIF});
  273. finally
  274. O.Free;
  275. end;
  276. FakeOptions.Clear;
  277. FakeOptions.Clear;
  278. FakeOptions.Add('directory=foobar');
  279. O := TBrookLoggerOutputFile.Create(FakeFilters, FakeOptions);
  280. try
  281. Assert(O.Directory = 'foobar');
  282. finally
  283. O.Free;
  284. end;
  285. end;
  286. procedure Test_LoggerOutputFileGetName;
  287. begin
  288. Assert(TBrookLoggerOutputFile.GetName = 'File');
  289. end;
  290. procedure Test_LoggerOutputFileLog;
  291. var
  292. O: TBrookLoggerOutputFile;
  293. F: TextFile;
  294. FN: TFileName;
  295. S: string;
  296. begin
  297. FakeOptions.Clear;
  298. FakeOptions.Clear;
  299. FakeFilters.Add('test');
  300. O := TBrookLoggerOutputFile.Create(FakeFilters, FakeOptions);
  301. try
  302. FN := O.FileName;
  303. DeleteFile(FN);
  304. O.Log('foo', 'bar');
  305. O.Log('bar', 'foo');
  306. finally
  307. O.Free;
  308. end;
  309. AssignFile(F, FN);
  310. Reset(F);
  311. S := '';
  312. ReadLn(F, S);
  313. Assert(S.SubString(24) = 'foo: bar');
  314. S := '';
  315. ReadLn(F, S);
  316. Assert(S.SubString(24) = 'bar: foo');
  317. S := '';
  318. ReadLn(F, S);
  319. Assert(S = '');
  320. CloseFile(F);
  321. end;
  322. procedure Test_LoggerOutputFileFail;
  323. var
  324. O: TBrookLoggerOutputFile;
  325. E: Exception;
  326. F: TextFile;
  327. FN: TFileName;
  328. S: string;
  329. begin
  330. FakeOptions.Clear;
  331. FakeOptions.Clear;
  332. O := TBrookLoggerOutputFile.Create(FakeFilters, FakeOptions);
  333. E := Exception.Create('bar');
  334. try
  335. FN := O.FileName;
  336. DeleteFile(FN);
  337. E.Message := 'bar';
  338. O.Fail('foo', E);
  339. E.Message := 'foo';
  340. O.Fail('bar', E);
  341. finally
  342. E.Free;
  343. O.Free;
  344. end;
  345. AssignFile(F, FN);
  346. Reset(F);
  347. S := '';
  348. ReadLn(F, S);
  349. Assert(S.SubString(24) = 'foo: Exception: bar');
  350. S := '';
  351. ReadLn(F, S);
  352. Assert(S.SubString(24) = 'bar: Exception: foo');
  353. CloseFile(F);
  354. end;
  355. procedure Test_LoggerOutputFileDirectory;
  356. var
  357. O: TFakeLoggerOutputFile;
  358. begin
  359. FakeFilters.Clear;
  360. FakeOptions.Clear;
  361. O := TFakeLoggerOutputFile.Create(FakeFilters, FakeOptions);
  362. try
  363. O.Log('foo', 'bar');
  364. Assert(O.LastDate > 0);
  365. O.Directory := 'foo';
  366. Assert(O.Directory = 'foo');
  367. Assert(O.LastDate = 0);
  368. finally
  369. O.Free;
  370. end;
  371. end;
  372. procedure Test_LoggerOutputFileFileName;
  373. var
  374. O: TFakeLoggerOutputFile;
  375. begin
  376. FakeFilters.Clear;
  377. FakeOptions.Clear;
  378. O := TFakeLoggerOutputFile.Create(FakeFilters, FakeOptions);
  379. try
  380. Assert(O.FileName = Concat(IncludeTrailingPathDelimiter(O.Directory),
  381. ChangeFileExt(ExtractFileName(ParamStr(0)), ''), '_',
  382. FormatDateTime('yyyymmdd', Date), '.log'));
  383. finally
  384. O.Free;
  385. end;
  386. end;
  387. procedure Test_LoggerLevelsCreate;
  388. var
  389. L: TBrookLoggerLevels;
  390. begin
  391. L := TBrookLoggerLevels.Create;
  392. try
  393. Assert(L.Info = SBrookLevelInfo);
  394. Assert(L.Hint = SBrookLevelHint);
  395. Assert(L.Warn = SBrookLevelWarn);
  396. Assert(L.Debug = SBrookLevelDebug);
  397. Assert(L.Error = SBrookLevelError);
  398. finally
  399. L.Free;
  400. end;
  401. end;
  402. procedure Test_LoggerLevelsAssign;
  403. var
  404. S, D: TBrookLoggerLevels;
  405. begin
  406. S := TBrookLoggerLevels.Create;
  407. D := TBrookLoggerLevels.Create;
  408. try
  409. Assert(D.Info = SBrookLevelInfo);
  410. Assert(D.Hint = SBrookLevelHint);
  411. Assert(D.Warn = SBrookLevelWarn);
  412. Assert(D.Debug = SBrookLevelDebug);
  413. Assert(D.Error = SBrookLevelError);
  414. S.Info := 'FakeInfo';
  415. S.Hint := 'FakeHint';
  416. S.Warn := 'FakeWarn';
  417. S.Debug := 'FakeDebug';
  418. S.Error := 'FakeError';
  419. D.Assign(S);
  420. Assert(D.Info = 'FakeInfo');
  421. Assert(D.Hint = 'FakeHint');
  422. Assert(D.Warn = 'FakeWarn');
  423. Assert(D.Debug = 'FakeDebug');
  424. Assert(D.Error = 'FakeError');
  425. finally
  426. S.Free;
  427. D.Free;
  428. end;
  429. end;
  430. procedure Test_LoggerCreate;
  431. var
  432. L: TBrookLogger;
  433. begin
  434. L := TBrookLogger.Create(nil);
  435. try
  436. Assert(not Assigned(L.Owner));
  437. finally
  438. L.Free;
  439. end;
  440. L := TBrookLogger.Create(FakeComponent);
  441. try
  442. Assert(L.Owner = FakeComponent);
  443. Assert(Assigned(L.Levels));
  444. Assert(Assigned(L.Filters));
  445. Assert(Assigned(L.Options));
  446. Assert(L.OutputName = BROOK_LOGGER_OUTPUT_NAME);
  447. finally
  448. L.Free;
  449. end;
  450. end;
  451. procedure DoLoggerGetOutputClassUnknownOutputName(const AArgs: array of const);
  452. begin
  453. TBrookLogger(AArgs[0].VObject).GetOutputClass;
  454. end;
  455. procedure DoLoggerGetOutputClassInvalidOutputClass(const AArgs: array of const);
  456. begin
  457. TBrookLogger(AArgs[0].VObject).GetOutputClass;
  458. end;
  459. procedure Test_LoggerGetOutputClass;
  460. var
  461. L: TBrookLogger;
  462. begin
  463. L := TBrookLogger.Create(nil);
  464. try
  465. Assert(L.GetOutputClass = TBrookLoggerOutputConsole);
  466. L.OutputName := 'test';
  467. AssertExcept(DoLoggerGetOutputClassUnknownOutputName, EClassNotFound,
  468. Format(SBrookUnknownOutputName, ['test']), [L]);
  469. RegisterClassAlias(TBrookLoggerLevels, Concat(BROOK_LOGGER_TAG, 'test'));
  470. AssertExcept(DoLoggerGetOutputClassInvalidOutputClass, EInvalidCast,
  471. Format(SBrookInvalidOutputClass, [TBrookLoggerLevels.ClassName]), [L]);
  472. finally
  473. L.Free;
  474. end;
  475. end;
  476. procedure Test_LoggerOpen;
  477. var
  478. L: TBrookLogger;
  479. begin
  480. L := TBrookLogger.Create(nil);
  481. try
  482. Assert(not L.Active);
  483. L.Open;
  484. Assert(L.Active);
  485. L.Open;
  486. finally
  487. L.Free;
  488. end;
  489. end;
  490. procedure Test_LoggerClose;
  491. var
  492. L: TBrookLogger;
  493. begin
  494. L := TBrookLogger.Create(nil);
  495. try
  496. L.Open;
  497. Assert(L.Active);
  498. L.Close;
  499. Assert(not L.Active);
  500. L.Close;
  501. finally
  502. L.Free;
  503. end;
  504. end;
  505. procedure Test_LoggerLog;
  506. var
  507. L: TBrookLogger;
  508. S: string;
  509. begin
  510. L := TBrookLogger.Create(nil);
  511. try
  512. DeleteFile(FAKE_LOG_FILE_NAME);
  513. AssignFile(Output, FAKE_LOG_FILE_NAME);
  514. Rewrite(Output);
  515. L.Log('test', 'ok');
  516. L.Open;
  517. L.Filters.Add('test');
  518. L.Log('foo', 'bar');
  519. L.Log('bar', 'foo');
  520. L.Log('test', 'ok');
  521. Flush(Output);
  522. CloseFile(Output);
  523. Reset(Output);
  524. S := '';
  525. ReadLn(Output, S);
  526. Assert(S.SubString(24) = 'foo: bar');
  527. S := '';
  528. ReadLn(Output, S);
  529. Assert(S.SubString(24) = 'bar: foo');
  530. S := '';
  531. ReadLn(Output, S);
  532. Assert(S = '');
  533. CloseFile(Output);
  534. finally
  535. L.Free;
  536. end;
  537. end;
  538. procedure Test_LoggerFail;
  539. var
  540. L: TBrookLogger;
  541. E: Exception;
  542. S: string;
  543. begin
  544. FakeFilters.Clear;
  545. FakeOptions.Clear;
  546. L := TBrookLogger.Create(nil);
  547. E := Exception.Create('bar');
  548. try
  549. DeleteFile(FAKE_ERR_LOG_FILE_NAME);
  550. AssignFile(ErrOutput, FAKE_ERR_LOG_FILE_NAME);
  551. Rewrite(ErrOutput);
  552. E.Message := 'ok';
  553. L.Fail('test', E);
  554. L.Open;
  555. E.Message := 'bar';
  556. L.Fail('foo', E);
  557. E.Message := 'foo';
  558. L.Fail('bar', E);
  559. Flush(ErrOutput);
  560. CloseFile(ErrOutput);
  561. Reset(ErrOutput);
  562. S := '';
  563. ReadLn(ErrOutput, S);
  564. Assert(S.SubString(24) = 'foo: Exception: bar');
  565. S := '';
  566. ReadLn(ErrOutput, S);
  567. Assert(S.SubString(24) = 'bar: Exception: foo');
  568. S := '';
  569. ReadLn(ErrOutput, S);
  570. Assert(S = '');
  571. CloseFile(ErrOutput);
  572. finally
  573. E.Free;
  574. L.Free;
  575. end;
  576. end;
  577. procedure Test_LoggerInfo;
  578. var
  579. L: TBrookLogger;
  580. S: string;
  581. begin
  582. L := TBrookLogger.Create(nil);
  583. try
  584. L.Open;
  585. DeleteFile(FAKE_LOG_FILE_NAME);
  586. AssignFile(Output, FAKE_LOG_FILE_NAME);
  587. Rewrite(Output);
  588. L.Open;
  589. L.Info('test');
  590. L.Filters.Clear;
  591. L.Close;
  592. L.Info('test');
  593. L.Open;
  594. L.Info('foo');
  595. L.Info('bar');
  596. L.Filters.Add(SBrookLevelInfo);
  597. L.Info('test');
  598. Flush(Output);
  599. CloseFile(Output);
  600. Reset(Output);
  601. S := '';
  602. ReadLn(Output, S);
  603. Assert(S.SubString(24) = Concat(SBrookLevelInfo, ': foo'));
  604. S := '';
  605. ReadLn(Output, S);
  606. Assert(S.SubString(24) = Concat(SBrookLevelInfo, ': bar'));
  607. S := '';
  608. ReadLn(Output, S);
  609. Assert(S = '');
  610. CloseFile(Output);
  611. finally
  612. L.Free;
  613. end;
  614. end;
  615. procedure Test_LoggerHint;
  616. var
  617. L: TBrookLogger;
  618. S: string;
  619. begin
  620. L := TBrookLogger.Create(nil);
  621. try
  622. L.Open;
  623. DeleteFile(FAKE_LOG_FILE_NAME);
  624. AssignFile(Output, FAKE_LOG_FILE_NAME);
  625. Rewrite(Output);
  626. L.Hint('test');
  627. L.Filters.Clear;
  628. L.Close;
  629. L.Hint('test');
  630. L.Open;
  631. L.Hint('foo');
  632. L.Hint('bar');
  633. L.Filters.Add(SBrookLevelHint);
  634. L.Hint('test');
  635. Flush(Output);
  636. CloseFile(Output);
  637. Reset(Output);
  638. S := '';
  639. ReadLn(Output, S);
  640. Assert(S.SubString(24) = Concat(SBrookLevelHint, ': foo'));
  641. S := '';
  642. ReadLn(Output, S);
  643. Assert(S.SubString(24) = Concat(SBrookLevelHint, ': bar'));
  644. S := '';
  645. ReadLn(Output, S);
  646. Assert(S = '');
  647. CloseFile(Output);
  648. finally
  649. L.Free;
  650. end;
  651. end;
  652. procedure Test_LoggerWarn;
  653. var
  654. L: TBrookLogger;
  655. S: string;
  656. begin
  657. L := TBrookLogger.Create(nil);
  658. try
  659. L.Open;
  660. DeleteFile(FAKE_LOG_FILE_NAME);
  661. AssignFile(Output, FAKE_LOG_FILE_NAME);
  662. Rewrite(Output);
  663. L.Filters.Clear;
  664. L.Close;
  665. L.Warn('test');
  666. L.Open;
  667. L.Warn('foo');
  668. L.Warn('bar');
  669. L.Filters.Add(SBrookLevelWarn);
  670. L.Warn('test');
  671. Flush(Output);
  672. CloseFile(Output);
  673. Reset(Output);
  674. S := '';
  675. ReadLn(Output, S);
  676. Assert(S.SubString(24) = Concat(SBrookLevelWarn, ': foo'));
  677. S := '';
  678. ReadLn(Output, S);
  679. Assert(S.SubString(24) = Concat(SBrookLevelWarn, ': bar'));
  680. S := '';
  681. ReadLn(Output, S);
  682. Assert(S = '');
  683. CloseFile(Output);
  684. finally
  685. L.Free;
  686. end;
  687. end;
  688. procedure Test_LoggerDebug;
  689. var
  690. L: TBrookLogger;
  691. S: string;
  692. begin
  693. L := TBrookLogger.Create(nil);
  694. try
  695. L.Open;
  696. DeleteFile(FAKE_LOG_FILE_NAME);
  697. AssignFile(Output, FAKE_LOG_FILE_NAME);
  698. Rewrite(Output);
  699. L.Debug('test');
  700. L.Filters.Clear;
  701. L.Close;
  702. L.Debug('test');
  703. L.Open;
  704. L.Debug('foo');
  705. L.Debug('bar');
  706. L.Filters.Add(SBrookLevelDebug);
  707. L.Debug('test');
  708. Flush(Output);
  709. CloseFile(Output);
  710. Reset(Output);
  711. S := '';
  712. ReadLn(Output, S);
  713. Assert(S.SubString(24) = Concat(SBrookLevelDebug, ': foo'));
  714. S := '';
  715. ReadLn(Output, S);
  716. Assert(S.SubString(24) = Concat(SBrookLevelDebug, ': bar'));
  717. S := '';
  718. ReadLn(Output, S);
  719. Assert(S = '');
  720. CloseFile(Output);
  721. finally
  722. L.Free;
  723. end;
  724. end;
  725. procedure Test_LoggerError;
  726. var
  727. L: TBrookLogger;
  728. E1, E2, E3: Exception;
  729. S: string;
  730. begin
  731. L := TBrookLogger.Create(nil);
  732. E1 := Exception.Create('test');
  733. E2 := Exception.Create('foo');
  734. E3 := Exception.Create('bar');
  735. try
  736. L.Open;
  737. DeleteFile(FAKE_ERR_LOG_FILE_NAME);
  738. AssignFile(ErrOutput, FAKE_ERR_LOG_FILE_NAME);
  739. Rewrite(ErrOutput);
  740. L.Filters.Clear;
  741. L.Close;
  742. L.Error(E1);
  743. L.Open;
  744. L.Error(E2);
  745. L.Error(E3);
  746. L.Filters.Add(SBrookLevelError);
  747. L.Error(E1);
  748. Flush(ErrOutput);
  749. CloseFile(ErrOutput);
  750. Reset(ErrOutput);
  751. S := '';
  752. ReadLn(ErrOutput, S);
  753. Assert(S.SubString(24) = Concat(SBrookLevelError, ': Exception: foo'));
  754. S := '';
  755. ReadLn(ErrOutput, S);
  756. Assert(S.SubString(24) = Concat(SBrookLevelError, ': Exception: bar'));
  757. S := '';
  758. ReadLn(ErrOutput, S);
  759. Assert(S = '');
  760. CloseFile(ErrOutput);
  761. finally
  762. E3.Free;
  763. E2.Free;
  764. E1.Free;
  765. L.Free;
  766. end;
  767. end;
  768. procedure DoLoggerOutputInactiveOutput(const AArgs: array of const);
  769. begin
  770. TBrookLogger(AArgs[0].VObject).Output;
  771. end;
  772. procedure Test_LoggerOutput;
  773. var
  774. L: TBrookLogger;
  775. begin
  776. L := TBrookLogger.Create(nil);
  777. try
  778. L.Open;
  779. Assert(Assigned(L.Output));
  780. L.Close;
  781. AssertExcept(DoLoggerOutputInactiveOutput, EInvalidOpException,
  782. SBrookInactiveOutput, [L]);
  783. finally
  784. L.Free;
  785. end;
  786. end;
  787. procedure Test_LoggerActive;
  788. var
  789. L: TBrookLogger;
  790. begin
  791. L := TBrookLogger.Create(nil);
  792. try
  793. Assert(not L.Active);
  794. L.Active := not L.Active;
  795. Assert(L.Active);
  796. finally
  797. L.Free;
  798. end;
  799. end;
  800. procedure Test_LoggerLevels;
  801. var
  802. L: TBrookLogger;
  803. begin
  804. L := TBrookLogger.Create(nil);
  805. try
  806. Assert(Assigned(L.Levels));
  807. finally
  808. L.Free;
  809. end;
  810. end;
  811. procedure Test_LoggerOutputName;
  812. var
  813. L: TBrookLogger;
  814. begin
  815. L := TBrookLogger.Create(nil);
  816. try
  817. Assert(L.OutputName = 'Console');
  818. L.OutputName := 'File';
  819. Assert(L.OutputName = 'File');
  820. L.OutputName := '';
  821. Assert(L.OutputName = 'Console');
  822. finally
  823. L.Free;
  824. end;
  825. end;
  826. procedure Test_LoggerFilters;
  827. var
  828. L: TBrookLogger;
  829. begin
  830. L := TBrookLogger.Create(nil);
  831. try
  832. Assert(Assigned(L.Filters));
  833. Assert(L.Filters.Count = 3);
  834. Assert(L.Filters.IndexOf(SBrookLevelInfo) > -1);
  835. Assert(L.Filters.IndexOf(SBrookLevelHint) > -1);
  836. Assert(L.Filters.IndexOf(SBrookLevelDebug) > -1);
  837. finally
  838. L.Free;
  839. end;
  840. end;
  841. procedure Test_LoggerOptions;
  842. var
  843. L: TBrookLogger;
  844. begin
  845. L := TBrookLogger.Create(nil);
  846. try
  847. Assert(Assigned(L.Options));
  848. Assert(L.Options.Count = 0);
  849. finally
  850. L.Free;
  851. end;
  852. end;
  853. begin
  854. {$IF (NOT DEFINED(FPC)) AND DEFINED(DEBUG)}
  855. ReportMemoryLeaksOnShutdown := True;
  856. {$ENDIF}
  857. TBrookLibraryLoader.Load;
  858. FakeComponent := TComponent.Create(nil);
  859. FakeFilters := TStringList.Create;
  860. FakeOptions := TStringList.Create;
  861. try
  862. Test_LoggerOutputCreate;
  863. Test_LoggerOutputGetRegisterAlias;
  864. Test_LoggerOutputGetName;
  865. Test_LoggerOutputIsFiltered;
  866. Test_LoggerOutputLog;
  867. Test_LoggerOutputFail;
  868. Test_LoggerOutputFilters;
  869. Test_LoggerOutputOptions;
  870. Test_LoggerOutputConsoleGetName;
  871. Test_LoggerOutputConsoleLog;
  872. Test_LoggerOutputConsoleFail;
  873. Test_LoggerOutputFileAfterConstruction;
  874. // Test_LoggerOutputFileDestroy - not required
  875. Test_LoggerOutputFileGetName;
  876. Test_LoggerOutputFileLog;
  877. Test_LoggerOutputFileFail;
  878. Test_LoggerOutputFileDirectory;
  879. Test_LoggerOutputFileFileName;
  880. Test_LoggerLevelsCreate;
  881. Test_LoggerLevelsAssign;
  882. // Test_LoggerDestroy - not required
  883. Test_LoggerCreate;
  884. Test_LoggerGetOutputClass;
  885. Test_LoggerOpen;
  886. Test_LoggerClose;
  887. Test_LoggerLog;
  888. Test_LoggerFail;
  889. Test_LoggerInfo;
  890. Test_LoggerHint;
  891. Test_LoggerWarn;
  892. Test_LoggerDebug;
  893. Test_LoggerError;
  894. Test_LoggerOutput;
  895. Test_LoggerActive;
  896. Test_LoggerLevels;
  897. Test_LoggerOutputName;
  898. Test_LoggerFilters;
  899. Test_LoggerOptions;
  900. finally
  901. FakeFilters.Free;
  902. FakeOptions.Free;
  903. FakeComponent.Free;
  904. TBrookLibraryLoader.Unload;
  905. end;
  906. end.