Skip to content

Commit

Permalink
Merge pull request #43 from dansiegel/codewriter-fix
Browse files Browse the repository at this point in the history
Fix CodeWriter Dispose
  • Loading branch information
dansiegel authored Oct 9, 2022
2 parents 37bc5ab + c275d5f commit 6ed1f70
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/CodeGenHelpers/Internals/CodeWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ private string GetIndentedValue(string value)

public void Dispose()
{
while (_indentLevel > 0)
if (_indentLevel > 0)
{
_indentLevel--;
EnsureStringBuilder().AppendLine(GetIndentedValue("}"));
Expand Down
22 changes: 22 additions & 0 deletions tests/CodeGenHelpers.Tests/SampleCode/MultipleMethodClass.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace CodeGenHelpers.SampleCode
{
partial class MultipleMethodClass
{
public void Bar()
{
}

public void Foo()
{
}
}
}
16 changes: 16 additions & 0 deletions tests/CodeGenHelpers.Tests/Tests/CodeWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,20 @@ public void GenerateCodeSubBlock()
var r = sut.Render();
Assert.Equal(expected, r);
}

[Fact]
public void GenerateIfElse()
{
var writer = new CodeWriter(IndentStyle.Tabs);

writer.If("true")
.WithBody(w => w.AppendLine("CrashTheSystem();"))
.Else()
.WithBody(w => w.AppendLine("DontCrashTheSystem();"))
.EndIf();

const string expected = "if (true)\r\n{\r\n\tCrashTheSystem();\r\n}\r\nelse\r\n{\r\n\tDontCrashTheSystem();\r\n}\r\n";
var r = writer.Render();
Assert.Equal(expected, r);
}
}
15 changes: 15 additions & 0 deletions tests/CodeGenHelpers.Tests/Tests/MethodTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,20 @@ public void GenerateMethodWithPrimitiveParameter()

MakeAssertion(builder);
}

[Fact]
public void GenerateMultipleMethodsInClass()
{
var builder = CodeBuilder.Create(Namespace)
.AddClass("MultipleMethodClass")
.AddMethod("Foo")
.MakePublicMethod()
.Class
.AddMethod("Bar")
.MakePublicMethod()
.Class;

MakeAssertion(builder);
}
}
}

0 comments on commit 6ed1f70

Please # to comment.