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.

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