Inheritance: SerialTextWriter, ILayoutTextWriter
Ejemplo n.º 1
0
        public static FormatTextWriter Format(
            [NotNull] this TextWriter writer,
            Optional <int> width,
            Optional <int> indentSize              = default(Optional <int>),
            Optional <int> rightMarginSize         = default(Optional <int>),
            Optional <char> indentChar             = default(Optional <char>),
            Optional <int> firstLineIndentSize     = default(Optional <int>),
            Optional <IEnumerable <int> > tabStops = default(Optional <IEnumerable <int> >),
            Optional <byte> tabSize            = default(Optional <byte>),
            Optional <char> tabChar            = default(Optional <char>),
            Optional <Alignment> alignment     = default(Optional <Alignment>),
            Optional <byte> splitLength        = default(Optional <byte>),
            Optional <bool> hyphenate          = default(Optional <bool>),
            Optional <char> hyphenChar         = default(Optional <char>),
            Optional <LayoutWrapMode> wrapMode = default(Optional <LayoutWrapMode>),
            ushort startPosition = 0)
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }

            FormatTextWriter ltw = writer as FormatTextWriter;

            if (ltw == null)
            {
                return(new FormatTextWriter(
                           writer,
                           width,
                           indentSize,
                           rightMarginSize,
                           indentChar,
                           firstLineIndentSize,
                           tabStops,
                           tabSize,
                           tabChar,
                           alignment,
                           splitLength,
                           hyphenate,
                           hyphenChar,
                           wrapMode,
                           startPosition));
            }

            ltw.ApplyLayout(
                width,
                indentSize,
                rightMarginSize,
                indentChar,
                firstLineIndentSize,
                tabStops,
                tabSize,
                tabChar,
                alignment,
                splitLength,
                hyphenate,
                hyphenChar,
                wrapMode);
            return(ltw);
        }
Ejemplo n.º 2
0
        public void TestThreadSafetyNestedLayoutLongLine()
        {
            using (StringWriter stringWriter = new StringWriter())
            using (FormatTextWriter formatTextWriter = new FormatTextWriter(stringWriter))
            {
                Stopwatch watch = Stopwatch.StartNew();
                Parallel.For(
                    0,
                    1000,
                    new ParallelOptions { MaxDegreeOfParallelism = 8 },
                    i => new FormatBuilder()
                        .Append(FormatResources.ButIMustExplain)
                        .WriteTo(formatTextWriter));
                watch.Stop();
                Trace.WriteLine(watch.Elapsed.TotalMilliseconds);
                Assert.AreEqual(970000, formatTextWriter.Position);
                string result = stringWriter.ToString();

                string[] lines = result
                    .Split(new[] { Environment.NewLine }, StringSplitOptions.None);

                // Check number of lines and maximum line length, if we have any race conditions we expect these to change.
                Assert.AreEqual(1, lines.Length);
            }
        }
Ejemplo n.º 3
0
        public static FormatTextWriter Format(
            [NotNull] this TextWriter writer,
            [CanBeNull] Layout layout = null,
            ushort startPosition      = 0)
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }

            FormatTextWriter ltw = writer as FormatTextWriter;

            if (ltw == null)
            {
                return(new FormatTextWriter(writer, layout, startPosition));
            }

            ltw.ApplyLayout(layout);
            return(ltw);
        }
        public void TestLayoutWriter()
        {
            const ushort width = 15;
            using (StringWriter stringWriter = new StringWriter())
            using (FormatTextWriter formatTextWriter = new FormatTextWriter(
                    stringWriter,
                    width,
                    alignment: Alignment.Justify))
            {
                formatTextWriter.Write("A short string that requires justification ");
                formatTextWriter.Write("but is written in multiple steps ");
                formatTextWriter.Write("whicher prevents any kind of justification on the resultant join points.");

                string result = stringWriter.ToString();
                Trace.WriteLine(result);

                string[] lines = result
                    .Split(new[] { Environment.NewLine }, StringSplitOptions.None);

                // Check number of lines and maximum line length, if we have any race conditions we expect these to change.
                Assert.AreEqual(12, lines.Length);
                Assert.AreEqual(width, lines.Select(l => l.Length).Max());
            }
        }
        public void TestThreadSafety()
        {
            const ushort width = 80;
            using (StringWriter stringWriter = new StringWriter())
            using (FormatTextWriter formatTextWriter = new FormatTextWriter(
                    stringWriter,
                    width,
                    alignment: Alignment.Justify))
            {
                Stopwatch watch = Stopwatch.StartNew();
                Parallel.For(
                    0,
                    1000,
                    new ParallelOptions {MaxDegreeOfParallelism = 8},
                    i => formatTextWriter.Write(FormatResources.ButIMustExplain));
                watch.Stop();
                Trace.WriteLine(watch.Elapsed.TotalMilliseconds);
                Assert.AreEqual(50, formatTextWriter.Position);
                string result = stringWriter.ToString();

                string[] lines = result
                    .Split(new[] {Environment.NewLine}, StringSplitOptions.None);

                // Check number of lines and maximum line length, if we have any race conditions we expect these to change.
                Assert.AreEqual(12500, lines.Length);
                Assert.AreEqual(width, lines.Select(l => l.Length).Max());
            }
        }
Ejemplo n.º 6
0
 public void TestAutoPositioning()
 {
     using (StringWriter writer = new StringWriter())
     using (FormatTextWriter fw = new FormatTextWriter(writer, 5))
     {
         fw.WriteLine();
         Assert.AreEqual(0, fw.Position);
         fw.WriteLine("12345");
         Assert.AreEqual(0, fw.Position);
         fw.Write("12345\r\n");
         Assert.AreEqual(0, fw.Position);
         fw.Write("1234\r\n");
         Assert.AreEqual(0, fw.Position);
         fw.Write("1234");
         Assert.AreEqual(4, fw.Position);
         fw.Write("123456");
         Assert.AreEqual(1, fw.Position);
         fw.Write("123456");
         Assert.AreEqual(1, fw.Position);
         fw.Write("12345\r\n1");
         Assert.AreEqual(1, fw.Position);
     }
 }