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.

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