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.

232 lines
12 KiB

  1. import { beautify, signAlignSettings } from "../VHDLFormatter";
  2. import { NewLineSettings } from "../VHDLFormatter";
  3. import { BeautifierSettings } from "../VHDLFormatter";
  4. import { RemoveAsserts } from "../VHDLFormatter";
  5. import { ApplyNoNewLineAfter } from "../VHDLFormatter";
  6. import { SetNewLinesAfterSymbols } from "../VHDLFormatter";
  7. import { beautify3 } from "../VHDLFormatter";
  8. import { FormattedLine } from "../VHDLFormatter";
  9. import { FormattedLineToString } from "../VHDLFormatter";
  10. describe('VHDLFormatter', function () {
  11. it('do not format block comments', function () {
  12. let settings = GetDefaultSettings();
  13. let input = 'a;\r\n/*a; \r\n b;\r\nport ( ) ;\r\n/*\r\n*/c;';
  14. let result = beautify(input, settings);
  15. expect(result).toBe(input);
  16. });
  17. it('do not format block comments 2', function () {
  18. let settings = GetDefaultSettings();
  19. let input = 'a;/*a;*/b;\r\nc;';
  20. let result = beautify(input, settings);
  21. expect(result).toBe(input);
  22. });
  23. it('do not format block comments 3', function () {
  24. let settings = GetDefaultSettings();
  25. let input = 'a;\r\n/*a;*//*\r\n*/c;';
  26. let result = beautify(input, settings);
  27. expect(result).toBe(input);
  28. });
  29. it('do not format block comments 4', function () {
  30. let settings = GetDefaultSettings();
  31. let input = '/*\r\nwoof\r\n*//*\r\nwoof\r\n*//*\r\nwoof\r\n*/';
  32. let result = beautify(input, settings);
  33. expect(result).toBe(input);
  34. });
  35. it('add new line at the end of the file', function () {
  36. let settings = GetDefaultSettings();
  37. settings.AddNewLine = true;
  38. let input = 'test';
  39. let result = beautify(input, settings);
  40. expect(result).toBe("test\r\n");
  41. });
  42. it('upper case types', function () {
  43. let settings = GetDefaultSettings();
  44. let input = "x : string;\r\ny : std_logic_vector;";
  45. let result = beautify(input, settings);
  46. expect(result).toBe("x : STRING;\r\ny : STD_LOGIC_VECTOR;");
  47. });
  48. it('package ends with ;', function () {
  49. let settings = GetDefaultSettings();
  50. let input = "PACKAGE p IS NEW work.p_template\r\n GENERIC MAP(N => N);\r\nUSE p.ALL;";
  51. let result = beautify(input, settings);
  52. expect(result).toBe(input);
  53. });
  54. it('package ends with ; (same line)', function () {
  55. let settings = GetDefaultSettings();
  56. let input = "PACKAGE p IS NEW work.p_template;\r\nUSE p.ALL;";
  57. let result = beautify(input, settings);
  58. expect(result).toBe(input);
  59. });
  60. it('align in, out, inout, buffer', function () {
  61. let settings = GetDefaultSettings();
  62. settings.SignAlignSettings.isAll = true;
  63. let input = "Incr, Load, Clock : IN BIT;\r\nCarry : OUT BIT;\r\nData_Out : BUFFER bit_vector(7 DOWNTO 0);\r\nData_In : IN bit_vector(7 DOWNTO 0)";
  64. let result = beautify(input, settings);
  65. expect(result).toBe(input);
  66. });
  67. it('align sign in PORT & GENERIC', function () {
  68. let new_line_after_symbols: NewLineSettings = new NewLineSettings();
  69. new_line_after_symbols.newLineAfter = ["then", ";"];
  70. let settings = getDefaultBeautifierSettings(new_line_after_symbols);
  71. settings.SignAlignSettings = new signAlignSettings(true, false, "global", ["PORT", "GENERIC"]);
  72. let input = "entity p is\r\n generic\r\n (\r\n -- INCLK\r\n INCLK0_INPUT_FREQUENCY : natural;\r\n\r\n -- CLK1\r\n CLK1_DIVIDE_BY : natural := 1;\r\n CLK1_MULTIPLY_BY : unnatural:= 1;\r\n CLK1_PHASE_SHIFT : string := \"0\"\r\n );\r\n port\r\n (\r\n inclk0 : in bit := '0';\r\n c0 : out bit;\r\n c1 : out bit \r\n );\r\nEND pll;";
  73. let expected = "ENTITY p IS\r\n GENERIC (\r\n -- INCLK\r\n INCLK0_INPUT_FREQUENCY : NATURAL;\r\n\r\n -- CLK1\r\n CLK1_DIVIDE_BY : NATURAL := 1;\r\n CLK1_MULTIPLY_BY : unnatural := 1;\r\n CLK1_PHASE_SHIFT : STRING := \"0\"\r\n );\r\n PORT (\r\n inclk0 : IN BIT := '0';\r\n c0 : OUT BIT;\r\n c1 : OUT BIT\r\n );\r\nEND pll;";
  74. let result = beautify(input, settings);
  75. expect(result).toBe(expected);
  76. });
  77. it('align signs in all places', function () {
  78. let setting = getDefaultBeautifierSettings(new NewLineSettings());
  79. setting.SignAlignSettings = new signAlignSettings(false, true, "", []);
  80. setting.NewLineSettings.newLineAfter = ["then", ";", "generic", "port"];
  81. setting.NewLineSettings.noNewLineAfter = [];
  82. let input = "entity a is\r\n port ( w : in bit;\r\n w_s : out bit; ) ;\r\nend a ;\r\narchitecture b of a is\r\nbegin\r\n process ( w )\r\n variable t : bit;\r\n variable bcd : bit;\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;";
  83. let expected = "ENTITY a IS\r\n PORT\r\n (\r\n w : IN BIT;\r\n w_s : OUT BIT;\r\n );\r\nEND a;\r\nARCHITECTURE b OF a IS\r\nBEGIN\r\n PROCESS (w)\r\n VARIABLE t : BIT;\r\n VARIABLE bcd : BIT;\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;";
  84. let result = beautify(input, setting);
  85. expect(result).toBe(expected);
  86. });
  87. it('semicolon blocks are aligned', function () {
  88. let settings = GetDefaultSettings();
  89. let input = 'OUT <= In0 AFTER 2ns WHEN "00",\r\n In1 AFTER 2ns WHEN "01",\r\n In2 AFTER 2ns WHEN "10",\r\n In3 AFTER 2ns WHEN "11";';
  90. let result = beautify(input, settings);
  91. expect(result).toBe(input);
  92. });
  93. it('align comments', function () {
  94. let settings = GetDefaultSettings();
  95. settings.SignAlignSettings = new signAlignSettings(false, true, "", [], true);
  96. let input = 'test := loooong; -- test\r\ntest := short; -- test';
  97. let expected = 'test := loooong; -- test\r\ntest := short; -- test';
  98. let result = beautify(input, settings);
  99. expect(result).toBe(expected);
  100. });
  101. it('do not align comments', function () {
  102. let settings = GetDefaultSettings();
  103. settings.SignAlignSettings = new signAlignSettings(false, true, "", [], false);
  104. let input = 'test := loooong; -- test\r\ntest := short; -- test';
  105. let result = beautify(input, settings);
  106. expect(result).toBe(input);
  107. });
  108. it('support invalid line', function () {
  109. let settings = GetDefaultSettings();
  110. settings.SignAlignSettings = new signAlignSettings(false, true, "", [], false);
  111. let input = '(5 * 32 - 1 DOWNTO 0) := (\r\n);';
  112. let result = beautify(input, settings);
  113. expect(result).toBe(input);
  114. });
  115. it('align initial values for constant', function () {
  116. let settings = GetDefaultSettings();
  117. settings.SignAlignSettings = new signAlignSettings(false, true, "", [], false);
  118. let input = 'CONSTANT ADDR_MATCH : STD_LOGIC_VECTOR(5 * 32 - 1 DOWNTO 0) := (\r\n X"00000000" &\r\n X"00010000" &\r\n X"00020000" &\r\n X"00030000" &\r\n X"00040000"\r\n);';
  119. let result = beautify(input, settings);
  120. expect(result).toBe(input);
  121. });
  122. it('one-line initial values for constant', function () {
  123. let settings = GetDefaultSettings();
  124. settings.SignAlignSettings = new signAlignSettings(false, true, "", [], false);
  125. let input = "CONSTANT Vcc : SIGNAL := '1'; --logic 1 constant\r\nCONSTANT zero4 : bit_vector(0 TO 3) := ('0', '0', '0', '0');\r\nCONSTANT Vcc : SIGNAL := '1'; --logic 1 constant";
  126. let result = beautify(input, settings);
  127. expect(result).toBe(input);
  128. });
  129. it('indent assignment statement (multiline, no comment)', function () {
  130. let settings = GetDefaultSettings();
  131. settings.SignAlignSettings = new signAlignSettings(false, true, "", [], false);
  132. let input = "CONSTANT Vcc : SIGNAL :=\r\n'1'; --logic 1 constant\r\nCONSTANT zero4 : bit_vector(0 TO 3) :=\r\n('0', '0', '0', '0');";
  133. let output = "CONSTANT Vcc : SIGNAL :=\r\n '1'; --logic 1 constant\r\nCONSTANT zero4 : bit_vector(0 TO 3) :=\r\n ('0', '0', '0', '0');";
  134. let result = beautify(input, settings);
  135. expect(result).toBe(output);
  136. })
  137. it('indent assignment statement (with comment)', function () {
  138. let settings = GetDefaultSettings();
  139. settings.SignAlignSettings = new signAlignSettings(false, true, "", [], false);
  140. let input = "CONSTANT Vcc : SIGNAL := --logic 1 constant\r\n'1';\r\nCONSTANT zero4 : bit_vector(0 TO 3) :=--test\r\n('0', '0', '0', '0');";
  141. let output = "CONSTANT Vcc : SIGNAL := --logic 1 constant\r\n '1';\r\nCONSTANT zero4 : bit_vector(0 TO 3) := --test\r\n ('0', '0', '0', '0');";
  142. let result = beautify(input, settings);
  143. expect(result).toBe(output);
  144. })
  145. it('indent assignment statement (multi line)', function () {
  146. let settings = GetDefaultSettings();
  147. settings.SignAlignSettings = new signAlignSettings(false, true, "", [], false);
  148. let input = [
  149. "CONSTANT ALMOST_EMPTY_OFFSET : bit_vector(12 DOWNTO 0) :=",
  150. " to_bitvector(STD_ULOGIC_VECTOR(to_unsigned(C_M_AXI_BURST_LEN - 1, 13)));",
  151. "CONSTANT ALMOST_FULL_OFFSET : bit_vector(12 DOWNTO 0) :=",
  152. " to_bitvector(STD_ULOGIC_VECTOR(to_unsigned(C_M_AXI_BURST_LEN - 1, 13)));"
  153. ];
  154. let output = [
  155. "CONSTANT ALMOST_EMPTY_OFFSET : bit_vector(12 DOWNTO 0) :=",
  156. " to_bitvector(STD_ULOGIC_VECTOR(to_unsigned(C_M_AXI_BURST_LEN - 1, 13)));",
  157. "CONSTANT ALMOST_FULL_OFFSET : bit_vector(12 DOWNTO 0) :=",
  158. " to_bitvector(STD_ULOGIC_VECTOR(to_unsigned(C_M_AXI_BURST_LEN - 1, 13)));"
  159. ];
  160. let result = beautifyTestHelper(input, settings);
  161. expect(result).toStrictEqual(output);
  162. })
  163. it('indent assignment statement (multi line (2))', function () {
  164. let settings = GetDefaultSettings();
  165. settings.SignAlignSettings = new signAlignSettings(false, true, "", [], false);
  166. let input = [
  167. "CONSTANT ALMOST_EMPTY_OFFSET : bit_vector(12 DOWNTO 0) :=",
  168. "to_bitvector(",
  169. "STD_ULOGIC_VECTOR(",
  170. "to_unsigned(",
  171. "C_M_AXI_BURST_LEN - 1, 13)));",
  172. "CONSTANT ALMOST_FULL_OFFSET : bit_vector(12 DOWNTO 0) :=",
  173. "to_bitvector(",
  174. "STD_ULOGIC_VECTOR(",
  175. "to_unsigned(",
  176. "C_M_AXI_BURST_LEN - 1, 13)));"
  177. ];
  178. let output = [
  179. "CONSTANT ALMOST_EMPTY_OFFSET : bit_vector(12 DOWNTO 0) :=",
  180. " to_bitvector(",
  181. " STD_ULOGIC_VECTOR(",
  182. " to_unsigned(",
  183. " C_M_AXI_BURST_LEN - 1, 13)));",
  184. "CONSTANT ALMOST_FULL_OFFSET : bit_vector(12 DOWNTO 0) :=",
  185. " to_bitvector(",
  186. " STD_ULOGIC_VECTOR(",
  187. " to_unsigned(",
  188. " C_M_AXI_BURST_LEN - 1, 13)));"
  189. ];
  190. let result = beautifyTestHelper(input, settings);
  191. expect(result).toStrictEqual(output);
  192. })
  193. });
  194. function beautifyTestHelper(array: Array<string>, settings: BeautifierSettings): Array<string> {
  195. let input = array.join("\r\n");
  196. let result = beautify(input, settings);
  197. return result.split("\r\n");
  198. }
  199. function GetDefaultSettings(indentation: string = " "): BeautifierSettings {
  200. let new_line_after_symbols = new NewLineSettings();
  201. new_line_after_symbols.newLineAfter = ["then", ";"];
  202. new_line_after_symbols.noNewLineAfter = ["generic"];
  203. let signAligns = new signAlignSettings(false, false, "", []);
  204. let settings = getDefaultBeautifierSettings(new_line_after_symbols, signAligns, indentation);
  205. return settings;
  206. }
  207. function getDefaultBeautifierSettings(newLineSettings: NewLineSettings, signAlignSettings: signAlignSettings = null, indentation: string = " "): BeautifierSettings {
  208. return new BeautifierSettings(false, false, false, signAlignSettings, "uppercase", "uppercase", indentation, newLineSettings, "\r\n", false);
  209. }