public void TestIndentingWriter() { char[] line1 = new char[] { 'l', 'i', 'n', 'e', '1' }; char nl = '\n'; char[] line2 = new char[] { 'z', 'l', 'i', 'n', 'e', '2' }; string line3 = "\nline3"; StringWriter strWriter = new StringWriter(); IndentingWriter indWriter = new IndentingWriter(strWriter, 4); Assert.AreEqual(indWriter.Encoding, strWriter.Encoding); indWriter.Write(line1); indWriter.Write(nl); indWriter.Write(line2, 1, 5); indWriter.Write(line3); Assert.AreEqual(" line1\n line2\n line3", strWriter.ToString()); strWriter = new StringWriter(); indWriter = new IndentingWriter(strWriter, 4); indWriter.Suspend(); indWriter.Write(line1); indWriter.Write(nl); indWriter.Write(line2, 1, 5); indWriter.Write(line3); Assert.AreEqual("line1\nline2\nline3", strWriter.ToString()); indWriter.Resume(); indWriter.WriteLine(); Assert.AreEqual("line1\nline2\nline3\r\n", strWriter.ToString()); indWriter.Write(line1); Assert.AreEqual("line1\nline2\nline3\r\n line1", strWriter.ToString()); strWriter = new StringWriter(); indWriter = new IndentingWriter(strWriter, "ZZZ"); indWriter.Write(line1); indWriter.Write(nl); indWriter.Write(line2, 1, 5); indWriter.Write(line3); Assert.AreEqual("ZZZline1\nZZZline2\nZZZline3", strWriter.ToString()); strWriter = new StringWriter(); indWriter = new IndentingWriter(strWriter, "ZZZ"); indWriter.Suspend(); indWriter.Write(line1); indWriter.Write(nl); indWriter.Write(line2, 1, 5); indWriter.Write(line3); Assert.AreEqual("line1\nline2\nline3", strWriter.ToString()); strWriter = new StringWriter(); indWriter = new IndentingWriter(strWriter, "ZZZ"); IndentingWriter indWriter2 = new IndentingWriter(indWriter, "XXX"); indWriter2.Write(line1); indWriter2.Write(nl); indWriter2.Write(line2, 1, 5); indWriter2.Write(line3); Assert.AreEqual("ZZZXXXline1\nZZZXXXline2\nZZZXXXline3", strWriter.ToString()); }
/// <summary> /// Construct an IndentingWriter that indents using an indention /// string. /// </summary> /// <param name="writer"> /// The underlying <b>TextWriter</b> to write to. /// </param> /// <param name="indent"> /// The string value to indent each line with. /// </param> public IndentingWriter(TextWriter writer, string indent) { m_writer = writer; if (indent.IndexOf('\n') >= 0) { throw new ArgumentException("indent contains new line char"); } // if this and underlying writers are both indenting writers, combine // the indentation strings Type type = typeof(IndentingWriter); if (GetType() == type && writer.GetType() == type) { IndentingWriter that = (IndentingWriter)writer; indent = new StringBuilder().Append(that.m_achIndent).Append(indent).ToString(); m_writer = that.m_writer; } m_achIndent = indent.ToCharArray(); }