public void BasicTest() { using var sw = new StringWriter(); using var iw = new IndentedWriter(sw) { IndentString = "#", NewLine = "\n" }; Assert.Equal("\n", iw.NewLine); Assert.Equal(sw.Encoding, iw.Encoding); iw.WriteLine("line1"); iw.Write("line"); iw.IncreaseIndent(); iw.WriteLine("2"); iw.Write("line"); iw.Write('3'); iw.WriteLine(); iw.IncreaseIndent(2); iw.WriteLine("line4"); iw.DecreaseIndent(); iw.WriteLine("line5"); iw.DecreaseIndent(6); iw.WriteLine("line6"); Assert.Equal("line1\nline2\n#line3\n###line4\n##line5\nline6\n", sw.ToString()); }
private static void WriteConstructors(IndentedWriter sb, Block block) { sb.Line($"public {block.CodeName}() {{ }}"); sb.Line($"public {block.CodeName}("); sb.IncreaseIndent(); foreach (var indexed in block.OrderedProperties().Select((param, index) => new { param, index })) { var property = indexed.param; var argument = $"{property.ConstructorArgumentType} {property.ConstructorArgumentName}" + (property.IsRequired ? string.Empty : $" = {property.DefaultValue}"); sb.Line(argument + (indexed.index == block.Properties.Count() - 1 ? ")" : ",")); } sb.DecreaseIndent(); sb.OpenParen(); foreach (var property in block.OrderedProperties()) { switch (property) { case Field field: sb.Line($"{field.PropertyName} = {field.ConstructorArgumentName};"); break; case BlockList requiredBlockList when requiredBlockList.IsRequired: sb.Line($"{requiredBlockList.PropertyName}.AddRange({requiredBlockList.ConstructorArgumentName});"); break; case BlockList blockList: sb.Line($"{blockList.PropertyName}.AddRange({blockList.ConstructorArgumentName} ?? Enumerable.Empty<{blockList.SingularName}>());"); break; } } sb.Line(@"AdditionalSemanticChecks();"); sb.CloseParen(); }
private static void WriteCloneMethod(IndentedWriter output, Block block) { output. Line(). Line($"public {block.CodeName} Clone()"). OpenParen(). Line($"return new {block.CodeName}(").IncreaseIndent(); foreach (var indexed in block.OrderedProperties().Select((param, index) => new { param, index })) { var postfix = indexed.index == block.Properties.Count() - 1 ? ");" : ","; if (indexed.param is BlockList) { postfix = ".Select(item => item.Clone())" + postfix; } output.Line(indexed.param.ConstructorArgumentName + ": " + indexed.param.PropertyName + postfix); } output. DecreaseIndent(). CloseParen(); }