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.

237 lines
16 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
  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. let testCount = 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. console.log("total tests: " + testCount);
  20. }
  21. function UnitTestSetNewLinesAfterSymbols() {
  22. console.log("=== SetNewLinesAfterSymbols ===");
  23. let input = "a; @@comments1\r\nb;";
  24. let expected = "a; @@comments1\r\nb;";
  25. let parameters = new VHDLFormatter_3.NewLineSettings();
  26. parameters.newLineAfter = ["then", ";"];
  27. parameters.noNewLineAfter = ["port", "generic"];
  28. UnitTest5(VHDLFormatter_7.SetNewLinesAfterSymbols, "no new line after comment", parameters, input, expected);
  29. input = "a; b;";
  30. expected = "a;\r\nb;";
  31. UnitTest5(VHDLFormatter_7.SetNewLinesAfterSymbols, "new line after ;", parameters, input, expected);
  32. }
  33. function UnitTestApplyNoNewLineAfter() {
  34. console.log("=== ApplyNoNewLineAfter ===");
  35. let input = ["a;", "b;"];
  36. let expected = ["a;@@singleline", "b;@@singleline"];
  37. let parameters = [";"];
  38. UnitTest4(VHDLFormatter_6.ApplyNoNewLineAfter, "one blankspace", parameters, input, expected);
  39. input = ["a;", "b THEN", "c"];
  40. expected = ["a;@@singleline", "b THEN@@singleline", "c"];
  41. parameters = [";", "then"];
  42. UnitTest4(VHDLFormatter_6.ApplyNoNewLineAfter, "one blankspace", parameters, input, expected);
  43. }
  44. function UnitTestRemoveAsserts() {
  45. console.log("=== RemoveAsserts ===");
  46. let input = ["ASSERT a;"];
  47. let expected = [""];
  48. UnitTest3(VHDLFormatter_5.RemoveAsserts, "one assert", input, expected);
  49. input = ["ASSERT a", "b;", "c"];
  50. expected = ["", "", "c"];
  51. UnitTest3(VHDLFormatter_5.RemoveAsserts, "multiline assert", input, expected);
  52. }
  53. function UnitTestIndentDecode() {
  54. console.log("=== IndentDecode ===");
  55. UnitTest2(VHDLFormatter_2.indentDecode, "one blankspace", " ", "one blankspace");
  56. UnitTest2(VHDLFormatter_2.indentDecode, "mixed chars", " A ", "one blankspace & one A & one blankspace");
  57. UnitTest2(VHDLFormatter_2.indentDecode, "4 blankspaces", " ", "four blankspace");
  58. UnitTest2(VHDLFormatter_2.indentDecode, "9 blankspaces", " ", "many blankspace");
  59. }
  60. function assert(testName, expected, actual, message) {
  61. var result = CompareString(actual, expected);
  62. if (result != true) {
  63. console.log(testName + " failed: " + result);
  64. }
  65. else {
  66. //console.log(testName + " pass");
  67. }
  68. testCount++;
  69. }
  70. function assertArray(testName, expected, actual, message) {
  71. var result = CompareArray(actual, expected);
  72. if (result != true) {
  73. console.log(testName + " failed: " + result);
  74. }
  75. else {
  76. //console.log(testName + " pass");
  77. }
  78. testCount++;
  79. }
  80. function UnitTest5(func, testName, parameters, inputs, expected) {
  81. let actual = func(inputs, parameters);
  82. assert(testName, expected, actual);
  83. }
  84. function UnitTest4(func, testName, parameters, inputs, expected) {
  85. let actual = JSON.parse(JSON.stringify(inputs));
  86. func(actual, parameters);
  87. assertArray(testName, expected, actual);
  88. }
  89. function UnitTest3(func, testName, inputs, expected) {
  90. let actual = JSON.parse(JSON.stringify(inputs));
  91. func(actual);
  92. assertArray(testName, expected, actual);
  93. }
  94. function UnitTest2(func, testName, inputs, expected) {
  95. let actual = func(inputs);
  96. assert(testName, expected, actual);
  97. }
  98. function deepCopy(objectToCopy) {
  99. return (JSON.parse(JSON.stringify(objectToCopy)));
  100. }
  101. function UnitTest() {
  102. let new_line_after_symbols = new VHDLFormatter_3.NewLineSettings();
  103. new_line_after_symbols.newLineAfter = ["then", ";"];
  104. new_line_after_symbols.noNewLineAfter = ["port", "generic"];
  105. let settings = new VHDLFormatter_4.BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
  106. 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;";
  107. 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;";
  108. let actual = VHDLFormatter_1.beautify(input, settings);
  109. assert("General", expected, actual);
  110. let newSettings = deepCopy(settings);
  111. newSettings.RemoveComments = true;
  112. 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;";
  113. actual = VHDLFormatter_1.beautify(input, newSettings);
  114. assert("Remove comments", expected, actual);
  115. let new_line_after_symbols_2 = new VHDLFormatter_3.NewLineSettings();
  116. new_line_after_symbols_2.newLineAfter = [];
  117. new_line_after_symbols_2.noNewLineAfter = ["then", ";", "generic", "port"];
  118. newSettings = deepCopy(settings);
  119. newSettings.NewLineSettings = new_line_after_symbols_2;
  120. expected = "a; b; c;";
  121. input = "a; \r\nb;\r\n c;";
  122. actual = VHDLFormatter_1.beautify(input, newSettings);
  123. assert("Remove line after ;", expected, actual);
  124. newSettings = deepCopy(settings);
  125. newSettings.RemoveAsserts = true;
  126. 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;";
  127. expected = "ARCHITECTURE arch OF ent IS\r\nBEGIN\r\nEND ARCHITECTURE;";
  128. actual = VHDLFormatter_1.beautify(input, newSettings);
  129. assert("Remove asserts", expected, actual);
  130. 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;";
  131. 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;";
  132. actual = VHDLFormatter_1.beautify(input, settings);
  133. assert("ENTITY ARCHITECTURE", expected, actual);
  134. newSettings = deepCopy(settings);
  135. newSettings.SignAlign = true;
  136. input = "port map(\r\ninput_1 => input_1_sig,\r\ninput_2 => input_2_sig,\r\noutput => output_sig\r\n);";
  137. expected = "PORT MAP(\r\n input_1 => input_1_sig, \r\n input_2 => input_2_sig, \r\n output => output_sig\r\n);";
  138. actual = VHDLFormatter_1.beautify(input, newSettings);
  139. assert("Sign align in PORT", expected, actual);
  140. input = 'if a(3 downto 0) > "0100" then\r\na(3 downto 0) := a(3 downto 0) + "0011" ;\r\nend if ;';
  141. expected = 'IF a(3 DOWNTO 0) > "0100" THEN\r\n a(3 DOWNTO 0) := a(3 DOWNTO 0) + "0011";\r\nEND IF;';
  142. actual = VHDLFormatter_1.beautify(input, settings);
  143. assert("IF END IF case 1", expected, actual);
  144. input = "if s = '1' then\r\no <= \"010\";\r\nelse\r\no <= \"101\";\r\nend if;";
  145. expected = "IF s = '1' THEN\r\n o <= \"010\";\r\nELSE\r\n o <= \"101\";\r\nEND IF;";
  146. actual = VHDLFormatter_1.beautify(input, settings);
  147. assert("IF ELSE END IF case 1", expected, actual);
  148. input = "IF (s = r) THEN rr := '0'; ELSE rr := '1'; END IF;";
  149. expected = "IF (s = r) THEN\r\n rr := '0';\r\nELSE\r\n rr := '1';\r\nEND IF;";
  150. actual = VHDLFormatter_1.beautify(input, settings);
  151. assert("IF ELSE END IF case 2", expected, actual);
  152. 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;';
  153. 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;';
  154. actual = VHDLFormatter_1.beautify(input, settings);
  155. assert("WHEN CASE", expected, actual);
  156. 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;";
  157. 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;";
  158. actual = VHDLFormatter_1.beautify(input, settings);
  159. assert("WHEN CASE & IF", expected, actual);
  160. 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;";
  161. 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;";
  162. actual = VHDLFormatter_1.beautify(input, settings);
  163. assert("PORT MAP", expected, actual);
  164. 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;";
  165. 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;";
  166. actual = VHDLFormatter_1.beautify(input, settings);
  167. assert("Multiple PORT MAPs", expected, actual);
  168. input = "port (a : in std_logic;\r\n b : in std_logic;\r\n);";
  169. expected = "PORT \r\n(\r\n a : IN std_logic;\r\n b : IN std_logic;\r\n);";
  170. new_line_after_symbols_2 = new VHDLFormatter_3.NewLineSettings();
  171. new_line_after_symbols_2.newLineAfter = ["then", ";", "generic", "port"];
  172. newSettings = deepCopy(settings);
  173. newSettings.NewLineSettings = new_line_after_symbols_2;
  174. actual = VHDLFormatter_1.beautify(input, newSettings);
  175. assert("New line after PORT", expected, actual);
  176. input = "component a is\r\nport( Data : inout Std_Logic_Vector(7 downto 0););\r\nend component a;";
  177. expected = "COMPONENT a IS\r\n PORT (Data : INOUT Std_Logic_Vector(7 DOWNTO 0););\r\nEND COMPONENT a;";
  178. actual = VHDLFormatter_1.beautify(input, newSettings);
  179. assert("New line aster PORT (single line)", expected, actual);
  180. input = "process xyx (vf,fr,\r\nde -- comment\r\n)";
  181. expected = "PROCESS xyx (vf, fr, \r\n de -- comment\r\n )";
  182. actual = VHDLFormatter_1.beautify(input, newSettings);
  183. assert("Align parameters in PROCESS", expected, actual);
  184. 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;";
  185. 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;";
  186. actual = VHDLFormatter_1.beautify(input, newSettings);
  187. assert("Double BEGIN", expected, actual);
  188. let newSettings2 = deepCopy(newSettings);
  189. newSettings2.SignAlignAll = true;
  190. 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 ;";
  191. 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;";
  192. actual = VHDLFormatter_1.beautify(input, newSettings2);
  193. assert("Align signs in all places", expected, actual);
  194. 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;";
  195. 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;";
  196. actual = VHDLFormatter_1.beautify(input, newSettings);
  197. assert("Indent after Begin", expected, actual);
  198. }
  199. function CompareString(actual, expected) {
  200. var l = Math.min(actual.length, expected.length);
  201. for (var i = 0; i < l; i++) {
  202. if (actual[i] != expected[i]) {
  203. var toEnd = Math.min(i + 50, l);
  204. return '\ndifferent at ' + i.toString() +
  205. '\nactual: "\n' + actual.substring(i, toEnd) +
  206. '\nexpected: "\n' + expected.substring(i, toEnd) + '"\n---' +
  207. "\nactual (full): \n" + actual + "\n---" +
  208. "\nexpected (full): \n" + expected + "\n====\n";
  209. }
  210. }
  211. if (actual != expected) {
  212. return 'actual: \n"' + actual + '"\nexpected: \n"' + expected + '"';
  213. }
  214. return true;
  215. }
  216. function CompareArray(actual, expected) {
  217. var l = Math.min(actual.length, expected.length);
  218. let result = "";
  219. for (var i = 0; i < l; i++) {
  220. if (actual[i] != expected[i]) {
  221. result += CompareString(actual[i], expected[i]) + "\n";
  222. }
  223. }
  224. if (actual.length > expected.length) {
  225. result += "actual has more items";
  226. for (var i = expected.length; i < actual.length; i++) {
  227. result += "actual[" + i + "] = " + actual[i];
  228. }
  229. }
  230. else if (actual.length < expected.length) {
  231. result += "expected has more items";
  232. for (var i = actual.length; i < expected.length; i++) {
  233. result += "expected[" + i + "] = " + expected[i];
  234. }
  235. }
  236. return true;
  237. }
  238. //# sourceMappingURL=VHDLFormatterUnitTests.js.map