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.

781 lines
38 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. import { beautify } from "./VHDLFormatter";
  2. import { indentDecode } from "./VHDLFormatter";
  3. import { NewLineSettings } from "./VHDLFormatter";
  4. import { BeautifierSettings } from "./VHDLFormatter";
  5. import { RemoveAsserts } from "./VHDLFormatter";
  6. import { ApplyNoNewLineAfter } from "./VHDLFormatter";
  7. import { SetNewLinesAfterSymbols } from "./VHDLFormatter";
  8. import { beautify3 } from "./VHDLFormatter";
  9. import { FormattedLine } from "./VHDLFormatter";
  10. import { FormattedLineToString } from "./VHDLFormatter";
  11. let testCount: number = 0;
  12. var showUnitTests = true;//window.location.href.indexOf("http") < 0;
  13. if (showUnitTests) {
  14. testCount = 0;
  15. UnitTest();
  16. UnitTestIndentDecode();
  17. UnitTestRemoveAsserts();
  18. UnitTestApplyNoNewLineAfter();
  19. UnitTestSetNewLinesAfterSymbols();
  20. UnitTestFormattedLineToString();
  21. UnitTestbeautify3();
  22. console.log("total tests: " + testCount);
  23. }
  24. interface Function {
  25. readonly name: string;
  26. }
  27. function UnitTestFormattedLineToString() {
  28. console.log("=== FormattedLineToString ===");
  29. FormattedLineToStringCase1();
  30. FormattedLineToStringCase2();
  31. }
  32. function FormattedLineToStringCase1() {
  33. let inputs: (FormattedLine | FormattedLine[])[] = [
  34. new FormattedLine("a;", 0),
  35. new FormattedLine("b;", 0)];
  36. let expected: Array<string> = ["a;", "b;"];
  37. UnitTest7(FormattedLineToString, "General", " ", inputs, expected);
  38. }
  39. function FormattedLineToStringCase2() {
  40. let inputs: (FormattedLine | FormattedLine[])[] = [
  41. new FormattedLine("a;", 1),
  42. new FormattedLine("b;", 2)];
  43. let expected: Array<string> = [" a;", " b;"];
  44. UnitTest7(FormattedLineToString, "General", " ", inputs, expected);
  45. }
  46. function UnitTestbeautify3() {
  47. console.log("=== beautify3 ===");
  48. Beautify3Case1();
  49. Beautify3Case2();
  50. Beautify3Case3();
  51. Beautify3Case4();
  52. Beautify3Case5();
  53. Beautify3Case6();
  54. Beautify3Case7();
  55. Beautify3Case8();
  56. Beautify3Case9();
  57. Beautify3Case10();
  58. Beautify3Case11();
  59. Beautify3Case12();
  60. Beautify3Case13();
  61. Beautify3Case14();
  62. }
  63. function Beautify3Case1() {
  64. let new_line_after_symbols: NewLineSettings = new NewLineSettings();
  65. new_line_after_symbols.newLineAfter = ["then", ";"];
  66. new_line_after_symbols.noNewLineAfter = ["port", "generic"];
  67. let settings: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
  68. let inputs: Array<string> = ["a;", "b;"];
  69. let expected: (FormattedLine | FormattedLine[])[] = [new FormattedLine("a;", 0), new FormattedLine("b;", 0)];
  70. UnitTest6(beautify3, "General", settings, inputs, expected, 0, 1, 0);
  71. }
  72. function Beautify3Case2() {
  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> = ["IF x = '1' THEN", "RETURN 1;", "END IF;"];
  78. let expected: (FormattedLine | FormattedLine[])[] = [
  79. new FormattedLine("IF x = '1' THEN", 0),
  80. new FormattedLine("RETURN 1;", 1),
  81. new FormattedLine("END IF;", 0)
  82. ];
  83. UnitTest6(beautify3, "IF END", settings, inputs, expected, 0, 2, 0);
  84. }
  85. function Beautify3Case3() {
  86. let new_line_after_symbols: NewLineSettings = new NewLineSettings();
  87. new_line_after_symbols.newLineAfter = ["then", ";"];
  88. new_line_after_symbols.noNewLineAfter = ["port", "generic"];
  89. let settings: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
  90. let inputs: Array<string> = [
  91. "IF x = '1' THEN",
  92. "RETURN 1;",
  93. "ELSIF x = '0' THEN",
  94. "RETURN 0;",
  95. "ELSE",
  96. "RETURN -1;",
  97. "END IF;"];
  98. let expected: (FormattedLine | FormattedLine[])[] = [
  99. new FormattedLine("IF x = '1' THEN", 0),
  100. new FormattedLine("RETURN 1;", 1),
  101. new FormattedLine("ELSIF x = '0' THEN", 0),
  102. new FormattedLine("RETURN 0;", 1),
  103. new FormattedLine("ELSE", 0),
  104. new FormattedLine("RETURN -1;", 1),
  105. new FormattedLine("END IF;", 0)
  106. ];
  107. UnitTest6(beautify3, "if elsif else end", settings, inputs, expected, 0, 6, 0);
  108. }
  109. function Beautify3Case4() {
  110. let new_line_after_symbols: NewLineSettings = new NewLineSettings();
  111. new_line_after_symbols.newLineAfter = ["then", ";"];
  112. new_line_after_symbols.noNewLineAfter = ["port", "generic"];
  113. let settings: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
  114. let inputs: Array<string> = ["END"];
  115. let expected: (FormattedLine | FormattedLine[])[] = [new FormattedLine("END", 0)];
  116. UnitTest6(beautify3, "one line END", settings, inputs, expected, 0, 0, 0);
  117. }
  118. function Beautify3Case5() {
  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> = [
  124. "CASE b",
  125. "WHEN 1 =>",
  126. "c <= d;",
  127. "WHEN 2 =>",
  128. "d <= f;",
  129. "END CASE;"
  130. ];
  131. let expected: (FormattedLine | FormattedLine[])[] = [
  132. new FormattedLine("CASE b", 0),
  133. new FormattedLine("WHEN 1 =>", 1),
  134. new FormattedLine("c <= d;", 2),
  135. new FormattedLine("WHEN 2 =>", 1),
  136. new FormattedLine("d <= f;", 2),
  137. new FormattedLine("END CASE;", 0)
  138. ];
  139. UnitTest6(beautify3, "case when when end", settings, inputs, expected, 0, 5, 0);
  140. }
  141. function Beautify3Case6() {
  142. let new_line_after_symbols: NewLineSettings = new NewLineSettings();
  143. new_line_after_symbols.newLineAfter = ["then", ";"];
  144. new_line_after_symbols.noNewLineAfter = ["port", "generic"];
  145. let settings: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
  146. let inputs: Array<string> = [
  147. "CASE b",
  148. "WHEN 1 =>",
  149. "c <= d;",
  150. "CASE b",
  151. "WHEN 1 =>",
  152. "c <= d;",
  153. "WHEN 2 =>",
  154. "d <= f;",
  155. "END CASE;",
  156. "WHEN 2 =>",
  157. "d <= f;",
  158. "END CASE;"
  159. ];
  160. let expected: (FormattedLine | FormattedLine[])[] = [
  161. new FormattedLine("CASE b", 0),
  162. new FormattedLine("WHEN 1 =>", 1),
  163. new FormattedLine("c <= d;", 2),
  164. new FormattedLine("CASE b", 2),
  165. new FormattedLine("WHEN 1 =>", 3),
  166. new FormattedLine("c <= d;", 4),
  167. new FormattedLine("WHEN 2 =>", 3),
  168. new FormattedLine("d <= f;", 4),
  169. new FormattedLine("END CASE;", 2),
  170. new FormattedLine("WHEN 2 =>", 1),
  171. new FormattedLine("d <= f;", 2),
  172. new FormattedLine("END CASE;", 0)
  173. ];
  174. UnitTest6(beautify3, "case & case end", settings, inputs, expected, 0, 11, 0);
  175. }
  176. function Beautify3Case7() {
  177. let new_line_after_symbols: NewLineSettings = new NewLineSettings();
  178. new_line_after_symbols.newLineAfter = ["then", ";"];
  179. new_line_after_symbols.noNewLineAfter = ["port", "generic"];
  180. let settings: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
  181. let inputs: Array<string> = [
  182. "ARCHITECTURE a OF one IS",
  183. "SIGNAL x : INTEGER;",
  184. "BEGIN",
  185. "-- architecture",
  186. "END ARCHITECTURE;"
  187. ];
  188. let expected: (FormattedLine | FormattedLine[])[] = [
  189. new FormattedLine("ARCHITECTURE a OF one IS", 0),
  190. new FormattedLine("SIGNAL x : INTEGER;", 1),
  191. new FormattedLine("BEGIN", 0),
  192. new FormattedLine("-- architecture", 1),
  193. new FormattedLine("END ARCHITECTURE;", 0),
  194. ];
  195. UnitTest6(beautify3, "architecture", settings, inputs, expected, 0, 4, 0);
  196. }
  197. function Beautify3Case8() {
  198. let new_line_after_symbols: NewLineSettings = new NewLineSettings();
  199. new_line_after_symbols.newLineAfter = ["then", ";"];
  200. new_line_after_symbols.noNewLineAfter = ["port", "generic"];
  201. let settings: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
  202. let inputs: Array<string> = [
  203. "ARCHITECTURE a OF one IS",
  204. "SIGNAL x : INTEGER;",
  205. "BEGIN",
  206. "-- architecture",
  207. "END ARCHITECTURE;",
  208. "ARCHITECTURE b OF one IS",
  209. "SIGNAL x : INTEGER;",
  210. "BEGIN",
  211. "-- architecture",
  212. "END ARCHITECTURE;"
  213. ];
  214. let expected: (FormattedLine | FormattedLine[])[] = [
  215. new FormattedLine("ARCHITECTURE a OF one IS", 0),
  216. new FormattedLine("SIGNAL x : INTEGER;", 1),
  217. new FormattedLine("BEGIN", 0),
  218. new FormattedLine("-- architecture", 1),
  219. new FormattedLine("END ARCHITECTURE;", 0),
  220. new FormattedLine("ARCHITECTURE b OF one IS", 0),
  221. new FormattedLine("SIGNAL x : INTEGER;", 1),
  222. new FormattedLine("BEGIN", 0),
  223. new FormattedLine("-- architecture", 1),
  224. new FormattedLine("END ARCHITECTURE;", 0),
  225. ];
  226. UnitTest6(beautify3, "architecture 2", settings, inputs, expected, 0, 9, 0);
  227. }
  228. function Beautify3Case9() {
  229. let new_line_after_symbols: NewLineSettings = new NewLineSettings();
  230. new_line_after_symbols.newLineAfter = ["then", ";"];
  231. new_line_after_symbols.noNewLineAfter = ["port", "generic"];
  232. let settings: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
  233. let inputs: Array<string> = [
  234. "PROCEDURE foo(x : IN INTEGER; y : OUT INTEGER) IS",
  235. "VARIABLE i : INTEGER;",
  236. "BEGIN",
  237. "y := x + 1;",
  238. "END PROCEDURE;"
  239. ];
  240. let expected: (FormattedLine | FormattedLine[])[] = [
  241. new FormattedLine("PROCEDURE foo(x : IN INTEGER; y : OUT INTEGER) IS", 0),
  242. new FormattedLine("VARIABLE i : INTEGER;", 1),
  243. new FormattedLine("BEGIN", 0),
  244. new FormattedLine("y := x + 1;", 1),
  245. new FormattedLine("END PROCEDURE;", 0)
  246. ];
  247. UnitTest6(beautify3, "procedure", settings, inputs, expected, 0, 4, 0);
  248. }
  249. function Beautify3Case10() {
  250. let new_line_after_symbols: NewLineSettings = new NewLineSettings();
  251. new_line_after_symbols.newLineAfter = ["then", ";"];
  252. new_line_after_symbols.noNewLineAfter = ["port", "generic"];
  253. let settings: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
  254. let inputs: Array<string> = [
  255. "PACKAGE three IS",
  256. "SIGNAL s : INTEGER;",
  257. "ALIAS sa IS s;",
  258. "END PACKAGE;"
  259. ];
  260. let expected: (FormattedLine | FormattedLine[])[] = [
  261. new FormattedLine("PACKAGE three IS", 0),
  262. new FormattedLine("SIGNAL s : INTEGER;", 1),
  263. new FormattedLine("ALIAS sa IS s;", 1),
  264. new FormattedLine("END PACKAGE;", 0)
  265. ];
  266. UnitTest6(beautify3, "package", settings, inputs, expected, 0, 3, 0);
  267. }
  268. function Beautify3Case11() {
  269. let new_line_after_symbols: NewLineSettings = new NewLineSettings();
  270. new_line_after_symbols.newLineAfter = ["then", ";"];
  271. new_line_after_symbols.noNewLineAfter = ["port", "generic"];
  272. let settings: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
  273. let inputs: Array<string> = [
  274. "PACKAGE p IS",
  275. "PROCEDURE foo(x : IN INTEGER; y : OUT INTEGER);",
  276. "END PACKAGE;",
  277. "PACKAGE BODY p IS",
  278. "PROCEDURE foo(x : IN INTEGER; y : OUT INTEGER) IS",
  279. "VARIABLE i : INTEGER;",
  280. "BEGIN",
  281. "y := x + 1;",
  282. "END PROCEDURE;",
  283. "PROCEDURE bar(FILE x : text);",
  284. "PROCEDURE baz IS",
  285. "TYPE foo;",
  286. "ALIAS x IS y;",
  287. "BEGIN",
  288. "END PROCEDURE;",
  289. "PROCEDURE tralala IS",
  290. "USE work.foo;",
  291. "BEGIN",
  292. "END PROCEDURE;",
  293. "END PACKAGE BODY;"
  294. ];
  295. let expected: (FormattedLine | FormattedLine[])[] = [
  296. new FormattedLine("PACKAGE p IS", 0),
  297. new FormattedLine("PROCEDURE foo(x : IN INTEGER; y : OUT INTEGER);", 1),
  298. new FormattedLine("END PACKAGE;", 0),
  299. new FormattedLine("PACKAGE BODY p IS", 0),
  300. new FormattedLine("PROCEDURE foo(x : IN INTEGER; y : OUT INTEGER) IS", 1),
  301. new FormattedLine("VARIABLE i : INTEGER;", 2),
  302. new FormattedLine("BEGIN", 1),
  303. new FormattedLine("y := x + 1;", 2),
  304. new FormattedLine("END PROCEDURE;", 1),
  305. new FormattedLine("PROCEDURE bar(FILE x : text);", 1),
  306. new FormattedLine("PROCEDURE baz IS", 1),
  307. new FormattedLine("TYPE foo;", 2),
  308. new FormattedLine("ALIAS x IS y;", 2),
  309. new FormattedLine("BEGIN", 1),
  310. new FormattedLine("END PROCEDURE;", 1),
  311. new FormattedLine("PROCEDURE tralala IS", 1),
  312. new FormattedLine("USE work.foo;", 2),
  313. new FormattedLine("BEGIN", 1),
  314. new FormattedLine("END PROCEDURE;", 1),
  315. new FormattedLine("END PACKAGE BODY;", 0)
  316. ];
  317. UnitTest6(beautify3, "package", settings, inputs, expected, 0, expected.length - 1, 0);
  318. }
  319. function Beautify3Case12() {
  320. let new_line_after_symbols: NewLineSettings = new NewLineSettings();
  321. new_line_after_symbols.newLineAfter = ["then", ";"];
  322. new_line_after_symbols.noNewLineAfter = ["port", "generic"];
  323. let settings: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
  324. let inputs: Array<string> = [
  325. "ARCHITECTURE a OF b IS",
  326. "SIGNAL x : INTEGER := 0;",
  327. "BEGIN",
  328. "p: PROCESS IS",
  329. "BEGIN",
  330. "END PROCESS;",
  331. "PROCESS",
  332. "VARIABLE y : INTEGER := 5;",
  333. "BEGIN",
  334. "x <= y;",
  335. "END PROCESS;",
  336. "PROCESS (x) IS",
  337. "BEGIN",
  338. "x <= x + 1;",
  339. "END PROCESS;",
  340. "POSTPONED PROCESS IS",
  341. "BEGIN",
  342. "END PROCESS;",
  343. "POSTPONED assert x = 1;",
  344. "END ARCHITECTURE;"
  345. ];
  346. let expected: (FormattedLine | FormattedLine[])[] = [
  347. new FormattedLine("ARCHITECTURE a OF b IS", 0),
  348. new FormattedLine("SIGNAL x : INTEGER := 0;", 1),
  349. new FormattedLine("BEGIN", 0),
  350. new FormattedLine("p: PROCESS IS", 1),
  351. new FormattedLine("BEGIN", 1),
  352. new FormattedLine("END PROCESS;", 1),
  353. new FormattedLine("PROCESS", 1),
  354. new FormattedLine("VARIABLE y : INTEGER := 5;", 2),
  355. new FormattedLine("BEGIN", 1),
  356. new FormattedLine("x <= y;", 2),
  357. new FormattedLine("END PROCESS;", 1),
  358. new FormattedLine("PROCESS (x) IS", 1),
  359. new FormattedLine("BEGIN", 1),
  360. new FormattedLine("x <= x + 1;", 2),
  361. new FormattedLine("END PROCESS;", 1),
  362. new FormattedLine("POSTPONED PROCESS IS", 1),
  363. new FormattedLine("BEGIN", 1),
  364. new FormattedLine("END PROCESS;", 1),
  365. new FormattedLine("POSTPONED assert x = 1;", 1),
  366. new FormattedLine("END ARCHITECTURE;", 0)
  367. ];
  368. UnitTest6(beautify3, "package", settings, inputs, expected, 0, expected.length - 1, 0);
  369. }
  370. function Beautify3Case13() {
  371. let new_line_after_symbols: NewLineSettings = new NewLineSettings();
  372. new_line_after_symbols.newLineAfter = ["then", ";"];
  373. new_line_after_symbols.noNewLineAfter = ["port", "generic"];
  374. let settings: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
  375. let inputs: Array<string> = [
  376. "TYPE SharedCounter IS PROTECTED",
  377. "PROCEDURE increment (N : INTEGER := 1);",
  378. "IMPURE FUNCTION value RETURN INTEGER;",
  379. "END PROTECTED SharedCounter;"
  380. ];
  381. let expected: (FormattedLine | FormattedLine[])[] = [
  382. new FormattedLine("TYPE SharedCounter IS PROTECTED", 0),
  383. new FormattedLine("PROCEDURE increment (N : INTEGER := 1);", 1),
  384. new FormattedLine("IMPURE FUNCTION value RETURN INTEGER;", 1),
  385. new FormattedLine("END PROTECTED SharedCounter;", 0)
  386. ];
  387. UnitTest6(beautify3, "type projected", settings, inputs, expected, 0, expected.length - 1, 0);
  388. }
  389. function Beautify3Case14() {
  390. let new_line_after_symbols: NewLineSettings = new NewLineSettings();
  391. new_line_after_symbols.newLineAfter = ["then", ";"];
  392. new_line_after_symbols.noNewLineAfter = ["port", "generic"];
  393. let settings: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
  394. let inputs: Array<string> = [
  395. "PACKAGE p IS",
  396. "TYPE SharedCounter IS PROTECTED",
  397. "PROCEDURE increment (N : INTEGER := 1);",
  398. "IMPURE FUNCTION value RETURN INTEGER;",
  399. "END PROTECTED SharedCounter;",
  400. "TYPE SharedCounter IS PROTECTED BODY"
  401. ];
  402. let expected: (FormattedLine | FormattedLine[])[] = [
  403. new FormattedLine("PACKAGE p IS", 0),
  404. new FormattedLine("TYPE SharedCounter IS PROTECTED", 1),
  405. new FormattedLine("PROCEDURE increment (N : INTEGER := 1);", 2),
  406. new FormattedLine("IMPURE FUNCTION value RETURN INTEGER;", 2),
  407. new FormattedLine("END PROTECTED SharedCounter;", 1),
  408. new FormattedLine("TYPE SharedCounter IS PROTECTED BODY", 1)
  409. ];
  410. UnitTest6(beautify3, "type projected", settings, inputs, expected, 0, expected.length - 1, 0);
  411. }
  412. function UnitTestSetNewLinesAfterSymbols() {
  413. console.log("=== SetNewLinesAfterSymbols ===");
  414. let input = "a; @@comments1\r\nb;"
  415. let expected = "a; @@comments1\r\nb;";
  416. let parameters: NewLineSettings = new NewLineSettings();
  417. parameters.newLineAfter = ["then", ";"];
  418. parameters.noNewLineAfter = ["port", "generic"];
  419. UnitTest5(SetNewLinesAfterSymbols, "no new line after comment", parameters, input, expected);
  420. input = "a; b;"
  421. expected = "a;\r\nb;";
  422. UnitTest5(SetNewLinesAfterSymbols, "new line after ;", parameters, input, expected);
  423. }
  424. function UnitTestApplyNoNewLineAfter() {
  425. console.log("=== ApplyNoNewLineAfter ===");
  426. let input: Array<string> = ["a;", "b;"];
  427. let expected: Array<string> = ["a;@@singleline", "b;@@singleline"];
  428. let parameters: Array<string> = [";"];
  429. UnitTest4(ApplyNoNewLineAfter, "one blankspace", parameters, input, expected);
  430. input = ["a;", "b THEN", "c"];
  431. expected = ["a;@@singleline", "b THEN@@singleline", "c"];
  432. parameters = [";", "then"];
  433. UnitTest4(ApplyNoNewLineAfter, "one blankspace", parameters, input, expected);
  434. }
  435. function UnitTestRemoveAsserts() {
  436. console.log("=== RemoveAsserts ===");
  437. let input: Array<string> = ["ASSERT a;"];
  438. let expected: Array<string> = [""];
  439. UnitTest3(RemoveAsserts, "one assert", input, expected);
  440. input = ["ASSERT a", "b;", "c"];
  441. expected = ["", "", "c"];
  442. UnitTest3(RemoveAsserts, "multiline assert", input, expected);
  443. }
  444. function UnitTestIndentDecode() {
  445. console.log("=== IndentDecode ===");
  446. UnitTest2(indentDecode, "one blankspace", " ", "one blankspace");
  447. UnitTest2(indentDecode, "mixed chars", " A ", "one blankspace & one A & one blankspace");
  448. UnitTest2(indentDecode, "4 blankspaces", " ", "four blankspace");
  449. UnitTest2(indentDecode, "9 blankspaces", " ", "many blankspace");
  450. }
  451. function compareFormattedLines(expected: (FormattedLine | FormattedLine[])[], actual: (FormattedLine | FormattedLine[])[], message?): string {
  452. var l = Math.min(actual.length, expected.length);
  453. let result: string = "";
  454. for (var i = 0; i < l; i++) {
  455. if (actual[i] instanceof FormattedLine) {
  456. if (expected[i] instanceof FormattedLine) {
  457. let compareResult = compareFormattedLine(<FormattedLine>(expected[i]), <FormattedLine>(actual[i]), message, false);
  458. if (compareResult.length > 0) {
  459. result += "index " + i + "\n" + compareResult;
  460. }
  461. }
  462. else {
  463. result += "index " + i + "\nexpected FormatLine[], actual FormattedLine. actual:" + (<FormattedLine>(actual[i])).Line;
  464. }
  465. }
  466. else {
  467. if (expected[i] instanceof FormattedLine) {
  468. result += "index " + i + "\nexpected FormatLine, actual FormattedLine[]. expected:" + (<FormattedLine>(expected[i])).Line;
  469. }
  470. else {
  471. let compareResult = compareFormattedLines(<FormattedLine[]>(actual[i]), <FormattedLine[]>(expected[i]), message);
  472. if (compareResult.length > 0) {
  473. result += "index " + i + "\n" + compareResult;
  474. }
  475. }
  476. }
  477. }
  478. if (actual.length > expected.length) {
  479. result += "actual has more items";
  480. for (var i = expected.length; i < actual.length; i++) {
  481. result += "actual[" + i + "] = " + actual[i];
  482. }
  483. }
  484. else if (actual.length < expected.length) {
  485. result += "expected has more items";
  486. for (var i = actual.length; i < expected.length; i++) {
  487. result += "expected[" + i + "] = " + expected[i];
  488. }
  489. }
  490. return result;
  491. }
  492. function assertFormattedLines(testName, expected: (FormattedLine | FormattedLine[])[], actual: (FormattedLine | FormattedLine[])[], message?) {
  493. let result = compareFormattedLines(expected, actual, message);
  494. if (result.length > 0) {
  495. console.log(testName + " failed:\n" + result);
  496. }
  497. testCount++;
  498. }
  499. function compareFormattedLine(expected: FormattedLine, actual: FormattedLine, message?, cumulateTestCount?: boolean) {
  500. let result = "";
  501. if (expected.Indent != actual.Indent) {
  502. result += 'indents are not equal;\nexpected: "' + expected.Line + '", ' + expected.Indent
  503. + ';\nactual: "' + actual.Line + '", ' + actual.Indent + "\n";
  504. }
  505. let compareResult = CompareString(actual.Line, expected.Line);
  506. if (compareResult != true) {
  507. result += compareResult;
  508. }
  509. return result;
  510. }
  511. function assert(testName, expected: string, actual: string, message?) {
  512. var result = CompareString(actual, expected);
  513. if (result != true) {
  514. console.log(testName + " failed: " + result);
  515. }
  516. else {
  517. //console.log(testName + " pass");
  518. }
  519. testCount++;
  520. }
  521. function assertArray(testName, expected, actual, message?) {
  522. var result = CompareArray(actual, expected);
  523. if (result != true) {
  524. console.log(testName + " failed: " + result);
  525. }
  526. else {
  527. //console.log(testName + " pass");
  528. }
  529. testCount++;
  530. }
  531. type StringCallback = (text: string) => string;
  532. type ArrayCallback = (arr: Array<string>) => void;
  533. type Array2Callback = (arr: Array<string>, parameters: Array<string>) => void;
  534. type String2Callback = (text: string, parameters: NewLineSettings) => string;
  535. type BeautifyCallback = (inputs: Array<string>, result: (FormattedLine | FormattedLine[])[], settings: BeautifierSettings, startIndex: number, indent: number) => number;
  536. type FormattedLinesCallback = (inputs: (FormattedLine | FormattedLine[])[], indentation: string) => Array<string>;
  537. function UnitTest7(func: FormattedLinesCallback, testName: string, indentation: string, inputs: (FormattedLine | FormattedLine[])[], expected: Array<string>) {
  538. let actual = func(inputs, indentation);
  539. assertArray(testName, expected, actual);
  540. }
  541. function UnitTest6(func: BeautifyCallback, testName: string, parameters: BeautifierSettings, inputs: Array<string>, expected: (FormattedLine | FormattedLine[])[], startIndex: number, expectedEndIndex: number, indent: number) {
  542. let actual: (FormattedLine | FormattedLine[])[] = []
  543. let endIndex: number = func(inputs, actual, parameters, startIndex, indent);
  544. if (endIndex != expectedEndIndex) {
  545. console.log(testName + " failed;\nend index, actual: " + endIndex + "; expected: " + expectedEndIndex)
  546. }
  547. assertFormattedLines(testName, expected, actual);
  548. }
  549. function UnitTest5(func: String2Callback, testName: string, parameters: NewLineSettings, inputs, expected: string) {
  550. let actual: string = func(inputs, parameters);
  551. assert(testName, expected, actual);
  552. }
  553. function UnitTest4(func: Array2Callback, testName: string, parameters: Array<string>, inputs: Array<string>, expected: Array<string>) {
  554. let actual = JSON.parse(JSON.stringify(inputs));
  555. func(actual, parameters);
  556. assertArray(testName, expected, actual);
  557. }
  558. function UnitTest3(func: ArrayCallback, testName: string, inputs: Array<string>, expected: Array<string>) {
  559. let actual = JSON.parse(JSON.stringify(inputs));
  560. func(actual);
  561. assertArray(testName, expected, actual);
  562. }
  563. function UnitTest2(func: StringCallback, testName: string, inputs, expected: string) {
  564. let actual: string = func(inputs);
  565. assert(testName, expected, actual);
  566. }
  567. function deepCopy(objectToCopy: BeautifierSettings): BeautifierSettings {
  568. return (JSON.parse(JSON.stringify(objectToCopy)));
  569. }
  570. function UnitTest() {
  571. let new_line_after_symbols: NewLineSettings = new NewLineSettings();
  572. new_line_after_symbols.newLineAfter = ["then", ";"];
  573. new_line_after_symbols.noNewLineAfter = ["port", "generic"];
  574. let settings: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
  575. 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;"
  576. 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;";
  577. let actual = beautify(input, settings);
  578. assert("General", expected, actual);
  579. IntegrationTest2();
  580. let new_line_after_symbols_2: NewLineSettings = new NewLineSettings();
  581. new_line_after_symbols_2.newLineAfter = [];
  582. new_line_after_symbols_2.noNewLineAfter = ["then", ";", "generic", "port"];
  583. let newSettings = deepCopy(settings);
  584. newSettings.NewLineSettings = new_line_after_symbols_2;
  585. expected = "a; b; c;";
  586. input = "a; \r\nb;\r\n c;"
  587. actual = beautify(input, newSettings);
  588. assert("Remove line after ;", expected, actual);
  589. newSettings = deepCopy(settings);
  590. newSettings.RemoveAsserts = true;
  591. 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;";
  592. expected = "ARCHITECTURE arch OF ent IS\r\nBEGIN\r\nEND ARCHITECTURE;"
  593. actual = beautify(input, newSettings);
  594. assert("Remove asserts", expected, actual);
  595. 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;";
  596. 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;";
  597. actual = beautify(input, settings);
  598. assert("ENTITY ARCHITECTURE", expected, actual);
  599. newSettings = deepCopy(settings);
  600. newSettings.SignAlign = true;
  601. input = "port map(\r\ninput_1 => input_1_sig,\r\ninput_2 => input_2_sig,\r\noutput => output_sig\r\n);";
  602. expected = "PORT MAP(\r\n input_1 => input_1_sig, \r\n input_2 => input_2_sig, \r\n output => output_sig\r\n);";
  603. actual = beautify(input, newSettings);
  604. assert("Sign align in PORT", expected, actual);
  605. input = 'if a(3 downto 0) > "0100" then\r\na(3 downto 0) := a(3 downto 0) + "0011" ;\r\nend if ;';
  606. expected = 'IF a(3 DOWNTO 0) > "0100" THEN\r\n a(3 DOWNTO 0) := a(3 DOWNTO 0) + "0011";\r\nEND IF;';
  607. actual = beautify(input, settings);
  608. assert("IF END IF case 1", expected, actual);
  609. input = "if s = '1' then\r\no <= \"010\";\r\nelse\r\no <= \"101\";\r\nend if;";
  610. expected = "IF s = '1' THEN\r\n o <= \"010\";\r\nELSE\r\n o <= \"101\";\r\nEND IF;";
  611. actual = beautify(input, settings);
  612. assert("IF ELSE END IF case 1", expected, actual);
  613. input = "IF (s = r) THEN rr := '0'; ELSE rr := '1'; END IF;";
  614. expected = "IF (s = r) THEN\r\n rr := '0';\r\nELSE\r\n rr := '1';\r\nEND IF;";
  615. actual = beautify(input, settings);
  616. assert("IF ELSE END IF case 2", expected, actual);
  617. 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;';
  618. 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;';
  619. actual = beautify(input, settings);
  620. assert("WHEN CASE", expected, actual);
  621. 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;";
  622. 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;";
  623. actual = beautify(input, settings);
  624. assert("WHEN CASE & IF", expected, actual);
  625. 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;";
  626. 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\r\n PORT MAP(\r\n long => a, \r\n b => b\r\n );\r\nEND;";
  627. actual = beautify(input, settings);
  628. assert("PORT MAP", expected, actual);
  629. 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;";
  630. 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\r\n PORT MAP(\r\n long => a, \r\n b => b\r\n );\r\n D : cc\r\n PORT MAP(\r\n long => a, \r\n b => b\r\n );\r\nEND;";
  631. actual = beautify(input, settings);
  632. assert("Multiple PORT MAPs", expected, actual);
  633. input = "port (a : in std_logic;\r\n b : in std_logic;\r\n);";
  634. expected = "PORT \r\n(\r\n a : IN std_logic;\r\n b : IN std_logic;\r\n);";
  635. new_line_after_symbols_2 = new NewLineSettings();
  636. new_line_after_symbols_2.newLineAfter = ["then", ";", "generic", "port"];
  637. newSettings = deepCopy(settings);
  638. newSettings.NewLineSettings = new_line_after_symbols_2;
  639. actual = beautify(input, newSettings);
  640. assert("New line after PORT", expected, actual);
  641. input = "component a is\r\nport( Data : inout Std_Logic_Vector(7 downto 0););\r\nend component a;";
  642. expected = "COMPONENT a IS\r\n PORT (Data : INOUT Std_Logic_Vector(7 DOWNTO 0););\r\nEND COMPONENT a;";
  643. actual = beautify(input, newSettings);
  644. assert("New line aster PORT (single line)", expected, actual);
  645. input = "process xyx (vf,fr,\r\nde -- comment\r\n)";
  646. expected = "PROCESS xyx (vf, fr, \r\n de -- comment\r\n )";
  647. actual = beautify(input, newSettings);
  648. assert("Align parameters in PROCESS", expected, actual);
  649. 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;";
  650. 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;";
  651. actual = beautify(input, newSettings);
  652. assert("Double BEGIN", expected, actual);
  653. let newSettings2 = deepCopy(newSettings);
  654. newSettings2.SignAlignAll = true;
  655. 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 ;";
  656. 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;";
  657. actual = beautify(input, newSettings2);
  658. assert("Align signs in all places", expected, actual);
  659. input = "begin\r\n P0 : process(input)\r\n variable value: Integer;\r\n begin\r\n result(i) := '0';\r\n end process P0;\r\nend behavior;";
  660. expected = "BEGIN\r\n P0 : PROCESS (input)\r\n VARIABLE value : INTEGER;\r\n BEGIN\r\n result(i) := '0';\r\n END PROCESS P0;\r\nEND behavior;";
  661. actual = beautify(input, newSettings);
  662. assert("Indent after Begin", expected, actual);
  663. }
  664. function IntegrationTest2() {
  665. let new_line_after_symbols: NewLineSettings = new NewLineSettings();
  666. new_line_after_symbols.newLineAfter = ["then", ";"];
  667. new_line_after_symbols.noNewLineAfter = ["generic"];
  668. let settings: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
  669. settings.RemoveComments = true;
  670. 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;"
  671. 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;";
  672. let actual = beautify(input, settings);
  673. assert("Remove comments", expected, actual);
  674. }
  675. function CompareString(actual: string, expected: string) {
  676. var l = Math.min(actual.length, expected.length);
  677. for (var i = 0; i < l; i++) {
  678. if (actual[i] != expected[i]) {
  679. var toEnd = Math.min(i + 50, l);
  680. return '\ndifferent at ' + i.toString() +
  681. '\nactual: "\n' + actual.substring(i, toEnd) +
  682. '\nexpected: "\n' + expected.substring(i, toEnd) + '"\n---' +
  683. "\nactual (full): \n" + actual + "\n---" +
  684. "\nexpected (full): \n" + expected + "\n====\n";
  685. }
  686. }
  687. if (actual != expected) {
  688. return 'actual: \n"' + actual + '"\nexpected: \n"' + expected + '"';
  689. }
  690. return true;
  691. }
  692. function CompareArray(actual: Array<string>, expected: Array<string>) {
  693. var l = Math.min(actual.length, expected.length);
  694. let result: string = "";
  695. for (var i = 0; i < l; i++) {
  696. if (actual[i] != expected[i]) {
  697. result += CompareString(actual[i], expected[i]) + "\n";
  698. }
  699. }
  700. if (actual.length > expected.length) {
  701. result += "actual has more items";
  702. for (var i = expected.length; i < actual.length; i++) {
  703. result += "actual[" + i + "] = " + actual[i];
  704. }
  705. }
  706. else if (actual.length < expected.length) {
  707. result += "expected has more items";
  708. for (var i = actual.length; i < expected.length; i++) {
  709. result += "expected[" + i + "] = " + expected[i];
  710. }
  711. }
  712. return true;
  713. }