|
| 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 | +} |
0 commit comments