Skip to content

Commit

Permalink
Closes #21. Remove empty variable type declarations.
Browse files Browse the repository at this point in the history
  • Loading branch information
Roald87 committed Jun 28, 2020
1 parent 8440ab0 commit c361b63
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 14 deletions.
17 changes: 12 additions & 5 deletions src/TcBlack/CompositeCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,18 @@ public CompositeCode Tokenize()
}
else if (line.StartsWith("END_VAR"))
{
Add(new VariableBlockEnd(
unformattedCode: line,
singleIndent: _singleIndent,
lineEnding: _lineEnding
));
if (codeLines.Last() is VariableBlockStart)
{
codeLines.RemoveAt(codeLines.Count - 1);
}
else
{
Add(new VariableBlockEnd(
unformattedCode: line,
singleIndent: _singleIndent,
lineEnding: _lineEnding
));
}
}
else if (line.StartsWith("VAR"))
{
Expand Down
30 changes: 30 additions & 0 deletions src/TcBlackTests/CompositeCodeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,35 @@ public void FormatEmptyDeclaration()
string expected = "";
Assert.Equal(expected, actual);
}

[Fact]
public void RemoveEmptyVariableTypeDeclarations()
{
string unformattedCode =
"FUNCTION_BLOCK Something\n"
+ "VAR_INPUT\n"
+ "END_VAR\n"
+ "VAR_OUTPUT\n"
+ "END_VAR\n"
+ "VAR\n"
+ " isThatTrue : BOOL;\n"
+ "END_VAR";
uint indents = 0;
string actual =
new CompositeCode(
unformattedCode: unformattedCode,
singleIndent: singleIndent,
lineEnding: lineEnding
)
.Tokenize()
.Format(ref indents);

string expected =
"FUNCTION_BLOCK Something\n"
+ "VAR\n"
+ " isThatTrue : BOOL;\n"
+ "END_VAR\n";
Assert.Equal(expected, actual);
}
}
}
36 changes: 36 additions & 0 deletions src/TcBlackTests/TcPouTestData/FB_ExpectedWithEmptyVars.TcPOU
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="utf-8"?>
<TcPlcObject Version="1.1.0.1" ProductVersion="3.1.4024.3">
<POU Name="FB_WithEmptyVars" Id="{106fe56b-b616-4a50-9a9f-10b6fa9ebaf9}" SpecialFunc="None">
<Declaration><![CDATA[FUNCTION_BLOCK FB_WithEmptyVars
VAR_INPUT
OneInput : REAL;
END_VAR
]]></Declaration>
<Implementation>
<ST><![CDATA[]]></ST>
</Implementation>
<Method Name="DoesSomething" Id="{9b942f00-b876-4ffe-b4c8-ac0a0f43615a}">
<Declaration><![CDATA[METHOD DoesSomething : BOOL
]]></Declaration>
<Implementation>
<ST><![CDATA[]]></ST>
</Implementation>
</Method>
<Property Name="SomeProperty" Id="{ff352683-7d51-441b-a700-2f80c7229c20}">
<Declaration><![CDATA[PROPERTY SomeProperty : REAL
]]></Declaration>
<Get Name="Get" Id="{0498394d-8554-4775-8a47-1f6f5f4e3612}">
<Declaration><![CDATA[]]></Declaration>
<Implementation>
<ST><![CDATA[]]></ST>
</Implementation>
</Get>
<Set Name="Set" Id="{ab0d6451-72bc-4e52-bc8c-978a54b39b98}">
<Declaration><![CDATA[]]></Declaration>
<Implementation>
<ST><![CDATA[]]></ST>
</Implementation>
</Set>
</Property>
</POU>
</TcPlcObject>
45 changes: 45 additions & 0 deletions src/TcBlackTests/TcPouTestData/FB_InputWithEmptyVars.TcPOU
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?xml version="1.0" encoding="utf-8"?>
<TcPlcObject Version="1.1.0.1" ProductVersion="3.1.4024.3">
<POU Name="FB_WithEmptyVars" Id="{106fe56b-b616-4a50-9a9f-10b6fa9ebaf9}" SpecialFunc="None">
<Declaration><![CDATA[FUNCTION_BLOCK FB_WithEmptyVars
VAR_INPUT
OneInput : REAL;
END_VAR
VAR_OUTPUT
END_VAR
VAR
END_VAR
]]></Declaration>
<Implementation>
<ST><![CDATA[]]></ST>
</Implementation>
<Method Name="DoesSomething" Id="{9b942f00-b876-4ffe-b4c8-ac0a0f43615a}">
<Declaration><![CDATA[METHOD DoesSomething : BOOL
VAR_INPUT
END_VAR
]]></Declaration>
<Implementation>
<ST><![CDATA[]]></ST>
</Implementation>
</Method>
<Property Name="SomeProperty" Id="{ff352683-7d51-441b-a700-2f80c7229c20}">
<Declaration><![CDATA[PROPERTY SomeProperty : REAL]]></Declaration>
<Get Name="Get" Id="{0498394d-8554-4775-8a47-1f6f5f4e3612}">
<Declaration><![CDATA[VAR
END_VAR
]]></Declaration>
<Implementation>
<ST><![CDATA[]]></ST>
</Implementation>
</Get>
<Set Name="Set" Id="{ab0d6451-72bc-4e52-bc8c-978a54b39b98}">
<Declaration><![CDATA[VAR
END_VAR
]]></Declaration>
<Implementation>
<ST><![CDATA[]]></ST>
</Implementation>
</Set>
</Property>
</POU>
</TcPlcObject>
1 change: 1 addition & 0 deletions src/TcBlackTests/TcPouTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class TcPouTests
"FB_InputWithPropertiesAndMethods.TcPOU",
"FB_ExpectedWithPropertiesAndMethods.TcPOU"
)]
[InlineData("FB_InputWithEmptyVars.TcPOU", "FB_ExpectedWithEmptyVars.TcPOU")]
public void LoadChangeAndSaveDeclaration(string fbInput, string fbExpected)
{
string workingDirectory = Environment.CurrentDirectory;
Expand Down
33 changes: 24 additions & 9 deletions src/WorkingProjectForUnitTests/PLC/PLC.plcproj
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@
<ProjectExtensions>
<PlcProjectOptions>
<XmlArchive>
<Data>
<o xml:space="preserve" t="OptionKey">
<Data>
<o xml:space="preserve" t="OptionKey">
<v n="Name">"&lt;ProjectRoot&gt;"</v>
<d n="SubKeys" t="Hashtable" ckt="String" cvt="OptionKey">
<v>{192FAD59-8248-4824-A8DE-9177C94C195A}</v>
Expand All @@ -68,6 +68,21 @@
<d n="SubKeys" t="Hashtable" />
<d n="Values" t="Hashtable" />
</o>
<v>{F66C7017-BDD8-4114-926C-81D6D687E35F}</v>
<o>
<v n="Name">"{F66C7017-BDD8-4114-926C-81D6D687E35F}"</v>
<d n="SubKeys" t="Hashtable" />
<d n="Values" t="Hashtable" />
</o>
<v>{246001F4-279D-43AC-B241-948EB31120E1}</v>
<o>
<v n="Name">"{246001F4-279D-43AC-B241-948EB31120E1}"</v>
<d n="SubKeys" t="Hashtable" />
<d n="Values" t="Hashtable" ckt="String" cvt="String">
<v>GlobalVisuImageFilePath</v>
<v>%APPLICATIONPATH%</v>
</d>
</o>
<v>{29BD8D0C-3586-4548-BB48-497B9A01693F}</v>
<o>
<v n="Name">"{29BD8D0C-3586-4548-BB48-497B9A01693F}"</v>
Expand All @@ -93,13 +108,13 @@
</d>
<d n="Values" t="Hashtable" />
</o>
</Data>
<TypeList>
<Type n="Hashtable">System.Collections.Hashtable</Type>
<Type n="OptionKey">{54dd0eac-a6d8-46f2-8c27-2f43c7e49861}</Type>
<Type n="String">System.String</Type>
</TypeList>
</XmlArchive>
</Data>
<TypeList>
<Type n="Hashtable">System.Collections.Hashtable</Type>
<Type n="OptionKey">{54dd0eac-a6d8-46f2-8c27-2f43c7e49861}</Type>
<Type n="String">System.String</Type>
</TypeList>
</XmlArchive>
</PlcProjectOptions>
</ProjectExtensions>
</Project>

0 comments on commit c361b63

Please sign in to comment.