Skip to content

Commit 8c4a9b1

Browse files
committed
Added ability to directly write indents, also added indent tests, daveaglick#15
1 parent 94594fe commit 8c4a9b1

7 files changed

Lines changed: 395 additions & 61 deletions

File tree

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Scripty.Core.Tests
8+
{
9+
public class BaseFixture
10+
{
11+
}
12+
}

src/Scripty.Core.Tests/Output/OutputFileCollectionFixture.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace Scripty.Core.Tests.Output
1010
{
1111
[TestFixture]
1212
[Parallelizable(ParallelScope.Self | ParallelScope.Children)]
13-
public class OutputFileCollectionFixture
13+
public class OutputFileCollectionFixture : BaseFixture
1414
{
1515
public class SetExtensionTests : OutputFileCollectionFixture
1616
{
Lines changed: 281 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,281 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using NUnit.Framework;
8+
using Scripty.Core.Output;
9+
10+
namespace Scripty.Core.Tests.Output
11+
{
12+
[TestFixture]
13+
[Parallelizable(ParallelScope.Self | ParallelScope.Children)]
14+
public class OutputFileWriterFixture : BaseFixture
15+
{
16+
public class IndentTests : OutputFileWriterFixture
17+
{
18+
[Test]
19+
public void IncrementsIndentLevel()
20+
{
21+
// Given
22+
StringBuilder builder = new StringBuilder();
23+
OutputFile output = new OutputFileWriter(new StringWriter(builder));
24+
25+
// When
26+
output.IndentLevel = 3;
27+
output.Indent();
28+
29+
// Then
30+
Assert.AreEqual(4, output.IndentLevel);
31+
}
32+
33+
[Test]
34+
public void ReturnsPreviousIndentLevel()
35+
{
36+
// Given
37+
StringBuilder builder = new StringBuilder();
38+
OutputFile output = new OutputFileWriter(new StringWriter(builder));
39+
40+
// When
41+
output.IndentLevel = 3;
42+
int previousIndentLevel = output.Indent();
43+
44+
// Then
45+
Assert.AreEqual(3, previousIndentLevel);
46+
}
47+
48+
[Test]
49+
public void SetsToSpecifiedIndentLevel()
50+
{
51+
// Given
52+
StringBuilder builder = new StringBuilder();
53+
OutputFile output = new OutputFileWriter(new StringWriter(builder));
54+
55+
// When
56+
output.IndentLevel = 3;
57+
output.Indent(2);
58+
59+
// Then
60+
Assert.AreEqual(2, output.IndentLevel);
61+
}
62+
63+
[Test]
64+
public void ReturnsPreviousIndentLevelWhenSettingToSpecifiedIndentLevel()
65+
{
66+
// Given
67+
StringBuilder builder = new StringBuilder();
68+
OutputFile output = new OutputFileWriter(new StringWriter(builder));
69+
70+
// When
71+
output.IndentLevel = 3;
72+
int previousIndentLevel = output.Indent(2);
73+
74+
// Then
75+
Assert.AreEqual(3, previousIndentLevel);
76+
}
77+
}
78+
79+
public class IndentLevelTests : OutputFileWriterFixture
80+
{
81+
[Test]
82+
public void SetsToSpecifiedIndentLevel()
83+
{
84+
// Given
85+
StringBuilder builder = new StringBuilder();
86+
OutputFile output = new OutputFileWriter(new StringWriter(builder));
87+
88+
// When
89+
output.IndentLevel = 3;
90+
91+
// Then
92+
Assert.AreEqual(3, output.IndentLevel);
93+
}
94+
}
95+
96+
public class WithIndentTests : OutputFileWriterFixture
97+
{
98+
[Test]
99+
public void IndentsUntilDisposal()
100+
{
101+
// Given
102+
StringBuilder builder = new StringBuilder();
103+
OutputFile output = new OutputFileWriter(new StringWriter(builder));
104+
string expected = @"A
105+
B
106+
C
107+
";
108+
109+
// When
110+
output.WriteLine("A");
111+
using (output.WithIndent())
112+
{
113+
output.WriteLine("B");
114+
}
115+
output.WriteLine("C");
116+
117+
// Then
118+
Assert.AreEqual(expected, builder.ToString());
119+
}
120+
121+
[Test]
122+
public void RestoresPreviousIndentLevelOnDisposal()
123+
{
124+
// Given
125+
StringBuilder builder = new StringBuilder();
126+
OutputFile output = new OutputFileWriter(new StringWriter(builder));
127+
string expected = @"A
128+
B
129+
C
130+
D
131+
";
132+
133+
// When
134+
output.WriteLine("A");
135+
using (output.WithIndent())
136+
{
137+
using (output.WithIndent())
138+
{
139+
output.WriteLine("B");
140+
}
141+
output.WriteLine("C");
142+
}
143+
output.WriteLine("D");
144+
145+
// Then
146+
Assert.AreEqual(expected, builder.ToString());
147+
}
148+
149+
[Test]
150+
public void IndentsSpecifiedAmount()
151+
{
152+
// Given
153+
StringBuilder builder = new StringBuilder();
154+
OutputFile output = new OutputFileWriter(new StringWriter(builder));
155+
string expected = @"A
156+
B
157+
C
158+
";
159+
160+
// When
161+
output.WriteLine("A");
162+
using (output.WithIndent(2))
163+
{
164+
output.WriteLine("B");
165+
}
166+
output.WriteLine("C");
167+
168+
// Then
169+
Assert.AreEqual(expected, builder.ToString());
170+
}
171+
172+
[Test]
173+
public void RestoresPreviousIndentLevelOnDisposalWithSpecifiedAmount()
174+
{
175+
// Given
176+
StringBuilder builder = new StringBuilder();
177+
OutputFile output = new OutputFileWriter(new StringWriter(builder));
178+
string expected = @"A
179+
B
180+
C
181+
D
182+
E
183+
";
184+
185+
// When
186+
output.WriteLine("A");
187+
using (output.WithIndent())
188+
{
189+
output.WriteLine("B");
190+
using (output.WithIndent(0))
191+
{
192+
output.WriteLine("C");
193+
}
194+
output.WriteLine("D");
195+
}
196+
output.WriteLine("E");
197+
198+
// Then
199+
Assert.AreEqual(expected, builder.ToString());
200+
}
201+
}
202+
203+
public class WriteIndentTests
204+
{
205+
[Test]
206+
public void WritesTheCurrentIndentString()
207+
{
208+
// Given
209+
StringBuilder builder = new StringBuilder();
210+
OutputFile output = new OutputFileWriter(new StringWriter(builder));
211+
string expected = @"..";
212+
213+
// When
214+
output.IndentString = "..";
215+
output.IndentLevel = 1;
216+
output.WriteIndent();
217+
218+
// Then
219+
Assert.AreEqual(expected, builder.ToString());
220+
}
221+
222+
[Test]
223+
public void DoesNotWriteIfNoIndentLevel()
224+
{
225+
// Given
226+
StringBuilder builder = new StringBuilder();
227+
OutputFile output = new OutputFileWriter(new StringWriter(builder));
228+
229+
// When
230+
output.IndentString = "..";
231+
output.IndentLevel = 0;
232+
output.WriteIndent();
233+
234+
// Then
235+
Assert.AreEqual(string.Empty, builder.ToString());
236+
}
237+
238+
[Test]
239+
public void WritesTheCurrentIndentStringMultipleTimes()
240+
{
241+
// Given
242+
StringBuilder builder = new StringBuilder();
243+
OutputFile output = new OutputFileWriter(new StringWriter(builder));
244+
string expected = @"....";
245+
246+
// When
247+
output.IndentString = "..";
248+
output.IndentLevel = 2;
249+
output.WriteIndent();
250+
251+
// Then
252+
Assert.AreEqual(expected, builder.ToString());
253+
}
254+
255+
[Test]
256+
public void DoesNotResetIndentWriteFlag()
257+
{
258+
// Given
259+
StringBuilder builder = new StringBuilder();
260+
OutputFile output = new OutputFileWriter(new StringWriter(builder));
261+
string expected = @"A
262+
....B
263+
C
264+
";
265+
266+
// When
267+
output.IndentString = "..";
268+
output.WriteLine("A");
269+
using (output.WithIndent())
270+
{
271+
output.WriteIndent();
272+
output.WriteLine("B");
273+
}
274+
output.WriteLine("C");
275+
276+
// Then
277+
Assert.AreEqual(expected, builder.ToString());
278+
}
279+
}
280+
}
281+
}

src/Scripty.Core.Tests/Scripty.Core.Tests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@
4747
<Compile Include="..\SolutionInfo.cs">
4848
<Link>Properties\SolutionInfo.cs</Link>
4949
</Compile>
50+
<Compile Include="BaseFixture.cs" />
5051
<Compile Include="Output\OutputFileCollectionFixture.cs" />
52+
<Compile Include="Output\OutputFileWriterFixture.cs" />
5153
<Compile Include="Properties\AssemblyInfo.cs" />
5254
</ItemGroup>
5355
<ItemGroup>

src/Scripty.Core/Output/OutputFileBase.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.IO;
3+
using System.Threading.Tasks;
34

45
namespace Scripty.Core.Output
56
{
@@ -55,6 +56,16 @@ internal OutputFileBase()
5556
/// <returns>A disposable object that will reset the indent to the previous value once disposed.</returns>
5657
public abstract IDisposable WithIndent(int indentLevel);
5758

59+
/// <summary>
60+
/// Writes the indent string a number of times equal to the indent level .
61+
/// </summary>
62+
public abstract void WriteIndent();
63+
64+
/// <summary>
65+
/// Writes the indent string a number of times equal to the indent level .
66+
/// </summary>
67+
public abstract Task WriteIndentAsync();
68+
5869
public sealed override void Close() => InternalClose();
5970

6071
public sealed override void Flush() => InternalFlush();

src/Scripty.Core/Output/OutputFileCollection.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,10 @@ public override string IndentString
141141

142142
public override IDisposable WithIndent(int indentLevel) => DefaultOutput.WithIndent(indentLevel);
143143

144+
public override void WriteIndent() => DefaultOutput.WriteIndent();
145+
146+
public override Task WriteIndentAsync() => DefaultOutput.WriteIndentAsync();
147+
144148
public override OutputFile Close() => DefaultOutput.Close();
145149

146150
public override OutputFile Flush() => DefaultOutput.Flush();

0 commit comments

Comments
 (0)