Esempio n. 1
0
        public void TestResolveObjects()
        {
            TestClass tc = new TestClass(123, true, new List <int> {
                1, 1, 2, 3, 5, 8
            });
            FormatBuilder builder = new FormatBuilder();

            builder.AppendFormat(
                "{0:({Number:N2}, {Boolean}) [{List:{<items>:{<item>}}{<join>:,}}]}",
                tc);
            Assert.AreEqual("(123.00, True) [1,1,2,3,5,8]", builder.ToString());

            builder.Clear();
            builder.AppendFormat(
                "{0:Count: {Count} \\{ {<items>:\\{{Key}: {Value}\\}}{<join>:, } \\}}",
                new Dictionary <string, int>
            {
                { "Foo", 123 },
                { "Bar", 456 },
                { "Test", 789 }
            });
            Assert.AreEqual("Count: 3 { {Foo: 123}, {Bar: 456}, {Test: 789} }", builder.ToString());

            builder.Clear();
            builder.AppendFormat(
                "Length: {0:{Length}} \"{0}\"",
                FormatResources.LoremIpsum);
            Assert.AreEqual(string.Format("Length: {0} \"{1}\"", FormatResources.LoremIpsum.Length, FormatResources.LoremIpsum), builder.ToString());
        }
        public void TestEmptyChunksIgnored()
        {
            Dictionary <string, object> dictionary = new Dictionary <string, object> {
                { "tag", Resolution.Empty }
            };

            FormatBuilder builder = new FormatBuilder(100, 4, firstLineIndentSize: 4)
                                    .AppendFormat("Text{tag}text");

            Assert.AreEqual("    Texttext", builder.ToString(dictionary));

            builder.Clear();
            builder.AppendFormat("Text{tag}");

            Assert.AreEqual("    Text", builder.ToString(dictionary));

            builder.Clear();
            builder.AppendFormat("Text{tag: sub {tag}}");

            Assert.AreEqual("    Text sub ", builder.ToString(dictionary));

            builder.Clear();
            builder.AppendFormat("Text{tag:{<items>:{<item>}}}");

            Assert.AreEqual("    Text", builder.ToString(dictionary));
        }
Esempio n. 3
0
 /// <summary>
 /// Writes the specified value.
 /// </summary>
 /// <param name="value">The value.</param>
 public override void Write([CanBeNull] string value)
 {
     if (!_isOpen)
     {
         throw new InvalidOperationException(Resources.FormatWriter_IsClosed);
     }
     _builder.AppendFormat(value);
 }
Esempio n. 4
0
 /// <summary>
 /// Writes the specified value.
 /// </summary>
 /// <param name="value">The value.</param>
 public override void Write([CanBeNull] string value)
 {
     Debug.Assert(Builder.IsEmpty);
     Builder.AppendFormat(value);
     Position = Builder.WriteTo(_writer, null, Position);
     Builder.Clear();
 }
        public void TestAlignment()
        {
            Dictionary <string, object> dictionary = new Dictionary <string, object> {
                { "tag", "1" }
            };
            FormatBuilder builder = new FormatBuilder("{tag,3}");

            Assert.AreEqual("  1", builder.ToString(dictionary));
            builder.Clear();

            builder.AppendFormat("{tag,-3}");
            Assert.AreEqual("1  ", builder.ToString(dictionary));
            builder.Clear();

            builder.AppendFormat("{tag,3:2{tag}}");
            Assert.AreEqual(" 21", builder.ToString(dictionary));
            builder.Clear();

            builder.AppendFormat("{tag,-3:2{tag}}");
            Assert.AreEqual("21 ", builder.ToString(dictionary));
            builder.Clear();
        }
Esempio n. 6
0
        public void TestResolveObjects()
        {
            TestClass tc = new TestClass(123, true, new List<int>{1, 1, 2, 3, 5, 8});
            FormatBuilder builder = new FormatBuilder();
            builder.AppendFormat(
                "{0:({Number:N2}, {Boolean}) [{List:{<items>:{<item>}}{<join>:,}}]}",
                tc);
            Assert.AreEqual("(123.00, True) [1,1,2,3,5,8]", builder.ToString());

            builder.Clear();
            builder.AppendFormat(
                "{0:Count: {Count} \\{ {<items>:\\{{Key}: {Value}\\}}{<join>:, } \\}}",
                new Dictionary<string, int>
                {
                    {"Foo", 123},
                    {"Bar", 456},
                    {"Test", 789}
                });
            Assert.AreEqual("Count: 3 { {Foo: 123}, {Bar: 456}, {Test: 789} }", builder.ToString());

            builder.Clear();
            builder.AppendFormat(
                "Length: {0:{Length}} \"{0}\"",
                FormatResources.LoremIpsum);
            Assert.AreEqual(string.Format("Length: {0} \"{1}\"", FormatResources.LoremIpsum.Length, FormatResources.LoremIpsum), builder.ToString());
        }
Esempio n. 7
0
        private static FormatBuilder ValidatePathFormat(
            [NotNull] string directory,
            [NotNull] FormatBuilder fileNameFormat,
            [NotNull] ref string extension,
            [NotNull] FormatBuilder format)
        {
            if (directory == null) throw new ArgumentNullException("directory");
            if (fileNameFormat == null) throw new ArgumentNullException("fileNameFormat");
            if (extension == null) throw new ArgumentNullException("extension");
            if (format == null) throw new ArgumentNullException("format");

            if (string.IsNullOrWhiteSpace(directory))
                directory = DefaultDirectory;

            try
            {
                directory = Path.GetFullPath(directory.Trim());
                if (!System.IO.Directory.Exists(directory))
                    System.IO.Directory.CreateDirectory(directory);
            }
            catch (Exception e)
            {
                // ReSharper disable once AssignNullToNotNullAttribute
                throw new LoggingException(
                    e,
                    LoggingLevel.Critical,
                    () => Resources.FileLogger_DirectoryAccessOrCreationError,
                    directory);
            }

            if (fileNameFormat.IsEmpty)
                // ReSharper disable once AssignNullToNotNullAttribute
                throw new LoggingException(LoggingLevel.Critical, () => Resources.FileLogger_FileNameFormatNotSpecified);


            if (string.IsNullOrWhiteSpace(extension))
                if (format == Log.XMLFormat)
                    extension = ".xml";
                else if (format == Log.JSONFormat)
                    extension = ".json";
                else
                    extension = ".log";
            else
            {
                extension = extension.Trim();
                if (extension.StartsWith("."))
                    extension = extension.Substring(1);
                if (extension.Any(c => !Char.IsLetterOrDigit(c)))
                    throw new LoggingException(
                        LoggingLevel.Critical,
                        // ReSharper disable once AssignNullToNotNullAttribute
                        () => Resources.FileLogger_Extension_Invalid_Char,
                        extension);
                if (extension.Length > 5)
                    throw new LoggingException(
                        LoggingLevel.Critical,
                        // ReSharper disable once AssignNullToNotNullAttribute
                        () => Resources.FileLogger_ExtensionLongerThanFiveCharacters,
                        extension);
                extension = "." + extension;
            }

            FormatBuilder pathBuilder = new FormatBuilder()
                .Append(directory)
                .Append('\\')
                .AppendFormat(fileNameFormat);

            // Add a dedupe tag if not already present
            // ReSharper disable once PossibleNullReferenceException
            Stack<FormatChunk> formatStack = new Stack<FormatChunk>();
            formatStack.Push(fileNameFormat.RootChunk);
            bool found = false;
            while (formatStack.Count > 0)
            {
                FormatChunk chunk = formatStack.Pop();
                // ReSharper disable once PossibleNullReferenceException
                if (string.Equals(chunk.Tag, FormatTagDedupe, StringComparison.CurrentCultureIgnoreCase))
                {
                    found = true;
                    break;
                }

                foreach (var child in chunk.Children)
                    formatStack.Push(child);
            }

            if (!found)
                pathBuilder.AppendFormat("{dedupe: ({dedupe:D})}");

            return pathBuilder
                .Append(extension)
                .MakeReadOnly();
        }
Esempio n. 8
0
        private static FormatBuilder ValidatePathFormat(
            [NotNull] string directory,
            [NotNull] FormatBuilder fileNameFormat,
            [NotNull] ref string extension,
            [NotNull] FormatBuilder format)
        {
            if (directory == null)
            {
                throw new ArgumentNullException("directory");
            }
            if (fileNameFormat == null)
            {
                throw new ArgumentNullException("fileNameFormat");
            }
            if (extension == null)
            {
                throw new ArgumentNullException("extension");
            }
            if (format == null)
            {
                throw new ArgumentNullException("format");
            }

            if (string.IsNullOrWhiteSpace(directory))
            {
                directory = DefaultDirectory;
            }

            try
            {
                directory = Path.GetFullPath(directory.Trim());
                if (!System.IO.Directory.Exists(directory))
                {
                    System.IO.Directory.CreateDirectory(directory);
                }
            }
            catch (Exception e)
            {
                // ReSharper disable once AssignNullToNotNullAttribute
                throw new LoggingException(
                          e,
                          LoggingLevel.Critical,
                          () => Resources.FileLogger_DirectoryAccessOrCreationError,
                          directory);
            }

            if (fileNameFormat.IsEmpty)
            {
                // ReSharper disable once AssignNullToNotNullAttribute
                throw new LoggingException(LoggingLevel.Critical, () => Resources.FileLogger_FileNameFormatNotSpecified);
            }


            if (string.IsNullOrWhiteSpace(extension))
            {
                if (format == Log.XMLFormat)
                {
                    extension = ".xml";
                }
                else if (format == Log.JSONFormat)
                {
                    extension = ".json";
                }
                else
                {
                    extension = ".log";
                }
            }
            else
            {
                extension = extension.Trim();
                if (extension.StartsWith("."))
                {
                    extension = extension.Substring(1);
                }
                if (extension.Any(c => !Char.IsLetterOrDigit(c)))
                {
                    throw new LoggingException(
                              LoggingLevel.Critical,
                              // ReSharper disable once AssignNullToNotNullAttribute
                              () => Resources.FileLogger_Extension_Invalid_Char,
                              extension);
                }
                if (extension.Length > 5)
                {
                    throw new LoggingException(
                              LoggingLevel.Critical,
                              // ReSharper disable once AssignNullToNotNullAttribute
                              () => Resources.FileLogger_ExtensionLongerThanFiveCharacters,
                              extension);
                }
                extension = "." + extension;
            }

            FormatBuilder pathBuilder = new FormatBuilder()
                                        .Append(directory)
                                        .Append('\\')
                                        .AppendFormat(fileNameFormat);

            // Add a dedupe tag if not already present
            // ReSharper disable once PossibleNullReferenceException
            Stack <FormatChunk> formatStack = new Stack <FormatChunk>();

            formatStack.Push(fileNameFormat.RootChunk);
            bool found = false;

            while (formatStack.Count > 0)
            {
                FormatChunk chunk = formatStack.Pop();
                // ReSharper disable once PossibleNullReferenceException
                if (string.Equals(chunk.Tag, FormatTagDedupe, StringComparison.CurrentCultureIgnoreCase))
                {
                    found = true;
                    break;
                }

                foreach (var child in chunk.Children)
                {
                    formatStack.Push(child);
                }
            }

            if (!found)
            {
                pathBuilder.AppendFormat("{dedupe: ({dedupe:D})}");
            }

            return(pathBuilder
                   .Append(extension)
                   .MakeReadOnly());
        }
        public void TestAlignment()
        {
            Dictionary<string, object> dictionary = new Dictionary<string, object> { { "tag", "1" } };
            FormatBuilder builder = new FormatBuilder("{tag,3}");
            Assert.AreEqual("  1",builder.ToString(dictionary));
            builder.Clear();

            builder.AppendFormat("{tag,-3}");
            Assert.AreEqual("1  ", builder.ToString(dictionary));
            builder.Clear();

            builder.AppendFormat("{tag,3:2{tag}}");
            Assert.AreEqual(" 21", builder.ToString(dictionary));
            builder.Clear();

            builder.AppendFormat("{tag,-3:2{tag}}");
            Assert.AreEqual("21 ", builder.ToString(dictionary));
            builder.Clear();
        }
        public void TestEmptyChunksIgnored()
        {
            Dictionary<string, object> dictionary = new Dictionary<string, object> { { "tag", Resolution.Empty } };

            FormatBuilder builder = new FormatBuilder(100, 4, firstLineIndentSize: 4)
                .AppendFormat("Text{tag}text");

            Assert.AreEqual("    Texttext", builder.ToString(dictionary));

            builder.Clear();
            builder.AppendFormat("Text{tag}");

            Assert.AreEqual("    Text", builder.ToString(dictionary));

            builder.Clear();
            builder.AppendFormat("Text{tag: sub {tag}}");

            Assert.AreEqual("    Text sub ", builder.ToString(dictionary));

            builder.Clear();
            builder.AppendFormat("Text{tag:{<items>:{<item>}}}");

            Assert.AreEqual("    Text", builder.ToString(dictionary));
        }