diff --git a/VHDLFiles/mixed_if_case.vhd b/VHDLFiles/mixed_if_case.vhd new file mode 100644 index 0000000..bbb10cb --- /dev/null +++ b/VHDLFiles/mixed_if_case.vhd @@ -0,0 +1,34 @@ +ARCHITECTURE ARCH OF ENTITY IS +BEGIN + PROC_1 : PROCESS (a, b, c, d) IS + BEGIN + IF (a = 1) THEN + CASE b + WHEN 1 => + c <= d; + CASE b + WHEN 1 => + c <= d; + WHEN 2 => + d <= f; + END CASE; + WHEN 2 => + d <= f; + END CASE; + ELSIF (b = 1) THEN + CASE b + WHEN 1 => + c <= d; + WHEN 2 => + d <= f; + END CASE; + ELSE + CASE b + WHEN 1 => + c <= d; + WHEN 2 => + d <= f; + END CASE; + END IF; + END PROCESS PROC_1; +END ARCHITECTURE ARCH; \ No newline at end of file diff --git a/VHDLFormatter.js b/VHDLFormatter.js index a1d2890..25d3ddf 100644 --- a/VHDLFormatter.js +++ b/VHDLFormatter.js @@ -314,18 +314,16 @@ function beautifyCaseBlock(inputs, result, settings, startIndex, indent, isFirst exports.beautifyCaseBlock = beautifyCaseBlock; function beautify3(inputs, result, settings, startIndex, indent, isFirstKeyWord) { let i; - let ignoreFirstKeyWords = ["WHEN"]; - let blockMidKeyWords = ["ELSE", "ELSIF"].concat(ignoreFirstKeyWords); - let blockStartsKeyWords = ["IF", "CASE"]; + let regexOneLineBlockKeyWords = new RegExp(/(PROCEDURE|FUNCTION|IMPURE FUNCTION)[^\w_](?!.+[^\w_]IS([^\w_]|$))/); //match PROCEDURE..; but not PROCEDURE .. IS; + let blockMidKeyWords = ["ELSE", "ELSIF", "WHEN", "BEGIN"]; + let blockStartsKeyWords = ["IF", "CASE", "ARCHITECTURE", "PROCEDURE", "PACKAGE", "PROCESS", "POSTPONED PROCESS", "(\\w+:\\s+PROCESS)", "FUNCTION", "IMPURE FUNCTION", "TYPE\\s.+\\sPROTECTED"]; let blockEndsKeyWords = ["END"]; - let ignoreFirstKeyWordsStr = ignoreFirstKeyWords.join("|"); let newLineAfterKeyWordsStr = blockStartsKeyWords.join("|"); let blockEndKeyWordsStr = blockEndsKeyWords.join("|"); let blockMidKeyWordsStr = blockMidKeyWords.join("|"); - let regexBlockMidKeyWords = new RegExp("(" + blockMidKeyWordsStr + ")([\\s]|$)"); - let regexIgnoreFirstKeyWords = new RegExp("(" + ignoreFirstKeyWordsStr + ")([\\s]|$)"); - let regexBlockStartsKeywords = new RegExp("(" + newLineAfterKeyWordsStr + ")([\\s]|$)"); - let regexBlockEndsKeyWords = new RegExp("(" + blockEndKeyWordsStr + ")([\\s]|$)"); + let regexBlockMidKeyWords = new RegExp("(" + blockMidKeyWordsStr + ")([^\\w_]|$)"); + let regexBlockStartsKeywords = new RegExp("(" + newLineAfterKeyWordsStr + ")([^\\w_]|$)"); + let regexBlockEndsKeyWords = new RegExp("(" + blockEndKeyWordsStr + ")([^\\w_]|$)"); for (i = startIndex; i < inputs.length; i++) { let input = inputs[i]; if (input.regexStartsWith(/(CASE)([\s]|$)/)) { @@ -342,6 +340,9 @@ function beautify3(inputs, result, settings, startIndex, indent, isFirstKeyWord) result[i].Indent--; return i; } + if (input.regexStartsWith(regexOneLineBlockKeyWords)) { + continue; + } if (input.regexStartsWith(regexBlockStartsKeywords)) { i = beautify3(inputs, result, settings, i + 1, indent + 1); } diff --git a/VHDLFormatter.ts b/VHDLFormatter.ts index 183fa7a..a9a3636 100644 --- a/VHDLFormatter.ts +++ b/VHDLFormatter.ts @@ -358,19 +358,17 @@ export function beautifyCaseBlock(inputs: Array, result: (FormattedLine export function beautify3(inputs: Array, result: (FormattedLine | FormattedLine[])[], settings: BeautifierSettings, startIndex: number, indent: number, isFirstKeyWord?: boolean): number { let i: number; - let ignoreFirstKeyWords: Array = ["WHEN"]; - let blockMidKeyWords: Array = ["ELSE", "ELSIF"].concat(ignoreFirstKeyWords); - let blockStartsKeyWords: Array = ["IF", "CASE"]; + let regexOneLineBlockKeyWords: RegExp = new RegExp(/(PROCEDURE|FUNCTION|IMPURE FUNCTION)[^\w_](?!.+[^\w_]IS([^\w_]|$))/);//match PROCEDURE..; but not PROCEDURE .. IS; + let blockMidKeyWords: Array = ["ELSE", "ELSIF", "WHEN", "BEGIN"]; + let blockStartsKeyWords: Array = ["IF", "CASE", "ARCHITECTURE", "PROCEDURE", "PACKAGE", "PROCESS", "POSTPONED PROCESS","(\\w+:\\s+PROCESS)","FUNCTION","IMPURE FUNCTION","TYPE\\s.+\\sPROTECTED"]; let blockEndsKeyWords: Array = ["END"]; - let ignoreFirstKeyWordsStr: string = ignoreFirstKeyWords.join("|"); let newLineAfterKeyWordsStr: string = blockStartsKeyWords.join("|"); let blockEndKeyWordsStr: string = blockEndsKeyWords.join("|"); let blockMidKeyWordsStr: string = blockMidKeyWords.join("|"); - let regexBlockMidKeyWords: RegExp = new RegExp("(" + blockMidKeyWordsStr + ")([\\s]|$)") - let regexIgnoreFirstKeyWords: RegExp = new RegExp("(" + ignoreFirstKeyWordsStr + ")([\\s]|$)") - let regexBlockStartsKeywords: RegExp = new RegExp("(" + newLineAfterKeyWordsStr + ")([\\s]|$)") - let regexBlockEndsKeyWords: RegExp = new RegExp("(" + blockEndKeyWordsStr + ")([\\s]|$)") + let regexBlockMidKeyWords: RegExp = new RegExp("(" + blockMidKeyWordsStr + ")([^\\w_]|$)") + let regexBlockStartsKeywords: RegExp = new RegExp("(" + newLineAfterKeyWordsStr + ")([^\\w_]|$)") + let regexBlockEndsKeyWords: RegExp = new RegExp("(" + blockEndKeyWordsStr + ")([^\\w_]|$)") for (i = startIndex; i < inputs.length; i++) { let input: string = inputs[i]; if (input.regexStartsWith(/(CASE)([\s]|$)/)) { @@ -387,6 +385,9 @@ export function beautify3(inputs: Array, result: (FormattedLine | Format (result[i]).Indent--; return i; } + if (input.regexStartsWith(regexOneLineBlockKeyWords)) { + continue; + } if (input.regexStartsWith(regexBlockStartsKeywords)) { i = beautify3(inputs, result, settings, i + 1, indent + 1); } diff --git a/VHDLFormatterUnitTests.js b/VHDLFormatterUnitTests.js index 7365418..6827ac2 100644 --- a/VHDLFormatterUnitTests.js +++ b/VHDLFormatterUnitTests.js @@ -29,6 +29,13 @@ function UnitTestbeautify3() { Beautify3Case4(); Beautify3Case5(); Beautify3Case6(); + Beautify3Case7(); + Beautify3Case8(); + Beautify3Case9(); + Beautify3Case10(); + Beautify3Case11(); + Beautify3Case12(); + Beautify3Case13(); } function Beautify3Case1() { let new_line_after_symbols = new VHDLFormatter_3.NewLineSettings(); @@ -144,6 +151,219 @@ function Beautify3Case6() { ]; UnitTest6(VHDLFormatter_8.beautify3, "case & case end", settings, inputs, expected, 0, 11, 0); } +function Beautify3Case7() { + let new_line_after_symbols = new VHDLFormatter_3.NewLineSettings(); + new_line_after_symbols.newLineAfter = ["then", ";"]; + new_line_after_symbols.noNewLineAfter = ["port", "generic"]; + let settings = new VHDLFormatter_4.BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols); + let inputs = [ + "ARCHITECTURE a OF one IS", + "SIGNAL x : INTEGER;", + "BEGIN", + "-- architecture", + "END ARCHITECTURE;" + ]; + let expected = [ + new VHDLFormatter_9.FormattedLine("ARCHITECTURE a OF one IS", 0), + new VHDLFormatter_9.FormattedLine("SIGNAL x : INTEGER;", 1), + new VHDLFormatter_9.FormattedLine("BEGIN", 0), + new VHDLFormatter_9.FormattedLine("-- architecture", 1), + new VHDLFormatter_9.FormattedLine("END ARCHITECTURE;", 0), + ]; + UnitTest6(VHDLFormatter_8.beautify3, "architecture", settings, inputs, expected, 0, 4, 0); +} +function Beautify3Case8() { + let new_line_after_symbols = new VHDLFormatter_3.NewLineSettings(); + new_line_after_symbols.newLineAfter = ["then", ";"]; + new_line_after_symbols.noNewLineAfter = ["port", "generic"]; + let settings = new VHDLFormatter_4.BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols); + let inputs = [ + "ARCHITECTURE a OF one IS", + "SIGNAL x : INTEGER;", + "BEGIN", + "-- architecture", + "END ARCHITECTURE;", + "ARCHITECTURE b OF one IS", + "SIGNAL x : INTEGER;", + "BEGIN", + "-- architecture", + "END ARCHITECTURE;" + ]; + let expected = [ + new VHDLFormatter_9.FormattedLine("ARCHITECTURE a OF one IS", 0), + new VHDLFormatter_9.FormattedLine("SIGNAL x : INTEGER;", 1), + new VHDLFormatter_9.FormattedLine("BEGIN", 0), + new VHDLFormatter_9.FormattedLine("-- architecture", 1), + new VHDLFormatter_9.FormattedLine("END ARCHITECTURE;", 0), + new VHDLFormatter_9.FormattedLine("ARCHITECTURE b OF one IS", 0), + new VHDLFormatter_9.FormattedLine("SIGNAL x : INTEGER;", 1), + new VHDLFormatter_9.FormattedLine("BEGIN", 0), + new VHDLFormatter_9.FormattedLine("-- architecture", 1), + new VHDLFormatter_9.FormattedLine("END ARCHITECTURE;", 0), + ]; + UnitTest6(VHDLFormatter_8.beautify3, "architecture 2", settings, inputs, expected, 0, 9, 0); +} +function Beautify3Case9() { + let new_line_after_symbols = new VHDLFormatter_3.NewLineSettings(); + new_line_after_symbols.newLineAfter = ["then", ";"]; + new_line_after_symbols.noNewLineAfter = ["port", "generic"]; + let settings = new VHDLFormatter_4.BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols); + let inputs = [ + "PROCEDURE foo(x : IN INTEGER; y : OUT INTEGER) IS", + "VARIABLE i : INTEGER;", + "BEGIN", + "y := x + 1;", + "END PROCEDURE;" + ]; + let expected = [ + new VHDLFormatter_9.FormattedLine("PROCEDURE foo(x : IN INTEGER; y : OUT INTEGER) IS", 0), + new VHDLFormatter_9.FormattedLine("VARIABLE i : INTEGER;", 1), + new VHDLFormatter_9.FormattedLine("BEGIN", 0), + new VHDLFormatter_9.FormattedLine("y := x + 1;", 1), + new VHDLFormatter_9.FormattedLine("END PROCEDURE;", 0) + ]; + UnitTest6(VHDLFormatter_8.beautify3, "procedure", settings, inputs, expected, 0, 4, 0); +} +function Beautify3Case10() { + let new_line_after_symbols = new VHDLFormatter_3.NewLineSettings(); + new_line_after_symbols.newLineAfter = ["then", ";"]; + new_line_after_symbols.noNewLineAfter = ["port", "generic"]; + let settings = new VHDLFormatter_4.BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols); + let inputs = [ + "PACKAGE three IS", + "SIGNAL s : INTEGER;", + "ALIAS sa IS s;", + "END PACKAGE;" + ]; + let expected = [ + new VHDLFormatter_9.FormattedLine("PACKAGE three IS", 0), + new VHDLFormatter_9.FormattedLine("SIGNAL s : INTEGER;", 1), + new VHDLFormatter_9.FormattedLine("ALIAS sa IS s;", 1), + new VHDLFormatter_9.FormattedLine("END PACKAGE;", 0) + ]; + UnitTest6(VHDLFormatter_8.beautify3, "package", settings, inputs, expected, 0, 3, 0); +} +function Beautify3Case11() { + let new_line_after_symbols = new VHDLFormatter_3.NewLineSettings(); + new_line_after_symbols.newLineAfter = ["then", ";"]; + new_line_after_symbols.noNewLineAfter = ["port", "generic"]; + let settings = new VHDLFormatter_4.BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols); + let inputs = [ + "PACKAGE p IS", + "PROCEDURE foo(x : IN INTEGER; y : OUT INTEGER);", + "END PACKAGE;", + "PACKAGE BODY p IS", + "PROCEDURE foo(x : IN INTEGER; y : OUT INTEGER) IS", + "VARIABLE i : INTEGER;", + "BEGIN", + "y := x + 1;", + "END PROCEDURE;", + "PROCEDURE bar(FILE x : text);", + "PROCEDURE baz IS", + "TYPE foo;", + "ALIAS x IS y;", + "BEGIN", + "END PROCEDURE;", + "PROCEDURE tralala IS", + "USE work.foo;", + "BEGIN", + "END PROCEDURE;", + "END PACKAGE BODY;" + ]; + let expected = [ + new VHDLFormatter_9.FormattedLine("PACKAGE p IS", 0), + new VHDLFormatter_9.FormattedLine("PROCEDURE foo(x : IN INTEGER; y : OUT INTEGER);", 1), + new VHDLFormatter_9.FormattedLine("END PACKAGE;", 0), + new VHDLFormatter_9.FormattedLine("PACKAGE BODY p IS", 0), + new VHDLFormatter_9.FormattedLine("PROCEDURE foo(x : IN INTEGER; y : OUT INTEGER) IS", 1), + new VHDLFormatter_9.FormattedLine("VARIABLE i : INTEGER;", 2), + new VHDLFormatter_9.FormattedLine("BEGIN", 1), + new VHDLFormatter_9.FormattedLine("y := x + 1;", 2), + new VHDLFormatter_9.FormattedLine("END PROCEDURE;", 1), + new VHDLFormatter_9.FormattedLine("PROCEDURE bar(FILE x : text);", 1), + new VHDLFormatter_9.FormattedLine("PROCEDURE baz IS", 1), + new VHDLFormatter_9.FormattedLine("TYPE foo;", 2), + new VHDLFormatter_9.FormattedLine("ALIAS x IS y;", 2), + new VHDLFormatter_9.FormattedLine("BEGIN", 1), + new VHDLFormatter_9.FormattedLine("END PROCEDURE;", 1), + new VHDLFormatter_9.FormattedLine("PROCEDURE tralala IS", 1), + new VHDLFormatter_9.FormattedLine("USE work.foo;", 2), + new VHDLFormatter_9.FormattedLine("BEGIN", 1), + new VHDLFormatter_9.FormattedLine("END PROCEDURE;", 1), + new VHDLFormatter_9.FormattedLine("END PACKAGE BODY;", 0) + ]; + UnitTest6(VHDLFormatter_8.beautify3, "package", settings, inputs, expected, 0, expected.length - 1, 0); +} +function Beautify3Case12() { + let new_line_after_symbols = new VHDLFormatter_3.NewLineSettings(); + new_line_after_symbols.newLineAfter = ["then", ";"]; + new_line_after_symbols.noNewLineAfter = ["port", "generic"]; + let settings = new VHDLFormatter_4.BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols); + let inputs = [ + "ARCHITECTURE a OF b IS", + "SIGNAL x : INTEGER := 0;", + "BEGIN", + "p: PROCESS IS", + "BEGIN", + "END PROCESS;", + "PROCESS", + "VARIABLE y : INTEGER := 5;", + "BEGIN", + "x <= y;", + "END PROCESS;", + "PROCESS (x) IS", + "BEGIN", + "x <= x + 1;", + "END PROCESS;", + "POSTPONED PROCESS IS", + "BEGIN", + "END PROCESS;", + "POSTPONED assert x = 1;", + "END ARCHITECTURE;" + ]; + let expected = [ + new VHDLFormatter_9.FormattedLine("ARCHITECTURE a OF b IS", 0), + new VHDLFormatter_9.FormattedLine("SIGNAL x : INTEGER := 0;", 1), + new VHDLFormatter_9.FormattedLine("BEGIN", 0), + new VHDLFormatter_9.FormattedLine("p: PROCESS IS", 1), + new VHDLFormatter_9.FormattedLine("BEGIN", 1), + new VHDLFormatter_9.FormattedLine("END PROCESS;", 1), + new VHDLFormatter_9.FormattedLine("PROCESS", 1), + new VHDLFormatter_9.FormattedLine("VARIABLE y : INTEGER := 5;", 2), + new VHDLFormatter_9.FormattedLine("BEGIN", 1), + new VHDLFormatter_9.FormattedLine("x <= y;", 2), + new VHDLFormatter_9.FormattedLine("END PROCESS;", 1), + new VHDLFormatter_9.FormattedLine("PROCESS (x) IS", 1), + new VHDLFormatter_9.FormattedLine("BEGIN", 1), + new VHDLFormatter_9.FormattedLine("x <= x + 1;", 2), + new VHDLFormatter_9.FormattedLine("END PROCESS;", 1), + new VHDLFormatter_9.FormattedLine("POSTPONED PROCESS IS", 1), + new VHDLFormatter_9.FormattedLine("BEGIN", 1), + new VHDLFormatter_9.FormattedLine("END PROCESS;", 1), + new VHDLFormatter_9.FormattedLine("POSTPONED assert x = 1;", 1), + new VHDLFormatter_9.FormattedLine("END ARCHITECTURE;", 0) + ]; + UnitTest6(VHDLFormatter_8.beautify3, "package", settings, inputs, expected, 0, expected.length - 1, 0); +} +function Beautify3Case13() { + let new_line_after_symbols = new VHDLFormatter_3.NewLineSettings(); + new_line_after_symbols.newLineAfter = ["then", ";"]; + new_line_after_symbols.noNewLineAfter = ["port", "generic"]; + let settings = new VHDLFormatter_4.BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols); + let inputs = [ + "TYPE SharedCounter IS PROTECTED", + "PROCEDURE increment (N : INTEGER := 1);", + "IMPURE FUNCTION value RETURN INTEGER;", + "END PROTECTED SharedCounter;" + ]; + let expected = [ + new VHDLFormatter_9.FormattedLine("TYPE SharedCounter IS PROTECTED", 0), + new VHDLFormatter_9.FormattedLine("PROCEDURE increment (N : INTEGER := 1);", 1), + new VHDLFormatter_9.FormattedLine("IMPURE FUNCTION value RETURN INTEGER;", 1), + new VHDLFormatter_9.FormattedLine("END PROTECTED SharedCounter;", 0) + ]; + UnitTest6(VHDLFormatter_8.beautify3, "package", settings, inputs, expected, 0, expected.length - 1, 0); +} function UnitTestSetNewLinesAfterSymbols() { console.log("=== SetNewLinesAfterSymbols ==="); let input = "a; @@comments1\r\nb;"; @@ -183,24 +403,30 @@ function UnitTestIndentDecode() { UnitTest2(VHDLFormatter_2.indentDecode, "4 blankspaces", " ", "four blankspace"); UnitTest2(VHDLFormatter_2.indentDecode, "9 blankspaces", " ", "many blankspace"); } -function assertFormattedLines(testName, expected, actual, message, cumulateTestCount) { +function compareFormattedLines(expected, actual, message) { var l = Math.min(actual.length, expected.length); let result = ""; for (var i = 0; i < l; i++) { if (actual[i] instanceof VHDLFormatter_9.FormattedLine) { if (expected[i] instanceof VHDLFormatter_9.FormattedLine) { - assertFormattedLine(testName, (expected[i]), (actual[i]), message, false); + let compareResult = compareFormattedLine((expected[i]), (actual[i]), message, false); + if (compareResult.length > 0) { + result += "index " + i + "\n" + compareResult; + } } else { - console.log("expected FormatLine[], actual FormattedLine. actual:" + (actual[i]).Line); + result += "index " + i + "\nexpected FormatLine[], actual FormattedLine. actual:" + (actual[i]).Line; } } else { if (expected[i] instanceof VHDLFormatter_9.FormattedLine) { - console.log("expected FormatLine, actual FormattedLine[]. expected:" + (expected[i]).Line); + result += "index " + i + "\nexpected FormatLine, actual FormattedLine[]. expected:" + (expected[i]).Line; } else { - assertFormattedLines(testName, (actual[i]), (expected[i]), message, false); + let compareResult = compareFormattedLines((actual[i]), (expected[i]), message); + if (compareResult.length > 0) { + result += "index " + i + "\n" + compareResult; + } } } } @@ -216,28 +442,26 @@ function assertFormattedLines(testName, expected, actual, message, cumulateTestC result += "expected[" + i + "] = " + expected[i]; } } + return result; +} +function assertFormattedLines(testName, expected, actual, message) { + let result = compareFormattedLines(expected, actual, message); if (result.length > 0) { - console.log(result); - } - if (cumulateTestCount != false) { - testCount++; + console.log(testName + " failed:\n" + result); } + testCount++; } -function assertFormattedLine(testName, expected, actual, message, cumulateTestCount) { +function compareFormattedLine(expected, actual, message, cumulateTestCount) { + let result = ""; if (expected.Indent != actual.Indent) { - console.log(testName + ' failed:\nexpected: "' + expected.Line + '", ' + expected.Indent - + ';\nactual: "' + actual.Line + '", ' + actual.Indent); - } - var result = CompareString(actual.Line, expected.Line); - if (result != true) { - console.log(testName + " failed: " + result); - } - else { - //console.log(testName + " pass"); + result += 'indents are not equal;\nexpected: "' + expected.Line + '", ' + expected.Indent + + ';\nactual: "' + actual.Line + '", ' + actual.Indent + "\n"; } - if (cumulateTestCount != false) { - testCount++; + let compareResult = CompareString(actual.Line, expected.Line); + if (compareResult != true) { + result += compareResult; } + return result; } function assert(testName, expected, actual, message) { var result = CompareString(actual, expected); diff --git a/VHDLFormatterUnitTests.ts b/VHDLFormatterUnitTests.ts index deeb761..589d213 100644 --- a/VHDLFormatterUnitTests.ts +++ b/VHDLFormatterUnitTests.ts @@ -36,6 +36,13 @@ function UnitTestbeautify3() { Beautify3Case4(); Beautify3Case5(); Beautify3Case6(); + Beautify3Case7(); + Beautify3Case8(); + Beautify3Case9(); + Beautify3Case10(); + Beautify3Case11(); + Beautify3Case12(); + Beautify3Case13(); } function Beautify3Case1() { @@ -157,6 +164,225 @@ function Beautify3Case6() { UnitTest6(beautify3, "case & case end", settings, inputs, expected, 0, 11, 0); } +function Beautify3Case7() { + let new_line_after_symbols: NewLineSettings = new NewLineSettings(); + new_line_after_symbols.newLineAfter = ["then", ";"]; + new_line_after_symbols.noNewLineAfter = ["port", "generic"]; + let settings: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols); + let inputs: Array = [ + "ARCHITECTURE a OF one IS", + "SIGNAL x : INTEGER;", + "BEGIN", + "-- architecture", + "END ARCHITECTURE;" + ]; + let expected: (FormattedLine | FormattedLine[])[] = [ + new FormattedLine("ARCHITECTURE a OF one IS", 0), + new FormattedLine("SIGNAL x : INTEGER;", 1), + new FormattedLine("BEGIN", 0), + new FormattedLine("-- architecture", 1), + new FormattedLine("END ARCHITECTURE;", 0), + ]; + UnitTest6(beautify3, "architecture", settings, inputs, expected, 0, 4, 0); +} + +function Beautify3Case8() { + let new_line_after_symbols: NewLineSettings = new NewLineSettings(); + new_line_after_symbols.newLineAfter = ["then", ";"]; + new_line_after_symbols.noNewLineAfter = ["port", "generic"]; + let settings: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols); + let inputs: Array = [ + "ARCHITECTURE a OF one IS", + "SIGNAL x : INTEGER;", + "BEGIN", + "-- architecture", + "END ARCHITECTURE;", + "ARCHITECTURE b OF one IS", + "SIGNAL x : INTEGER;", + "BEGIN", + "-- architecture", + "END ARCHITECTURE;" + ]; + let expected: (FormattedLine | FormattedLine[])[] = [ + new FormattedLine("ARCHITECTURE a OF one IS", 0), + new FormattedLine("SIGNAL x : INTEGER;", 1), + new FormattedLine("BEGIN", 0), + new FormattedLine("-- architecture", 1), + new FormattedLine("END ARCHITECTURE;", 0), + new FormattedLine("ARCHITECTURE b OF one IS", 0), + new FormattedLine("SIGNAL x : INTEGER;", 1), + new FormattedLine("BEGIN", 0), + new FormattedLine("-- architecture", 1), + new FormattedLine("END ARCHITECTURE;", 0), + ]; + UnitTest6(beautify3, "architecture 2", settings, inputs, expected, 0, 9, 0); +} + +function Beautify3Case9() { + let new_line_after_symbols: NewLineSettings = new NewLineSettings(); + new_line_after_symbols.newLineAfter = ["then", ";"]; + new_line_after_symbols.noNewLineAfter = ["port", "generic"]; + let settings: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols); + let inputs: Array = [ + "PROCEDURE foo(x : IN INTEGER; y : OUT INTEGER) IS", + "VARIABLE i : INTEGER;", + "BEGIN", + "y := x + 1;", + "END PROCEDURE;" + ]; + let expected: (FormattedLine | FormattedLine[])[] = [ + new FormattedLine("PROCEDURE foo(x : IN INTEGER; y : OUT INTEGER) IS", 0), + new FormattedLine("VARIABLE i : INTEGER;", 1), + new FormattedLine("BEGIN", 0), + new FormattedLine("y := x + 1;", 1), + new FormattedLine("END PROCEDURE;", 0) + ]; + UnitTest6(beautify3, "procedure", settings, inputs, expected, 0, 4, 0); +} + +function Beautify3Case10() { + let new_line_after_symbols: NewLineSettings = new NewLineSettings(); + new_line_after_symbols.newLineAfter = ["then", ";"]; + new_line_after_symbols.noNewLineAfter = ["port", "generic"]; + let settings: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols); + let inputs: Array = [ + "PACKAGE three IS", + "SIGNAL s : INTEGER;", + "ALIAS sa IS s;", + "END PACKAGE;" + ]; + let expected: (FormattedLine | FormattedLine[])[] = [ + new FormattedLine("PACKAGE three IS", 0), + new FormattedLine("SIGNAL s : INTEGER;", 1), + new FormattedLine("ALIAS sa IS s;", 1), + new FormattedLine("END PACKAGE;", 0) + ]; + UnitTest6(beautify3, "package", settings, inputs, expected, 0, 3, 0); +} + +function Beautify3Case11() { + let new_line_after_symbols: NewLineSettings = new NewLineSettings(); + new_line_after_symbols.newLineAfter = ["then", ";"]; + new_line_after_symbols.noNewLineAfter = ["port", "generic"]; + let settings: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols); + let inputs: Array = [ + "PACKAGE p IS", + "PROCEDURE foo(x : IN INTEGER; y : OUT INTEGER);", + "END PACKAGE;", + "PACKAGE BODY p IS", + "PROCEDURE foo(x : IN INTEGER; y : OUT INTEGER) IS", + "VARIABLE i : INTEGER;", + "BEGIN", + "y := x + 1;", + "END PROCEDURE;", + "PROCEDURE bar(FILE x : text);", + "PROCEDURE baz IS", + "TYPE foo;", + "ALIAS x IS y;", + "BEGIN", + "END PROCEDURE;", + "PROCEDURE tralala IS", + "USE work.foo;", + "BEGIN", + "END PROCEDURE;", + "END PACKAGE BODY;" + ]; + let expected: (FormattedLine | FormattedLine[])[] = [ + new FormattedLine("PACKAGE p IS", 0), + new FormattedLine("PROCEDURE foo(x : IN INTEGER; y : OUT INTEGER);", 1), + new FormattedLine("END PACKAGE;", 0), + new FormattedLine("PACKAGE BODY p IS", 0), + new FormattedLine("PROCEDURE foo(x : IN INTEGER; y : OUT INTEGER) IS", 1), + new FormattedLine("VARIABLE i : INTEGER;", 2), + new FormattedLine("BEGIN", 1), + new FormattedLine("y := x + 1;", 2), + new FormattedLine("END PROCEDURE;", 1), + new FormattedLine("PROCEDURE bar(FILE x : text);", 1), + new FormattedLine("PROCEDURE baz IS", 1), + new FormattedLine("TYPE foo;", 2), + new FormattedLine("ALIAS x IS y;", 2), + new FormattedLine("BEGIN", 1), + new FormattedLine("END PROCEDURE;", 1), + new FormattedLine("PROCEDURE tralala IS", 1), + new FormattedLine("USE work.foo;", 2), + new FormattedLine("BEGIN", 1), + new FormattedLine("END PROCEDURE;", 1), + new FormattedLine("END PACKAGE BODY;", 0) + ]; + UnitTest6(beautify3, "package", settings, inputs, expected, 0, expected.length - 1, 0); +} + +function Beautify3Case12() { + let new_line_after_symbols: NewLineSettings = new NewLineSettings(); + new_line_after_symbols.newLineAfter = ["then", ";"]; + new_line_after_symbols.noNewLineAfter = ["port", "generic"]; + let settings: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols); + let inputs: Array = [ + "ARCHITECTURE a OF b IS", + "SIGNAL x : INTEGER := 0;", + "BEGIN", + "p: PROCESS IS", + "BEGIN", + "END PROCESS;", + "PROCESS", + "VARIABLE y : INTEGER := 5;", + "BEGIN", + "x <= y;", + "END PROCESS;", + "PROCESS (x) IS", + "BEGIN", + "x <= x + 1;", + "END PROCESS;", + "POSTPONED PROCESS IS", + "BEGIN", + "END PROCESS;", + "POSTPONED assert x = 1;", + "END ARCHITECTURE;" + ]; + let expected: (FormattedLine | FormattedLine[])[] = [ + new FormattedLine("ARCHITECTURE a OF b IS", 0), + new FormattedLine("SIGNAL x : INTEGER := 0;", 1), + new FormattedLine("BEGIN", 0), + new FormattedLine("p: PROCESS IS", 1), + new FormattedLine("BEGIN", 1), + new FormattedLine("END PROCESS;", 1), + new FormattedLine("PROCESS", 1), + new FormattedLine("VARIABLE y : INTEGER := 5;", 2), + new FormattedLine("BEGIN", 1), + new FormattedLine("x <= y;", 2), + new FormattedLine("END PROCESS;", 1), + new FormattedLine("PROCESS (x) IS", 1), + new FormattedLine("BEGIN", 1), + new FormattedLine("x <= x + 1;", 2), + new FormattedLine("END PROCESS;", 1), + new FormattedLine("POSTPONED PROCESS IS", 1), + new FormattedLine("BEGIN", 1), + new FormattedLine("END PROCESS;", 1), + new FormattedLine("POSTPONED assert x = 1;", 1), + new FormattedLine("END ARCHITECTURE;", 0) + ]; + UnitTest6(beautify3, "package", settings, inputs, expected, 0, expected.length - 1, 0); +} + +function Beautify3Case13() { + let new_line_after_symbols: NewLineSettings = new NewLineSettings(); + new_line_after_symbols.newLineAfter = ["then", ";"]; + new_line_after_symbols.noNewLineAfter = ["port", "generic"]; + let settings: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols); + let inputs: Array = [ + "TYPE SharedCounter IS PROTECTED", + "PROCEDURE increment (N : INTEGER := 1);", + "IMPURE FUNCTION value RETURN INTEGER;", + "END PROTECTED SharedCounter;" + ]; + let expected: (FormattedLine | FormattedLine[])[] = [ + new FormattedLine("TYPE SharedCounter IS PROTECTED", 0), + new FormattedLine("PROCEDURE increment (N : INTEGER := 1);", 1), + new FormattedLine("IMPURE FUNCTION value RETURN INTEGER;", 1), + new FormattedLine("END PROTECTED SharedCounter;", 0) + ]; + UnitTest6(beautify3, "package", settings, inputs, expected, 0, expected.length - 1, 0); +} function UnitTestSetNewLinesAfterSymbols() { console.log("=== SetNewLinesAfterSymbols ==="); @@ -204,24 +430,30 @@ function UnitTestIndentDecode() { UnitTest2(indentDecode, "9 blankspaces", " ", "many blankspace"); } -function assertFormattedLines(testName, expected: (FormattedLine | FormattedLine[])[], actual: (FormattedLine | FormattedLine[])[], message?, cumulateTestCount?: boolean) { +function compareFormattedLines(expected: (FormattedLine | FormattedLine[])[], actual: (FormattedLine | FormattedLine[])[], message?): string { var l = Math.min(actual.length, expected.length); let result: string = ""; for (var i = 0; i < l; i++) { if (actual[i] instanceof FormattedLine) { if (expected[i] instanceof FormattedLine) { - assertFormattedLine(testName, (expected[i]), (actual[i]), message, false); + let compareResult = compareFormattedLine((expected[i]), (actual[i]), message, false); + if (compareResult.length > 0) { + result += "index " + i + "\n" + compareResult; + } } else { - console.log("expected FormatLine[], actual FormattedLine. actual:" + ((actual[i])).Line); + result += "index " + i + "\nexpected FormatLine[], actual FormattedLine. actual:" + ((actual[i])).Line; } } else { if (expected[i] instanceof FormattedLine) { - console.log("expected FormatLine, actual FormattedLine[]. expected:" + ((expected[i])).Line); + result += "index " + i + "\nexpected FormatLine, actual FormattedLine[]. expected:" + ((expected[i])).Line; } else { - assertFormattedLines(testName, (actual[i]), (expected[i]), message, false); + let compareResult = compareFormattedLines((actual[i]), (expected[i]), message); + if (compareResult.length > 0) { + result += "index " + i + "\n" + compareResult; + } } } } @@ -237,30 +469,28 @@ function assertFormattedLines(testName, expected: (FormattedLine | FormattedLine result += "expected[" + i + "] = " + expected[i]; } } - if (result.length > 0) { - console.log(result); - } + return result; +} - if (cumulateTestCount != false) { - testCount++; +function assertFormattedLines(testName, expected: (FormattedLine | FormattedLine[])[], actual: (FormattedLine | FormattedLine[])[], message?) { + let result = compareFormattedLines(expected, actual, message); + if (result.length > 0) { + console.log(testName + " failed:\n" + result); } + testCount++; } -function assertFormattedLine(testName, expected: FormattedLine, actual: FormattedLine, message?, cumulateTestCount?: boolean) { +function compareFormattedLine(expected: FormattedLine, actual: FormattedLine, message?, cumulateTestCount?: boolean) { + let result = ""; if (expected.Indent != actual.Indent) { - console.log(testName + ' failed:\nexpected: "' + expected.Line + '", ' + expected.Indent - + ';\nactual: "' + actual.Line + '", ' + actual.Indent); - } - var result = CompareString(actual.Line, expected.Line); - if (result != true) { - console.log(testName + " failed: " + result); - } - else { - //console.log(testName + " pass"); + result += 'indents are not equal;\nexpected: "' + expected.Line + '", ' + expected.Indent + + ';\nactual: "' + actual.Line + '", ' + actual.Indent + "\n"; } - if (cumulateTestCount != false) { - testCount++; + let compareResult = CompareString(actual.Line, expected.Line); + if (compareResult != true) { + result += compareResult; } + return result; } function assert(testName, expected, actual, message?) {