Skip to content

Commit 6ed1f70

Browse files
authored
Merge pull request #43 from dansiegel/codewriter-fix
Fix CodeWriter Dispose
2 parents 37bc5ab + c275d5f commit 6ed1f70

File tree

4 files changed

+54
-1
lines changed

4 files changed

+54
-1
lines changed

src/CodeGenHelpers/Internals/CodeWriter.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ private string GetIndentedValue(string value)
111111

112112
public void Dispose()
113113
{
114-
while (_indentLevel > 0)
114+
if (_indentLevel > 0)
115115
{
116116
_indentLevel--;
117117
EnsureStringBuilder().AppendLine(GetIndentedValue("}"));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated.
4+
//
5+
// Changes to this file may cause incorrect behavior and will be lost if
6+
// the code is regenerated.
7+
// </auto-generated>
8+
//------------------------------------------------------------------------------
9+
10+
namespace CodeGenHelpers.SampleCode
11+
{
12+
partial class MultipleMethodClass
13+
{
14+
public void Bar()
15+
{
16+
}
17+
18+
public void Foo()
19+
{
20+
}
21+
}
22+
}

tests/CodeGenHelpers.Tests/Tests/CodeWriterTests.cs

+16
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,20 @@ public void GenerateCodeSubBlock()
2929
var r = sut.Render();
3030
Assert.Equal(expected, r);
3131
}
32+
33+
[Fact]
34+
public void GenerateIfElse()
35+
{
36+
var writer = new CodeWriter(IndentStyle.Tabs);
37+
38+
writer.If("true")
39+
.WithBody(w => w.AppendLine("CrashTheSystem();"))
40+
.Else()
41+
.WithBody(w => w.AppendLine("DontCrashTheSystem();"))
42+
.EndIf();
43+
44+
const string expected = "if (true)\r\n{\r\n\tCrashTheSystem();\r\n}\r\nelse\r\n{\r\n\tDontCrashTheSystem();\r\n}\r\n";
45+
var r = writer.Render();
46+
Assert.Equal(expected, r);
47+
}
3248
}

tests/CodeGenHelpers.Tests/Tests/MethodTests.cs

+15
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,20 @@ public void GenerateMethodWithPrimitiveParameter()
3535

3636
MakeAssertion(builder);
3737
}
38+
39+
[Fact]
40+
public void GenerateMultipleMethodsInClass()
41+
{
42+
var builder = CodeBuilder.Create(Namespace)
43+
.AddClass("MultipleMethodClass")
44+
.AddMethod("Foo")
45+
.MakePublicMethod()
46+
.Class
47+
.AddMethod("Bar")
48+
.MakePublicMethod()
49+
.Class;
50+
51+
MakeAssertion(builder);
52+
}
3853
}
3954
}

0 commit comments

Comments
 (0)