tcpassrcutil.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. unit tcpassrcutil;
  2. {$mode objfpc}{$H+}
  3. {$DEFINE NOCONSOLE}
  4. interface
  5. uses
  6. Classes, SysUtils, fpcunit,passrcutil, testregistry;
  7. type
  8. { TPasSrcUtilTest }
  9. TPasSrcUtilTest= class(TTestCase)
  10. Protected
  11. FAnalyser : TPasSrcAnalysis;
  12. FSrc : TStrings;
  13. FList : TStrings;
  14. FStream: TMemoryStream;
  15. protected
  16. procedure SetUp; override;
  17. procedure TearDown; override;
  18. Procedure AddLine(Const ALine : String);
  19. Procedure AddUses(Const AUsesList : String);
  20. Procedure StartUnit;
  21. Procedure StartImplementation;
  22. Procedure EndSource;
  23. Procedure AssertList(Msg : String; Els : Array of string);
  24. Property Analyser : TPasSrcAnalysis Read FAnalyser;
  25. Property List : TStrings Read FList;
  26. published
  27. procedure TestGetInterfaceUses;
  28. procedure TestGetInterfaceUsesEmpty;
  29. procedure TestGetImplementationUses;
  30. procedure TestGetImplementationUsesEmpty;
  31. procedure TestGetAllUses;
  32. procedure TestGetInterfaceIdentifiers;
  33. procedure TestGetInterfaceVarIdentifiers;
  34. procedure TestGetInterface2VarIdentifiers;
  35. procedure TestGetInterfaceConstIdentifiers;
  36. procedure TestGetInterface2ConstsIdentifiers;
  37. procedure TestGetInterfaceTypeIdentifiers;
  38. procedure TestGetInterface2TypeIdentifiers;
  39. procedure TestGetInterfaceProcIdentifiers;
  40. procedure TestGetInterfaceResourcestringIdentifiers;
  41. procedure TestGetInterfaceEnumTypeIdentifiersNoRecurse;
  42. procedure TestGetInterfaceEnumTypeIdentifiersRecurse;
  43. procedure TestGetInterfaceRecordTypeIdentifiersNoRecurse;
  44. procedure TestGetInterfaceRecordTypeIdentifiersRecurse;
  45. procedure TestGetInterfaceRecordTypeIdentifiersRecurseVariant;
  46. procedure TestGetInterfaceClassTypeIdentifiersNoRecurse;
  47. procedure TestGetInterfaceClassTypeIdentifiersRecurse;
  48. procedure TestGetImplementationVarIdentifiers;
  49. procedure TestInterfaceHasResourceStrings;
  50. procedure TestInterfaceHasResourceStringsFalse;
  51. procedure TestImplementationHasResourceStrings;
  52. procedure TestHasResourceStrings;
  53. procedure TestHasResourceStrings2;
  54. procedure TestHasResourceStrings3;
  55. procedure TestHasResourceStrings4;
  56. end;
  57. implementation
  58. procedure TPasSrcUtilTest.TestGetInterfaceUses;
  59. begin
  60. StartUnit;
  61. AddUses('a,b,c');
  62. StartImplementation;
  63. EndSource;
  64. Analyser.GetInterfaceUnits(List);
  65. AssertList('4 interface units',['System','a','b','c']);
  66. end;
  67. procedure TPasSrcUtilTest.TestGetInterfaceUsesEmpty;
  68. begin
  69. StartUnit;
  70. StartImplementation;
  71. EndSource;
  72. Analyser.GetInterfaceUnits(List);
  73. AssertList('1 interface unit',['System']);
  74. end;
  75. procedure TPasSrcUtilTest.TestGetImplementationUses;
  76. begin
  77. StartUnit;
  78. StartImplementation;
  79. AddUses('d,a,b,c');
  80. EndSource;
  81. Analyser.GetImplementationUnits(List);
  82. AssertList('4 implementation units',['d','a','b','c']);
  83. end;
  84. procedure TPasSrcUtilTest.TestGetImplementationUsesEmpty;
  85. begin
  86. StartUnit;
  87. StartImplementation;
  88. EndSource;
  89. Analyser.GetImplementationUnits(List);
  90. AssertList('0 implementation units',[]);
  91. end;
  92. procedure TPasSrcUtilTest.TestGetAllUses;
  93. begin
  94. StartUnit;
  95. AddUses('a,b,c');
  96. StartImplementation;
  97. AddUses('d,e');
  98. EndSource;
  99. Analyser.GetUsedUnits(List);
  100. AssertList('6 units',['System','a','b','c','d','e']);
  101. end;
  102. procedure TPasSrcUtilTest.TestGetInterfaceIdentifiers;
  103. begin
  104. StartUnit;
  105. StartImplementation;
  106. EndSource;
  107. Analyser.GetInterfaceIdentifiers(List);
  108. AssertList('0 identifiers',[]);
  109. end;
  110. procedure TPasSrcUtilTest.TestGetInterfaceVarIdentifiers;
  111. begin
  112. StartUnit;
  113. AddLine('Var a : integer;');
  114. StartImplementation;
  115. EndSource;
  116. Analyser.GetInterfaceIdentifiers(List);
  117. AssertList('1 identifiers',['a']);
  118. end;
  119. procedure TPasSrcUtilTest.TestGetInterface2VarIdentifiers;
  120. begin
  121. StartUnit;
  122. AddLine('Var a,b : integer;');
  123. StartImplementation;
  124. EndSource;
  125. Analyser.GetInterfaceIdentifiers(List);
  126. AssertList('2 identifiers',['a','b']);
  127. end;
  128. procedure TPasSrcUtilTest.TestGetInterfaceConstIdentifiers;
  129. begin
  130. StartUnit;
  131. AddLine('Const a = 123;');
  132. StartImplementation;
  133. EndSource;
  134. Analyser.GetInterfaceIdentifiers(List);
  135. AssertList('1 identifiers',['a']);
  136. end;
  137. procedure TPasSrcUtilTest.TestGetInterface2ConstsIdentifiers;
  138. begin
  139. StartUnit;
  140. AddLine('Const a = 123;');
  141. AddLine(' b = 123;');
  142. StartImplementation;
  143. EndSource;
  144. Analyser.GetInterfaceIdentifiers(List);
  145. AssertList('2 identifiers',['a','b']);
  146. end;
  147. procedure TPasSrcUtilTest.TestGetInterfaceTypeIdentifiers;
  148. begin
  149. StartUnit;
  150. AddLine('Type a = Integer;');
  151. StartImplementation;
  152. EndSource;
  153. Analyser.GetInterfaceIdentifiers(List);
  154. AssertList('1 identifiers',['a']);
  155. end;
  156. procedure TPasSrcUtilTest.TestGetInterface2TypeIdentifiers;
  157. begin
  158. StartUnit;
  159. AddLine('Type a = Integer;');
  160. AddLine(' b = Word;');
  161. StartImplementation;
  162. EndSource;
  163. Analyser.GetInterfaceIdentifiers(List);
  164. AssertList('2 identifiers',['a','b']);
  165. end;
  166. procedure TPasSrcUtilTest.TestGetInterfaceProcIdentifiers;
  167. begin
  168. StartUnit;
  169. AddLine('Procedure a (b : Integer);');
  170. StartImplementation;
  171. EndSource;
  172. Analyser.GetInterfaceIdentifiers(List);
  173. AssertList('1 identifiers',['a']);
  174. end;
  175. procedure TPasSrcUtilTest.TestGetInterfaceResourcestringIdentifiers;
  176. begin
  177. StartUnit;
  178. AddLine('Resourcestring astring = ''Something'';');
  179. StartImplementation;
  180. EndSource;
  181. Analyser.GetInterfaceIdentifiers(List);
  182. AssertList('1 identifiers',['astring']);
  183. end;
  184. procedure TPasSrcUtilTest.TestGetInterfaceEnumTypeIdentifiersNoRecurse;
  185. begin
  186. StartUnit;
  187. AddLine('Type aenum = (one,two,three);');
  188. StartImplementation;
  189. EndSource;
  190. Analyser.GetInterfaceIdentifiers(List);
  191. AssertList('1 identifiers',['aenum']);
  192. end;
  193. procedure TPasSrcUtilTest.TestGetInterfaceEnumTypeIdentifiersRecurse;
  194. begin
  195. StartUnit;
  196. AddLine('Type aenum = (one,two,three);');
  197. StartImplementation;
  198. EndSource;
  199. Analyser.GetInterfaceIdentifiers(List,True);
  200. AssertList('4 identifiers',['aenum','aenum.one','aenum.two','aenum.three']);
  201. end;
  202. procedure TPasSrcUtilTest.TestGetInterfaceRecordTypeIdentifiersNoRecurse;
  203. begin
  204. StartUnit;
  205. AddLine('Type arec = record one,two,three : integer; end;');
  206. StartImplementation;
  207. EndSource;
  208. Analyser.GetInterfaceIdentifiers(List,False);
  209. AssertList('1 identifier',['arec']);
  210. end;
  211. procedure TPasSrcUtilTest.TestGetInterfaceRecordTypeIdentifiersRecurse;
  212. begin
  213. StartUnit;
  214. AddLine('Type arec = record one,two,three : integer; end;');
  215. StartImplementation;
  216. EndSource;
  217. Analyser.GetInterfaceIdentifiers(List,True);
  218. AssertList('4 identifiers',['arec','arec.one','arec.two','arec.three']);
  219. end;
  220. procedure TPasSrcUtilTest.TestGetInterfaceRecordTypeIdentifiersRecurseVariant;
  221. begin
  222. StartUnit;
  223. AddLine('Type arec = record one,two,three : integer; case integer of 1: (x : integer;); end;');
  224. StartImplementation;
  225. EndSource;
  226. Analyser.GetInterfaceIdentifiers(List,True);
  227. AssertList('4 identifiers',['arec','arec.one','arec.two','arec.three','arec.x']);
  228. end;
  229. procedure TPasSrcUtilTest.TestGetInterfaceClassTypeIdentifiersNoRecurse;
  230. begin
  231. StartUnit;
  232. AddLine('Type TMyClass = Class');
  233. AddLine(' one,two,three : integer;');
  234. AddLine('end;');
  235. StartImplementation;
  236. EndSource;
  237. Analyser.GetInterfaceIdentifiers(List,False);
  238. AssertList('4 identifiers',['TMyClass']);
  239. end;
  240. procedure TPasSrcUtilTest.TestGetInterfaceClassTypeIdentifiersRecurse;
  241. begin
  242. StartUnit;
  243. AddLine('Type TMyClass = Class');
  244. AddLine(' one,two,three : integer;');
  245. AddLine('end;');
  246. StartImplementation;
  247. EndSource;
  248. Analyser.GetInterfaceIdentifiers(List,True);
  249. AssertList('4 identifiers',['TMyClass','TMyClass.one','TMyClass.two','TMyClass.three']);
  250. end;
  251. procedure TPasSrcUtilTest.TestGetImplementationVarIdentifiers;
  252. begin
  253. StartUnit;
  254. StartImplementation;
  255. AddLine('Var a : integer;');
  256. EndSource;
  257. Analyser.GetImplementationIdentifiers(List);
  258. AssertList('1 identifiers',['a']);
  259. end;
  260. procedure TPasSrcUtilTest.TestInterfaceHasResourceStrings;
  261. begin
  262. StartUnit;
  263. AddLine('Resourcestring astring = ''Something'';');
  264. StartImplementation;
  265. EndSource;
  266. AssertEquals('Have res. strings',True,Analyser.InterfaceHasResourcestrings)
  267. end;
  268. procedure TPasSrcUtilTest.TestInterfaceHasResourceStringsFalse;
  269. begin
  270. StartUnit;
  271. StartImplementation;
  272. AddLine('Resourcestring astring = ''Something'';');
  273. EndSource;
  274. AssertEquals('Have no res. strings',False,Analyser.InterfaceHasResourcestrings)
  275. end;
  276. procedure TPasSrcUtilTest.TestImplementationHasResourceStrings;
  277. begin
  278. StartUnit;
  279. StartImplementation;
  280. AddLine('Resourcestring astring = ''Something'';');
  281. EndSource;
  282. AssertEquals('Have res. strings',True,Analyser.ImplementationHasResourcestrings)
  283. end;
  284. procedure TPasSrcUtilTest.TestHasResourceStrings;
  285. begin
  286. StartUnit;
  287. StartImplementation;
  288. EndSource;
  289. AssertEquals('No res. strings',False,Analyser.ImplementationHasResourcestrings)
  290. end;
  291. procedure TPasSrcUtilTest.TestHasResourceStrings2;
  292. begin
  293. StartUnit;
  294. AddLine('Resourcestring astring = ''Something'';');
  295. StartImplementation;
  296. EndSource;
  297. AssertEquals('Have res. strings',True,Analyser.HasResourcestrings)
  298. end;
  299. procedure TPasSrcUtilTest.TestHasResourceStrings3;
  300. begin
  301. StartUnit;
  302. AddLine('Resourcestring astring = ''Something'';');
  303. StartImplementation;
  304. EndSource;
  305. AssertEquals('Have res. strings',True,Analyser.HasResourcestrings)
  306. end;
  307. procedure TPasSrcUtilTest.TestHasResourceStrings4;
  308. begin
  309. StartUnit;
  310. AddLine('Resourcestring astring = ''Something'';');
  311. StartImplementation;
  312. AddLine('Resourcestring astring2 = ''Something'';');
  313. EndSource;
  314. AssertEquals('Have res. strings',True,Analyser.HasResourcestrings)
  315. end;
  316. procedure TPasSrcUtilTest.SetUp;
  317. begin
  318. FAnalyser:=TPasSrcAnalysis.Create(Nil);
  319. FSrc:=TStringList.Create;
  320. FList:=TStringList.Create;
  321. FStream:=TMemoryStream.Create;
  322. FAnalyser.FileName:='atest.pp';
  323. FAnalyser.Stream:=FStream;
  324. end;
  325. procedure TPasSrcUtilTest.TearDown;
  326. begin
  327. FreeAndNil(FAnalyser);
  328. FreeAndNil(FStream);
  329. FreeAndNil(FSrc);
  330. FreeAndNil(FList);
  331. end;
  332. procedure TPasSrcUtilTest.AddLine(const ALine: String);
  333. begin
  334. FSrc.Add(ALine);
  335. end;
  336. procedure TPasSrcUtilTest.AddUses(const AUsesList: String);
  337. begin
  338. AddLine('uses '+AUseslist+';');
  339. AddLine('');
  340. end;
  341. procedure TPasSrcUtilTest.StartUnit;
  342. begin
  343. AddLine('unit atest;');
  344. AddLine('');
  345. AddLine('Interface');
  346. AddLine('');
  347. end;
  348. procedure TPasSrcUtilTest.StartImplementation;
  349. begin
  350. AddLine('');
  351. AddLine('Implementation');
  352. AddLine('');
  353. end;
  354. procedure TPasSrcUtilTest.EndSource;
  355. begin
  356. AddLine('');
  357. AddLine('end.');
  358. {$IFNDEF PAS2JS}
  359. FSrc.SaveToStream(FStream);
  360. {$ENDIF}
  361. FStream.Position:=0;
  362. {$IFNDEF NOCONSOLE}
  363. Writeln('// Test name : ',Self.TestName);
  364. Writeln(FSrc.Text);
  365. {$ENDIF}
  366. end;
  367. procedure TPasSrcUtilTest.AssertList(Msg: String; Els: array of string);
  368. Var
  369. I : Integer;
  370. begin
  371. AssertEquals(Msg+': number of elements',Length(Els),List.Count);
  372. For I:=Low(Els) to High(Els) do
  373. AssertEquals(Msg+': list element '+IntToStr(i)+' matches : ',Els[i],List[i]);
  374. end;
  375. initialization
  376. RegisterTest(TPasSrcUtilTest);
  377. end.