From 5d1e8073ea169011901452435cd2bd7f7244abdb Mon Sep 17 00:00:00 2001 From: Dylan Lerch Date: Tue, 2 Aug 2022 14:24:08 +1000 Subject: [PATCH] Write fewer newlines in OCL - Do not write a newline before a block if it is the first child in a block - Write empty blocks as a single line --- source/Ocl/OclWriter.cs | 24 +++++++++++++------ ...LifeScenarioFixture.IisAction.approved.txt | 1 - ...alLifeScenarioFixture.Rolling.approved.txt | 1 - ...eScenarioFixture.ScriptAction.approved.txt | 1 - source/Tests/ToString/OclWriterFixture.cs | 19 ++++++--------- 5 files changed, 24 insertions(+), 22 deletions(-) diff --git a/source/Ocl/OclWriter.cs b/source/Ocl/OclWriter.cs index 71bff83..8acdc36 100644 --- a/source/Ocl/OclWriter.cs +++ b/source/Ocl/OclWriter.cs @@ -15,6 +15,7 @@ public class OclWriter : IDisposable readonly TextWriter writer; int currentIndent; bool isFirstLine = true; + bool isFirstChildInBlock; bool lastWrittenWasBlock; public OclWriter(StringBuilder sb, OclSerializerOptions? options = null) @@ -60,7 +61,7 @@ public void Write(OclAttribute attribute) public void Write(OclBlock block) { - if (!isFirstLine && !lastWrittenWasBlock) + if (!isFirstLine && !lastWrittenWasBlock && !isFirstChildInBlock) writer.WriteLine(); WriteNextLine(); WriteIndent(); @@ -74,14 +75,23 @@ public void Write(OclBlock block) writer.Write(" {"); - currentIndent++; - foreach (var child in block) - Write(child); - currentIndent--; + if (block.Any()) + { + currentIndent++; + isFirstChildInBlock = true; - writer.WriteLine(); + foreach (var child in block) + { + Write(child); + isFirstChildInBlock = false; + } + + currentIndent--; + + writer.WriteLine(); + WriteIndent(); + } - WriteIndent(); writer.Write("}"); lastWrittenWasBlock = true; diff --git a/source/Tests/RealLifeScenario/RealLifeScenarioFixture.IisAction.approved.txt b/source/Tests/RealLifeScenario/RealLifeScenarioFixture.IisAction.approved.txt index ff57914..44c7027 100644 --- a/source/Tests/RealLifeScenario/RealLifeScenarioFixture.IisAction.approved.txt +++ b/source/Tests/RealLifeScenario/RealLifeScenarioFixture.IisAction.approved.txt @@ -8,7 +8,6 @@ connectivity_policy { project_id = "Projects-1" steps "Deploy Website" { - actions "Deploy Website" { action_type = "Octopus.IIS" properties = { diff --git a/source/Tests/RealLifeScenario/RealLifeScenarioFixture.Rolling.approved.txt b/source/Tests/RealLifeScenario/RealLifeScenarioFixture.Rolling.approved.txt index de7da99..caefa18 100644 --- a/source/Tests/RealLifeScenario/RealLifeScenarioFixture.Rolling.approved.txt +++ b/source/Tests/RealLifeScenario/RealLifeScenarioFixture.Rolling.approved.txt @@ -8,7 +8,6 @@ connectivity_policy { project_id = "Projects-1" steps "My Rolling Step" { - actions "First" { action_type = "Octopus.Script" } diff --git a/source/Tests/RealLifeScenario/RealLifeScenarioFixture.ScriptAction.approved.txt b/source/Tests/RealLifeScenario/RealLifeScenarioFixture.ScriptAction.approved.txt index eeadaf6..fac14f4 100644 --- a/source/Tests/RealLifeScenario/RealLifeScenarioFixture.ScriptAction.approved.txt +++ b/source/Tests/RealLifeScenario/RealLifeScenarioFixture.ScriptAction.approved.txt @@ -8,7 +8,6 @@ connectivity_policy { project_id = "Projects-1" steps "Backup the Database" { - actions "Backup the Database" { action_type = "Octopus.Script" properties = { diff --git a/source/Tests/ToString/OclWriterFixture.cs b/source/Tests/ToString/OclWriterFixture.cs index 828890f..3a565c8 100644 --- a/source/Tests/ToString/OclWriterFixture.cs +++ b/source/Tests/ToString/OclWriterFixture.cs @@ -156,13 +156,13 @@ public void MultilineStringsUseHeredocAndTheHeredocIdentifierFromOptions() public void WriteBlockEmpty() => Execute(w => w.Write(new OclBlock("MyBlock"))) .Should() - .Be("MyBlock {\n}"); + .Be("MyBlock {}"); [Test] public void WriteBlockSpecialCharactersInName() => Execute(w => w.Write(new OclBlock("My0%&2_'\"-Block"))) .Should() - .Be("My0__2___-Block {\n}"); + .Be("My0__2___-Block {}"); [Test] public void WriteBlockSingleLabel() @@ -172,7 +172,7 @@ public void WriteBlockSingleLabel() Execute(w => w.Write(block)) .Should() - .Be("MyBlock \"MyLabel\" {\n}"); + .Be("MyBlock \"MyLabel\" {}"); } [Test] @@ -185,7 +185,7 @@ public void WriteBlockMultipleLabel() Execute(w => w.Write(block)) .Should() - .Be("MyBlock \"MyLabel\" \"OtherLabel\" \"LastLabel\" {\n}"); + .Be("MyBlock \"MyLabel\" \"OtherLabel\" \"LastLabel\" {}"); } [Test] @@ -196,7 +196,7 @@ public void WriteBlockDoubleQuotesInLabel() Execute(w => w.Write(block)) .Should() - .Be("MyBlock \"My\\\"Label\" {\n}"); + .Be("MyBlock \"My\\\"Label\" {}"); } [Test] @@ -208,9 +208,7 @@ public void WriteBlockSingleChildBlock() }; var expected = @"MyBlock { - - Child { - } + Child {} }"; Execute(w => w.Write(block)) @@ -281,8 +279,7 @@ public void WriteBlockMixedAttributesAndBlocks() ThirdChild = 3 } - Fourth { - } + Fourth {} Last = 9 }"; @@ -306,7 +303,6 @@ public void CollectionAttributeFollowedByEndOfBlock() }; const string expected = @"OuterBlock { - InnerBlock { MapAttribute = { alpha = 1 @@ -335,7 +331,6 @@ public void CollectionAttributeFollowedByAttribute() }; const string expected = @"OuterBlock { - InnerBlock { MapAttribute = { alpha = 1