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.

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