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.

488 lines
25 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. }
  34. function Beautify3Case1() {
  35. let new_line_after_symbols: NewLineSettings = new NewLineSettings();
  36. new_line_after_symbols.newLineAfter = ["then", ";"];
  37. new_line_after_symbols.noNewLineAfter = ["port", "generic"];
  38. let settings: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
  39. let inputs: Array<string> = ["a;", "b;"];
  40. let expected: (FormattedLine | FormattedLine[])[] = [new FormattedLine("a;", 0), new FormattedLine("b;", 0)];
  41. UnitTest6(beautify3, "General", settings, inputs, expected, 0, 1, 0);
  42. }
  43. function Beautify3Case2() {
  44. let new_line_after_symbols: NewLineSettings = new NewLineSettings();
  45. new_line_after_symbols.newLineAfter = ["then", ";"];
  46. new_line_after_symbols.noNewLineAfter = ["port", "generic"];
  47. let settings: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
  48. let inputs: Array<string> = ["IF x = '1' THEN", "RETURN 1;", "END IF;"];
  49. let expected: (FormattedLine | FormattedLine[])[] = [
  50. new FormattedLine("IF x = '1' THEN", 0),
  51. new FormattedLine("RETURN 1;", 1),
  52. new FormattedLine("END IF;", 0)
  53. ];
  54. UnitTest6(beautify3, "IF END", settings, inputs, expected, 0, 2, 0);
  55. }
  56. function Beautify3Case3() {
  57. let new_line_after_symbols: NewLineSettings = new NewLineSettings();
  58. new_line_after_symbols.newLineAfter = ["then", ";"];
  59. new_line_after_symbols.noNewLineAfter = ["port", "generic"];
  60. let settings: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
  61. let inputs: Array<string> = [
  62. "IF x = '1' THEN",
  63. "RETURN 1;",
  64. "ELSIF x = '0' THEN",
  65. "RETURN 0;",
  66. "ELSE",
  67. "RETURN -1;",
  68. "END IF;"];
  69. let expected: (FormattedLine | FormattedLine[])[] = [
  70. new FormattedLine("IF x = '1' THEN", 0),
  71. new FormattedLine("RETURN 1;", 1),
  72. new FormattedLine("ELSIF x = '0' THEN", 0),
  73. new FormattedLine("RETURN 0;", 1),
  74. new FormattedLine("ELSE", 0),
  75. new FormattedLine("RETURN -1;", 1),
  76. new FormattedLine("END IF;", 0)
  77. ];
  78. UnitTest6(beautify3, "if elsif else end", settings, inputs, expected, 0, 6, 0);
  79. }
  80. function Beautify3Case4() {
  81. let new_line_after_symbols: NewLineSettings = new NewLineSettings();
  82. new_line_after_symbols.newLineAfter = ["then", ";"];
  83. new_line_after_symbols.noNewLineAfter = ["port", "generic"];
  84. let settings: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
  85. let inputs: Array<string> = ["END"];
  86. let expected: (FormattedLine | FormattedLine[])[] = [new FormattedLine("END", 0)];
  87. UnitTest6(beautify3, "one line END", settings, inputs, expected, 0, 0, 0);
  88. }
  89. function Beautify3Case5() {
  90. let new_line_after_symbols: NewLineSettings = new NewLineSettings();
  91. new_line_after_symbols.newLineAfter = ["then", ";"];
  92. new_line_after_symbols.noNewLineAfter = ["port", "generic"];
  93. let settings: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
  94. let inputs: Array<string> = [
  95. "CASE b",
  96. "WHEN 1 =>",
  97. "c <= d;",
  98. "WHEN 2 =>",
  99. "d <= f;",
  100. "END CASE;"
  101. ];
  102. let expected: (FormattedLine | FormattedLine[])[] = [
  103. new FormattedLine("CASE b", 0),
  104. new FormattedLine("WHEN 1 =>", 1),
  105. new FormattedLine("c <= d;", 2),
  106. new FormattedLine("WHEN 2 =>", 1),
  107. new FormattedLine("d <= f;", 2),
  108. new FormattedLine("END CASE;", 0)
  109. ];
  110. UnitTest6(beautify3, "case when when end", settings, inputs, expected, 0, 5, 0);
  111. }
  112. function Beautify3Case6() {
  113. let new_line_after_symbols: NewLineSettings = new NewLineSettings();
  114. new_line_after_symbols.newLineAfter = ["then", ";"];
  115. new_line_after_symbols.noNewLineAfter = ["port", "generic"];
  116. let settings: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
  117. let inputs: Array<string> = [
  118. "CASE b",
  119. "WHEN 1 =>",
  120. "c <= d;",
  121. "CASE b",
  122. "WHEN 1 =>",
  123. "c <= d;",
  124. "WHEN 2 =>",
  125. "d <= f;",
  126. "END CASE;",
  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("CASE b", 2),
  136. new FormattedLine("WHEN 1 =>", 3),
  137. new FormattedLine("c <= d;", 4),
  138. new FormattedLine("WHEN 2 =>", 3),
  139. new FormattedLine("d <= f;", 4),
  140. new FormattedLine("END CASE;", 2),
  141. new FormattedLine("WHEN 2 =>", 1),
  142. new FormattedLine("d <= f;", 2),
  143. new FormattedLine("END CASE;", 0)
  144. ];
  145. UnitTest6(beautify3, "case & case end", settings, inputs, expected, 0, 11, 0);
  146. }
  147. function UnitTestSetNewLinesAfterSymbols() {
  148. console.log("=== SetNewLinesAfterSymbols ===");
  149. let input = "a; @@comments1\r\nb;"
  150. let expected = "a; @@comments1\r\nb;";
  151. let parameters: NewLineSettings = new NewLineSettings();
  152. parameters.newLineAfter = ["then", ";"];
  153. parameters.noNewLineAfter = ["port", "generic"];
  154. UnitTest5(SetNewLinesAfterSymbols, "no new line after comment", parameters, input, expected);
  155. input = "a; b;"
  156. expected = "a;\r\nb;";
  157. UnitTest5(SetNewLinesAfterSymbols, "new line after ;", parameters, input, expected);
  158. }
  159. function UnitTestApplyNoNewLineAfter() {
  160. console.log("=== ApplyNoNewLineAfter ===");
  161. let input: Array<string> = ["a;", "b;"];
  162. let expected: Array<string> = ["a;@@singleline", "b;@@singleline"];
  163. let parameters: Array<string> = [";"];
  164. UnitTest4(ApplyNoNewLineAfter, "one blankspace", parameters, input, expected);
  165. input = ["a;", "b THEN", "c"];
  166. expected = ["a;@@singleline", "b THEN@@singleline", "c"];
  167. parameters = [";", "then"];
  168. UnitTest4(ApplyNoNewLineAfter, "one blankspace", parameters, input, expected);
  169. }
  170. function UnitTestRemoveAsserts() {
  171. console.log("=== RemoveAsserts ===");
  172. let input: Array<string> = ["ASSERT a;"];
  173. let expected: Array<string> = [""];
  174. UnitTest3(RemoveAsserts, "one assert", input, expected);
  175. input = ["ASSERT a", "b;", "c"];
  176. expected = ["", "", "c"];
  177. UnitTest3(RemoveAsserts, "multiline assert", input, expected);
  178. }
  179. function UnitTestIndentDecode() {
  180. console.log("=== IndentDecode ===");
  181. UnitTest2(indentDecode, "one blankspace", " ", "one blankspace");
  182. UnitTest2(indentDecode, "mixed chars", " A ", "one blankspace & one A & one blankspace");
  183. UnitTest2(indentDecode, "4 blankspaces", " ", "four blankspace");
  184. UnitTest2(indentDecode, "9 blankspaces", " ", "many blankspace");
  185. }
  186. function assertFormattedLines(testName, expected: (FormattedLine | FormattedLine[])[], actual: (FormattedLine | FormattedLine[])[], message?, cumulateTestCount?: boolean) {
  187. var l = Math.min(actual.length, expected.length);
  188. let result: string = "";
  189. for (var i = 0; i < l; i++) {
  190. if (actual[i] instanceof FormattedLine) {
  191. if (expected[i] instanceof FormattedLine) {
  192. assertFormattedLine(testName, <FormattedLine>(expected[i]), <FormattedLine>(actual[i]), message, false);
  193. }
  194. else {
  195. console.log("expected FormatLine[], actual FormattedLine. actual:" + (<FormattedLine>(actual[i])).Line);
  196. }
  197. }
  198. else {
  199. if (expected[i] instanceof FormattedLine) {
  200. console.log("expected FormatLine, actual FormattedLine[]. expected:" + (<FormattedLine>(expected[i])).Line);
  201. }
  202. else {
  203. assertFormattedLines(testName, <FormattedLine[]>(actual[i]), <FormattedLine[]>(expected[i]), message, false);
  204. }
  205. }
  206. }
  207. if (actual.length > expected.length) {
  208. result += "actual has more items";
  209. for (var i = expected.length; i < actual.length; i++) {
  210. result += "actual[" + i + "] = " + actual[i];
  211. }
  212. }
  213. else if (actual.length < expected.length) {
  214. result += "expected has more items";
  215. for (var i = actual.length; i < expected.length; i++) {
  216. result += "expected[" + i + "] = " + expected[i];
  217. }
  218. }
  219. if (result.length > 0) {
  220. console.log(result);
  221. }
  222. if (cumulateTestCount != false) {
  223. testCount++;
  224. }
  225. }
  226. function assertFormattedLine(testName, expected: FormattedLine, actual: FormattedLine, message?, cumulateTestCount?: boolean) {
  227. if (expected.Indent != actual.Indent) {
  228. console.log(testName + ' failed:\nexpected: "' + expected.Line + '", ' + expected.Indent
  229. + ';\nactual: "' + actual.Line + '", ' + actual.Indent);
  230. }
  231. var result = CompareString(actual.Line, expected.Line);
  232. if (result != true) {
  233. console.log(testName + " failed: " + result);
  234. }
  235. else {
  236. //console.log(testName + " pass");
  237. }
  238. if (cumulateTestCount != false) {
  239. testCount++;
  240. }
  241. }
  242. function assert(testName, expected, actual, message?) {
  243. var result = CompareString(actual, expected);
  244. if (result != true) {
  245. console.log(testName + " failed: " + result);
  246. }
  247. else {
  248. //console.log(testName + " pass");
  249. }
  250. testCount++;
  251. }
  252. function assertArray(testName, expected, actual, message?) {
  253. var result = CompareArray(actual, expected);
  254. if (result != true) {
  255. console.log(testName + " failed: " + result);
  256. }
  257. else {
  258. //console.log(testName + " pass");
  259. }
  260. testCount++;
  261. }
  262. type StringCallback = (text: string) => string;
  263. type ArrayCallback = (arr: Array<string>) => void;
  264. type Array2Callback = (arr: Array<string>, parameters: Array<string>) => void;
  265. type String2Callback = (text: string, parameters: NewLineSettings) => string;
  266. type BeautifyCallback = (inputs: Array<string>, result: (FormattedLine | FormattedLine[])[], settings: BeautifierSettings, startIndex: number, indent: number) => number;
  267. function UnitTest6(func: BeautifyCallback, testName: string, parameters: BeautifierSettings, inputs: Array<string>, expected: (FormattedLine | FormattedLine[])[], startIndex: number, expectedEndIndex: number, indent: number) {
  268. let actual: (FormattedLine | FormattedLine[])[] = []
  269. let endIndex: number = func(inputs, actual, parameters, startIndex, indent);
  270. if (endIndex != expectedEndIndex) {
  271. console.log(testName + " failed;\nend index, actual: " + endIndex + "; expected: " + expectedEndIndex)
  272. }
  273. assertFormattedLines(testName, expected, actual);
  274. }
  275. function UnitTest5(func: String2Callback, testName: string, parameters: NewLineSettings, inputs, expected: string) {
  276. let actual: string = func(inputs, parameters);
  277. assert(testName, expected, actual);
  278. }
  279. function UnitTest4(func: Array2Callback, testName: string, parameters: Array<string>, inputs: Array<string>, expected: Array<string>) {
  280. let actual = JSON.parse(JSON.stringify(inputs));
  281. func(actual, parameters);
  282. assertArray(testName, expected, actual);
  283. }
  284. function UnitTest3(func: ArrayCallback, testName: string, inputs: Array<string>, expected: Array<string>) {
  285. let actual = JSON.parse(JSON.stringify(inputs));
  286. func(actual);
  287. assertArray(testName, expected, actual);
  288. }
  289. function UnitTest2(func: StringCallback, testName: string, inputs, expected: string) {
  290. let actual: string = func(inputs);
  291. assert(testName, expected, actual);
  292. }
  293. function deepCopy(objectToCopy: BeautifierSettings): BeautifierSettings {
  294. return (JSON.parse(JSON.stringify(objectToCopy)));
  295. }
  296. function UnitTest() {
  297. let new_line_after_symbols: NewLineSettings = new NewLineSettings();
  298. new_line_after_symbols.newLineAfter = ["then", ";"];
  299. new_line_after_symbols.noNewLineAfter = ["port", "generic"];
  300. let settings: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
  301. 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;"
  302. 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;";
  303. let actual = beautify(input, settings);
  304. assert("General", expected, actual);
  305. let newSettings = deepCopy(settings);
  306. newSettings.RemoveComments = true;
  307. 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;";
  308. actual = beautify(input, newSettings);
  309. assert("Remove comments", expected, actual);
  310. let new_line_after_symbols_2: NewLineSettings = new NewLineSettings();
  311. new_line_after_symbols_2.newLineAfter = [];
  312. new_line_after_symbols_2.noNewLineAfter = ["then", ";", "generic", "port"];
  313. newSettings = deepCopy(settings);
  314. newSettings.NewLineSettings = new_line_after_symbols_2;
  315. expected = "a; b; c;";
  316. input = "a; \r\nb;\r\n c;"
  317. actual = beautify(input, newSettings);
  318. assert("Remove line after ;", expected, actual);
  319. newSettings = deepCopy(settings);
  320. newSettings.RemoveAsserts = true;
  321. 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;";
  322. expected = "ARCHITECTURE arch OF ent IS\r\nBEGIN\r\nEND ARCHITECTURE;"
  323. actual = beautify(input, newSettings);
  324. assert("Remove asserts", expected, actual);
  325. 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;";
  326. 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;";
  327. actual = beautify(input, settings);
  328. assert("ENTITY ARCHITECTURE", expected, actual);
  329. newSettings = deepCopy(settings);
  330. newSettings.SignAlign = true;
  331. input = "port map(\r\ninput_1 => input_1_sig,\r\ninput_2 => input_2_sig,\r\noutput => output_sig\r\n);";
  332. expected = "PORT MAP(\r\n input_1 => input_1_sig, \r\n input_2 => input_2_sig, \r\n output => output_sig\r\n);";
  333. actual = beautify(input, newSettings);
  334. assert("Sign align in PORT", expected, actual);
  335. input = 'if a(3 downto 0) > "0100" then\r\na(3 downto 0) := a(3 downto 0) + "0011" ;\r\nend if ;';
  336. expected = 'IF a(3 DOWNTO 0) > "0100" THEN\r\n a(3 DOWNTO 0) := a(3 DOWNTO 0) + "0011";\r\nEND IF;';
  337. actual = beautify(input, settings);
  338. assert("IF END IF case 1", expected, actual);
  339. input = "if s = '1' then\r\no <= \"010\";\r\nelse\r\no <= \"101\";\r\nend if;";
  340. expected = "IF s = '1' THEN\r\n o <= \"010\";\r\nELSE\r\n o <= \"101\";\r\nEND IF;";
  341. actual = beautify(input, settings);
  342. assert("IF ELSE END IF case 1", expected, actual);
  343. input = "IF (s = r) THEN rr := '0'; ELSE rr := '1'; END IF;";
  344. expected = "IF (s = r) THEN\r\n rr := '0';\r\nELSE\r\n rr := '1';\r\nEND IF;";
  345. actual = beautify(input, settings);
  346. assert("IF ELSE END IF case 2", expected, actual);
  347. 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;';
  348. 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;';
  349. actual = beautify(input, settings);
  350. assert("WHEN CASE", expected, actual);
  351. 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;";
  352. 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;";
  353. actual = beautify(input, settings);
  354. assert("WHEN CASE & IF", expected, actual);
  355. 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;";
  356. 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;";
  357. actual = beautify(input, settings);
  358. assert("PORT MAP", expected, actual);
  359. 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;";
  360. 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;";
  361. actual = beautify(input, settings);
  362. assert("Multiple PORT MAPs", expected, actual);
  363. input = "port (a : in std_logic;\r\n b : in std_logic;\r\n);";
  364. expected = "PORT \r\n(\r\n a : IN std_logic;\r\n b : IN std_logic;\r\n);";
  365. new_line_after_symbols_2 = new NewLineSettings();
  366. new_line_after_symbols_2.newLineAfter = ["then", ";", "generic", "port"];
  367. newSettings = deepCopy(settings);
  368. newSettings.NewLineSettings = new_line_after_symbols_2;
  369. actual = beautify(input, newSettings);
  370. assert("New line after PORT", expected, actual);
  371. input = "component a is\r\nport( Data : inout Std_Logic_Vector(7 downto 0););\r\nend component a;";
  372. expected = "COMPONENT a IS\r\n PORT (Data : INOUT Std_Logic_Vector(7 DOWNTO 0););\r\nEND COMPONENT a;";
  373. actual = beautify(input, newSettings);
  374. assert("New line aster PORT (single line)", expected, actual);
  375. input = "process xyx (vf,fr,\r\nde -- comment\r\n)";
  376. expected = "PROCESS xyx (vf, fr, \r\n de -- comment\r\n )";
  377. actual = beautify(input, newSettings);
  378. assert("Align parameters in PROCESS", expected, actual);
  379. 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;";
  380. 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;";
  381. actual = beautify(input, newSettings);
  382. assert("Double BEGIN", expected, actual);
  383. let newSettings2 = deepCopy(newSettings);
  384. newSettings2.SignAlignAll = true;
  385. 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 ;";
  386. 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;";
  387. actual = beautify(input, newSettings2);
  388. assert("Align signs in all places", expected, actual);
  389. 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;";
  390. 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;";
  391. actual = beautify(input, newSettings);
  392. assert("Indent after Begin", expected, actual);
  393. }
  394. function CompareString(actual: string, expected: string) {
  395. var l = Math.min(actual.length, expected.length);
  396. for (var i = 0; i < l; i++) {
  397. if (actual[i] != expected[i]) {
  398. var toEnd = Math.min(i + 50, l);
  399. return '\ndifferent at ' + i.toString() +
  400. '\nactual: "\n' + actual.substring(i, toEnd) +
  401. '\nexpected: "\n' + expected.substring(i, toEnd) + '"\n---' +
  402. "\nactual (full): \n" + actual + "\n---" +
  403. "\nexpected (full): \n" + expected + "\n====\n";
  404. }
  405. }
  406. if (actual != expected) {
  407. return 'actual: \n"' + actual + '"\nexpected: \n"' + expected + '"';
  408. }
  409. return true;
  410. }
  411. function CompareArray(actual: Array<string>, expected: Array<string>) {
  412. var l = Math.min(actual.length, expected.length);
  413. let result: string = "";
  414. for (var i = 0; i < l; i++) {
  415. if (actual[i] != expected[i]) {
  416. result += CompareString(actual[i], expected[i]) + "\n";
  417. }
  418. }
  419. if (actual.length > expected.length) {
  420. result += "actual has more items";
  421. for (var i = expected.length; i < actual.length; i++) {
  422. result += "actual[" + i + "] = " + actual[i];
  423. }
  424. }
  425. else if (actual.length < expected.length) {
  426. result += "expected has more items";
  427. for (var i = actual.length; i < expected.length; i++) {
  428. result += "expected[" + i + "] = " + expected[i];
  429. }
  430. }
  431. return true;
  432. }