tcpassrcutil.pas 11 KB

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