Skip to content

Commit

Permalink
Bug fix: Doesn't remove space from ' ' declaration.
Browse files Browse the repository at this point in the history
  • Loading branch information
Roald87 committed Jun 20, 2020
1 parent c973635 commit 0c2326a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
6 changes: 2 additions & 4 deletions src/TcBlack/VariableDeclaration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,9 @@ public TcDeclaration Tokenize()

public string RemoveWhiteSpaceIfPossible(string str)
{
// https://stackoverflow.com/a/49386152/6329629
string spacesRemoved = Regex.Replace(str, @"(?!\b\s+\b)\s+", "");
string spaceAfterArray = spacesRemoved.Replace("]OF", "] OF");
string pattern = @"\s+(?=[^[\]]*\])|\s+(?=[^()]*\))";

return spaceAfterArray;
return Regex.Replace(str, pattern, "").Trim();
}
}

Expand Down
9 changes: 6 additions & 3 deletions src/TcBlackTests/VariableDeclarationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public void VarAllocationAndTypeVariousWhitespaceArangements
[InlineData("pid_controller ", "", "ST_Struct ", " (nVar1:=1, nVar2:=4)")]
[InlineData("Light", "", "photons ", "2.4 ")]
[InlineData("SomeWords ", "", "T_MaxString ", " 'Black quartz watch my vow.'")]
[InlineData(" Character ", "", " STRING(1)", "' '")]
[InlineData(
"aSample_3 ", "", "ARRAY[1..2, 2..3, 3..4] OF INT ", "[2(0),4(4),2,3]"
)]
Expand Down Expand Up @@ -123,7 +124,7 @@ public void VarAllocationTypeAndInitializationVariousWhitespaceArangements(
TcDeclaration expectedDecl = new TcDeclaration(
variable.Trim(),
_allocation.Trim(),
varDecl.RemoveWhiteSpaceIfPossible(type.Trim()),
varDecl.RemoveWhiteSpaceIfPossible(type),
varDecl.RemoveWhiteSpaceIfPossible(initialization), ""
);

Expand Down Expand Up @@ -184,8 +185,10 @@ private void AssertEquals(TcDeclaration expected, TcDeclaration actual)
"fbSample : FB_Sample(nId_Init:=11, fIn_Init:=33.44);"
)]
[InlineData(
"var1 : REAL := 8 ; // Comment",
"var1 : REAL := 8; // Comment"
"var1 : REAL := 8 ; // Comment", "var1 : REAL := 8; // Comment"
)]
[InlineData(
"character : STRING(1) := ' ' ; ", "character : STRING(1) := ' ';"
)]
public void FormatVariableDeclaration(string unformattedCode, string expected)
{
Expand Down

0 comments on commit 0c2326a

Please sign in to comment.