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.

129 lines
12 KiB

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. var showUnitTests = true; //window.location.href.indexOf("http") < 0;
  8. if (showUnitTests) {
  9. UnitTest();
  10. UnitTestIndentDecode();
  11. }
  12. function UnitTestIndentDecode() {
  13. console.log("=== IndentDecode ===");
  14. UnitTest2(VHDLFormatter_2.indentDecode, "one blankspace", " ", "one blankspace");
  15. UnitTest2(VHDLFormatter_2.indentDecode, "mixed chars", " A ", "one blankspace & one A & one blankspace");
  16. UnitTest2(VHDLFormatter_2.indentDecode, "4 blankspaces", " ", "four blankspace");
  17. UnitTest2(VHDLFormatter_2.indentDecode, "9 blankspaces", " ", "many blankspace");
  18. }
  19. function assert(testName, expected, actual, message) {
  20. var result = CompareString(actual, expected);
  21. if (result != true) {
  22. console.log(testName + " failed: " + result);
  23. }
  24. else {
  25. //console.log(testName + " pass");
  26. }
  27. }
  28. function UnitTest2(func, testName, inputs, expected) {
  29. let actual = func(inputs);
  30. assert(testName, expected, actual);
  31. }
  32. function deepCopy(objectToCopy) {
  33. return (JSON.parse(JSON.stringify(objectToCopy)));
  34. }
  35. function UnitTest() {
  36. let new_line_after_symbols = new VHDLFormatter_3.NewLineSettings();
  37. new_line_after_symbols.newLineAfter = ["Then", ";"];
  38. let settings = new VHDLFormatter_4.BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
  39. 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;";
  40. 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;";
  41. let actual = VHDLFormatter_1.beautify(input, settings);
  42. console.log("General", actual == expected);
  43. let newSettings = deepCopy(settings);
  44. newSettings.RemoveComments = true;
  45. 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;";
  46. actual = VHDLFormatter_1.beautify(input, newSettings);
  47. console.log("Remove comments", actual == expected);
  48. 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;";
  49. 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;";
  50. actual = VHDLFormatter_1.beautify(input, settings);
  51. console.log("ENTITY ARCHITECTURE", CompareString(actual, expected));
  52. newSettings = deepCopy(settings);
  53. newSettings.SignAlign = true;
  54. input = "port map(\r\ninput_1 => input_1_sig,\r\ninput_2 => input_2_sig,\r\noutput => output_sig\r\n);";
  55. expected = "PORT MAP(\r\n input_1 => input_1_sig, \r\n input_2 => input_2_sig, \r\n output => output_sig\r\n);";
  56. actual = VHDLFormatter_1.beautify(input, newSettings);
  57. console.log("Sign align in PORT", actual == expected);
  58. input = 'if a(3 downto 0) > "0100" then\r\na(3 downto 0) := a(3 downto 0) + "0011" ;\r\nend if ;';
  59. expected = 'IF a(3 DOWNTO 0) > "0100" THEN\r\n a(3 DOWNTO 0) := a(3 DOWNTO 0) + "0011";\r\nEND IF;';
  60. actual = VHDLFormatter_1.beautify(input, settings);
  61. console.log("IF END IF case 1", CompareString(actual, expected));
  62. input = "if s = '1' then\r\no <= \"010\";\r\nelse\r\no <= \"101\";\r\nend if;";
  63. expected = "IF s = '1' THEN\r\n o <= \"010\";\r\nELSE\r\n o <= \"101\";\r\nEND IF;";
  64. actual = VHDLFormatter_1.beautify(input, settings);
  65. console.log("IF ELSE END IF case 1", actual == expected);
  66. input = "IF (s = r) THEN rr := '0'; ELSE rr := '1'; END IF;";
  67. expected = "IF (s = r) THEN\r\n rr := '0';\r\nELSE\r\n rr := '1';\r\nEND IF;";
  68. actual = VHDLFormatter_1.beautify(input, settings);
  69. console.log("IF ELSE END IF case 2", actual == expected);
  70. 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;';
  71. 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;';
  72. actual = VHDLFormatter_1.beautify(input, settings);
  73. console.log("WHEN CASE", CompareString(actual, expected));
  74. 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;";
  75. 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;";
  76. actual = VHDLFormatter_1.beautify(input, settings);
  77. console.log("WHEN CASE & IF", CompareString(actual, expected));
  78. 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;";
  79. 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;";
  80. actual = VHDLFormatter_1.beautify(input, settings);
  81. console.log("PORT MAP", CompareString(actual, expected));
  82. 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;";
  83. 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;";
  84. actual = VHDLFormatter_1.beautify(input, settings);
  85. console.log("Multiple PORT MAPs", CompareString(actual, expected));
  86. input = "port (a : in std_logic;\r\n b : in std_logic;\r\n);";
  87. expected = "PORT \r\n(\r\n a : IN std_logic;\r\n b : IN std_logic;\r\n);";
  88. let new_line_after_symbols_2 = new VHDLFormatter_3.NewLineSettings();
  89. new_line_after_symbols_2.newLineAfter = ["Then", ";", "generic", "port"];
  90. newSettings = deepCopy(settings);
  91. newSettings.NewLineSettings = new_line_after_symbols_2;
  92. actual = VHDLFormatter_1.beautify(input, newSettings);
  93. console.log("New line aster PORT", CompareString(actual, expected));
  94. input = "component a is\r\nport( Data : inout Std_Logic_Vector(7 downto 0););\r\nend component a;";
  95. expected = "COMPONENT a IS\r\n PORT (Data : INOUT Std_Logic_Vector(7 DOWNTO 0););\r\nEND COMPONENT a;";
  96. actual = VHDLFormatter_1.beautify(input, newSettings);
  97. console.log("New line aster PORT (single line)", CompareString(actual, expected));
  98. input = "process xyx (vf,fr,\r\nde -- comment\r\n)";
  99. expected = "PROCESS xyx (vf, fr, \r\n de -- comment\r\n )";
  100. actual = VHDLFormatter_1.beautify(input, newSettings);
  101. console.log("Align parameters in PROCESS", CompareString(actual, expected));
  102. 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;";
  103. 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;";
  104. actual = VHDLFormatter_1.beautify(input, newSettings);
  105. console.log("Double BEGIN", CompareString(actual, expected));
  106. let newSettings2 = deepCopy(newSettings);
  107. newSettings2.SignAlignAll = true;
  108. 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 ;";
  109. 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;";
  110. actual = VHDLFormatter_1.beautify(input, newSettings2);
  111. console.log("Align signs in all places", CompareString(actual, expected));
  112. 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;";
  113. 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;";
  114. actual = VHDLFormatter_1.beautify(input, newSettings);
  115. console.log("Indent after Begin", CompareString(actual, expected));
  116. }
  117. function CompareString(actual, expected) {
  118. var l = Math.min(actual.length, expected.length);
  119. for (var i = 0; i < l; i++) {
  120. if (actual[i] != expected[i]) {
  121. var toEnd = Math.min(i + 50, l);
  122. return '\ndifferent at ' + i.toString() + '\nactual: "\n' + actual.substring(i, toEnd) + '\nexpected: "\n' + expected.substring(i, toEnd) + '"' + "\nactual: \n" + actual;
  123. }
  124. }
  125. if (actual != expected) {
  126. return 'actual: \n"' + actual + '"\nexpected: \n"' + expected + '"';
  127. }
  128. return true;
  129. }
  130. //# sourceMappingURL=VHDLFormatterUnitTests.js.map