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.

718 lines
34 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
  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. let testCount: number = 0;
  11. var showUnitTests = true;//window.location.href.indexOf("http") < 0;
  12. if (showUnitTests) {
  13. testCount = 0;
  14. //UnitTest();
  15. UnitTestIndentDecode();
  16. UnitTestRemoveAsserts();
  17. UnitTestApplyNoNewLineAfter();
  18. UnitTestSetNewLinesAfterSymbols();
  19. UnitTestbeautify3();
  20. console.log("total tests: " + testCount);
  21. }
  22. interface Function {
  23. readonly name: string;
  24. }
  25. function UnitTestbeautify3() {
  26. console.log("=== beautify3 ===");
  27. Beautify3Case1();
  28. Beautify3Case2();
  29. Beautify3Case3();
  30. Beautify3Case4();
  31. Beautify3Case5();
  32. Beautify3Case6();
  33. Beautify3Case7();
  34. Beautify3Case8();
  35. Beautify3Case9();
  36. Beautify3Case10();
  37. Beautify3Case11();
  38. Beautify3Case12();
  39. Beautify3Case13();
  40. }
  41. function Beautify3Case1() {
  42. let new_line_after_symbols: NewLineSettings = new NewLineSettings();
  43. new_line_after_symbols.newLineAfter = ["then", ";"];
  44. new_line_after_symbols.noNewLineAfter = ["port", "generic"];
  45. let settings: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
  46. let inputs: Array<string> = ["a;", "b;"];
  47. let expected: (FormattedLine | FormattedLine[])[] = [new FormattedLine("a;", 0), new FormattedLine("b;", 0)];
  48. UnitTest6(beautify3, "General", settings, inputs, expected, 0, 1, 0);
  49. }
  50. function Beautify3Case2() {
  51. let new_line_after_symbols: NewLineSettings = new NewLineSettings();
  52. new_line_after_symbols.newLineAfter = ["then", ";"];
  53. new_line_after_symbols.noNewLineAfter = ["port", "generic"];
  54. let settings: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
  55. let inputs: Array<string> = ["IF x = '1' THEN", "RETURN 1;", "END IF;"];
  56. let expected: (FormattedLine | FormattedLine[])[] = [
  57. new FormattedLine("IF x = '1' THEN", 0),
  58. new FormattedLine("RETURN 1;", 1),
  59. new FormattedLine("END IF;", 0)
  60. ];
  61. UnitTest6(beautify3, "IF END", settings, inputs, expected, 0, 2, 0);
  62. }
  63. function Beautify3Case3() {
  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> = [
  69. "IF x = '1' THEN",
  70. "RETURN 1;",
  71. "ELSIF x = '0' THEN",
  72. "RETURN 0;",
  73. "ELSE",
  74. "RETURN -1;",
  75. "END IF;"];
  76. let expected: (FormattedLine | FormattedLine[])[] = [
  77. new FormattedLine("IF x = '1' THEN", 0),
  78. new FormattedLine("RETURN 1;", 1),
  79. new FormattedLine("ELSIF x = '0' THEN", 0),
  80. new FormattedLine("RETURN 0;", 1),
  81. new FormattedLine("ELSE", 0),
  82. new FormattedLine("RETURN -1;", 1),
  83. new FormattedLine("END IF;", 0)
  84. ];
  85. UnitTest6(beautify3, "if elsif else end", settings, inputs, expected, 0, 6, 0);
  86. }
  87. function Beautify3Case4() {
  88. let new_line_after_symbols: NewLineSettings = new NewLineSettings();
  89. new_line_after_symbols.newLineAfter = ["then", ";"];
  90. new_line_after_symbols.noNewLineAfter = ["port", "generic"];
  91. let settings: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
  92. let inputs: Array<string> = ["END"];
  93. let expected: (FormattedLine | FormattedLine[])[] = [new FormattedLine("END", 0)];
  94. UnitTest6(beautify3, "one line END", settings, inputs, expected, 0, 0, 0);
  95. }
  96. function Beautify3Case5() {
  97. let new_line_after_symbols: NewLineSettings = new NewLineSettings();
  98. new_line_after_symbols.newLineAfter = ["then", ";"];
  99. new_line_after_symbols.noNewLineAfter = ["port", "generic"];
  100. let settings: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
  101. let inputs: Array<string> = [
  102. "CASE b",
  103. "WHEN 1 =>",
  104. "c <= d;",
  105. "WHEN 2 =>",
  106. "d <= f;",
  107. "END CASE;"
  108. ];
  109. let expected: (FormattedLine | FormattedLine[])[] = [
  110. new FormattedLine("CASE b", 0),
  111. new FormattedLine("WHEN 1 =>", 1),
  112. new FormattedLine("c <= d;", 2),
  113. new FormattedLine("WHEN 2 =>", 1),
  114. new FormattedLine("d <= f;", 2),
  115. new FormattedLine("END CASE;", 0)
  116. ];
  117. UnitTest6(beautify3, "case when when end", settings, inputs, expected, 0, 5, 0);
  118. }
  119. function Beautify3Case6() {
  120. let new_line_after_symbols: NewLineSettings = new NewLineSettings();
  121. new_line_after_symbols.newLineAfter = ["then", ";"];
  122. new_line_after_symbols.noNewLineAfter = ["port", "generic"];
  123. let settings: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
  124. let inputs: Array<string> = [
  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: (FormattedLine | FormattedLine[])[] = [
  139. new FormattedLine("CASE b", 0),
  140. new FormattedLine("WHEN 1 =>", 1),
  141. new FormattedLine("c <= d;", 2),
  142. new FormattedLine("CASE b", 2),
  143. new FormattedLine("WHEN 1 =>", 3),
  144. new FormattedLine("c <= d;", 4),
  145. new FormattedLine("WHEN 2 =>", 3),
  146. new FormattedLine("d <= f;", 4),
  147. new FormattedLine("END CASE;", 2),
  148. new FormattedLine("WHEN 2 =>", 1),
  149. new FormattedLine("d <= f;", 2),
  150. new FormattedLine("END CASE;", 0)
  151. ];
  152. UnitTest6(beautify3, "case & case end", settings, inputs, expected, 0, 11, 0);
  153. }
  154. function Beautify3Case7() {
  155. let new_line_after_symbols: NewLineSettings = new NewLineSettings();
  156. new_line_after_symbols.newLineAfter = ["then", ";"];
  157. new_line_after_symbols.noNewLineAfter = ["port", "generic"];
  158. let settings: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
  159. let inputs: Array<string> = [
  160. "ARCHITECTURE a OF one IS",
  161. "SIGNAL x : INTEGER;",
  162. "BEGIN",
  163. "-- architecture",
  164. "END ARCHITECTURE;"
  165. ];
  166. let expected: (FormattedLine | FormattedLine[])[] = [
  167. new FormattedLine("ARCHITECTURE a OF one IS", 0),
  168. new FormattedLine("SIGNAL x : INTEGER;", 1),
  169. new FormattedLine("BEGIN", 0),
  170. new FormattedLine("-- architecture", 1),
  171. new FormattedLine("END ARCHITECTURE;", 0),
  172. ];
  173. UnitTest6(beautify3, "architecture", settings, inputs, expected, 0, 4, 0);
  174. }
  175. function Beautify3Case8() {
  176. let new_line_after_symbols: NewLineSettings = new NewLineSettings();
  177. new_line_after_symbols.newLineAfter = ["then", ";"];
  178. new_line_after_symbols.noNewLineAfter = ["port", "generic"];
  179. let settings: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
  180. let inputs: Array<string> = [
  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: (FormattedLine | FormattedLine[])[] = [
  193. new FormattedLine("ARCHITECTURE a OF one IS", 0),
  194. new FormattedLine("SIGNAL x : INTEGER;", 1),
  195. new FormattedLine("BEGIN", 0),
  196. new FormattedLine("-- architecture", 1),
  197. new FormattedLine("END ARCHITECTURE;", 0),
  198. new FormattedLine("ARCHITECTURE b 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 2", settings, inputs, expected, 0, 9, 0);
  205. }
  206. function Beautify3Case9() {
  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. "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: (FormattedLine | FormattedLine[])[] = [
  219. new FormattedLine("PROCEDURE foo(x : IN INTEGER; y : OUT INTEGER) IS", 0),
  220. new FormattedLine("VARIABLE i : INTEGER;", 1),
  221. new FormattedLine("BEGIN", 0),
  222. new FormattedLine("y := x + 1;", 1),
  223. new FormattedLine("END PROCEDURE;", 0)
  224. ];
  225. UnitTest6(beautify3, "procedure", settings, inputs, expected, 0, 4, 0);
  226. }
  227. function Beautify3Case10() {
  228. let new_line_after_symbols: NewLineSettings = new NewLineSettings();
  229. new_line_after_symbols.newLineAfter = ["then", ";"];
  230. new_line_after_symbols.noNewLineAfter = ["port", "generic"];
  231. let settings: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
  232. let inputs: Array<string> = [
  233. "PACKAGE three IS",
  234. "SIGNAL s : INTEGER;",
  235. "ALIAS sa IS s;",
  236. "END PACKAGE;"
  237. ];
  238. let expected: (FormattedLine | FormattedLine[])[] = [
  239. new FormattedLine("PACKAGE three IS", 0),
  240. new FormattedLine("SIGNAL s : INTEGER;", 1),
  241. new FormattedLine("ALIAS sa IS s;", 1),
  242. new FormattedLine("END PACKAGE;", 0)
  243. ];
  244. UnitTest6(beautify3, "package", settings, inputs, expected, 0, 3, 0);
  245. }
  246. function Beautify3Case11() {
  247. let new_line_after_symbols: NewLineSettings = new NewLineSettings();
  248. new_line_after_symbols.newLineAfter = ["then", ";"];
  249. new_line_after_symbols.noNewLineAfter = ["port", "generic"];
  250. let settings: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
  251. let inputs: Array<string> = [
  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: (FormattedLine | FormattedLine[])[] = [
  274. new FormattedLine("PACKAGE p IS", 0),
  275. new FormattedLine("PROCEDURE foo(x : IN INTEGER; y : OUT INTEGER);", 1),
  276. new FormattedLine("END PACKAGE;", 0),
  277. new FormattedLine("PACKAGE BODY p IS", 0),
  278. new FormattedLine("PROCEDURE foo(x : IN INTEGER; y : OUT INTEGER) IS", 1),
  279. new FormattedLine("VARIABLE i : INTEGER;", 2),
  280. new FormattedLine("BEGIN", 1),
  281. new FormattedLine("y := x + 1;", 2),
  282. new FormattedLine("END PROCEDURE;", 1),
  283. new FormattedLine("PROCEDURE bar(FILE x : text);", 1),
  284. new FormattedLine("PROCEDURE baz IS", 1),
  285. new FormattedLine("TYPE foo;", 2),
  286. new FormattedLine("ALIAS x IS y;", 2),
  287. new FormattedLine("BEGIN", 1),
  288. new FormattedLine("END PROCEDURE;", 1),
  289. new FormattedLine("PROCEDURE tralala IS", 1),
  290. new FormattedLine("USE work.foo;", 2),
  291. new FormattedLine("BEGIN", 1),
  292. new FormattedLine("END PROCEDURE;", 1),
  293. new FormattedLine("END PACKAGE BODY;", 0)
  294. ];
  295. UnitTest6(beautify3, "package", settings, inputs, expected, 0, expected.length - 1, 0);
  296. }
  297. function Beautify3Case12() {
  298. let new_line_after_symbols: NewLineSettings = new NewLineSettings();
  299. new_line_after_symbols.newLineAfter = ["then", ";"];
  300. new_line_after_symbols.noNewLineAfter = ["port", "generic"];
  301. let settings: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
  302. let inputs: Array<string> = [
  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: (FormattedLine | FormattedLine[])[] = [
  325. new FormattedLine("ARCHITECTURE a OF b IS", 0),
  326. new FormattedLine("SIGNAL x : INTEGER := 0;", 1),
  327. new FormattedLine("BEGIN", 0),
  328. new FormattedLine("p: PROCESS IS", 1),
  329. new FormattedLine("BEGIN", 1),
  330. new FormattedLine("END PROCESS;", 1),
  331. new FormattedLine("PROCESS", 1),
  332. new FormattedLine("VARIABLE y : INTEGER := 5;", 2),
  333. new FormattedLine("BEGIN", 1),
  334. new FormattedLine("x <= y;", 2),
  335. new FormattedLine("END PROCESS;", 1),
  336. new FormattedLine("PROCESS (x) IS", 1),
  337. new FormattedLine("BEGIN", 1),
  338. new FormattedLine("x <= x + 1;", 2),
  339. new FormattedLine("END PROCESS;", 1),
  340. new FormattedLine("POSTPONED PROCESS IS", 1),
  341. new FormattedLine("BEGIN", 1),
  342. new FormattedLine("END PROCESS;", 1),
  343. new FormattedLine("POSTPONED assert x = 1;", 1),
  344. new FormattedLine("END ARCHITECTURE;", 0)
  345. ];
  346. UnitTest6(beautify3, "package", settings, inputs, expected, 0, expected.length - 1, 0);
  347. }
  348. function Beautify3Case13() {
  349. let new_line_after_symbols: NewLineSettings = new NewLineSettings();
  350. new_line_after_symbols.newLineAfter = ["then", ";"];
  351. new_line_after_symbols.noNewLineAfter = ["port", "generic"];
  352. let settings: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
  353. let inputs: Array<string> = [
  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: (FormattedLine | FormattedLine[])[] = [
  360. new FormattedLine("TYPE SharedCounter IS PROTECTED", 0),
  361. new FormattedLine("PROCEDURE increment (N : INTEGER := 1);", 1),
  362. new FormattedLine("IMPURE FUNCTION value RETURN INTEGER;", 1),
  363. new FormattedLine("END PROTECTED SharedCounter;", 0)
  364. ];
  365. UnitTest6(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: NewLineSettings = new NewLineSettings();
  372. parameters.newLineAfter = ["then", ";"];
  373. parameters.noNewLineAfter = ["port", "generic"];
  374. UnitTest5(SetNewLinesAfterSymbols, "no new line after comment", parameters, input, expected);
  375. input = "a; b;"
  376. expected = "a;\r\nb;";
  377. UnitTest5(SetNewLinesAfterSymbols, "new line after ;", parameters, input, expected);
  378. }
  379. function UnitTestApplyNoNewLineAfter() {
  380. console.log("=== ApplyNoNewLineAfter ===");
  381. let input: Array<string> = ["a;", "b;"];
  382. let expected: Array<string> = ["a;@@singleline", "b;@@singleline"];
  383. let parameters: Array<string> = [";"];
  384. UnitTest4(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(ApplyNoNewLineAfter, "one blankspace", parameters, input, expected);
  389. }
  390. function UnitTestRemoveAsserts() {
  391. console.log("=== RemoveAsserts ===");
  392. let input: Array<string> = ["ASSERT a;"];
  393. let expected: Array<string> = [""];
  394. UnitTest3(RemoveAsserts, "one assert", input, expected);
  395. input = ["ASSERT a", "b;", "c"];
  396. expected = ["", "", "c"];
  397. UnitTest3(RemoveAsserts, "multiline assert", input, expected);
  398. }
  399. function UnitTestIndentDecode() {
  400. console.log("=== IndentDecode ===");
  401. UnitTest2(indentDecode, "one blankspace", " ", "one blankspace");
  402. UnitTest2(indentDecode, "mixed chars", " A ", "one blankspace & one A & one blankspace");
  403. UnitTest2(indentDecode, "4 blankspaces", " ", "four blankspace");
  404. UnitTest2(indentDecode, "9 blankspaces", " ", "many blankspace");
  405. }
  406. function compareFormattedLines(expected: (FormattedLine | FormattedLine[])[], actual: (FormattedLine | FormattedLine[])[], message?): string {
  407. var l = Math.min(actual.length, expected.length);
  408. let result: string = "";
  409. for (var i = 0; i < l; i++) {
  410. if (actual[i] instanceof FormattedLine) {
  411. if (expected[i] instanceof FormattedLine) {
  412. let compareResult = compareFormattedLine(<FormattedLine>(expected[i]), <FormattedLine>(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:" + (<FormattedLine>(actual[i])).Line;
  419. }
  420. }
  421. else {
  422. if (expected[i] instanceof FormattedLine) {
  423. result += "index " + i + "\nexpected FormatLine, actual FormattedLine[]. expected:" + (<FormattedLine>(expected[i])).Line;
  424. }
  425. else {
  426. let compareResult = compareFormattedLines(<FormattedLine[]>(actual[i]), <FormattedLine[]>(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: (FormattedLine | FormattedLine[])[], actual: (FormattedLine | FormattedLine[])[], 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: FormattedLine, actual: FormattedLine, message?, cumulateTestCount?: boolean) {
  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. type StringCallback = (text: string) => string;
  487. type ArrayCallback = (arr: Array<string>) => void;
  488. type Array2Callback = (arr: Array<string>, parameters: Array<string>) => void;
  489. type String2Callback = (text: string, parameters: NewLineSettings) => string;
  490. type BeautifyCallback = (inputs: Array<string>, result: (FormattedLine | FormattedLine[])[], settings: BeautifierSettings, startIndex: number, indent: number) => number;
  491. function UnitTest6(func: BeautifyCallback, testName: string, parameters: BeautifierSettings, inputs: Array<string>, expected: (FormattedLine | FormattedLine[])[], startIndex: number, expectedEndIndex: number, indent: number) {
  492. let actual: (FormattedLine | FormattedLine[])[] = []
  493. let endIndex: number = func(inputs, actual, parameters, startIndex, indent);
  494. if (endIndex != expectedEndIndex) {
  495. console.log(testName + " failed;\nend index, actual: " + endIndex + "; expected: " + expectedEndIndex)
  496. }
  497. assertFormattedLines(testName, expected, actual);
  498. }
  499. function UnitTest5(func: String2Callback, testName: string, parameters: NewLineSettings, inputs, expected: string) {
  500. let actual: string = func(inputs, parameters);
  501. assert(testName, expected, actual);
  502. }
  503. function UnitTest4(func: Array2Callback, testName: string, parameters: Array<string>, inputs: Array<string>, expected: Array<string>) {
  504. let actual = JSON.parse(JSON.stringify(inputs));
  505. func(actual, parameters);
  506. assertArray(testName, expected, actual);
  507. }
  508. function UnitTest3(func: ArrayCallback, testName: string, inputs: Array<string>, expected: Array<string>) {
  509. let actual = JSON.parse(JSON.stringify(inputs));
  510. func(actual);
  511. assertArray(testName, expected, actual);
  512. }
  513. function UnitTest2(func: StringCallback, testName: string, inputs, expected: string) {
  514. let actual: string = func(inputs);
  515. assert(testName, expected, actual);
  516. }
  517. function deepCopy(objectToCopy: BeautifierSettings): BeautifierSettings {
  518. return (JSON.parse(JSON.stringify(objectToCopy)));
  519. }
  520. function UnitTest() {
  521. let new_line_after_symbols: NewLineSettings = new NewLineSettings();
  522. new_line_after_symbols.newLineAfter = ["then", ";"];
  523. new_line_after_symbols.noNewLineAfter = ["port", "generic"];
  524. let settings: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
  525. 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;"
  526. 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;";
  527. let actual = beautify(input, settings);
  528. assert("General", expected, actual);
  529. let newSettings = deepCopy(settings);
  530. newSettings.RemoveComments = true;
  531. 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;";
  532. actual = beautify(input, newSettings);
  533. assert("Remove comments", expected, actual);
  534. let new_line_after_symbols_2: NewLineSettings = new NewLineSettings();
  535. new_line_after_symbols_2.newLineAfter = [];
  536. new_line_after_symbols_2.noNewLineAfter = ["then", ";", "generic", "port"];
  537. newSettings = deepCopy(settings);
  538. newSettings.NewLineSettings = new_line_after_symbols_2;
  539. expected = "a; b; c;";
  540. input = "a; \r\nb;\r\n c;"
  541. actual = beautify(input, newSettings);
  542. assert("Remove line after ;", expected, actual);
  543. newSettings = deepCopy(settings);
  544. newSettings.RemoveAsserts = true;
  545. 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;";
  546. expected = "ARCHITECTURE arch OF ent IS\r\nBEGIN\r\nEND ARCHITECTURE;"
  547. actual = beautify(input, newSettings);
  548. assert("Remove asserts", expected, actual);
  549. 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;";
  550. 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;";
  551. actual = beautify(input, settings);
  552. assert("ENTITY ARCHITECTURE", expected, actual);
  553. newSettings = deepCopy(settings);
  554. newSettings.SignAlign = true;
  555. input = "port map(\r\ninput_1 => input_1_sig,\r\ninput_2 => input_2_sig,\r\noutput => output_sig\r\n);";
  556. expected = "PORT MAP(\r\n input_1 => input_1_sig, \r\n input_2 => input_2_sig, \r\n output => output_sig\r\n);";
  557. actual = beautify(input, newSettings);
  558. assert("Sign align in PORT", expected, actual);
  559. input = 'if a(3 downto 0) > "0100" then\r\na(3 downto 0) := a(3 downto 0) + "0011" ;\r\nend if ;';
  560. expected = 'IF a(3 DOWNTO 0) > "0100" THEN\r\n a(3 DOWNTO 0) := a(3 DOWNTO 0) + "0011";\r\nEND IF;';
  561. actual = beautify(input, settings);
  562. assert("IF END IF case 1", expected, actual);
  563. input = "if s = '1' then\r\no <= \"010\";\r\nelse\r\no <= \"101\";\r\nend if;";
  564. expected = "IF s = '1' THEN\r\n o <= \"010\";\r\nELSE\r\n o <= \"101\";\r\nEND IF;";
  565. actual = beautify(input, settings);
  566. assert("IF ELSE END IF case 1", expected, actual);
  567. input = "IF (s = r) THEN rr := '0'; ELSE rr := '1'; END IF;";
  568. expected = "IF (s = r) THEN\r\n rr := '0';\r\nELSE\r\n rr := '1';\r\nEND IF;";
  569. actual = beautify(input, settings);
  570. assert("IF ELSE END IF case 2", expected, actual);
  571. 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;';
  572. 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;';
  573. actual = beautify(input, settings);
  574. assert("WHEN CASE", expected, actual);
  575. 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;";
  576. 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;";
  577. actual = beautify(input, settings);
  578. assert("WHEN CASE & IF", expected, actual);
  579. 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;";
  580. 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;";
  581. actual = beautify(input, settings);
  582. assert("PORT MAP", expected, actual);
  583. 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;";
  584. 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;";
  585. actual = beautify(input, settings);
  586. assert("Multiple PORT MAPs", expected, actual);
  587. input = "port (a : in std_logic;\r\n b : in std_logic;\r\n);";
  588. expected = "PORT \r\n(\r\n a : IN std_logic;\r\n b : IN std_logic;\r\n);";
  589. new_line_after_symbols_2 = new NewLineSettings();
  590. new_line_after_symbols_2.newLineAfter = ["then", ";", "generic", "port"];
  591. newSettings = deepCopy(settings);
  592. newSettings.NewLineSettings = new_line_after_symbols_2;
  593. actual = beautify(input, newSettings);
  594. assert("New line after PORT", expected, actual);
  595. input = "component a is\r\nport( Data : inout Std_Logic_Vector(7 downto 0););\r\nend component a;";
  596. expected = "COMPONENT a IS\r\n PORT (Data : INOUT Std_Logic_Vector(7 DOWNTO 0););\r\nEND COMPONENT a;";
  597. actual = beautify(input, newSettings);
  598. assert("New line aster PORT (single line)", expected, actual);
  599. input = "process xyx (vf,fr,\r\nde -- comment\r\n)";
  600. expected = "PROCESS xyx (vf, fr, \r\n de -- comment\r\n )";
  601. actual = beautify(input, newSettings);
  602. assert("Align parameters in PROCESS", expected, actual);
  603. 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;";
  604. 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;";
  605. actual = beautify(input, newSettings);
  606. assert("Double BEGIN", expected, actual);
  607. let newSettings2 = deepCopy(newSettings);
  608. newSettings2.SignAlignAll = true;
  609. 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 ;";
  610. 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;";
  611. actual = beautify(input, newSettings2);
  612. assert("Align signs in all places", expected, actual);
  613. 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;";
  614. 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;";
  615. actual = beautify(input, newSettings);
  616. assert("Indent after Begin", expected, actual);
  617. }
  618. function CompareString(actual: string, expected: string) {
  619. var l = Math.min(actual.length, expected.length);
  620. for (var i = 0; i < l; i++) {
  621. if (actual[i] != expected[i]) {
  622. var toEnd = Math.min(i + 50, l);
  623. return '\ndifferent at ' + i.toString() +
  624. '\nactual: "\n' + actual.substring(i, toEnd) +
  625. '\nexpected: "\n' + expected.substring(i, toEnd) + '"\n---' +
  626. "\nactual (full): \n" + actual + "\n---" +
  627. "\nexpected (full): \n" + expected + "\n====\n";
  628. }
  629. }
  630. if (actual != expected) {
  631. return 'actual: \n"' + actual + '"\nexpected: \n"' + expected + '"';
  632. }
  633. return true;
  634. }
  635. function CompareArray(actual: Array<string>, expected: Array<string>) {
  636. var l = Math.min(actual.length, expected.length);
  637. let result: string = "";
  638. for (var i = 0; i < l; i++) {
  639. if (actual[i] != expected[i]) {
  640. result += CompareString(actual[i], expected[i]) + "\n";
  641. }
  642. }
  643. if (actual.length > expected.length) {
  644. result += "actual has more items";
  645. for (var i = expected.length; i < actual.length; i++) {
  646. result += "actual[" + i + "] = " + actual[i];
  647. }
  648. }
  649. else if (actual.length < expected.length) {
  650. result += "expected has more items";
  651. for (var i = actual.length; i < expected.length; i++) {
  652. result += "expected[" + i + "] = " + expected[i];
  653. }
  654. }
  655. return true;
  656. }