You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1492 lines
73 KiB

5 years ago
  1. import { beautify, signAlignSettings } from "../VHDLFormatter";
  2. import { NewLineSettings } from "../VHDLFormatter";
  3. import { BeautifierSettings } from "../VHDLFormatter";
  4. import { RemoveAsserts } from "../VHDLFormatter";
  5. import { ApplyNoNewLineAfter } from "../VHDLFormatter";
  6. import { SetNewLinesAfterSymbols } from "../VHDLFormatter";
  7. import { beautify3 } from "../VHDLFormatter";
  8. import { FormattedLine } from "../VHDLFormatter";
  9. import { FormattedLineToString } from "../VHDLFormatter";
  10. import { CompareString } from "./assert";
  11. import { assert } from "./assert";
  12. let testCount: number = 0;
  13. var showUnitTests = true;//window.location.href.indexOf("http") < 0;
  14. if (showUnitTests) {
  15. //IntegrationTest84();
  16. testCount = 0;
  17. IntegrationTest();
  18. UnitTestRemoveAsserts();
  19. UnitTestApplyNoNewLineAfter();
  20. UnitTestSetNewLinesAfterSymbols();
  21. UnitTestFormattedLineToString();
  22. UnitTestbeautify3();
  23. console.log("total tests: " + testCount);
  24. }
  25. interface Function {
  26. readonly name: string;
  27. }
  28. function UnitTestFormattedLineToString() {
  29. console.log("=== FormattedLineToString ===");
  30. FormattedLineToStringCase1();
  31. FormattedLineToStringCase2();
  32. }
  33. function FormattedLineToStringCase1() {
  34. let inputs: (FormattedLine | FormattedLine[])[] = [
  35. new FormattedLine("a;", 0),
  36. new FormattedLine("b;", 0)];
  37. let expected: Array<string> = ["a;", "b;"];
  38. UnitTest7(FormattedLineToString, "General", " ", inputs, expected);
  39. }
  40. function FormattedLineToStringCase2() {
  41. let inputs: (FormattedLine | FormattedLine[])[] = [
  42. new FormattedLine("a;", 1),
  43. new FormattedLine("b;", 2)];
  44. let expected: Array<string> = [" a;", " b;"];
  45. UnitTest7(FormattedLineToString, "General", " ", inputs, expected);
  46. }
  47. function UnitTestbeautify3() {
  48. console.log("=== beautify3 ===");
  49. Beautify3Case1();
  50. Beautify3Case2();
  51. Beautify3Case3();
  52. Beautify3Case4();
  53. Beautify3Case5();
  54. Beautify3Case6();
  55. Beautify3Case7();
  56. Beautify3Case8();
  57. Beautify3Case9();
  58. Beautify3Case10();
  59. Beautify3Case11();
  60. Beautify3Case12();
  61. Beautify3Case13();
  62. Beautify3Case14();
  63. Beautify3Case15();
  64. Beautify3Case16();
  65. Beautify3Case17();
  66. Beautify3Case18();
  67. Beautify3Case19();
  68. Beautify3Case20();
  69. Beautify3Case21();
  70. }
  71. function Beautify3Case1() {
  72. let new_line_after_symbols: NewLineSettings = new NewLineSettings();
  73. new_line_after_symbols.newLineAfter = ["then", ";"];
  74. new_line_after_symbols.noNewLineAfter = ["port", "generic"];
  75. let settings = getDefaultBeautifierSettings(new_line_after_symbols);
  76. let inputs: Array<string> = ["a;", "b;"];
  77. let expected: (FormattedLine | FormattedLine[])[] = [new FormattedLine("a;", 0), new FormattedLine("b;", 0)];
  78. UnitTest6(beautify3, "General", settings, inputs, expected, 0, 1, 0);
  79. }
  80. function Beautify3Case2() {
  81. let new_line_after_symbols: NewLineSettings = new NewLineSettings();
  82. new_line_after_symbols.newLineAfter = ["then", ";"];
  83. new_line_after_symbols.noNewLineAfter = ["port", "generic"];
  84. let settings = getDefaultBeautifierSettings(new_line_after_symbols);
  85. let inputs: Array<string> = ["IF x = '1' THEN", "RETURN 1;", "END IF;"];
  86. let expected: (FormattedLine | FormattedLine[])[] = [
  87. new FormattedLine("IF x = '1' THEN", 0),
  88. new FormattedLine("RETURN 1;", 1),
  89. new FormattedLine("END IF;", 0)
  90. ];
  91. UnitTest6(beautify3, "IF END", settings, inputs, expected, 0, 2, 0);
  92. }
  93. function Beautify3Case3() {
  94. let new_line_after_symbols: NewLineSettings = new NewLineSettings();
  95. new_line_after_symbols.newLineAfter = ["then", ";"];
  96. new_line_after_symbols.noNewLineAfter = ["port", "generic"];
  97. let settings = getDefaultBeautifierSettings(new_line_after_symbols);
  98. let inputs: Array<string> = [
  99. "IF x = '1' THEN",
  100. "RETURN 1;",
  101. "ELSIF x = '0' THEN",
  102. "RETURN 0;",
  103. "ELSE",
  104. "RETURN -1;",
  105. "END IF;"];
  106. let expected: (FormattedLine | FormattedLine[])[] = [
  107. new FormattedLine("IF x = '1' THEN", 0),
  108. new FormattedLine("RETURN 1;", 1),
  109. new FormattedLine("ELSIF x = '0' THEN", 0),
  110. new FormattedLine("RETURN 0;", 1),
  111. new FormattedLine("ELSE", 0),
  112. new FormattedLine("RETURN -1;", 1),
  113. new FormattedLine("END IF;", 0)
  114. ];
  115. UnitTest6(beautify3, "if elsif else end", settings, inputs, expected, 0, 6, 0);
  116. }
  117. function Beautify3Case4() {
  118. let new_line_after_symbols: NewLineSettings = new NewLineSettings();
  119. new_line_after_symbols.newLineAfter = ["then", ";"];
  120. new_line_after_symbols.noNewLineAfter = ["port", "generic"];
  121. let settings = getDefaultBeautifierSettings(new_line_after_symbols);
  122. let inputs: Array<string> = ["END"];
  123. let expected: (FormattedLine | FormattedLine[])[] = [new FormattedLine("END", 0)];
  124. UnitTest6(beautify3, "one line END", settings, inputs, expected, 0, 0, 0);
  125. }
  126. function Beautify3Case5() {
  127. let new_line_after_symbols: NewLineSettings = new NewLineSettings();
  128. new_line_after_symbols.newLineAfter = ["then", ";"];
  129. new_line_after_symbols.noNewLineAfter = ["port", "generic"];
  130. let settings = getDefaultBeautifierSettings(new_line_after_symbols);
  131. let inputs: Array<string> = [
  132. "CASE b",
  133. "WHEN 1 =>",
  134. "c <= d;",
  135. "WHEN 2 =>",
  136. "d <= f;",
  137. "END CASE;"
  138. ];
  139. let expected: (FormattedLine | FormattedLine[])[] = [
  140. new FormattedLine("CASE b", 0),
  141. new FormattedLine("WHEN 1 =>", 1),
  142. new FormattedLine("c <= d;", 2),
  143. new FormattedLine("WHEN 2 =>", 1),
  144. new FormattedLine("d <= f;", 2),
  145. new FormattedLine("END CASE;", 0)
  146. ];
  147. UnitTest6(beautify3, "case when when end", settings, inputs, expected, 0, 5, 0);
  148. }
  149. function Beautify3Case6() {
  150. let new_line_after_symbols: NewLineSettings = new NewLineSettings();
  151. new_line_after_symbols.newLineAfter = ["then", ";"];
  152. new_line_after_symbols.noNewLineAfter = ["port", "generic"];
  153. let settings = getDefaultBeautifierSettings(new_line_after_symbols);
  154. let inputs: Array<string> = [
  155. "CASE b",
  156. "WHEN 1 =>",
  157. "c <= d;",
  158. "CASE b",
  159. "WHEN 1 =>",
  160. "c <= d;",
  161. "WHEN 2 =>",
  162. "d <= f;",
  163. "END CASE;",
  164. "WHEN 2 =>",
  165. "d <= f;",
  166. "END CASE;"
  167. ];
  168. let expected: (FormattedLine | FormattedLine[])[] = [
  169. new FormattedLine("CASE b", 0),
  170. new FormattedLine("WHEN 1 =>", 1),
  171. new FormattedLine("c <= d;", 2),
  172. new FormattedLine("CASE b", 2),
  173. new FormattedLine("WHEN 1 =>", 3),
  174. new FormattedLine("c <= d;", 4),
  175. new FormattedLine("WHEN 2 =>", 3),
  176. new FormattedLine("d <= f;", 4),
  177. new FormattedLine("END CASE;", 2),
  178. new FormattedLine("WHEN 2 =>", 1),
  179. new FormattedLine("d <= f;", 2),
  180. new FormattedLine("END CASE;", 0)
  181. ];
  182. UnitTest6(beautify3, "case & case end", settings, inputs, expected, 0, 11, 0);
  183. }
  184. function Beautify3Case7() {
  185. let new_line_after_symbols: NewLineSettings = new NewLineSettings();
  186. new_line_after_symbols.newLineAfter = ["then", ";"];
  187. new_line_after_symbols.noNewLineAfter = ["port", "generic"];
  188. let settings = getDefaultBeautifierSettings(new_line_after_symbols);
  189. let inputs: Array<string> = [
  190. "ARCHITECTURE a OF one IS",
  191. "SIGNAL x : INTEGER;",
  192. "BEGIN",
  193. "-- architecture",
  194. "END ARCHITECTURE;"
  195. ];
  196. let expected: (FormattedLine | FormattedLine[])[] = [
  197. new FormattedLine("ARCHITECTURE a OF one IS", 0),
  198. new FormattedLine("SIGNAL x : INTEGER;", 1),
  199. new FormattedLine("BEGIN", 0),
  200. new FormattedLine("-- architecture", 1),
  201. new FormattedLine("END ARCHITECTURE;", 0),
  202. ];
  203. UnitTest6(beautify3, "architecture", settings, inputs, expected, 0, 4, 0);
  204. }
  205. function Beautify3Case8() {
  206. let new_line_after_symbols: NewLineSettings = new NewLineSettings();
  207. new_line_after_symbols.newLineAfter = ["then", ";"];
  208. new_line_after_symbols.noNewLineAfter = ["port", "generic"];
  209. let settings = getDefaultBeautifierSettings(new_line_after_symbols);
  210. let inputs: Array<string> = [
  211. "ARCHITECTURE a OF one IS",
  212. "SIGNAL x : INTEGER;",
  213. "BEGIN",
  214. "-- architecture",
  215. "END ARCHITECTURE;",
  216. "ARCHITECTURE b OF one IS",
  217. "SIGNAL x : INTEGER;",
  218. "BEGIN",
  219. "-- architecture",
  220. "END ARCHITECTURE;"
  221. ];
  222. let expected: (FormattedLine | FormattedLine[])[] = [
  223. new FormattedLine("ARCHITECTURE a OF one IS", 0),
  224. new FormattedLine("SIGNAL x : INTEGER;", 1),
  225. new FormattedLine("BEGIN", 0),
  226. new FormattedLine("-- architecture", 1),
  227. new FormattedLine("END ARCHITECTURE;", 0),
  228. new FormattedLine("ARCHITECTURE b OF one IS", 0),
  229. new FormattedLine("SIGNAL x : INTEGER;", 1),
  230. new FormattedLine("BEGIN", 0),
  231. new FormattedLine("-- architecture", 1),
  232. new FormattedLine("END ARCHITECTURE;", 0),
  233. ];
  234. UnitTest6(beautify3, "architecture 2", settings, inputs, expected, 0, 9, 0);
  235. }
  236. function Beautify3Case9() {
  237. let new_line_after_symbols: NewLineSettings = new NewLineSettings();
  238. new_line_after_symbols.newLineAfter = ["then", ";"];
  239. new_line_after_symbols.noNewLineAfter = ["port", "generic"];
  240. let settings = getDefaultBeautifierSettings(new_line_after_symbols);
  241. let inputs: Array<string> = [
  242. "PROCEDURE foo(x : IN INTEGER; y : OUT INTEGER) IS",
  243. "VARIABLE i : INTEGER;",
  244. "BEGIN",
  245. "y := x + 1;",
  246. "END PROCEDURE;"
  247. ];
  248. let expected: (FormattedLine | FormattedLine[])[] = [
  249. new FormattedLine("PROCEDURE foo(x : IN INTEGER; y : OUT INTEGER) IS", 0),
  250. new FormattedLine("VARIABLE i : INTEGER;", 1),
  251. new FormattedLine("BEGIN", 0),
  252. new FormattedLine("y := x + 1;", 1),
  253. new FormattedLine("END PROCEDURE;", 0)
  254. ];
  255. UnitTest6(beautify3, "procedure", settings, inputs, expected, 0, 4, 0);
  256. }
  257. function Beautify3Case10() {
  258. let new_line_after_symbols: NewLineSettings = new NewLineSettings();
  259. new_line_after_symbols.newLineAfter = ["then", ";"];
  260. new_line_after_symbols.noNewLineAfter = ["port", "generic"];
  261. let settings = getDefaultBeautifierSettings(new_line_after_symbols);
  262. let inputs: Array<string> = [
  263. "PACKAGE three IS",
  264. "SIGNAL s : INTEGER;",
  265. "ALIAS sa IS s;",
  266. "END PACKAGE;"
  267. ];
  268. let expected: (FormattedLine | FormattedLine[])[] = [
  269. new FormattedLine("PACKAGE three IS", 0),
  270. new FormattedLine("SIGNAL s : INTEGER;", 1),
  271. new FormattedLine("ALIAS sa IS s;", 1),
  272. new FormattedLine("END PACKAGE;", 0)
  273. ];
  274. UnitTest6(beautify3, "package", settings, inputs, expected, 0, 3, 0);
  275. }
  276. function Beautify3Case11() {
  277. let new_line_after_symbols: NewLineSettings = new NewLineSettings();
  278. new_line_after_symbols.newLineAfter = ["then", ";"];
  279. new_line_after_symbols.noNewLineAfter = ["port", "generic"];
  280. let settings = getDefaultBeautifierSettings(new_line_after_symbols);
  281. let inputs: Array<string> = [
  282. "PACKAGE p IS",
  283. "PROCEDURE foo(x : IN INTEGER; y : OUT INTEGER);",
  284. "END PACKAGE;",
  285. "PACKAGE BODY p IS",
  286. "PROCEDURE foo(x : IN INTEGER; y : OUT INTEGER) IS",
  287. "VARIABLE i : INTEGER;",
  288. "BEGIN",
  289. "y := x + 1;",
  290. "END PROCEDURE;",
  291. "PROCEDURE bar(FILE x : text);",
  292. "PROCEDURE baz IS",
  293. "TYPE foo;",
  294. "ALIAS x IS y;",
  295. "BEGIN",
  296. "END PROCEDURE;",
  297. "PROCEDURE tralala IS",
  298. "USE work.foo;",
  299. "BEGIN",
  300. "END PROCEDURE;",
  301. "END PACKAGE BODY;"
  302. ];
  303. let expected: (FormattedLine | FormattedLine[])[] = [
  304. new FormattedLine("PACKAGE p IS", 0),
  305. new FormattedLine("PROCEDURE foo(x : IN INTEGER; y : OUT INTEGER);", 1),
  306. new FormattedLine("END PACKAGE;", 0),
  307. new FormattedLine("PACKAGE BODY p IS", 0),
  308. new FormattedLine("PROCEDURE foo(x : IN INTEGER; y : OUT INTEGER) IS", 1),
  309. new FormattedLine("VARIABLE i : INTEGER;", 2),
  310. new FormattedLine("BEGIN", 1),
  311. new FormattedLine("y := x + 1;", 2),
  312. new FormattedLine("END PROCEDURE;", 1),
  313. new FormattedLine("PROCEDURE bar(FILE x : text);", 1),
  314. new FormattedLine("PROCEDURE baz IS", 1),
  315. new FormattedLine("TYPE foo;", 2),
  316. new FormattedLine("ALIAS x IS y;", 2),
  317. new FormattedLine("BEGIN", 1),
  318. new FormattedLine("END PROCEDURE;", 1),
  319. new FormattedLine("PROCEDURE tralala IS", 1),
  320. new FormattedLine("USE work.foo;", 2),
  321. new FormattedLine("BEGIN", 1),
  322. new FormattedLine("END PROCEDURE;", 1),
  323. new FormattedLine("END PACKAGE BODY;", 0)
  324. ];
  325. UnitTest6(beautify3, "package procedure", settings, inputs, expected, 0, expected.length - 1, 0);
  326. }
  327. function Beautify3Case12() {
  328. let new_line_after_symbols: NewLineSettings = new NewLineSettings();
  329. new_line_after_symbols.newLineAfter = ["then", ";"];
  330. new_line_after_symbols.noNewLineAfter = ["port", "generic"];
  331. let settings = getDefaultBeautifierSettings(new_line_after_symbols);
  332. let inputs: Array<string> = [
  333. "ARCHITECTURE a OF b IS",
  334. "SIGNAL x : INTEGER := 0;",
  335. "BEGIN",
  336. "p: PROCESS IS",
  337. "BEGIN",
  338. "END PROCESS;",
  339. "PROCESS",
  340. "VARIABLE y : INTEGER := 5;",
  341. "BEGIN",
  342. "x <= y;",
  343. "END PROCESS;",
  344. "PROCESS (x) IS",
  345. "BEGIN",
  346. "x <= x + 1;",
  347. "END PROCESS;",
  348. "POSTPONED PROCESS IS",
  349. "BEGIN",
  350. "END PROCESS;",
  351. "POSTPONED assert x = 1;",
  352. "END ARCHITECTURE;"
  353. ];
  354. let expected: (FormattedLine | FormattedLine[])[] = [
  355. new FormattedLine("ARCHITECTURE a OF b IS", 0),
  356. new FormattedLine("SIGNAL x : INTEGER := 0;", 1),
  357. new FormattedLine("BEGIN", 0),
  358. new FormattedLine("p: PROCESS IS", 1),
  359. new FormattedLine("BEGIN", 1),
  360. new FormattedLine("END PROCESS;", 1),
  361. new FormattedLine("PROCESS", 1),
  362. new FormattedLine("VARIABLE y : INTEGER := 5;", 2),
  363. new FormattedLine("BEGIN", 1),
  364. new FormattedLine("x <= y;", 2),
  365. new FormattedLine("END PROCESS;", 1),
  366. new FormattedLine("PROCESS (x) IS", 1),
  367. new FormattedLine("BEGIN", 1),
  368. new FormattedLine("x <= x + 1;", 2),
  369. new FormattedLine("END PROCESS;", 1),
  370. new FormattedLine("POSTPONED PROCESS IS", 1),
  371. new FormattedLine("BEGIN", 1),
  372. new FormattedLine("END PROCESS;", 1),
  373. new FormattedLine("POSTPONED assert x = 1;", 1),
  374. new FormattedLine("END ARCHITECTURE;", 0)
  375. ];
  376. UnitTest6(beautify3, "package postponed procedure", settings, inputs, expected, 0, expected.length - 1, 0);
  377. }
  378. function Beautify3Case13() {
  379. let new_line_after_symbols: NewLineSettings = new NewLineSettings();
  380. new_line_after_symbols.newLineAfter = ["then", ";"];
  381. new_line_after_symbols.noNewLineAfter = ["port", "generic"];
  382. let settings = getDefaultBeautifierSettings(new_line_after_symbols);
  383. let inputs: Array<string> = [
  384. "TYPE SharedCounter IS PROTECTED",
  385. "PROCEDURE increment (N : INTEGER := 1);",
  386. "IMPURE FUNCTION value RETURN INTEGER;",
  387. "END PROTECTED SharedCounter;"
  388. ];
  389. let expected: (FormattedLine | FormattedLine[])[] = [
  390. new FormattedLine("TYPE SharedCounter IS PROTECTED", 0),
  391. new FormattedLine("PROCEDURE increment (N : INTEGER := 1);", 1),
  392. new FormattedLine("IMPURE FUNCTION value RETURN INTEGER;", 1),
  393. new FormattedLine("END PROTECTED SharedCounter;", 0)
  394. ];
  395. UnitTest6(beautify3, "type projected", settings, inputs, expected, 0, expected.length - 1, 0);
  396. }
  397. function Beautify3Case14() {
  398. let new_line_after_symbols: NewLineSettings = new NewLineSettings();
  399. new_line_after_symbols.newLineAfter = ["then", ";"];
  400. new_line_after_symbols.noNewLineAfter = ["port", "generic"];
  401. let settings = getDefaultBeautifierSettings(new_line_after_symbols);
  402. let inputs: Array<string> = [
  403. "PACKAGE p IS",
  404. "TYPE SharedCounter IS PROTECTED",
  405. "PROCEDURE increment (N : INTEGER := 1);",
  406. "IMPURE FUNCTION value RETURN INTEGER;",
  407. "END PROTECTED SharedCounter;",
  408. "TYPE SharedCounter IS PROTECTED BODY"
  409. ];
  410. let expected: (FormattedLine | FormattedLine[])[] = [
  411. new FormattedLine("PACKAGE p IS", 0),
  412. new FormattedLine("TYPE SharedCounter IS PROTECTED", 1),
  413. new FormattedLine("PROCEDURE increment (N : INTEGER := 1);", 2),
  414. new FormattedLine("IMPURE FUNCTION value RETURN INTEGER;", 2),
  415. new FormattedLine("END PROTECTED SharedCounter;", 1),
  416. new FormattedLine("TYPE SharedCounter IS PROTECTED BODY", 1)
  417. ];
  418. UnitTest6(beautify3, "type projected", settings, inputs, expected, 0, expected.length - 1, 0);
  419. }
  420. function Beautify3Case15() {
  421. let new_line_after_symbols: NewLineSettings = new NewLineSettings();
  422. new_line_after_symbols.newLineAfter = ["then", ";"];
  423. new_line_after_symbols.noNewLineAfter = ["port", "generic"];
  424. let settings = getDefaultBeautifierSettings(new_line_after_symbols);
  425. let inputs: Array<string> = [
  426. "constant a : integer := 2#1101#",
  427. "constant b : integer := 3#20#;",
  428. "constant g : integer := 2:1_0:;"
  429. ];
  430. let expected: (FormattedLine | FormattedLine[])[] = [
  431. new FormattedLine("constant a : integer := 2#1101#", 0),
  432. new FormattedLine("constant b : integer := 3#20#;", 0),
  433. new FormattedLine("constant g : integer := 2:1_0:;", 0)
  434. ];
  435. UnitTest6(beautify3, "constant", settings, inputs, expected, 0, expected.length - 1, 0);
  436. }
  437. function Beautify3Case16() {
  438. let new_line_after_symbols: NewLineSettings = new NewLineSettings();
  439. new_line_after_symbols.newLineAfter = ["then", ";"];
  440. new_line_after_symbols.noNewLineAfter = ["port", "generic"];
  441. let settings = getDefaultBeautifierSettings(new_line_after_symbols);
  442. let inputs: Array<string> = [
  443. "x <= 1 WHEN foo",
  444. "ELSE 2 WHEN bar",
  445. "ELSE 3;",
  446. "y <= 2;"
  447. ];
  448. let expected: (FormattedLine | FormattedLine[])[] = [
  449. new FormattedLine("x <= 1 WHEN foo", 0),
  450. new FormattedLine("ELSE 2 WHEN bar", 1),
  451. new FormattedLine("ELSE 3;", 1),
  452. new FormattedLine("y <= 2;", 0)
  453. ];
  454. UnitTest6(beautify3, "one line ends with ;", settings, inputs, expected, 0, expected.length - 1, 0);
  455. }
  456. function Beautify3Case17() {
  457. let new_line_after_symbols: NewLineSettings = new NewLineSettings();
  458. new_line_after_symbols.newLineAfter = ["then", ";"];
  459. new_line_after_symbols.noNewLineAfter = ["port", "generic"];
  460. let settings = getDefaultBeautifierSettings(new_line_after_symbols);
  461. let inputs: Array<string> = [
  462. "WITH y SELECT x <=",
  463. "1 WHEN a,",
  464. "2 WHEN b,",
  465. "3 WHEN OTHERS;",
  466. "y <= 2;"
  467. ];
  468. let expected: (FormattedLine | FormattedLine[])[] = [
  469. new FormattedLine("WITH y SELECT x <=", 0),
  470. new FormattedLine("1 WHEN a,", 1),
  471. new FormattedLine("2 WHEN b,", 1),
  472. new FormattedLine("3 WHEN OTHERS;", 1),
  473. new FormattedLine("y <= 2;", 0)
  474. ];
  475. UnitTest6(beautify3, "WITH SELECT line ends with ;", settings, inputs, expected, 0, expected.length - 1, 0);
  476. }
  477. function Beautify3Case18() {
  478. let new_line_after_symbols: NewLineSettings = new NewLineSettings();
  479. new_line_after_symbols.newLineAfter = ["then", ";"];
  480. new_line_after_symbols.noNewLineAfter = ["port", "generic"];
  481. let settings = getDefaultBeautifierSettings(new_line_after_symbols);
  482. let inputs: Array<string> = [
  483. "CONFIGURATION conf OF ent IS",
  484. "USE work.foo;",
  485. "ATTRIBUTE x OF y : SIGNAL IS 5;",
  486. "FOR arch",
  487. "FOR ALL : comp",
  488. "USE ENTITY work.foo(x);",
  489. "END FOR;",
  490. "END FOR;",
  491. "END CONFIGURATION;",
  492. ];
  493. let expected: (FormattedLine | FormattedLine[])[] = [
  494. new FormattedLine("CONFIGURATION conf OF ent IS", 0),
  495. new FormattedLine("USE work.foo;", 1),
  496. new FormattedLine("ATTRIBUTE x OF y : SIGNAL IS 5;", 1),
  497. new FormattedLine("FOR arch", 1),
  498. new FormattedLine("FOR ALL : comp", 2),
  499. new FormattedLine("USE ENTITY work.foo(x);", 3),
  500. new FormattedLine("END FOR;", 2),
  501. new FormattedLine("END FOR;", 1),
  502. new FormattedLine("END CONFIGURATION;", 0)
  503. ];
  504. UnitTest6(beautify3, "configuration & for", settings, inputs, expected, 0, expected.length - 1, 0);
  505. }
  506. function Beautify3Case19() {
  507. let new_line_after_symbols: NewLineSettings = new NewLineSettings();
  508. new_line_after_symbols.newLineAfter = ["then", ";"];
  509. new_line_after_symbols.noNewLineAfter = ["port", "generic"];
  510. let settings = getDefaultBeautifierSettings(new_line_after_symbols);
  511. let inputs: Array<string> = [
  512. "FUNCTION \" + \"(x, y : integer) return integer IS",
  513. "BEGIN",
  514. "RETURN 42;",
  515. "END FUNCTION \"+\";",
  516. ];
  517. let expected: (FormattedLine | FormattedLine[])[] = [
  518. new FormattedLine("FUNCTION \" + \"(x, y : integer) return integer IS", 0),
  519. new FormattedLine("BEGIN", 0),
  520. new FormattedLine("RETURN 42;", 1),
  521. new FormattedLine("END FUNCTION \"+\";", 0),
  522. ];
  523. UnitTest6(beautify3, "function", settings, inputs, expected, 0, expected.length - 1, 0);
  524. }
  525. function Beautify3Case20() {
  526. let new_line_after_symbols: NewLineSettings = new NewLineSettings();
  527. new_line_after_symbols.newLineAfter = ["then", ";"];
  528. new_line_after_symbols.noNewLineAfter = ["port", "generic"];
  529. let settings = getDefaultBeautifierSettings(new_line_after_symbols);
  530. let inputs: Array<string> = [
  531. "m <= ((1, 2, 3, 4)",
  532. "(5, 6, 7, 8));",
  533. "y <= 2;"
  534. ];
  535. let expected: (FormattedLine | FormattedLine[])[] = [
  536. new FormattedLine("m <= ((1, 2, 3, 4)", 0),
  537. new FormattedLine("(5, 6, 7, 8));", 1),
  538. new FormattedLine("y <= 2;", 0)
  539. ];
  540. UnitTest6(beautify3, "function", settings, inputs, expected, 0, expected.length - 1, 0);
  541. }
  542. function Beautify3Case21() {
  543. let new_line_after_symbols: NewLineSettings = new NewLineSettings();
  544. new_line_after_symbols.newLineAfter = ["then", ";"];
  545. new_line_after_symbols.noNewLineAfter = ["port", "generic"];
  546. let settings = getDefaultBeautifierSettings(new_line_after_symbols);
  547. let inputs: Array<string> = [
  548. "g: ENTITY work.foo",
  549. "GENERIC MAP( X => 1 )",
  550. "PORT MAP( a, b );",
  551. "h: ENTITY work.foo",
  552. "PORT MAP( a => open );"
  553. ];
  554. let expected: (FormattedLine | FormattedLine[])[] = [
  555. new FormattedLine("g: ENTITY work.foo", 0),
  556. new FormattedLine("GENERIC MAP( X => 1 )", 1),
  557. new FormattedLine("PORT MAP( a, b );", 1),
  558. new FormattedLine("h: ENTITY work.foo", 0),
  559. new FormattedLine("PORT MAP( a => open );", 1)
  560. ];
  561. UnitTest6(beautify3, "function", settings, inputs, expected, 0, expected.length - 1, 0);
  562. }
  563. function UnitTestSetNewLinesAfterSymbols() {
  564. console.log("=== SetNewLinesAfterSymbols ===");
  565. let input = "a; @@comments1\r\nb;"
  566. let expected = "a; @@comments1\r\nb;";
  567. let parameters: NewLineSettings = new NewLineSettings();
  568. parameters.newLineAfter = ["then", ";"];
  569. parameters.noNewLineAfter = ["port", "generic"];
  570. UnitTest5(SetNewLinesAfterSymbols, "no new line after comment", parameters, input, expected);
  571. input = "a; b;"
  572. expected = "a;\r\nb;";
  573. UnitTest5(SetNewLinesAfterSymbols, "new line after ;", parameters, input, expected);
  574. }
  575. function UnitTestApplyNoNewLineAfter() {
  576. console.log("=== ApplyNoNewLineAfter ===");
  577. let input: Array<string> = ["a;", "b;"];
  578. let expected: Array<string> = ["a;@@singleline", "b;@@singleline"];
  579. let parameters: Array<string> = [";"];
  580. UnitTest4(ApplyNoNewLineAfter, "one blankspace", parameters, input, expected);
  581. input = ["a;", "b THEN", "c"];
  582. expected = ["a;@@singleline", "b THEN@@singleline", "c"];
  583. parameters = [";", "then"];
  584. UnitTest4(ApplyNoNewLineAfter, "one blankspace", parameters, input, expected);
  585. }
  586. function UnitTestRemoveAsserts() {
  587. console.log("=== RemoveAsserts ===");
  588. let input: Array<string> = ["ASSERT a;"];
  589. let expected: Array<string> = [""];
  590. UnitTest3(RemoveAsserts, "one assert", input, expected);
  591. input = ["ASSERT a", "b;", "c"];
  592. expected = ["", "", "c"];
  593. UnitTest3(RemoveAsserts, "multiline assert", input, expected);
  594. }
  595. function compareFormattedLines(expected: (FormattedLine | FormattedLine[])[], actual: (FormattedLine | FormattedLine[])[], message?): string {
  596. var l = Math.min(actual.length, expected.length);
  597. let result: string = "";
  598. for (var i = 0; i < l; i++) {
  599. if (actual[i] instanceof FormattedLine) {
  600. if (expected[i] instanceof FormattedLine) {
  601. let compareResult = compareFormattedLine(<FormattedLine>(expected[i]), <FormattedLine>(actual[i]), message, false);
  602. if (compareResult.length > 0) {
  603. result += "index " + i + "\n" + compareResult;
  604. }
  605. }
  606. else {
  607. result += "index " + i + "\nexpected FormatLine[], actual FormattedLine. actual:" + (<FormattedLine>(actual[i])).Line;
  608. }
  609. }
  610. else {
  611. if (expected[i] instanceof FormattedLine) {
  612. result += "index " + i + "\nexpected FormatLine, actual FormattedLine[]. expected:" + (<FormattedLine>(expected[i])).Line;
  613. }
  614. else {
  615. let compareResult = compareFormattedLines(<FormattedLine[]>(actual[i]), <FormattedLine[]>(expected[i]), message);
  616. if (compareResult.length > 0) {
  617. result += "index " + i + "\n" + compareResult;
  618. }
  619. }
  620. }
  621. }
  622. if (actual.length > expected.length) {
  623. result += "actual has more items";
  624. for (var i = expected.length; i < actual.length; i++) {
  625. result += "actual[" + i + "] = " + actual[i];
  626. }
  627. }
  628. else if (actual.length < expected.length) {
  629. result += "expected has more items";
  630. for (var i = actual.length; i < expected.length; i++) {
  631. result += "expected[" + i + "] = " + expected[i];
  632. }
  633. }
  634. return result;
  635. }
  636. function assertFormattedLines(testName, expected: (FormattedLine | FormattedLine[])[], actual: (FormattedLine | FormattedLine[])[], message?) {
  637. let result = compareFormattedLines(expected, actual, message);
  638. if (result.length > 0) {
  639. console.log(testName + " failed:\n" + result);
  640. }
  641. testCount++;
  642. }
  643. function compareFormattedLine(expected: FormattedLine, actual: FormattedLine, message?, cumulateTestCount?: boolean) {
  644. let result = "";
  645. if (expected.Indent != actual.Indent) {
  646. result += 'indents are not equal;\nexpected: "' + expected.Line + '", ' + expected.Indent
  647. + ';\nactual: "' + actual.Line + '", ' + actual.Indent + "\n";
  648. }
  649. let compareResult = CompareString(actual.Line, expected.Line);
  650. if (compareResult != true) {
  651. result += compareResult;
  652. }
  653. return result;
  654. }
  655. function assertArray(testName, expected, actual, message?) {
  656. var result = CompareArray(actual, expected);
  657. if (result != true) {
  658. console.log(testName + " failed: " + result);
  659. }
  660. else {
  661. //console.log(testName + " pass");
  662. }
  663. testCount++;
  664. }
  665. type StringCallback = (text: string) => string;
  666. type ArrayCallback = (arr: Array<string>) => void;
  667. type Array2Callback = (arr: Array<string>, parameters: Array<string>) => void;
  668. type String2Callback = (text: string, parameters: NewLineSettings) => string;
  669. type BeautifyCallback = (inputs: Array<string>, result: (FormattedLine | FormattedLine[])[], settings: BeautifierSettings, startIndex: number, indent: number) => number;
  670. type FormattedLinesCallback = (inputs: (FormattedLine | FormattedLine[])[], indentation: string) => Array<string>;
  671. function UnitTest7(func: FormattedLinesCallback, testName: string, indentation: string, inputs: (FormattedLine | FormattedLine[])[], expected: Array<string>) {
  672. let actual = func(inputs, indentation);
  673. assertArray(testName, expected, actual);
  674. }
  675. function UnitTest6(func: BeautifyCallback, testName: string, parameters: BeautifierSettings, inputs: Array<string>, expected: (FormattedLine | FormattedLine[])[], startIndex: number, expectedEndIndex: number, indent: number) {
  676. let actual: (FormattedLine | FormattedLine[])[] = []
  677. let endIndex: number = func(inputs, actual, parameters, startIndex, indent);
  678. if (endIndex != expectedEndIndex) {
  679. console.log(testName + " failed;\nend index, actual: " + endIndex + "; expected: " + expectedEndIndex)
  680. }
  681. assertFormattedLines(testName, expected, actual);
  682. }
  683. function UnitTest5(func: String2Callback, testName: string, parameters: NewLineSettings, inputs, expected: string) {
  684. let actual: string = func(inputs, parameters);
  685. assertAndCountTest(testName, expected, actual);
  686. }
  687. function UnitTest4(func: Array2Callback, testName: string, parameters: Array<string>, inputs: Array<string>, expected: Array<string>) {
  688. let actual = JSON.parse(JSON.stringify(inputs));
  689. func(actual, parameters);
  690. assertArray(testName, expected, actual);
  691. }
  692. function UnitTest3(func: ArrayCallback, testName: string, inputs: Array<string>, expected: Array<string>) {
  693. let actual = JSON.parse(JSON.stringify(inputs));
  694. func(actual);
  695. assertArray(testName, expected, actual);
  696. }
  697. function deepCopy(objectToCopy: BeautifierSettings): BeautifierSettings {
  698. return (JSON.parse(JSON.stringify(objectToCopy)));
  699. }
  700. function IntegrationTest() {
  701. console.log("=== IntegrationTests ===");
  702. let new_line_after_symbols: NewLineSettings = new NewLineSettings();
  703. new_line_after_symbols.newLineAfter = ["then", ";"];
  704. new_line_after_symbols.noNewLineAfter = ["port", "generic"];
  705. let settings = getDefaultBeautifierSettings(new_line_after_symbols);
  706. let input = "architecture TB of TB_CPU is\r\n component CPU_IF\r\n port -- port list\r\n end component;\r\n signal CPU_DATA_VALID: std_ulogic;\r\n signal CLK, RESET: std_ulogic := '0';\r\n constant PERIOD : time := 10 ns;\r\n constant MAX_SIM: time := 50 * PERIOD;\r\n begin\r\n -- concurrent statements\r\n end TB;"
  707. let expected = "ARCHITECTURE TB OF TB_CPU IS\r\n COMPONENT CPU_IF\r\n PORT -- port list\r\n END COMPONENT;\r\n SIGNAL CPU_DATA_VALID : std_ulogic;\r\n SIGNAL CLK, RESET : std_ulogic := '0';\r\n CONSTANT PERIOD : TIME := 10 ns;\r\n CONSTANT MAX_SIM : TIME := 50 * PERIOD;\r\nBEGIN\r\n -- concurrent statements\r\nEND TB;";
  708. let actual = beautify(input, settings);
  709. assertAndCountTest("General", expected, actual);
  710. IntegrationTest2();
  711. let new_line_after_symbols_2: NewLineSettings = new NewLineSettings();
  712. new_line_after_symbols_2.newLineAfter = [];
  713. new_line_after_symbols_2.noNewLineAfter = ["then", ";", "generic", "port"];
  714. let newSettings = deepCopy(settings);
  715. newSettings.NewLineSettings = new_line_after_symbols_2;
  716. expected = "a; b; c;";
  717. input = "a; \r\nb;\r\n c;"
  718. actual = beautify(input, newSettings);
  719. assertAndCountTest("Remove line after ;", expected, actual);
  720. newSettings = deepCopy(settings);
  721. newSettings.RemoveAsserts = true;
  722. input = "architecture arch of ent is\r\nbegin\r\n assert False report sdfjcsdfcsdj;\r\n assert False report sdfjcsdfcsdj severity note;\r\nend architecture;";
  723. expected = "ARCHITECTURE arch OF ent IS\r\nBEGIN\r\nEND ARCHITECTURE;"
  724. actual = beautify(input, newSettings);
  725. assertAndCountTest("Remove asserts", expected, actual);
  726. input = "entity TB_DISPLAY is\r\n-- port declarations\r\nend TB_DISPLAY;\r\n\r\narchitecture TEST of TB_DISPLAY is\r\n-- signal declarations\r\nbegin\r\n-- component instance(s)\r\nend TEST;";
  727. expected = "ENTITY TB_DISPLAY IS\r\n -- port declarations\r\nEND TB_DISPLAY;\r\n\r\nARCHITECTURE TEST OF TB_DISPLAY IS\r\n -- signal declarations\r\nBEGIN\r\n -- component instance(s)\r\nEND TEST;";
  728. actual = beautify(input, settings);
  729. assertAndCountTest("ENTITY ARCHITECTURE", expected, actual);
  730. IntegrationTest5();
  731. IntegrationTest6();
  732. input = 'if a(3 downto 0) > "0100" then\r\na(3 downto 0) := a(3 downto 0) + "0011" ;\r\nend if ;';
  733. expected = 'IF a(3 DOWNTO 0) > "0100" THEN\r\n a(3 DOWNTO 0) := a(3 DOWNTO 0) + "0011";\r\nEND IF;';
  734. actual = beautify(input, settings);
  735. assertAndCountTest("IF END IF case 1", expected, actual);
  736. input = "if s = '1' then\r\no <= \"010\";\r\nelse\r\no <= \"101\";\r\nend if;";
  737. expected = "IF s = '1' THEN\r\n o <= \"010\";\r\nELSE\r\n o <= \"101\";\r\nEND IF;";
  738. actual = beautify(input, settings);
  739. assertAndCountTest("IF ELSE END IF case 1", expected, actual);
  740. newSettings = deepCopy(settings);
  741. newSettings.NewLineSettings.newLineAfter.push("ELSE");
  742. input = "IF (s = r) THEN rr := '0'; ELSE rr := '1'; END IF;";
  743. expected = "IF (s = r) THEN\r\n rr := '0';\r\nELSE\r\n rr := '1';\r\nEND IF;";
  744. actual = beautify(input, newSettings);
  745. assertAndCountTest("IF ELSE END IF case 2", expected, actual);
  746. input = 'P1:process\r\nvariable x: Integer range 1 to 3;\r\nvariable y: BIT_VECTOR (0 to 1);\r\nbegin\r\n C1: case x is\r\n when 1 => Out_1 <= 0;\r\n when 2 => Out_1 <= 1;\r\n end case C1;\r\n C2: case y is\r\n when "00" => Out_2 <= 0;\r\n when "01" => Out_2 <= 1;\r\n end case C2;\r\nend process;';
  747. expected = 'P1 : PROCESS\r\n VARIABLE x : INTEGER RANGE 1 TO 3;\r\n VARIABLE y : BIT_VECTOR (0 TO 1);\r\nBEGIN\r\n C1 : CASE x IS\r\n WHEN 1 => Out_1 <= 0;\r\n WHEN 2 => Out_1 <= 1;\r\n END CASE C1;\r\n C2 : CASE y IS\r\n WHEN "00" => Out_2 <= 0;\r\n WHEN "01" => Out_2 <= 1;\r\n END CASE C2;\r\nEND PROCESS;';
  748. actual = beautify(input, settings);
  749. assertAndCountTest("WHEN CASE", expected, actual);
  750. input = "case READ_CPU_STATE is\r\n when WAITING =>\r\n if CPU_DATA_VALID = '1' then\r\n CPU_DATA_READ <= '1';\r\n READ_CPU_STATE <= DATA1;\r\n end if;\r\n when DATA1 =>\r\n -- etc.\r\nend case;";
  751. expected = "CASE READ_CPU_STATE IS\r\n WHEN WAITING =>\r\n IF CPU_DATA_VALID = '1' THEN\r\n CPU_DATA_READ <= '1';\r\n READ_CPU_STATE <= DATA1;\r\n END IF;\r\n WHEN DATA1 =>\r\n -- etc.\r\nEND CASE;";
  752. actual = beautify(input, settings);
  753. assertAndCountTest("WHEN CASE & IF", expected, actual);
  754. input = "entity aa is\r\n port (a : in std_logic;\r\n b : in std_logic;\r\n );\r\nend aa;\r\narchitecture bb of aa is\r\n component cc\r\n port(\r\n a : in std_logic;\r\n b : in std_logic;\r\n );\r\n end cc;\r\n\r\nbegin\r\n C : cc port map (\r\n long => a,\r\n b => b\r\n );\r\nend;";
  755. expected = "ENTITY aa IS\r\n PORT (\r\n a : IN std_logic;\r\n b : IN std_logic;\r\n );\r\nEND aa;\r\nARCHITECTURE bb OF aa IS\r\n COMPONENT cc\r\n PORT (\r\n a : IN std_logic;\r\n b : IN std_logic;\r\n );\r\n END cc;\r\n\r\nBEGIN\r\n C : cc PORT MAP(\r\n long => a,\r\n b => b\r\n );\r\nEND;";
  756. actual = beautify(input, settings);
  757. assertAndCountTest("PORT MAP", expected, actual);
  758. input = "entity aa is\r\n port (a : in std_logic;\r\n b : in std_logic;\r\n );\r\n port (a : in std_logic;\r\n b : in std_logic;\r\n );\r\nend aa;\r\narchitecture bb of aa is\r\n component cc\r\n port(\r\n a : in std_logic;\r\n b : in std_logic;\r\n );\r\n port(\r\n a : in std_logic;\r\n b : in std_logic;\r\n );\r\n end cc;\r\n\r\nbegin\r\n C : cc port map (\r\n long => a,\r\n b => b\r\n );\r\n D : cc port map (\r\n long => a,\r\n b => b\r\n );\r\nend;";
  759. expected = "ENTITY aa IS\r\n PORT (\r\n a : IN std_logic;\r\n b : IN std_logic;\r\n );\r\n PORT (\r\n a : IN std_logic;\r\n b : IN std_logic;\r\n );\r\nEND aa;\r\nARCHITECTURE bb OF aa IS\r\n COMPONENT cc\r\n PORT (\r\n a : IN std_logic;\r\n b : IN std_logic;\r\n );\r\n PORT (\r\n a : IN std_logic;\r\n b : IN std_logic;\r\n );\r\n END cc;\r\n\r\nBEGIN\r\n C : cc PORT MAP(\r\n long => a,\r\n b => b\r\n );\r\n D : cc PORT MAP(\r\n long => a,\r\n b => b\r\n );\r\nEND;";
  760. actual = beautify(input, settings);
  761. assertAndCountTest("Multiple PORT MAPs", expected, actual);
  762. input = "port (a : in std_logic;\r\n b : in std_logic;\r\n);";
  763. expected = "PORT\r\n(\r\n a : IN std_logic;\r\n b : IN std_logic;\r\n);";
  764. new_line_after_symbols_2 = new NewLineSettings();
  765. new_line_after_symbols_2.newLineAfter = ["then", ";", "generic", "port"];
  766. newSettings = deepCopy(settings);
  767. newSettings.NewLineSettings = new_line_after_symbols_2;
  768. actual = beautify(input, newSettings);
  769. assertAndCountTest("New line after PORT", expected, actual);
  770. newSettings = deepCopy(settings);
  771. newSettings.NewLineSettings.newLineAfter = [];
  772. input = "component a is\r\nport( Data : inout Std_Logic_Vector(7 downto 0););\r\nend component a;";
  773. expected = "COMPONENT a IS\r\n PORT (Data : INOUT Std_Logic_Vector(7 DOWNTO 0););\r\nEND COMPONENT a;";
  774. actual = beautify(input, newSettings);
  775. assertAndCountTest("New line after PORT (single line)", expected, actual);
  776. //IntegrationTest20();
  777. input = "architecture a of b is\r\nbegin\r\n process (w)\r\n variable t : std_logic_vector (4 downto 0) ;\r\nbegin\r\n a := (others => '0') ;\r\nend process ;\r\nend a;";
  778. expected = "ARCHITECTURE a OF b IS\r\nBEGIN\r\n PROCESS (w)\r\n VARIABLE t : std_logic_vector (4 DOWNTO 0);\r\n BEGIN\r\n a := (OTHERS => '0');\r\n END PROCESS;\r\nEND a;";
  779. actual = beautify(input, newSettings);
  780. assertAndCountTest("Double BEGIN", expected, actual);
  781. IntegrationTest23();
  782. IntegrationTest24();
  783. IntegrationTest25();
  784. IntegrationTest26();
  785. IntegrationTest27();
  786. IntegrationTest28();
  787. IntegrationTest29();
  788. IntegrationTest30();
  789. IntegrationTest31();
  790. IntegrationTest32();
  791. IntegrationTest33();
  792. IntegrationTest34();
  793. IntegrationTest35();
  794. IntegrationTest36();
  795. IntegrationTest37();
  796. IntegrationTest38();
  797. IntegrationTest39();
  798. IntegrationTest40();
  799. IntegrationTest41();
  800. IntegrationTest42();
  801. IntegrationTest43();
  802. IntegrationTest44();
  803. IntegrationTest45();
  804. IntegrationTest46();
  805. IntegrationTest47();
  806. IntegrationTest48();
  807. IntegrationTest49();
  808. IntegrationTest50();
  809. IntegrationTest51();
  810. IntegrationTest52();
  811. IntegrationTest53();
  812. IntegrationTest54();
  813. IntegrationTest55();
  814. IntegrationTest56();
  815. IntegrationTest57();
  816. IntegrationTest58();
  817. IntegrationTest59();
  818. IntegrationTest60();
  819. IntegrationTest61();
  820. IntegrationTest62();
  821. IntegrationTest63();
  822. IntegrationTest64();
  823. IntegrationTest65();
  824. IntegrationTest66();
  825. IntegrationTest67();
  826. IntegrationTest68();
  827. IntegrationTest69();
  828. IntegrationTest70();
  829. IntegrationTest71();
  830. IntegrationTest72();
  831. IntegrationTest73();
  832. IntegrationTest74();
  833. IntegrationTest76();
  834. IntegrationTest77();
  835. IntegrationTest78();
  836. IntegrationTest79();
  837. IntegrationTest80();
  838. IntegrationTest82();
  839. IntegrationTest84();
  840. IntegrationTest85();
  841. }
  842. function IntegrationTest23() {
  843. let settings = GetDefaultSettings("\t");
  844. let input = "PACKAGE p IS\r\n TYPE int_array IS ARRAY (INTEGER RANGE <>) OF INTEGER;\r\n TYPE ten_ints IS ARRAY (1 TO 10) OF INTEGER;\r\n TYPE chars IS (A, B, C);\r\n TYPE char_counts IS ARRAY (chars) OF INTEGER;\r\n TYPE two_d IS ARRAY (1 TO 3, 4 TO 6) OF INTEGER;\r\n TYPE ab_chars IS ARRAY (chars RANGE A TO B) OF INTEGER;\r\nEND PACKAGE;";
  845. let actual = beautify(input, settings);
  846. assertAndCountTest("Type array", input, actual);
  847. }
  848. function IntegrationTest24() {
  849. let settings = GetDefaultSettings();
  850. let input = "ARCHITECTURE a OF e IS\r\n ATTRIBUTE foo : INTEGER;\r\n ATTRIBUTE foo OF x : SIGNAL IS 5;\r\n ATTRIBUTE foo OF x : COMPONENT IS 5;\r\n ATTRIBUTE foo OF x : LABEL IS 6;\r\n ATTRIBUTE foo OF x : TYPE IS 4;\r\nBEGIN\r\n ASSERT x'foo(5);\r\nEND ARCHITECTURE;";
  851. let actual = beautify(input, settings);
  852. assertAndCountTest("attribute", input, actual);
  853. }
  854. function IntegrationTest25() {
  855. let settings = GetDefaultSettings();
  856. let input = 'PACKAGE bitstring IS\r\n CONSTANT x : t := X"1234";\r\n CONSTANT y : t := O"1234";\r\n CONSTANT z : t := X"ab";\r\n CONSTANT b : t := B"101";\r\n CONSTANT c : t := x"f";\r\n CONSTANT d : t := X"a_b";\r\nEND PACKAGE;\r\nPACKAGE bitstring_error IS\r\n CONSTANT e1 : t := O"9"; -- Error\r\nEND PACKAGE;';
  857. let actual = beautify(input, settings);
  858. assertAndCountTest("bitstring", input, actual);
  859. }
  860. function IntegrationTest26() {
  861. let settings = GetDefaultSettings();
  862. let input = 'ARCHITECTURE a OF e IS\r\nBEGIN\r\n b : BLOCK IS\r\n BEGIN\r\n END BLOCK;\r\n c : BLOCK IS\r\n SIGNAL x : INTEGER;\r\n SIGNAL y : real;\r\n BEGIN\r\n x <= y;\r\n END BLOCK;\r\nEND ARCHITECTURE;';
  863. let actual = beautify(input, settings);
  864. assertAndCountTest("block", input, actual);
  865. }
  866. function IntegrationTest27() {
  867. let settings = GetDefaultSettings(" ");
  868. let input = 'CONTEXT widget_context IS\r\n LIBRARY ieee;\r\n USE ieee.std_logic_1164.ALL, ieee.numeric_std.ALL;\r\n USE widget_lib.widget_defs.ALL;\r\n USE widget_lib.widget_comps.ALL;\r\nEND CONTEXT;\r\n\r\nCONTEXT dongle_context IS\r\n LIBRARY widget_lib;\r\n CONTEXT widget_lib.widget_context;\r\nEND CONTEXT;\r\n\r\nLIBRARY foo;\r\nUSE foo.moo;\r\n\r\nCONTEXT bad IS -- Error\r\nEND CONTEXT;';
  869. let actual = beautify(input, settings);
  870. assertAndCountTest("context", input, actual);
  871. }
  872. function IntegrationTest28() {
  873. let settings = GetDefaultSettings(" ");
  874. let input = 'ARCHITECTURE foo OF bar IS\r\n SIGNAL \\foo bar\\ : INTEGER;\r\n SIGNAL \\a\\\\b\\ : INTEGER;\r\n SIGNAL \\Thing!!! \\ : INTEGER;\r\n SIGNAL \\name\\ : INTEGER;\r\n SIGNAL name : INTEGER;\r\nBEGIN\r\n \\foo.bar.baz\\ <= \\hello\\;\r\nEND ARCHITECTURE;';
  875. let actual = beautify(input, settings);
  876. assertAndCountTest("extended \\ 28", input, actual);
  877. }
  878. function IntegrationTest29() {
  879. let settings = GetDefaultSettings(" ");
  880. let input = 'PACKAGE func IS\r\n FUNCTION add(x, y : INTEGER; y : IN INTEGER) RETURN INTEGER;\r\n IMPURE FUNCTION naughty RETURN INTEGER;\r\n FUNCTION "+"(x, y : INTEGER) RETURN INTEGER;\r\nEND PACKAGE;';
  881. let actual = beautify(input, settings);
  882. assertAndCountTest("extended \\ 29", input, actual);
  883. }
  884. function IntegrationTest30() {
  885. let settings = GetDefaultSettings(" ");
  886. let input = 'ARCHITECTURE a OF g IS\r\nBEGIN\r\n\r\n g1 : IF foo GENERATE\r\n SIGNAL x : INTEGER;\r\n BEGIN\r\n x <= 5;\r\n END GENERATE;\r\n\r\n g2 : IF bar GENERATE\r\n g2a : IF x < 5 GENERATE\r\n g <= 7;\r\n END GENERATE;\r\n END GENERATE;\r\n\r\n g3 : FOR i IN 1 TO 40 GENERATE\r\n SIGNAL x : INTEGER;\r\n BEGIN\r\n f <= h;\r\n END GENERATE;\r\n\r\n g4 : FOR i IN x\'RANGE GENERATE\r\n END GENERATE;\r\n\r\n g5 : FOR i IN x\'RANGE GENERATE\r\n BEGIN\r\n END GENERATE;\r\n\r\n g6 : FOR i IN 1 TO 3 GENERATE\r\n COMPONENT sub_ent IS\r\n PORT (val : OUT NATURAL);\r\n END COMPONENT sub_ent; -- OK\r\n BEGIN\r\n END GENERATE;\r\n\r\n g7 : IF true GENERATE\r\n PROCEDURE doit IS -- OK\r\n BEGIN\r\n write(OUTPUT, "OK." & LF);\r\n END PROCEDURE doit;\r\n BEGIN\r\n END GENERATE g7;\r\n\r\nEND ARCHITECTURE;';
  887. let actual = beautify(input, settings);
  888. assertAndCountTest("generate", input, actual);
  889. }
  890. function IntegrationTest31() {
  891. let settings = GetDefaultSettings(" ");
  892. let input = 'ENTITY ent IS\r\nEND ENTITY;\r\n\r\nARCHITECTURE a OF ent IS\r\nBEGIN\r\n main : PROCESS\r\n BEGIN\r\n REPORT """""";\r\n WAIT;\r\n END PROCESS;\r\nEND ARCHITECTURE;';
  893. let actual = beautify(input, settings);
  894. assertAndCountTest("report \"\"", input, actual);
  895. }
  896. function IntegrationTest32() {
  897. let settings = GetDefaultSettings(" ");
  898. let input = 'PACKAGE p IS\r\n\r\n TYPE SharedCounter IS PROTECTED\r\n PROCEDURE increment (N : INTEGER := 1);\r\n PROCEDURE decrement (N : INTEGER := 1);\r\n IMPURE FUNCTION value RETURN INTEGER;\r\n END PROTECTED SharedCounter;\r\n\r\n TYPE SharedCounter IS PROTECTED BODY\r\n VARIABLE counter : INTEGER := 0;\r\n\r\n PROCEDURE increment (N : INTEGER := 1) IS\r\n BEGIN\r\n counter := counter + N;\r\n END PROCEDURE increment;\r\n\r\n PROCEDURE decrement (N : INTEGER := 1) IS\r\n BEGIN\r\n counter := counter - N;\r\n END PROCEDURE decrement;\r\n\r\n IMPURE FUNCTION value RETURN INTEGER IS\r\n BEGIN\r\n RETURN counter;\r\n END FUNCTION value;\r\n END PROTECTED BODY;\r\n\r\nEND PACKAGE;';
  899. let actual = beautify(input, settings);
  900. assertAndCountTest("protected", input, actual);
  901. }
  902. function IntegrationTest33() {
  903. let settings = GetDefaultSettings(" ");
  904. let input = 'ARCHITECTURE a OF b IS\r\nBEGIN\r\n\r\n -- Wait statements\r\n PROCESS IS\r\n BEGIN\r\n WAIT FOR 1 ns;\r\n block_forever : WAIT;\r\n WAIT ON x;\r\n WAIT ON x, y, z(1 DOWNTO 0);\r\n WAIT ON w(1) FOR 2 ns;\r\n WAIT UNTIL x = 3;\r\n WAIT UNTIL y = x FOR 5 ns;\r\n WAIT ON x UNTIL x = 2 FOR 1 ns;\r\n END PROCESS;\r\n\r\n -- Blocking assignment\r\n PROCESS IS\r\n VARIABLE a : INTEGER;\r\n BEGIN\r\n a := 2;\r\n a := a + (a * 3);\r\n END PROCESS;\r\n\r\n -- Assert and report\r\n PROCESS IS\r\n BEGIN\r\n ASSERT true;\r\n ASSERT false SEVERITY note;\r\n ASSERT 1 > 2 REPORT "oh no" SEVERITY failure;\r\n REPORT "hello";\r\n REPORT "boo" SEVERITY error;\r\n END PROCESS;\r\n\r\n -- Function calls\r\n PROCESS IS\r\n BEGIN\r\n x := foo(1, 2, 3);\r\n a := "ABS"(b);\r\n END PROCESS;\r\n\r\n -- If\r\n PROCESS IS\r\n BEGIN\r\n IF true THEN\r\n x := 1;\r\n END IF;\r\n test : IF true THEN\r\n x := y;\r\n END IF test;\r\n IF x > 2 THEN\r\n x := 5;\r\n ELSE\r\n y := 2;\r\n END IF;\r\n IF x > 3 THEN\r\n NULL;\r\n ELSIF x > 5 THEN\r\n NULL;\r\n ELSIF true THEN\r\n NULL;\r\n ELSE\r\n x := 2;\r\n END IF;\r\n END PROCESS;\r\n\r\n -- Null\r\n PROCESS IS\r\n BEGIN\r\n NULL;\r\n END PROCESS;\r\n\r\n -- Return\r\n PROCESS IS\r\n BEGIN\r\n RETURN 4 * 4;\r\n END PROCESS;\r\n\r\n -- While\r\n PROCESS IS\r\n BEGIN\r\n WHILE n > 0 LOOP\r\n n := n - 1;\r\n END LOOP;\r\n LOOP\r\n NULL;\r\n END LOOP;\r\n END PROCESS;\r\n\r\n -- Delayed assignment\r\n PROCESS IS\r\n BEGIN\r\n x <= 4 AFTER 5 ns;\r\n x <= 5 AFTER 1 ns, 7 AFTER 8 ns;\r\n x <= 5, 7 AFTER 8 ns;\r\n x <= INERTIAL 5;\r\n x <= TRANSPORT 4 AFTER 2 ns;\r\n x <= REJECT 4 ns INERTIAL 6 AFTER 10 ns;\r\n END PROCESS;\r\n\r\n -- For\r\n PROCESS IS\r\n BEGIN\r\n FOR i IN 0 TO 10 LOOP\r\n NULL;\r\n END LOOP;\r\n FOR i IN foo\'RANGE LOOP\r\n NULL;\r\n END LOOP;\r\n END PROCESS;\r\n\r\n -- Exit\r\n PROCESS IS\r\n BEGIN\r\n EXIT;\r\n EXIT WHEN x = 1;\r\n END PROCESS;\r\n\r\n -- Procedure call\r\n PROCESS IS\r\n BEGIN\r\n foo(x, y, 1);\r\n bar;\r\n foo(a => 1, b => 2, 3);\r\n END PROCESS;\r\n\r\n -- Case\r\n PROCESS IS\r\n BEGIN\r\n CASE x IS\r\n WHEN 1 =>\r\n NULL;\r\n WHEN 2 =>\r\n NULL;\r\n WHEN 3 | 4 =>\r\n NULL;\r\n WHEN OTHERS =>\r\n NULL;\r\n END CASE;\r\n END PROCESS;\r\n\r\n -- Next\r\n PROCESS IS\r\n BEGIN\r\n NEXT;\r\n NEXT WHEN foo = 5;\r\n END PROCESS;\r\n\r\n -- Signal assignment to aggregate\r\n PROCESS IS\r\n BEGIN\r\n (x, y, z) <= foo;\r\n END PROCESS;\r\n\r\n -- Case statement range bug\r\n PROCESS IS\r\n BEGIN\r\n CASE f IS\r\n WHEN 1 =>\r\n FOR i IN x\'RANGE LOOP\r\n END LOOP;\r\n END CASE;\r\n END PROCESS;\r\n\r\nEND ARCHITECTURE;';
  905. let actual = beautify(input, settings);
  906. assertAndCountTest("sequence", input, actual);
  907. }
  908. function IntegrationTest34() {
  909. let settings = GetDefaultSettings(" ");
  910. let input = 'ARCHITECTURE a OF b IS\r\n FOR x : y USE ENTITY work.foo;\r\n FOR x1, x2 : y USE ENTITY work.foo;\r\n FOR x : y USE ENTITY work.foo(bar);\r\n FOR x : y USE ENTITY work.foo(bar)\r\n GENERIC MAP(a => 1)\r\n PORT MAP(b => g);\r\n FOR ALL : y USE CONFIGURATION yah;\r\n FOR OTHERS : y USE OPEN;\r\nBEGIN\r\nEND ARCHITECTURE;';
  911. let actual = beautify(input, settings);
  912. assertAndCountTest("spec", input, actual);
  913. }
  914. function IntegrationTest35() {
  915. let settings = GetDefaultSettings(" ");
  916. let input = 'ARCHITECTURE a OF b IS\r\n TYPE my_int IS RANGE 0 TO 100;\r\n SIGNAL x : my_int := 2;\r\n\r\n TYPE resistance IS RANGE 0 TO 10000000\r\n UNITS\r\n ohm;\r\n kohm = 1000 ohm;\r\n Mohm = 1000 kohm;\r\n END UNITS;\r\n SIGNAL r : resistance := 100 ohm;\r\n\r\n SUBTYPE big_r IS resistance RANGE 1000 TO 2000;\r\n\r\n SUBTYPE my_small_int IS my_int RANGE 0 TO 5;\r\n\r\n SUBTYPE foo IS my_int RANGE 2 TO my_int\'high;\r\n\r\n SUBTYPE rint IS resolved my_int;\r\n\r\n TYPE p IS ACCESS my_int;\r\n\r\n TYPE f IS FILE OF my_int;\r\n\r\n FILE f1 : f OPEN READ_MODE IS "foo";\r\n\r\n FILE f2 : f IS "bar";\r\n\r\n FILE f3 : f;\r\n\r\n TYPE r1 IS RECORD\r\n a : INTEGER;\r\n b : INTEGER;\r\n c : foo(1 TO 5);\r\n END RECORD;\r\n\r\n FILE f4 : f IS OUT "bar"; -- VHDL-87 compat\r\n\r\n FILE f5 : f IS IN "bar"; -- VHDL-87 compat\r\n\r\n TYPE r2 IS RECORD\r\n x : INTEGER;\r\n END RECORD r2;\r\n\r\nBEGIN\r\n\r\nEND ARCHITECTURE;';
  917. let actual = beautify(input, settings);
  918. assertAndCountTest("types", input, actual);
  919. }
  920. function IntegrationTest36() {
  921. let settings = GetDefaultSettings(" ");
  922. let input = 'ARCHITECTURE test OF bufr_test IS\r\nBEGIN\r\n\r\n BUF_DATA_CLK : BUFR\r\n GENERIC MAP(\r\n BUFR_DIVIDE => "BYPASS",\r\n SIM_DEVICE => "7SERIES")\r\n PORT MAP(\r\n O => amu_adc_dco,\r\n CE => \'1\',\r\n CLR => \'0\',\r\n I => amu_adc_dco_i);\r\n\r\nEND ARCHITECTURE;';
  923. let actual = beautify(input, settings);
  924. assertAndCountTest("new line after (", input, actual);
  925. }
  926. function IntegrationTest37() {
  927. let settings = GetDefaultSettings(" ");
  928. let input = 'ENTITY nest1 IS\r\nEND ENTITY;\r\nARCHITECTURE test OF nest1 IS\r\nBEGIN\r\n PROCESS IS\r\n VARIABLE x : INTEGER := 2;\r\n VARIABLE y : bit_vector(7 DOWNTO 0);\r\n IMPURE FUNCTION add_to_x(y : INTEGER) RETURN INTEGER IS\r\n IMPURE FUNCTION do_it RETURN INTEGER IS\r\n BEGIN\r\n RETURN x + y;\r\n END FUNCTION;\r\n BEGIN\r\n RETURN do_it;\r\n END FUNCTION;\r\n BEGIN\r\n ASSERT add_to_x(5) = 7;\r\n WAIT;\r\n END PROCESS;\r\nEND ARCHITECTURE;';
  929. let actual = beautify(input, settings);
  930. assertAndCountTest("nested functions", input, actual);
  931. }
  932. function IntegrationTest38() {
  933. let settings = GetDefaultSettings(" ");
  934. let input = 'REPORT INTEGER\'image(a) & " " & INTEGER\'image(b) & " "\r\n & INTEGER\'image(c) & " " & INTEGER\'image(d) & " "\r\n & INTEGER\'image(e) & " " & INTEGER\'image(f);\r\nWAIT;';
  935. let actual = beautify(input, settings);
  936. assertAndCountTest("report severl lines", input, actual);
  937. }
  938. function IntegrationTest39() {
  939. let settings = GetDefaultSettings(" ");
  940. let input = 'assert v / = ( X "01", X "02" ) ;';
  941. let expected = 'ASSERT v /= (X "01", X "02");';
  942. let actual = beautify(input, settings);
  943. assertAndCountTest("signs", expected, actual);
  944. }
  945. function IntegrationTest40() {
  946. let settings = GetDefaultSettings();
  947. let input = 'PORT MAP\r\n (we => NOT cpu_rw, spo => ram_dout);\r\nPORT MAP(we => NOT cpu_rw, spo => ram_dout);\r\nPORT MAP\r\n(\r\n we => NOT cpu_rw, spo => ram_dout\r\n);';
  948. let actual = beautify(input, settings);
  949. assertAndCountTest("port map in newline", input, actual);
  950. }
  951. function IntegrationTest41() {
  952. let settings = GetDefaultSettings(" ");
  953. let input = 'ARCHITECTURE test3 OF test IS\r\n COMPONENT comp IS PORT (a : BOOLEAN);\r\n END COMPONENT;\r\n SIGNAL s_ok : BOOLEAN;\r\nBEGIN\r\n comp PORT MAP(a => s_ok); -- unlabeled component instantiation\r\nEND ARCHITECTURE;';
  954. let actual = beautify(input, settings);
  955. assertAndCountTest("end component", input, actual);
  956. }
  957. function IntegrationTest42() {
  958. let settings = GetDefaultSettings(" ");
  959. let input = 'ENTITY bar IS\r\nEND ENTITY bar;\r\nENTITY \\foo\\ IS\r\n PORT (test : IN BIT);\r\nEND ENTITY \\foo\\;\r\nARCHITECTURE structural OF \\foo\\ IS\r\nBEGIN -- architecture structural\r\nEND ARCHITECTURE structural;\r\nARCHITECTURE structural OF bar IS\r\n SIGNAL test : BIT;\r\nBEGIN -- architecture structural\r\n foo_1 : ENTITY work.\\foo\\\r\n PORT MAP(test => test);\r\nEND ARCHITECTURE structural;';
  960. let actual = beautify(input, settings);
  961. assertAndCountTest("end component", input, actual);
  962. }
  963. function IntegrationTest43() {
  964. let settings = GetDefaultSettings(" ");
  965. let input = 'ARCHITECTURE test OF issue122 IS\r\n IMPURE FUNCTION func(x : INTEGER) RETURN INTEGER IS\r\n IMPURE FUNCTION nested RETURN INTEGER IS\r\n BEGIN\r\n RETURN x;\r\n END FUNCTION;\r\n VARIABLE v : INTEGER := nested;\r\n BEGIN\r\n RETURN v;\r\n END FUNCTION;\r\nBEGIN\r\nEND ARCHITECTURE;';
  966. let actual = beautify(input, settings);
  967. assertAndCountTest("end component", input, actual);
  968. }
  969. function IntegrationTest44() {
  970. let settings = GetDefaultSettings(" ");
  971. let input = 'REPORT\n"A_ARITH_MOD_tester.main Tester is now ready. A total of " &\nINTEGER\'image(totalTests) & " tests have been detected.";';
  972. let expected = 'REPORT\r\n "A_ARITH_MOD_tester.main Tester is now ready. A total of " &\r\n INTEGER\'image(totalTests) & " tests have been detected.";';
  973. let actual = beautify(input, settings);
  974. assertAndCountTest("ingore keywords in quotes", expected, actual);
  975. }
  976. function IntegrationTest45() {
  977. let settings = GetDefaultSettings();
  978. settings.KeywordCase = "lowercase";
  979. settings.TypeNameCase = "lowercase";
  980. settings.Indentation = " ";
  981. let input = 'REPORT\n"A_ARITH_MOD_tester.main Tester is now ready. A total OF " &\nINTEGER\'image(totalTests) & " tests have been detected.";';
  982. let expected = 'report\r\n "A_ARITH_MOD_tester.main Tester is now ready. A total OF " &\r\n integer\'image(totalTests) & " tests have been detected.";';
  983. let actual = beautify(input, settings);
  984. assertAndCountTest("ingore keywords in quotes & convert to lowercase", expected, actual);
  985. }
  986. function IntegrationTest46() {
  987. let settings = GetDefaultSettings();
  988. settings.KeywordCase = "lowercase";
  989. settings.TypeNameCase = "lowercase";
  990. let input = 'impure function delay(\r\n l : integer\r\n) return time is\r\n variable r : real;\r\nbegin\r\n result := 2ps;\r\n return result;\r\nend function;';
  991. let actual = beautify(input, settings);
  992. assertAndCountTest("impure function indent", input, actual);
  993. }
  994. function IntegrationTest47() {
  995. let settings = GetDefaultSettings();
  996. settings.KeywordCase = "lowercase";
  997. settings.TypeNameCase = "lowercase";
  998. settings.Indentation = " ";
  999. let input = 'result := 1\r\n 1\r\n + 1; -- hello';
  1000. let actual = beautify(input, settings);
  1001. assertAndCountTest("multiline expression & comment", input, actual);
  1002. }
  1003. function IntegrationTest48() {
  1004. let settings = GetDefaultSettings();
  1005. settings.KeywordCase = "lowercase";
  1006. settings.TypeNameCase = "lowercase";
  1007. let input = 'function delay(\r\n l : integer\r\n) return time is\r\n variable r : real;\r\nbegin\r\n result := 2ps;\r\n return result;\r\nend function;';
  1008. let actual = beautify(input, settings);
  1009. assertAndCountTest("function indent", input, actual);
  1010. }
  1011. function IntegrationTest49() {
  1012. let settings = GetDefaultSettings();
  1013. settings.SignAlignSettings = new signAlignSettings(true, false, "", ["PROCEDURE"]);
  1014. let input = 'PROCEDURE wait_until(\r\n SIGNAL a : IN data_status;\r\n b : data_status\r\n);';
  1015. let actual = beautify(input, settings);
  1016. assertAndCountTest("align sign in procedure", input, actual);
  1017. }
  1018. function IntegrationTest50() {
  1019. let settings = GetDefaultSettings();
  1020. settings.SignAlignSettings = new signAlignSettings(true, false, "", []);
  1021. let input = 'PROCEDURE wait_until(\r\n SIGNAL a : IN data_status;\r\n b : data_status\r\n);';
  1022. let actual = beautify(input, settings);
  1023. assertAndCountTest("does not align sign in procedure", input, actual);
  1024. }
  1025. function IntegrationTest51() {
  1026. let settings = GetDefaultSettings();
  1027. settings.SignAlignSettings = new signAlignSettings(false, true, "", []);
  1028. let input = 'architecture behaviour of a is\r\nbegin\r\n main : process\r\n variable b : e := (others => DR_INIT);\r\n variable c, d : positive := 8;\r\n begin\r\n end process main;\r\nend architecture behaviour;';
  1029. let expected = 'ARCHITECTURE behaviour OF a IS\r\nBEGIN\r\n main : PROCESS\r\n VARIABLE b : e := (OTHERS => DR_INIT);\r\n VARIABLE c, d : POSITIVE := 8;\r\n BEGIN\r\n END PROCESS main;\r\nEND ARCHITECTURE behaviour;';
  1030. let actual = beautify(input, settings);
  1031. assertAndCountTest("process with name", expected, actual);
  1032. }
  1033. function IntegrationTest52() {
  1034. let settings = GetDefaultSettings();
  1035. settings.KeywordCase = "lowercase";
  1036. settings.TypeNameCase = "lowercase";
  1037. let input = 'function a(\r\n b : integer\r\n c : integer\r\n) return integer;\r\n\r\nimpure function a(\r\n b : integer\r\n c : integer\r\n) return integer;\r\n\r\nfunction a(\r\n b : integer\r\n c : integer\r\n) return integer;';
  1038. let actual = beautify(input, settings);
  1039. assertAndCountTest("function without sequential statements", input, actual);
  1040. }
  1041. function IntegrationTest53() {
  1042. let settings = GetDefaultSettings();
  1043. settings.KeywordCase = "lowercase";
  1044. settings.TypeNameCase = "lowercase";
  1045. let input = 'function a(\r\n b : integer\r\n c : integer\r\n) return integer;\r\n\r\nimpure function a(\r\n b : integer\r\n c : integer\r\n) return integer;\r\n\r\nfunction a(\r\n b : integer\r\n c : integer\r\n) return integer;';
  1046. let actual = beautify(input, settings);
  1047. assertAndCountTest("function without sequential statements, without new line", input, actual);
  1048. }
  1049. function IntegrationTest54() {
  1050. let settings = GetDefaultSettings();
  1051. let input = 'PACKAGE a IS\r\n FUNCTION b(\r\n c : INTEGER\r\n ) RETURN INTEGER;\r\nEND PACKAGE;';
  1052. let actual = beautify(input, settings);
  1053. assertAndCountTest("package & function without sequential statements", input, actual);
  1054. }
  1055. function IntegrationTest55() {
  1056. let settings = GetDefaultSettings();
  1057. settings.SignAlignSettings = new signAlignSettings(false, true, "", []);
  1058. let input = 'main :\r\nPROCESS\r\n VARIABLE b : a := (OTHERS => DR_INIT);\r\nBEGIN';
  1059. let actual = beautify(input, settings);
  1060. assertAndCountTest("package with label and align all symbols", input, actual);
  1061. }
  1062. function IntegrationTest56() {
  1063. let settings = GetDefaultSettings();
  1064. settings.SignAlignSettings = new signAlignSettings(false, true, "", []);
  1065. let input = 'a <= (2 => DR_ONE, 5 => DR_ZERO);\r\nbc <= (32 => DR_ONE);';
  1066. let actual = beautify(input, settings);
  1067. assertAndCountTest("package with label and align all symbols", input, actual);
  1068. }
  1069. function IntegrationTest57() {
  1070. let settings = GetDefaultSettings();
  1071. let input = 'result := (\'-\', \'1\');';
  1072. let actual = beautify(input, settings);
  1073. assertAndCountTest("- in quotes", input, actual);
  1074. }
  1075. function IntegrationTest58() {
  1076. let settings = GetDefaultSettings();
  1077. settings.KeywordCase = "lowercase";
  1078. settings.TypeNameCase = "lowercase";
  1079. let input = 'package body a is\r\n procedure b(\r\n signal a : in boolean;\r\n b : boolean\r\n ) is\r\n begin\r\n a = 1\r\n end procedure b;\r\nend a;';
  1080. let actual = beautify(input, settings);
  1081. assertAndCountTest("package body", input, actual);
  1082. }
  1083. function IntegrationTest59() {
  1084. let settings = GetDefaultSettings();
  1085. settings.KeywordCase = "lowercase";
  1086. settings.TypeNameCase = "lowercase";
  1087. let input = 'package body a is\r\n procedure b(\r\n signal a : in boolean;\r\n b : boolean) is\r\n begin\r\n a = 1\r\n end procedure b;\r\nend a;';
  1088. let actual = beautify(input, settings);
  1089. assertAndCountTest("package body 2", input, actual);
  1090. }
  1091. function IntegrationTest60() {
  1092. let settings = GetDefaultSettings();
  1093. let input = 'abcde : FOR i IN 0 TO b - 1 GENERATE\r\n b : A_REG_MOD\r\nEND GENERATE aaa;';
  1094. let actual = beautify(input, settings);
  1095. assertAndCountTest("generate with label", input, actual);
  1096. }
  1097. function IntegrationTest61() {
  1098. let settings = GetDefaultSettings();
  1099. settings.NewLineSettings.newLineAfter.push("port map")
  1100. let input = 'port\r\nmap(\r\na => a(i)\r\n);';
  1101. let expected = 'PORT MAP\r\n(\r\n a => a(i)\r\n);';
  1102. let actual = beautify(input, settings);
  1103. assertAndCountTest("port new line map", expected, actual);
  1104. }
  1105. function IntegrationTest62() {
  1106. let settings = GetDefaultSettings();
  1107. settings.NewLineSettings.newLineAfter.push("port map")
  1108. let input = 'port map(\r\na => a(i)\r\n);';
  1109. let expected = 'PORT MAP\r\n(\r\n a => a(i)\r\n);';
  1110. let actual = beautify(input, settings);
  1111. assertAndCountTest("port map new line", expected, actual);
  1112. }
  1113. function IntegrationTest63() {
  1114. let settings = GetDefaultSettings();
  1115. settings.NewLineSettings.newLineAfter.push("port", "port map");
  1116. let input = 'reg : a PORT\r\nMAP(\r\nb => c(i)\r\n);';
  1117. let expected = 'reg : a PORT MAP\r\n(\r\n b => c(i)\r\n);';
  1118. let actual = beautify(input, settings);
  1119. assertAndCountTest("port map new line 2", expected, actual);
  1120. }
  1121. function IntegrationTest64() {
  1122. let settings = GetDefaultSettings();
  1123. let input = 'reg : a PORT\r\nMAP(\r\nb => c(i)\r\n);';
  1124. let expected = 'reg : a PORT MAP(\r\n b => c(i)\r\n);';
  1125. let actual = beautify(input, settings);
  1126. assertAndCountTest("port map no new line", expected, actual);
  1127. }
  1128. function IntegrationTest65() {
  1129. let settings = GetDefaultSettings();
  1130. settings.NewLineSettings.noNewLineAfter.push("port", "port map");
  1131. let input = 'reg : a PORT\r\nMAP\r\n(\r\nb => c(i)\r\n);';
  1132. let expected = 'reg : a PORT MAP(\r\n b => c(i)\r\n);';
  1133. let actual = beautify(input, settings);
  1134. assertAndCountTest("port map no new line 2", expected, actual);
  1135. }
  1136. function IntegrationTest66() {
  1137. let settings = GetDefaultSettings();
  1138. settings.NewLineSettings.noNewLineAfter.push("port", "port map");
  1139. let input = 'component a is\r\n generic (b\r\n );\r\n port (\r\n c\r\n );\r\n end component;\r\n-- anything1\r\n-- anything2';
  1140. let expected = 'COMPONENT a IS\r\n GENERIC (\r\n b\r\n );\r\n PORT (\r\n c\r\n );\r\nEND COMPONENT;\r\n-- anything1\r\n-- anything2';
  1141. let actual = beautify(input, settings);
  1142. assertAndCountTest("component generic", expected, actual);
  1143. }
  1144. function IntegrationTest67() {
  1145. let settings = GetDefaultSettings();
  1146. let input = 'type STATE_TYPE is (\r\n A,\r\nB,\r\n C);\r\nA';
  1147. let expected = 'TYPE STATE_TYPE IS (\r\n A,\r\n B,\r\n C);\r\nA';
  1148. let actual = beautify(input, settings);
  1149. assertAndCountTest("multiline enumerated type is", expected, actual);
  1150. }
  1151. function IntegrationTest68() {
  1152. let settings = GetDefaultSettings();
  1153. let input = 'type STATE_TYPE is (A, B, C);\r\nA';
  1154. let expected = 'TYPE STATE_TYPE IS (A, B, C);\r\nA';
  1155. let actual = beautify(input, settings);
  1156. assertAndCountTest("single line enumerated type is", expected, actual);
  1157. }
  1158. function IntegrationTest69() {
  1159. let settings = GetDefaultSettings();
  1160. let input = 'type STATE_TYPE is (\r\n A,\r\nB,\r\n C\r\n);\r\nA';
  1161. let expected = 'TYPE STATE_TYPE IS (\r\n A,\r\n B,\r\n C\r\n);\r\nA';
  1162. let actual = beautify(input, settings);
  1163. assertAndCountTest("multiline enumerated type is", expected, actual);
  1164. }
  1165. function IntegrationTest70() {
  1166. let settings = GetDefaultSettings();
  1167. let input = 'test\r\n := test';
  1168. let expected = 'test\r\n:= test';
  1169. let actual = beautify(input, settings);
  1170. assertAndCountTest("multiline assignment", expected, actual);
  1171. }
  1172. function IntegrationTest71() {
  1173. let settings = GetDefaultSettings();
  1174. let input = 'VARIABLE \\#$)!?\\ : INTEGER;\r\nVARIABLE \\try this in verilog\\ : BIT;\r\nVARIABLE \\Buffer\\ : BIT;';
  1175. let expected = 'VARIABLE \\#$)!?\\ : INTEGER;\r\nVARIABLE \\try this in verilog\\ : BIT;\r\nVARIABLE \\Buffer\\ : BIT;';
  1176. let actual = beautify(input, settings);
  1177. assertAndCountTest("backslash, extended indentifier", expected, actual);
  1178. }
  1179. function IntegrationTest72() {
  1180. let settings = GetDefaultSettings();
  1181. let input = 'test := 12e+6';
  1182. let expected = 'test := 12e+6';
  1183. let actual = beautify(input, settings);
  1184. assertAndCountTest("scientific notation", expected, actual);
  1185. }
  1186. function IntegrationTest73() {
  1187. let settings = GetDefaultSettings();
  1188. settings.EndOfLine = "\n";
  1189. let input = 'test := test;\ntest := test;';
  1190. let actual = beautify(input, settings);
  1191. assertAndCountTest("end of line", input, actual);
  1192. }
  1193. function IntegrationTest74() {
  1194. let settings = GetDefaultSettings();
  1195. settings.EndOfLine = " EOF ";
  1196. let input = 'test := test;\ntest := test;';
  1197. let expected = 'test := test; EOF test := test;';
  1198. let actual = beautify(input, settings);
  1199. assertAndCountTest("end of line 2", expected, actual);
  1200. }
  1201. function IntegrationTest76() {
  1202. let settings = GetDefaultSettings();
  1203. settings.SignAlignSettings = new signAlignSettings(false, true, "", []);
  1204. let input = "a <= (b => '000'); -- test\r\nlooong <= (others => '0'); -- test";
  1205. let expected = "a <= (b => '000'); -- test\r\nlooong <= (OTHERS => '0'); -- test";
  1206. let actual = beautify(input, settings);
  1207. assertAndCountTest("align <= => signs", expected, actual);
  1208. }
  1209. function IntegrationTest77() {
  1210. let settings = GetDefaultSettings();
  1211. let input = "WHEN -2;\r\nSIGNAL +0;";
  1212. let actual = beautify(input, settings);
  1213. assertAndCountTest("negative sign and number after key word", input, actual);
  1214. }
  1215. function IntegrationTest78() {
  1216. let settings = GetDefaultSettings();
  1217. let input = "sfixed(4 downto - 18);\r\nx <= to_sfixed( - 2.3, x);\r\nresize(x + 1, 4, - 18) when others;";
  1218. let expected = "sfixed(4 DOWNTO -18);\r\nx <= to_sfixed(-2.3, x);\r\nresize(x + 1, 4, -18) WHEN OTHERS;";
  1219. let actual = beautify(input, settings);
  1220. assertAndCountTest("negative sign and number after symbol", expected, actual);
  1221. }
  1222. function IntegrationTest79() {
  1223. let settings = new BeautifierSettings(false, false, false, null, "lowercase", "uppercase", null, null, "\r\n", false);
  1224. let input = "case when others;\r\nx : STRING;\r\ny : BIT;";
  1225. let actual = beautify(input, settings);
  1226. assertAndCountTest("uppercase typename and lowercase keyword", input, actual);
  1227. }
  1228. function IntegrationTest80() {
  1229. let settings = GetDefaultSettings();
  1230. let input = 'FUNCTION "abs" (arg : sfixed) RETURN sfixed;';
  1231. let actual = beautify(input, settings);
  1232. assertAndCountTest("function name in quotes", input, actual);
  1233. }
  1234. function IntegrationTest81() { // TODO
  1235. let settings = GetDefaultSettings();
  1236. let input = 'test : IN type := 0\r\ntest : OUT type := 0\r\ntest : INOUT type := 0\r\ntest : BUFFER type := 0\r\ntest : LINKAGE type := 0';
  1237. let expected = 'test : IN type := 0\r\ntest : OUT type := 0\r\ntest : INOUT type := 0\r\ntest : BUFFER type := 0\r\ntest : LINKAGE type := 0';
  1238. let actual = beautify(input, settings);
  1239. assertAndCountTest("signal in out alignment", expected, actual);
  1240. }
  1241. function IntegrationTest82() {
  1242. let settings = GetDefaultSettings();
  1243. settings.SignAlignSettings = new signAlignSettings(false, true, "local", []);
  1244. let input = 'long : test;\r\n-- comment\r\nloooooong : test;';
  1245. let actual = beautify(input, settings);
  1246. assertAndCountTest("block alignment local", input, actual);
  1247. }
  1248. function IntegrationTest83() { // TODO
  1249. let settings = GetDefaultSettings();
  1250. let input = 'FUNCTION "<=" (l : ufixed; long : NATURAL) return BOOLEAN;\r\nFUNCTION ">" (l : ufixed; r : NATURAL) RETURN BOOLEAN;';
  1251. let actual = beautify(input, settings);
  1252. assertAndCountTest("signal in out alignment", input, actual);
  1253. }
  1254. function IntegrationTest84() {
  1255. let settings = GetDefaultSettings();
  1256. let input = 'PACKAGE f IS\r\n FUNCTION s (\r\n arg : sfixed) -- fp vector\r\n RETURN t;\r\nEND PACKAGE f;';
  1257. let actual = beautify(input, settings);
  1258. assertAndCountTest("empty body and return", input, actual);
  1259. }
  1260. function IntegrationTest85() {
  1261. let settings = GetDefaultSettings();
  1262. settings.NewLineSettings.newLineAfter = ["port"]
  1263. let input = 'REPORT "FIXED_GENERIC_PKG: Vector passed using a ""to"" range, expected is ""downto"""\r\n SEVERITY error;';
  1264. let actual = beautify(input, settings);
  1265. assertAndCountTest("report and severity", input, actual);
  1266. }
  1267. function IntegrationTest86() { // TODO
  1268. let settings = GetDefaultSettings();
  1269. let input = 'round_up(arg => arg,\r\n result => result,\r\n overflowx => round_overflow);';
  1270. let actual = beautify(input, settings);
  1271. assertAndCountTest("multilines in bracket", input, actual);
  1272. }
  1273. //TODO multiline setting, if true, \r\n\r\n -> \r\n\r\n, if false, \r\n\r\n -> \r\n
  1274. function GetDefaultSettings(indentation: string = " "): BeautifierSettings {
  1275. let new_line_after_symbols = new NewLineSettings();
  1276. new_line_after_symbols.newLineAfter = ["then", ";"];
  1277. new_line_after_symbols.noNewLineAfter = ["generic"];
  1278. let signAligns = new signAlignSettings(false, false, "", []);
  1279. let settings = getDefaultBeautifierSettings(new_line_after_symbols, signAligns, indentation);
  1280. return settings;
  1281. }
  1282. function getDefaultBeautifierSettings(newLineSettings: NewLineSettings, signAlignSettings: signAlignSettings = null, indentation: string = " "): BeautifierSettings {
  1283. return new BeautifierSettings(false, false, false, signAlignSettings, "uppercase", "uppercase", indentation, newLineSettings, "\r\n", false);
  1284. }
  1285. function IntegrationTest20() {
  1286. let settings = GetDefaultSettings();
  1287. let input = "process xyx (vf,fr,\r\nde -- comment\r\n)";
  1288. let expected = "PROCESS xyx (vf, fr, \r\n de -- comment\r\n )";
  1289. let actual = beautify(input, settings);
  1290. assertAndCountTest("Align parameters in PROCESS", expected, actual);
  1291. }
  1292. function IntegrationTest5() {
  1293. let settings = GetDefaultSettings();
  1294. settings.SignAlignSettings = new signAlignSettings(true, false, "", ["PORT"]);
  1295. let input = "port map(\r\ninput_1 => input_1_sig,\r\ninput_2 => input_2_sig,\r\noutput => output_sig\r\n);";
  1296. let expected = "PORT MAP(\r\n input_1 => input_1_sig,\r\n input_2 => input_2_sig,\r\n output => output_sig\r\n);";
  1297. let actual = beautify(input, settings);
  1298. assertAndCountTest("Sign align in PORT", expected, actual);
  1299. }
  1300. function IntegrationTest6() {
  1301. let new_line_after_symbols: NewLineSettings = new NewLineSettings();
  1302. new_line_after_symbols.newLineAfter = ["then", ";", "port map"];
  1303. new_line_after_symbols.noNewLineAfter = ["generic"];
  1304. let settings = getDefaultBeautifierSettings(new_line_after_symbols);
  1305. settings.SignAlignSettings = new signAlignSettings(true, false, "", ["PORT"]);
  1306. let input = "port map(\r\ninput_1 => input_1_sig,\r\ninput_2 => input_2_sig,\r\noutput => output_sig\r\n);";
  1307. let expected = "PORT MAP\r\n(\r\n input_1 => input_1_sig,\r\n input_2 => input_2_sig,\r\n output => output_sig\r\n);";
  1308. let actual = beautify(input, settings);
  1309. assertAndCountTest("Sign align in PORT & new line after MAP", expected, actual);
  1310. }
  1311. function IntegrationTest2() {
  1312. let new_line_after_symbols: NewLineSettings = new NewLineSettings();
  1313. new_line_after_symbols.newLineAfter = ["then", ";"];
  1314. new_line_after_symbols.noNewLineAfter = ["generic"];
  1315. let settings = getDefaultBeautifierSettings(new_line_after_symbols);
  1316. settings.RemoveComments = true;
  1317. let input = "architecture TB of TB_CPU is\r\n component CPU_IF\r\n port -- port list\r\n end component;\r\n signal CPU_DATA_VALID: std_ulogic;\r\n signal CLK, RESET: std_ulogic := '0';\r\n constant PERIOD : time := 10 ns;\r\n constant MAX_SIM: time := 50 * PERIOD;\r\n begin\r\n -- concurrent statements\r\n end TB;"
  1318. let expected = "ARCHITECTURE TB OF TB_CPU IS\r\n COMPONENT CPU_IF\r\n PORT\r\n END COMPONENT;\r\n SIGNAL CPU_DATA_VALID : std_ulogic;\r\n SIGNAL CLK, RESET : std_ulogic := '0';\r\n CONSTANT PERIOD : TIME := 10 ns;\r\n CONSTANT MAX_SIM : TIME := 50 * PERIOD;\r\nBEGIN\r\nEND TB;";
  1319. let actual = beautify(input, settings);
  1320. assertAndCountTest("Remove comments", expected, actual);
  1321. }
  1322. function assertAndCountTest(testName: string, expected: string, actual: string, message?: undefined) {
  1323. testCount++;
  1324. return assert(testName, expected, actual, message);
  1325. }
  1326. function CompareArray(actual: Array<string>, expected: Array<string>) {
  1327. var l = Math.min(actual.length, expected.length);
  1328. let result: string = "";
  1329. for (var i = 0; i < l; i++) {
  1330. if (actual[i] != expected[i]) {
  1331. result += CompareString(actual[i], expected[i]) + "\n";
  1332. }
  1333. }
  1334. if (actual.length > expected.length) {
  1335. result += "actual has more items";
  1336. for (var i = expected.length; i < actual.length; i++) {
  1337. result += "actual[" + i + "] = " + actual[i];
  1338. }
  1339. }
  1340. else if (actual.length < expected.length) {
  1341. result += "expected has more items";
  1342. for (var i = actual.length; i < expected.length; i++) {
  1343. result += "expected[" + i + "] = " + expected[i];
  1344. }
  1345. }
  1346. return true;
  1347. }