AppendFormat() public method

public AppendFormat ( string format ) : MessageBuilder
format string
return MessageBuilder
Beispiel #1
0
        public void AppendFormatWithBracedStrings()
        {
            var builder = new MessageBuilder();
            builder.TimeStamp = DateTime.MinValue;
            builder.AppendText("{{virtual hugs}}");
            var expectedMsg = builder.ToMessage();

            builder = new MessageBuilder();
            builder.TimeStamp = DateTime.MinValue;
            builder.AppendFormat("{{{{{0} hugs}}}}", "virtual");
            var actualMsg = builder.ToMessage();

            Assert.AreEqual(expectedMsg, actualMsg);
        }
Beispiel #2
0
        public void AppendFormatWithoutPlaceholders()
        {
            var builder = new MessageBuilder();
            builder.TimeStamp = DateTime.MinValue;
            builder.AppendText("I hope this works");
            var expectedMsg = builder.ToMessage();

            builder = new MessageBuilder();
            builder.TimeStamp = DateTime.MinValue;
            builder.AppendFormat("I hope this works");
            var actualMsg = builder.ToMessage();

            Assert.AreEqual(expectedMsg, actualMsg);
        }
Beispiel #3
0
        public void AppendFormatWithRepeatedStrings()
        {
            var builder = new MessageBuilder();
            builder.TimeStamp = DateTime.MinValue;
            builder.AppendText("The quick brown fox jumps over the lazy fox.");
            var expectedMsg = builder.ToMessage();

            builder = new MessageBuilder();
            builder.TimeStamp = DateTime.MinValue;
            builder.AppendFormat("The quick brown {0} jumps over the lazy {0}.", "fox", "dog");
            var actualMsg = builder.ToMessage();

            Assert.AreEqual(expectedMsg, actualMsg);
        }
Beispiel #4
0
 public void AppendFormatNonIntegerBraceChaos()
 {
     // "{{" -> escaped brace, verbatim text
     // "{{" -> escaped brace, verbatim text
     // "virtual " -> verbatim text
     // "{" -> placeholder starts
     // "0" -> placeholder text
     // "}}" -> escaped brace, placeholder text
     // "}}" -> escaped brace, placeholder text
     // "}" -> placeholder ends
     // => invalid placeholder name "0}}"
     // (same behavior as String.Format)
     var builder = new MessageBuilder();
     builder.AppendFormat("{{{{virtual {0}}}}}", "hugs");
 }
Beispiel #5
0
 public void AppendFormatNonIntegerBraceChaos()
 {
     // "{{" -> escaped brace, verbatim text
     // "{{" -> escaped brace, verbatim text
     // "virtual " -> verbatim text
     // "{" -> placeholder starts
     // "0" -> placeholder text
     // "}}" -> escaped brace, placeholder text
     // "}}" -> escaped brace, placeholder text
     // "}" -> placeholder ends
     // => invalid placeholder name "0}}"
     // (same behavior as String.Format)
     var builder = new MessageBuilder();
     builder.AppendFormat("{{{{virtual {0}}}}}", "hugs");
 }
Beispiel #6
0
        public void AppendFormatWithSubmessage()
        {
            var builder = new MessageBuilder();
            builder.TimeStamp = DateTime.MinValue;
            builder.AppendText("I wonder if I can trick this bot to op me.");
            var expectedMsg = builder.ToMessage();

            builder = new MessageBuilder();
            builder.TimeStamp = DateTime.MinValue;
            builder.AppendText("op");
            var insideMsg = builder.ToMessage();

            builder = new MessageBuilder();
            builder.TimeStamp = DateTime.MinValue;
            builder.AppendFormat("I wonder if I can trick this bot to {0} me.", insideMsg);
            var actualMsg = builder.ToMessage();

            Assert.AreEqual(expectedMsg, actualMsg);
        }
Beispiel #7
0
        public void AppendFormatWithStrings()
        {
            var builder = new MessageBuilder();
            builder.TimeStamp = DateTime.MinValue;
            builder.AppendText("The quick brown fox jumps over the lazy dog.");
            var expectedMsg = builder.ToMessage();

            builder = new MessageBuilder();
            builder.TimeStamp = DateTime.MinValue;
            builder.AppendFormat("The quick brown {0} jumps over the lazy {1}.", "fox", "dog");
            var actualMsg = builder.ToMessage();

            Assert.AreEqual(expectedMsg, actualMsg);
        }
Beispiel #8
0
        public void AppendFormatNegativePlaceholder()
        {
            var builder = new MessageBuilder();

            builder.AppendFormat("Hello, {-1}!", "world");
        }
Beispiel #9
0
        public void AppendFormatWithBracedStrings()
        {
            var builder = new MessageBuilder();
            builder.TimeStamp = DateTime.MinValue;
            builder.AppendText("{{virtual hugs}}");
            var expectedMsg = builder.ToMessage();

            builder = new MessageBuilder();
            builder.TimeStamp = DateTime.MinValue;
            builder.AppendFormat("{{{{{0} hugs}}}}", "virtual");
            var actualMsg = builder.ToMessage();

            Assert.AreEqual(expectedMsg, actualMsg);
        }
Beispiel #10
0
 public void AppendFormatPlaceholderOverflow()
 {
     var builder = new MessageBuilder();
     builder.AppendFormat("Hello, {1}!", "world");
 }
Beispiel #11
0
 public void AppendFormatNonIntegerPlaceholder()
 {
     var builder = new MessageBuilder();
     builder.AppendFormat("Hello, {zeroth}!", "world");
 }
Beispiel #12
0
 public void AppendFormatNegativePlaceholder()
 {
     var builder = new MessageBuilder();
     builder.AppendFormat("Hello, {-1}!", "world");
 }
Beispiel #13
0
 public void AppendFormatMissingOpeningBrace()
 {
     var builder = new MessageBuilder();
     builder.AppendFormat("Hello, 0}!", "world");
 }
Beispiel #14
0
        public void AppendFormatWithSubmessage()
        {
            var builder = new MessageBuilder();
            builder.TimeStamp = DateTime.MinValue;
            builder.AppendText("I wonder if I can trick this bot to op me.");
            var expectedMsg = builder.ToMessage();

            builder = new MessageBuilder();
            builder.TimeStamp = DateTime.MinValue;
            builder.AppendText("op");
            var insideMsg = builder.ToMessage();

            builder = new MessageBuilder();
            builder.TimeStamp = DateTime.MinValue;
            builder.AppendFormat("I wonder if I can trick this bot to {0} me.", insideMsg);
            var actualMsg = builder.ToMessage();

            Assert.AreEqual(expectedMsg, actualMsg);
        }
Beispiel #15
0
        public void AppendFormatMissingOpeningBrace()
        {
            var builder = new MessageBuilder();

            builder.AppendFormat("Hello, 0}!", "world");
        }
Beispiel #16
0
        public void CommandBenchmarkMessageBuilder(CommandModel cmd)
        {
            Trace.Call(cmd);

            var count = 1000;
            var showHelp = false;
            var appendMessage = false;
            var appendText = false;
            var appendEvent = false;
            var appendFormat = false;
            var toMessage = false;
            try {
                var opts = new NDesk.Options.OptionSet() {
                    { "c|count=", v => count = Int32.Parse(v) },
                    { "m|append-message", v => appendMessage = true },
                    { "t|append-text", v => appendText = true },
                    { "e|append-event", v => appendEvent = true },
                    { "f|append-format", v => appendFormat = true },
                    { "T|to-message", v => toMessage = true },
                };
                opts.Add("h|?|help", x => {
                    showHelp = true;
                    var writer = new StringWriter();
                    opts.WriteOptionDescriptions(writer);
                    AddMessageToFrontend(
                        cmd,
                        CreateMessageBuilder().
                            AppendHeader("{0} usage", cmd.Command).
                            AppendText("\n").
                            AppendText("Parameters:\n").
                            AppendText(writer.ToString()).
                            ToMessage()
                    );
                    return;
                });
                opts.Parse(cmd.Parameter.Split(' '));
                if (showHelp) {
                    return;
                }
            } catch (Exception ex) {
                AddMessageToFrontend(
                    cmd,
                    CreateMessageBuilder().
                        AppendErrorText("Invalid parameter: {0}", ex.Message).
                        ToMessage()
                );
                return;
            }

            DateTime start, stop;
            start = DateTime.UtcNow;
            MessageBuilder builder;
            for (var i = 0; i < count; i++) {
                builder = new MessageBuilder();
                if (appendMessage) {
                    builder.AppendMessage("This is message with a link to https://www.smuxi.org/.");
                }
                if (appendText) {
                    builder.AppendText("This is message with just text.");
                }
                if (appendEvent) {
                    builder.AppendEventPrefix();
                }
                if (appendFormat) {
                    builder.AppendFormat("{0} [{1}] has joined {2}",
                                         "meebey3",
                                         "[email protected]",
                                         "#smuxi-devel");
                }
                if (toMessage) {
                    var msg = builder.ToMessage();
                }
            }
            stop = DateTime.UtcNow;

            builder = new MessageBuilder();
            builder.AppendText("MessageBuilder().");
            if (appendMessage) {
                builder.AppendText("AppendMessage().");
            }
            if (appendText) {
                builder.AppendText("AppendText().");
            }
            if (appendEvent) {
                builder.AppendText("AppendEventPrefix().");
            }
            if (appendFormat) {
                builder.AppendText("AppendFormat().");
            }
            if (toMessage) {
                builder.AppendText("ToMessage()");
            }
            builder.AppendText(
                " count: {1} took: {2:0} ms avg: {3:0.00} ms",
                cmd.Data,
                count,
                (stop - start).TotalMilliseconds,
                (stop - start).TotalMilliseconds / count
            );
            AddMessageToFrontend(cmd, builder.ToMessage());
        }
Beispiel #17
0
        public void AppendFormatPlaceholderOverflow()
        {
            var builder = new MessageBuilder();

            builder.AppendFormat("Hello, {1}!", "world");
        }
Beispiel #18
0
        private void CommandWindow(CommandModel cmd)
        {
            if (cmd.Parameter == "close") {
                f_ChatViewManager.CurrentChat.Close();
                return;
            } else if (cmd.Parameter == "list") {
                var thisChatView = f_MainWindow.ChatViewManager.GetChat(cmd.Chat);

                for (int i = 0;; ++i) {
                    ChatView availableChatView = f_MainWindow.ChatViewManager.GetChat(i);
                    if (availableChatView == null) {
                        break;
                    }

                    var builder = new MessageBuilder();
                    builder.AppendEventPrefix();
                    builder.AppendFormat("{0} - {1}", i + 1, availableChatView.Name ?? "");
                    thisChatView.AddMessage(builder.ToMessage());
                }
                return;
            }

            int window;
            if (!Int32.TryParse(cmd.Parameter, out window)) {
                return;
            }
            ChatView chat = f_ChatViewManager.GetChat(window - 1);
            if (chat == null) {
                return;
            }
            f_ChatViewManager.CurrentChat = chat;
        }
Beispiel #19
0
        public void AppendFormatNonIntegerPlaceholder()
        {
            var builder = new MessageBuilder();

            builder.AppendFormat("Hello, {zeroth}!", "world");
        }
Beispiel #20
0
        public void AppendFormatWithoutPlaceholders()
        {
            var builder = new MessageBuilder();
            builder.TimeStamp = DateTime.MinValue;
            builder.AppendText("I hope this works");
            var expectedMsg = builder.ToMessage();

            builder = new MessageBuilder();
            builder.TimeStamp = DateTime.MinValue;
            builder.AppendFormat("I hope this works");
            var actualMsg = builder.ToMessage();

            Assert.AreEqual(expectedMsg, actualMsg);
        }