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.

134 lines
7.3 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. });
  109. function GetDefaultSettings(indentation: string = " "): BeautifierSettings {
  110. let new_line_after_symbols = new NewLineSettings();
  111. new_line_after_symbols.newLineAfter = ["then", ";"];
  112. new_line_after_symbols.noNewLineAfter = ["generic"];
  113. let signAligns = new signAlignSettings(false, false, "", []);
  114. let settings = getDefaultBeautifierSettings(new_line_after_symbols, signAligns, indentation);
  115. return settings;
  116. }
  117. function getDefaultBeautifierSettings(newLineSettings: NewLineSettings, signAlignSettings: signAlignSettings = null, indentation: string = " "): BeautifierSettings {
  118. return new BeautifierSettings(false, false, false, signAlignSettings, "uppercase", "uppercase", indentation, newLineSettings, "\r\n", false);
  119. }