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.

1436 lines
76 KiB

  1. import { beautify } 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. testCount = 0;
  17. IntegrationTest();
  18. UnitTestRemoveAsserts();
  19. UnitTestApplyNoNewLineAfter();
  20. UnitTestSetNewLinesAfterSymbols();
  21. UnitTestFormattedLineToString();
  22. UnitTestbeautify3();
  23. console.log("total tests: " + testCount);
  24. testDescriptiveCounter();
  25. }
  26. interface Function {
  27. readonly name: string;
  28. }
  29. function UnitTestFormattedLineToString() {
  30. console.log("=== FormattedLineToString ===");
  31. FormattedLineToStringCase1();
  32. FormattedLineToStringCase2();
  33. }
  34. function FormattedLineToStringCase1() {
  35. let inputs: (FormattedLine | FormattedLine[])[] = [
  36. new FormattedLine("a;", 0),
  37. new FormattedLine("b;", 0)];
  38. let expected: Array<string> = ["a;", "b;"];
  39. UnitTest7(FormattedLineToString, "General", " ", inputs, expected);
  40. }
  41. function FormattedLineToStringCase2() {
  42. let inputs: (FormattedLine | FormattedLine[])[] = [
  43. new FormattedLine("a;", 1),
  44. new FormattedLine("b;", 2)];
  45. let expected: Array<string> = [" a;", " b;"];
  46. UnitTest7(FormattedLineToString, "General", " ", inputs, expected);
  47. }
  48. function UnitTestbeautify3() {
  49. console.log("=== beautify3 ===");
  50. Beautify3Case1();
  51. Beautify3Case2();
  52. Beautify3Case3();
  53. Beautify3Case4();
  54. Beautify3Case5();
  55. Beautify3Case6();
  56. Beautify3Case7();
  57. Beautify3Case8();
  58. Beautify3Case9();
  59. Beautify3Case10();
  60. Beautify3Case11();
  61. Beautify3Case12();
  62. Beautify3Case13();
  63. Beautify3Case14();
  64. Beautify3Case15();
  65. Beautify3Case16();
  66. Beautify3Case17();
  67. Beautify3Case18();
  68. Beautify3Case19();
  69. Beautify3Case20();
  70. Beautify3Case21();
  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: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", 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: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", 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: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", 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: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", 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: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", 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: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", 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: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", 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: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", 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: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", 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: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", 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: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", 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: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", 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: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", 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: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", 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: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", 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: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", 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: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", 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: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", 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: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", 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: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", 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: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", 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 UnitTestSetNewLinesAfterSymbols() {
  565. console.log("=== SetNewLinesAfterSymbols ===");
  566. let input = "a; @@comments1\r\nb;"
  567. let expected = "a; @@comments1\r\nb;";
  568. let parameters: NewLineSettings = new NewLineSettings();
  569. parameters.newLineAfter = ["then", ";"];
  570. parameters.noNewLineAfter = ["port", "generic"];
  571. UnitTest5(SetNewLinesAfterSymbols, "no new line after comment", parameters, input, expected);
  572. input = "a; b;"
  573. expected = "a;\r\nb;";
  574. UnitTest5(SetNewLinesAfterSymbols, "new line after ;", parameters, input, expected);
  575. }
  576. function UnitTestApplyNoNewLineAfter() {
  577. console.log("=== ApplyNoNewLineAfter ===");
  578. let input: Array<string> = ["a;", "b;"];
  579. let expected: Array<string> = ["a;@@singleline", "b;@@singleline"];
  580. let parameters: Array<string> = [";"];
  581. UnitTest4(ApplyNoNewLineAfter, "one blankspace", parameters, input, expected);
  582. input = ["a;", "b THEN", "c"];
  583. expected = ["a;@@singleline", "b THEN@@singleline", "c"];
  584. parameters = [";", "then"];
  585. UnitTest4(ApplyNoNewLineAfter, "one blankspace", parameters, input, expected);
  586. }
  587. function UnitTestRemoveAsserts() {
  588. console.log("=== RemoveAsserts ===");
  589. let input: Array<string> = ["ASSERT a;"];
  590. let expected: Array<string> = [""];
  591. UnitTest3(RemoveAsserts, "one assert", input, expected);
  592. input = ["ASSERT a", "b;", "c"];
  593. expected = ["", "", "c"];
  594. UnitTest3(RemoveAsserts, "multiline assert", input, expected);
  595. }
  596. function compareFormattedLines(expected: (FormattedLine | FormattedLine[])[], actual: (FormattedLine | FormattedLine[])[], message?): string {
  597. var l = Math.min(actual.length, expected.length);
  598. let result: string = "";
  599. for (var i = 0; i < l; i++) {
  600. if (actual[i] instanceof FormattedLine) {
  601. if (expected[i] instanceof FormattedLine) {
  602. let compareResult = compareFormattedLine(<FormattedLine>(expected[i]), <FormattedLine>(actual[i]), message, false);
  603. if (compareResult.length > 0) {
  604. result += "index " + i + "\n" + compareResult;
  605. }
  606. }
  607. else {
  608. result += "index " + i + "\nexpected FormatLine[], actual FormattedLine. actual:" + (<FormattedLine>(actual[i])).Line;
  609. }
  610. }
  611. else {
  612. if (expected[i] instanceof FormattedLine) {
  613. result += "index " + i + "\nexpected FormatLine, actual FormattedLine[]. expected:" + (<FormattedLine>(expected[i])).Line;
  614. }
  615. else {
  616. let compareResult = compareFormattedLines(<FormattedLine[]>(actual[i]), <FormattedLine[]>(expected[i]), message);
  617. if (compareResult.length > 0) {
  618. result += "index " + i + "\n" + compareResult;
  619. }
  620. }
  621. }
  622. }
  623. if (actual.length > expected.length) {
  624. result += "actual has more items";
  625. for (var i = expected.length; i < actual.length; i++) {
  626. result += "actual[" + i + "] = " + actual[i];
  627. }
  628. }
  629. else if (actual.length < expected.length) {
  630. result += "expected has more items";
  631. for (var i = actual.length; i < expected.length; i++) {
  632. result += "expected[" + i + "] = " + expected[i];
  633. }
  634. }
  635. return result;
  636. }
  637. function assertFormattedLines(testName, expected: (FormattedLine | FormattedLine[])[], actual: (FormattedLine | FormattedLine[])[], message?) {
  638. let result = compareFormattedLines(expected, actual, message);
  639. if (result.length > 0) {
  640. console.log(testName + " failed:\n" + result);
  641. }
  642. testCount++;
  643. }
  644. function compareFormattedLine(expected: FormattedLine, actual: FormattedLine, message?, cumulateTestCount?: boolean) {
  645. let result = "";
  646. if (expected.Indent != actual.Indent) {
  647. result += 'indents are not equal;\nexpected: "' + expected.Line + '", ' + expected.Indent
  648. + ';\nactual: "' + actual.Line + '", ' + actual.Indent + "\n";
  649. }
  650. let compareResult = CompareString(actual.Line, expected.Line);
  651. if (compareResult != true) {
  652. result += compareResult;
  653. }
  654. return result;
  655. }
  656. function assertArray(testName, expected, actual, message?) {
  657. var result = CompareArray(actual, expected);
  658. if (result != true) {
  659. console.log(testName + " failed: " + result);
  660. }
  661. else {
  662. //console.log(testName + " pass");
  663. }
  664. testCount++;
  665. }
  666. type StringCallback = (text: string) => string;
  667. type ArrayCallback = (arr: Array<string>) => void;
  668. type Array2Callback = (arr: Array<string>, parameters: Array<string>) => void;
  669. type String2Callback = (text: string, parameters: NewLineSettings) => string;
  670. type BeautifyCallback = (inputs: Array<string>, result: (FormattedLine | FormattedLine[])[], settings: BeautifierSettings, startIndex: number, indent: number) => number;
  671. type FormattedLinesCallback = (inputs: (FormattedLine | FormattedLine[])[], indentation: string) => Array<string>;
  672. function UnitTest7(func: FormattedLinesCallback, testName: string, indentation: string, inputs: (FormattedLine | FormattedLine[])[], expected: Array<string>) {
  673. let actual = func(inputs, indentation);
  674. assertArray(testName, expected, actual);
  675. }
  676. function UnitTest6(func: BeautifyCallback, testName: string, parameters: BeautifierSettings, inputs: Array<string>, expected: (FormattedLine | FormattedLine[])[], startIndex: number, expectedEndIndex: number, indent: number) {
  677. let actual: (FormattedLine | FormattedLine[])[] = []
  678. let endIndex: number = func(inputs, actual, parameters, startIndex, indent);
  679. if (endIndex != expectedEndIndex) {
  680. console.log(testName + " failed;\nend index, actual: " + endIndex + "; expected: " + expectedEndIndex)
  681. }
  682. assertFormattedLines(testName, expected, actual);
  683. }
  684. function UnitTest5(func: String2Callback, testName: string, parameters: NewLineSettings, inputs, expected: string) {
  685. let actual: string = func(inputs, parameters);
  686. assertAndCountTest(testName, expected, actual);
  687. }
  688. function UnitTest4(func: Array2Callback, testName: string, parameters: Array<string>, inputs: Array<string>, expected: Array<string>) {
  689. let actual = JSON.parse(JSON.stringify(inputs));
  690. func(actual, parameters);
  691. assertArray(testName, expected, actual);
  692. }
  693. function UnitTest3(func: ArrayCallback, testName: string, inputs: Array<string>, expected: Array<string>) {
  694. let actual = JSON.parse(JSON.stringify(inputs));
  695. func(actual);
  696. assertArray(testName, expected, actual);
  697. }
  698. function deepCopy(objectToCopy: BeautifierSettings): BeautifierSettings {
  699. return (JSON.parse(JSON.stringify(objectToCopy)));
  700. }
  701. function IntegrationTest() {
  702. console.log("=== IntegrationTests ===");
  703. let new_line_after_symbols: NewLineSettings = new NewLineSettings();
  704. new_line_after_symbols.newLineAfter = ["then", ";"];
  705. new_line_after_symbols.noNewLineAfter = ["port", "generic"];
  706. let settings: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
  707. 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;"
  708. 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;";
  709. let actual = beautify(input, settings);
  710. assertAndCountTest("General", expected, actual);
  711. IntegrationTest2();
  712. let new_line_after_symbols_2: NewLineSettings = new NewLineSettings();
  713. new_line_after_symbols_2.newLineAfter = [];
  714. new_line_after_symbols_2.noNewLineAfter = ["then", ";", "generic", "port"];
  715. let newSettings = deepCopy(settings);
  716. newSettings.NewLineSettings = new_line_after_symbols_2;
  717. expected = "a; b; c;";
  718. input = "a; \r\nb;\r\n c;"
  719. actual = beautify(input, newSettings);
  720. assertAndCountTest("Remove line after ;", expected, actual);
  721. newSettings = deepCopy(settings);
  722. newSettings.RemoveAsserts = true;
  723. 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;";
  724. expected = "ARCHITECTURE arch OF ent IS\r\nBEGIN\r\nEND ARCHITECTURE;"
  725. actual = beautify(input, newSettings);
  726. assertAndCountTest("Remove asserts", expected, actual);
  727. 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;";
  728. 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;";
  729. actual = beautify(input, settings);
  730. assertAndCountTest("ENTITY ARCHITECTURE", expected, actual);
  731. IntegrationTest5();
  732. IntegrationTest6();
  733. IntegrationTest7();
  734. input = 'if a(3 downto 0) > "0100" then\r\na(3 downto 0) := a(3 downto 0) + "0011" ;\r\nend if ;';
  735. expected = 'IF a(3 DOWNTO 0) > "0100" THEN\r\n a(3 DOWNTO 0) := a(3 DOWNTO 0) + "0011";\r\nEND IF;';
  736. actual = beautify(input, settings);
  737. assertAndCountTest("IF END IF case 1", expected, actual);
  738. input = "if s = '1' then\r\no <= \"010\";\r\nelse\r\no <= \"101\";\r\nend if;";
  739. expected = "IF s = '1' THEN\r\n o <= \"010\";\r\nELSE\r\n o <= \"101\";\r\nEND IF;";
  740. actual = beautify(input, settings);
  741. assertAndCountTest("IF ELSE END IF case 1", expected, actual);
  742. newSettings = deepCopy(settings);
  743. newSettings.NewLineSettings.newLineAfter.push("ELSE");
  744. input = "IF (s = r) THEN rr := '0'; ELSE rr := '1'; END IF;";
  745. expected = "IF (s = r) THEN\r\n rr := '0';\r\nELSE\r\n rr := '1';\r\nEND IF;";
  746. actual = beautify(input, newSettings);
  747. assertAndCountTest("IF ELSE END IF case 2", expected, actual);
  748. 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;';
  749. 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;';
  750. actual = beautify(input, settings);
  751. assertAndCountTest("WHEN CASE", expected, actual);
  752. 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;";
  753. 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;";
  754. actual = beautify(input, settings);
  755. assertAndCountTest("WHEN CASE & IF", expected, actual);
  756. 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;";
  757. 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;";
  758. actual = beautify(input, settings);
  759. assertAndCountTest("PORT MAP", expected, actual);
  760. 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;";
  761. 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;";
  762. actual = beautify(input, settings);
  763. assertAndCountTest("Multiple PORT MAPs", expected, actual);
  764. input = "port (a : in std_logic;\r\n b : in std_logic;\r\n);";
  765. expected = "PORT\r\n(\r\n a : IN std_logic;\r\n b : IN std_logic;\r\n);";
  766. new_line_after_symbols_2 = new NewLineSettings();
  767. new_line_after_symbols_2.newLineAfter = ["then", ";", "generic", "port"];
  768. newSettings = deepCopy(settings);
  769. newSettings.NewLineSettings = new_line_after_symbols_2;
  770. actual = beautify(input, newSettings);
  771. assertAndCountTest("New line after PORT", expected, actual);
  772. newSettings = deepCopy(settings);
  773. newSettings.NewLineSettings.newLineAfter = [];
  774. input = "component a is\r\nport( Data : inout Std_Logic_Vector(7 downto 0););\r\nend component a;";
  775. expected = "COMPONENT a IS\r\n PORT (Data : INOUT Std_Logic_Vector(7 DOWNTO 0););\r\nEND COMPONENT a;";
  776. actual = beautify(input, newSettings);
  777. assertAndCountTest("New line after PORT (single line)", expected, actual);
  778. //IntegrationTest20();
  779. 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;";
  780. 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;";
  781. actual = beautify(input, newSettings);
  782. assertAndCountTest("Double BEGIN", expected, actual);
  783. let newSettings2 = deepCopy(newSettings);
  784. newSettings2.SignAlignAll = true;
  785. newSettings2.NewLineSettings.newLineAfter = ["then", ";", "generic", "port"];
  786. newSettings2.NewLineSettings.noNewLineAfter = [];
  787. 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 ;";
  788. 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;";
  789. actual = beautify(input, newSettings2);
  790. assertAndCountTest("Align signs in all places", expected, actual);
  791. IntegrationTest23();
  792. IntegrationTest24();
  793. IntegrationTest25();
  794. IntegrationTest26();
  795. IntegrationTest27();
  796. IntegrationTest28();
  797. IntegrationTest29();
  798. IntegrationTest30();
  799. IntegrationTest31();
  800. IntegrationTest32();
  801. IntegrationTest33();
  802. IntegrationTest34();
  803. IntegrationTest35();
  804. IntegrationTest36();
  805. IntegrationTest37();
  806. IntegrationTest38();
  807. IntegrationTest39();
  808. IntegrationTest40();
  809. IntegrationTest41();
  810. IntegrationTest42();
  811. IntegrationTest43();
  812. IntegrationTest44();
  813. IntegrationTest45();
  814. IntegrationTest46();
  815. IntegrationTest47();
  816. IntegrationTest48();
  817. IntegrationTest49();
  818. IntegrationTest50();
  819. IntegrationTest51();
  820. IntegrationTest52();
  821. IntegrationTest53();
  822. IntegrationTest54();
  823. IntegrationTest55();
  824. IntegrationTest56();
  825. IntegrationTest57();
  826. IntegrationTest58();
  827. IntegrationTest59();
  828. IntegrationTest60();
  829. IntegrationTest61();
  830. IntegrationTest62();
  831. IntegrationTest63();
  832. IntegrationTest64();
  833. IntegrationTest65();
  834. IntegrationTest66();
  835. IntegrationTest67();
  836. IntegrationTest68();
  837. IntegrationTest69();
  838. }
  839. function IntegrationTest23() {
  840. let new_line_after_symbols = new NewLineSettings();
  841. new_line_after_symbols.newLineAfter = ["then", ";"];
  842. new_line_after_symbols.noNewLineAfter = ["generic"];
  843. let settings = new BeautifierSettings(false, false, false, false, false, "uppercase", "\t", new_line_after_symbols);
  844. let input = "PACKAGE p IS\r\n TYPE int_array IS ARRAY (INTEGER RANGE <>) OF INTEGER;\r\n TYPE ten_ints IS ARRAY (1 TO 10) OF INTEGER;\r\n TYPE chars IS (A, B, C);\r\n TYPE char_counts IS ARRAY (chars) OF INTEGER;\r\n TYPE two_d IS ARRAY (1 TO 3, 4 TO 6) OF INTEGER;\r\n TYPE ab_chars IS ARRAY (chars RANGE A TO B) OF INTEGER;\r\nEND PACKAGE;";
  845. let actual = beautify(input, settings);
  846. assertAndCountTest("Type array", input, actual);
  847. }
  848. function IntegrationTest24() {
  849. let new_line_after_symbols = new NewLineSettings();
  850. new_line_after_symbols.newLineAfter = ["then", ";"];
  851. new_line_after_symbols.noNewLineAfter = ["generic"];
  852. let settings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
  853. 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;";
  854. let actual = beautify(input, settings);
  855. assertAndCountTest("attribute", input, actual);
  856. }
  857. function IntegrationTest25() {
  858. let new_line_after_symbols = new NewLineSettings();
  859. new_line_after_symbols.newLineAfter = ["then", ";"];
  860. new_line_after_symbols.noNewLineAfter = ["generic"];
  861. let settings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
  862. 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;';
  863. let actual = beautify(input, settings);
  864. assertAndCountTest("bitstring", input, actual);
  865. }
  866. function IntegrationTest26() {
  867. let new_line_after_symbols = new NewLineSettings();
  868. new_line_after_symbols.newLineAfter = ["then", ";"];
  869. new_line_after_symbols.noNewLineAfter = ["generic"];
  870. let settings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
  871. 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;';
  872. let actual = beautify(input, settings);
  873. assertAndCountTest("block", input, actual);
  874. }
  875. function IntegrationTest27() {
  876. let new_line_after_symbols = new NewLineSettings();
  877. new_line_after_symbols.newLineAfter = ["then", ";"];
  878. new_line_after_symbols.noNewLineAfter = ["generic"];
  879. let settings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
  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 new_line_after_symbols = new NewLineSettings();
  886. new_line_after_symbols.newLineAfter = ["then", ";"];
  887. new_line_after_symbols.noNewLineAfter = ["generic"];
  888. let settings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
  889. 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;';
  890. let actual = beautify(input, settings);
  891. assertAndCountTest("extended \\ 28", input, actual);
  892. }
  893. function IntegrationTest29() {
  894. let new_line_after_symbols = new NewLineSettings();
  895. new_line_after_symbols.newLineAfter = ["then", ";"];
  896. new_line_after_symbols.noNewLineAfter = ["generic"];
  897. let settings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
  898. 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;';
  899. let actual = beautify(input, settings);
  900. assertAndCountTest("extended \\ 29", input, actual);
  901. }
  902. function IntegrationTest30() {
  903. let new_line_after_symbols = new NewLineSettings();
  904. new_line_after_symbols.newLineAfter = ["then", ";"];
  905. new_line_after_symbols.noNewLineAfter = ["generic"];
  906. let settings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
  907. 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;';
  908. let actual = beautify(input, settings);
  909. assertAndCountTest("generate", input, actual);
  910. }
  911. function IntegrationTest31() {
  912. let new_line_after_symbols = new NewLineSettings();
  913. new_line_after_symbols.newLineAfter = ["then", ";"];
  914. new_line_after_symbols.noNewLineAfter = ["generic"];
  915. let settings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
  916. 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;';
  917. let actual = beautify(input, settings);
  918. assertAndCountTest("report \"\"", input, actual);
  919. }
  920. function IntegrationTest32() {
  921. let new_line_after_symbols = new NewLineSettings();
  922. new_line_after_symbols.newLineAfter = ["then", ";"];
  923. new_line_after_symbols.noNewLineAfter = ["generic"];
  924. let settings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
  925. 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;';
  926. let actual = beautify(input, settings);
  927. assertAndCountTest("protected", input, actual);
  928. }
  929. function IntegrationTest33() {
  930. let new_line_after_symbols = new NewLineSettings();
  931. new_line_after_symbols.newLineAfter = ["then", ";"];
  932. new_line_after_symbols.noNewLineAfter = ["generic"];
  933. let settings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
  934. 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;';
  935. let actual = beautify(input, settings);
  936. assertAndCountTest("sequence", input, actual);
  937. }
  938. function IntegrationTest34() {
  939. let new_line_after_symbols = new NewLineSettings();
  940. new_line_after_symbols.newLineAfter = ["then", ";"];
  941. new_line_after_symbols.noNewLineAfter = ["generic"];
  942. let settings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
  943. 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;';
  944. let actual = beautify(input, settings);
  945. assertAndCountTest("spec", input, actual);
  946. }
  947. function IntegrationTest35() {
  948. let new_line_after_symbols = new NewLineSettings();
  949. new_line_after_symbols.newLineAfter = ["then", ";"];
  950. new_line_after_symbols.noNewLineAfter = ["generic"];
  951. let settings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
  952. 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;';
  953. let actual = beautify(input, settings);
  954. assertAndCountTest("types", input, actual);
  955. }
  956. function IntegrationTest36() {
  957. let new_line_after_symbols = new NewLineSettings();
  958. new_line_after_symbols.newLineAfter = ["then", ";"];
  959. new_line_after_symbols.noNewLineAfter = ["generic"];
  960. let settings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
  961. 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;';
  962. let actual = beautify(input, settings);
  963. assertAndCountTest("new line after (", input, actual);
  964. }
  965. function IntegrationTest37() {
  966. let new_line_after_symbols = new NewLineSettings();
  967. new_line_after_symbols.newLineAfter = ["then", ";"];
  968. new_line_after_symbols.noNewLineAfter = ["generic"];
  969. let settings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
  970. 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;';
  971. let actual = beautify(input, settings);
  972. assertAndCountTest("nested functions", input, actual);
  973. }
  974. function IntegrationTest38() {
  975. let new_line_after_symbols = new NewLineSettings();
  976. new_line_after_symbols.newLineAfter = ["then", ";"];
  977. new_line_after_symbols.noNewLineAfter = ["generic"];
  978. let settings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
  979. 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;';
  980. let actual = beautify(input, settings);
  981. assertAndCountTest("report severl lines", input, actual);
  982. }
  983. function IntegrationTest39() {
  984. let new_line_after_symbols = new NewLineSettings();
  985. new_line_after_symbols.newLineAfter = ["then", ";"];
  986. new_line_after_symbols.noNewLineAfter = ["generic"];
  987. let settings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
  988. let input = 'assert v / = ( X "01", X "02" ) ;';
  989. let expected = 'ASSERT v /= (X "01", X "02");';
  990. let actual = beautify(input, settings);
  991. assertAndCountTest("signs", expected, actual);
  992. }
  993. function IntegrationTest40() {
  994. let new_line_after_symbols = new NewLineSettings();
  995. new_line_after_symbols.newLineAfter = ["then", ";"];
  996. new_line_after_symbols.noNewLineAfter = ["generic"];
  997. let settings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
  998. 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);';
  999. let actual = beautify(input, settings);
  1000. assertAndCountTest("port map in newline", input, actual);
  1001. }
  1002. function IntegrationTest41() {
  1003. let new_line_after_symbols = new NewLineSettings();
  1004. new_line_after_symbols.newLineAfter = ["then", ";"];
  1005. new_line_after_symbols.noNewLineAfter = ["generic"];
  1006. let settings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
  1007. 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;';
  1008. let actual = beautify(input, settings);
  1009. assertAndCountTest("end component", input, actual);
  1010. }
  1011. function IntegrationTest42() {
  1012. let new_line_after_symbols = new NewLineSettings();
  1013. new_line_after_symbols.newLineAfter = ["then", ";"];
  1014. new_line_after_symbols.noNewLineAfter = ["generic"];
  1015. let settings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
  1016. 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;';
  1017. let actual = beautify(input, settings);
  1018. assertAndCountTest("end component", input, actual);
  1019. }
  1020. function IntegrationTest43() {
  1021. let new_line_after_symbols = new NewLineSettings();
  1022. new_line_after_symbols.newLineAfter = ["then", ";"];
  1023. new_line_after_symbols.noNewLineAfter = ["generic"];
  1024. let settings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
  1025. 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;';
  1026. let actual = beautify(input, settings);
  1027. assertAndCountTest("end component", input, actual);
  1028. }
  1029. function IntegrationTest44() {
  1030. let new_line_after_symbols = new NewLineSettings();
  1031. new_line_after_symbols.newLineAfter = ["then", ";"];
  1032. new_line_after_symbols.noNewLineAfter = ["generic"];
  1033. let settings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
  1034. let input = 'REPORT\n"A_ARITH_MOD_tester.main Tester is now ready. A total of " &\nINTEGER\'image(totalTests) & " tests have been detected.";';
  1035. 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.";';
  1036. let actual = beautify(input, settings);
  1037. assertAndCountTest("ingore keywords in quotes", expected, actual);
  1038. }
  1039. function IntegrationTest45() {
  1040. let settings = GetDefaultSettings();
  1041. settings.KeywordCase = "lowercase";
  1042. settings.Indentation = " ";
  1043. let input = 'REPORT\n"A_ARITH_MOD_tester.main Tester is now ready. A total OF " &\nINTEGER\'image(totalTests) & " tests have been detected.";';
  1044. 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.";';
  1045. let actual = beautify(input, settings);
  1046. assertAndCountTest("ingore keywords in quotes & convert to lowercase", expected, actual);
  1047. }
  1048. function IntegrationTest46() {
  1049. let settings = GetDefaultSettings();
  1050. settings.KeywordCase = "lowercase";
  1051. 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;';
  1052. let actual = beautify(input, settings);
  1053. assertAndCountTest("impure function indent", input, actual);
  1054. }
  1055. function IntegrationTest47() {
  1056. let settings = GetDefaultSettings();
  1057. settings.KeywordCase = "lowercase";
  1058. settings.Indentation = " ";
  1059. let input = 'result := 1\r\n 1\r\n + 1; -- hello';
  1060. let actual = beautify(input, settings);
  1061. assertAndCountTest("multiline expression & comment", input, actual);
  1062. }
  1063. function IntegrationTest48() {
  1064. let settings = GetDefaultSettings();
  1065. settings.KeywordCase = "lowercase";
  1066. 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;';
  1067. let actual = beautify(input, settings);
  1068. assertAndCountTest("function indent", input, actual);
  1069. }
  1070. function IntegrationTest49() {
  1071. let settings = GetDefaultSettings();
  1072. settings.SignAlignRegional = true;
  1073. settings.SignAlignKeyWords = ["PROCEDURE"];
  1074. let input = 'PROCEDURE wait_until(\r\n SIGNAL a : IN data_status;\r\n b : data_status\r\n);';
  1075. let actual = beautify(input, settings);
  1076. assertAndCountTest("align sign in procedure", input, actual);
  1077. }
  1078. function IntegrationTest50() {
  1079. let settings = GetDefaultSettings();
  1080. settings.SignAlignRegional = true;
  1081. let input = 'PROCEDURE wait_until(\r\n SIGNAL a : IN data_status;\r\n b : data_status\r\n);';
  1082. let actual = beautify(input, settings);
  1083. assertAndCountTest("does not align sign in procedure", input, actual);
  1084. }
  1085. function IntegrationTest51() {
  1086. let settings = GetDefaultSettings();
  1087. settings.SignAlignAll = true;
  1088. 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;';
  1089. 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;';
  1090. let actual = beautify(input, settings);
  1091. assertAndCountTest("process with name", expected, actual);
  1092. }
  1093. function IntegrationTest52() {
  1094. let settings = GetDefaultSettings();
  1095. settings.KeywordCase = "lowercase";
  1096. 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;';
  1097. let actual = beautify(input, settings);
  1098. assertAndCountTest("function without sequential statements", input, actual);
  1099. }
  1100. function IntegrationTest53() {
  1101. let settings = GetDefaultSettings();
  1102. settings.KeywordCase = "lowercase";
  1103. 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;';
  1104. let actual = beautify(input, settings);
  1105. assertAndCountTest("function without sequential statements, without new line", input, actual);
  1106. }
  1107. function IntegrationTest54() {
  1108. let settings = GetDefaultSettings();
  1109. let input = 'PACKAGE a IS\r\n FUNCTION b(\r\n c : INTEGER\r\n ) RETURN INTEGER;\r\nEND PACKAGE;';
  1110. let actual = beautify(input, settings);
  1111. assertAndCountTest("package & function without sequential statements", input, actual);
  1112. }
  1113. function IntegrationTest55() {
  1114. let settings = GetDefaultSettings();
  1115. settings.SignAlignAll = true;
  1116. let input = 'main :\r\nPROCESS\r\n VARIABLE b : a := (OTHERS => DR_INIT);\r\nBEGIN';
  1117. let actual = beautify(input, settings);
  1118. assertAndCountTest("package with label and align all symbols", input, actual);
  1119. }
  1120. function IntegrationTest56() {
  1121. let settings = GetDefaultSettings();
  1122. settings.SignAlignAll = true;
  1123. let input = 'a <= (2 => DR_ONE, 5 => DR_ZERO);\r\nbc <= (32 => DR_ONE);';
  1124. let actual = beautify(input, settings);
  1125. assertAndCountTest("package with label and align all symbols", input, actual);
  1126. }
  1127. function IntegrationTest57() {
  1128. let settings = GetDefaultSettings();
  1129. let input = 'result := (\'-\', \'1\');';
  1130. let actual = beautify(input, settings);
  1131. assertAndCountTest("- in quotes", input, actual);
  1132. }
  1133. function IntegrationTest58() {
  1134. let settings = GetDefaultSettings();
  1135. settings.KeywordCase = "lowercase";
  1136. 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;';
  1137. let actual = beautify(input, settings);
  1138. assertAndCountTest("package body", input, actual);
  1139. }
  1140. function IntegrationTest59() {
  1141. let settings = GetDefaultSettings();
  1142. settings.KeywordCase = "lowercase";
  1143. 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;';
  1144. let actual = beautify(input, settings);
  1145. assertAndCountTest("package body 2", input, actual);
  1146. }
  1147. function IntegrationTest60() {
  1148. let settings = GetDefaultSettings();
  1149. let input = 'abcde : FOR i IN 0 TO b - 1 GENERATE\r\n b : A_REG_MOD\r\nEND GENERATE aaa;';
  1150. let actual = beautify(input, settings);
  1151. assertAndCountTest("generate with label", input, actual);
  1152. }
  1153. function IntegrationTest61() {
  1154. let settings = GetDefaultSettings();
  1155. settings.NewLineSettings.newLineAfter.push("port map")
  1156. let input = 'port\r\nmap(\r\na => a(i)\r\n);';
  1157. let expected = 'PORT MAP\r\n(\r\n a => a(i)\r\n);';
  1158. let actual = beautify(input, settings);
  1159. assertAndCountTest("port new line map", expected, actual);
  1160. }
  1161. function IntegrationTest62() {
  1162. let settings = GetDefaultSettings();
  1163. settings.NewLineSettings.newLineAfter.push("port map")
  1164. let input = 'port map(\r\na => a(i)\r\n);';
  1165. let expected = 'PORT MAP\r\n(\r\n a => a(i)\r\n);';
  1166. let actual = beautify(input, settings);
  1167. assertAndCountTest("port map new line", expected, actual);
  1168. }
  1169. function IntegrationTest63() {
  1170. let settings = GetDefaultSettings();
  1171. settings.NewLineSettings.newLineAfter.push("port", "port map");
  1172. let input = 'reg : a PORT\r\nMAP(\r\nb => c(i)\r\n);';
  1173. let expected = 'reg : a PORT MAP\r\n(\r\n b => c(i)\r\n);';
  1174. let actual = beautify(input, settings);
  1175. assertAndCountTest("port map new line 2", expected, actual);
  1176. }
  1177. function IntegrationTest64() {
  1178. let settings = GetDefaultSettings();
  1179. let input = 'reg : a PORT\r\nMAP(\r\nb => c(i)\r\n);';
  1180. let expected = 'reg : a PORT MAP(\r\n b => c(i)\r\n);';
  1181. let actual = beautify(input, settings);
  1182. assertAndCountTest("port map no new line", expected, actual);
  1183. }
  1184. function IntegrationTest65() {
  1185. let settings = GetDefaultSettings();
  1186. settings.NewLineSettings.noNewLineAfter.push("port", "port map");
  1187. let input = 'reg : a PORT\r\nMAP\r\n(\r\nb => c(i)\r\n);';
  1188. let expected = 'reg : a PORT MAP(\r\n b => c(i)\r\n);';
  1189. let actual = beautify(input, settings);
  1190. assertAndCountTest("port map no new line 2", expected, actual);
  1191. }
  1192. function IntegrationTest66() {
  1193. let settings = GetDefaultSettings();
  1194. settings.NewLineSettings.noNewLineAfter.push("port", "port map");
  1195. 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';
  1196. 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';
  1197. let actual = beautify(input, settings);
  1198. assertAndCountTest("port map no new line 2", expected, actual);
  1199. }
  1200. function IntegrationTest67() {
  1201. let settings = GetDefaultSettings();
  1202. let input = 'type STATE_TYPE is (\r\n A,\r\nB,\r\n C);\r\nA';
  1203. let expected = 'TYPE STATE_TYPE IS (\r\n A,\r\n B,\r\n C);\r\nA';
  1204. let actual = beautify(input, settings);
  1205. assertAndCountTest("multiline enumerated type is", expected, actual);
  1206. }
  1207. function IntegrationTest68() {
  1208. let settings = GetDefaultSettings();
  1209. let input = 'type STATE_TYPE is (A, B, C);\r\nA';
  1210. let expected = 'TYPE STATE_TYPE IS (A, B, C);\r\nA';
  1211. let actual = beautify(input, settings);
  1212. assertAndCountTest("single line enumerated type is", expected, actual);
  1213. }
  1214. function IntegrationTest69() {
  1215. let settings = GetDefaultSettings();
  1216. let input = 'type STATE_TYPE is (\r\n A,\r\nB,\r\n C\r\n);\r\nA';
  1217. let expected = 'TYPE STATE_TYPE IS (\r\n A,\r\n B,\r\n C\r\n);\r\nA';
  1218. let actual = beautify(input, settings);
  1219. assertAndCountTest("multiline enumerated type is", expected, actual);
  1220. }
  1221. function GetDefaultSettings() {
  1222. let new_line_after_symbols = new NewLineSettings();
  1223. new_line_after_symbols.newLineAfter = ["then", ";"];
  1224. new_line_after_symbols.noNewLineAfter = ["generic"];
  1225. let settings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
  1226. return settings;
  1227. }
  1228. function IntegrationTest20() {
  1229. let new_line_after_symbols = new NewLineSettings();
  1230. new_line_after_symbols.newLineAfter = ["then", ";"];
  1231. new_line_after_symbols.noNewLineAfter = ["generic"];
  1232. let settings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
  1233. let input = "process xyx (vf,fr,\r\nde -- comment\r\n)";
  1234. let expected = "PROCESS xyx (vf, fr, \r\n de -- comment\r\n )";
  1235. let actual = beautify(input, settings);
  1236. assertAndCountTest("Align parameters in PROCESS", expected, actual);
  1237. }
  1238. function IntegrationTest5() {
  1239. let new_line_after_symbols: NewLineSettings = new NewLineSettings();
  1240. new_line_after_symbols.newLineAfter = ["then", ";"];
  1241. new_line_after_symbols.noNewLineAfter = ["generic"];
  1242. let settings: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
  1243. settings.SignAlignRegional = true;
  1244. settings.SignAlignKeyWords = ["PORT"];
  1245. let input = "port map(\r\ninput_1 => input_1_sig,\r\ninput_2 => input_2_sig,\r\noutput => output_sig\r\n);";
  1246. 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);";
  1247. let actual = beautify(input, settings);
  1248. assertAndCountTest("Sign align in PORT", expected, actual);
  1249. }
  1250. function IntegrationTest6() {
  1251. let new_line_after_symbols: NewLineSettings = new NewLineSettings();
  1252. new_line_after_symbols.newLineAfter = ["then", ";", "port map"];
  1253. new_line_after_symbols.noNewLineAfter = ["generic"];
  1254. let settings: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
  1255. settings.SignAlignRegional = true;
  1256. settings.SignAlignKeyWords = ["PORT"];
  1257. let input = "port map(\r\ninput_1 => input_1_sig,\r\ninput_2 => input_2_sig,\r\noutput => output_sig\r\n);";
  1258. 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);";
  1259. let actual = beautify(input, settings);
  1260. assertAndCountTest("Sign align in PORT & new line after MAP", expected, actual);
  1261. }
  1262. function IntegrationTest7() {
  1263. let new_line_after_symbols: NewLineSettings = new NewLineSettings();
  1264. new_line_after_symbols.newLineAfter = ["then", ";"];
  1265. let settings: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
  1266. settings.SignAlignRegional = true;
  1267. settings.SignAlignKeyWords = ["PORT", "GENERIC"];
  1268. 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;";
  1269. 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;";
  1270. let actual = beautify(input, settings);
  1271. assertAndCountTest("Sign align in PORT & GENERIC", expected, actual);
  1272. }
  1273. function IntegrationTest2() {
  1274. let new_line_after_symbols: NewLineSettings = new NewLineSettings();
  1275. new_line_after_symbols.newLineAfter = ["then", ";"];
  1276. new_line_after_symbols.noNewLineAfter = ["generic"];
  1277. let settings: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
  1278. settings.RemoveComments = true;
  1279. 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;"
  1280. 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;";
  1281. let actual = beautify(input, settings);
  1282. assertAndCountTest("Remove comments", expected, actual);
  1283. }
  1284. function assertAndCountTest(testName: string, expected: string, actual: string, message?: undefined) {
  1285. testCount++;
  1286. return assert(testName, expected, actual, message);
  1287. }
  1288. function CompareArray(actual: Array<string>, expected: Array<string>) {
  1289. var l = Math.min(actual.length, expected.length);
  1290. let result: string = "";
  1291. for (var i = 0; i < l; i++) {
  1292. if (actual[i] != expected[i]) {
  1293. result += CompareString(actual[i], expected[i]) + "\n";
  1294. }
  1295. }
  1296. if (actual.length > expected.length) {
  1297. result += "actual has more items";
  1298. for (var i = expected.length; i < actual.length; i++) {
  1299. result += "actual[" + i + "] = " + actual[i];
  1300. }
  1301. }
  1302. else if (actual.length < expected.length) {
  1303. result += "expected has more items";
  1304. for (var i = actual.length; i < expected.length; i++) {
  1305. result += "expected[" + i + "] = " + expected[i];
  1306. }
  1307. }
  1308. return true;
  1309. }