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.

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