diff --git a/src/CodeGenHelpers/Internals/CodeWriter.cs b/src/CodeGenHelpers/Internals/CodeWriter.cs index e2d97a1..d62ae46 100644 --- a/src/CodeGenHelpers/Internals/CodeWriter.cs +++ b/src/CodeGenHelpers/Internals/CodeWriter.cs @@ -111,7 +111,7 @@ private string GetIndentedValue(string value) public void Dispose() { - while (_indentLevel > 0) + if (_indentLevel > 0) { _indentLevel--; EnsureStringBuilder().AppendLine(GetIndentedValue("}")); diff --git a/tests/CodeGenHelpers.Tests/SampleCode/MultipleMethodClass.cs b/tests/CodeGenHelpers.Tests/SampleCode/MultipleMethodClass.cs new file mode 100644 index 0000000..2cf622d --- /dev/null +++ b/tests/CodeGenHelpers.Tests/SampleCode/MultipleMethodClass.cs @@ -0,0 +1,22 @@ +//------------------------------------------------------------------------------ +// +// This code was generated. +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace CodeGenHelpers.SampleCode +{ + partial class MultipleMethodClass + { + public void Bar() + { + } + + public void Foo() + { + } + } +} diff --git a/tests/CodeGenHelpers.Tests/Tests/CodeWriterTests.cs b/tests/CodeGenHelpers.Tests/Tests/CodeWriterTests.cs index ac40125..41fd5f9 100644 --- a/tests/CodeGenHelpers.Tests/Tests/CodeWriterTests.cs +++ b/tests/CodeGenHelpers.Tests/Tests/CodeWriterTests.cs @@ -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); + } } diff --git a/tests/CodeGenHelpers.Tests/Tests/MethodTests.cs b/tests/CodeGenHelpers.Tests/Tests/MethodTests.cs index bc73247..2b81c88 100644 --- a/tests/CodeGenHelpers.Tests/Tests/MethodTests.cs +++ b/tests/CodeGenHelpers.Tests/Tests/MethodTests.cs @@ -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); + } } }