From 7e6742e70c15701f87833677a41c759f3d353f48 Mon Sep 17 00:00:00 2001 From: tidusjar Date: Wed, 29 Jun 2016 12:58:41 +0100 Subject: [PATCH] Some more unit tests around the NotificationMessageResolver --- .../AuthenticationSettingsTests.cs | 25 +++++++++++++++++++ .../NotificationMessageResolver.cs | 6 +++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/PlexRequests.Core.Tests/AuthenticationSettingsTests.cs b/PlexRequests.Core.Tests/AuthenticationSettingsTests.cs index 3072ab82a..ba4a666da 100644 --- a/PlexRequests.Core.Tests/AuthenticationSettingsTests.cs +++ b/PlexRequests.Core.Tests/AuthenticationSettingsTests.cs @@ -84,6 +84,31 @@ namespace PlexRequests.Core.Tests new Dictionary { { "son", "HEY!" } }) .Returns(new string(')', 5000)) .SetName("Long String"); + + + yield return new TestCaseData( + new Dictionary { { NotificationType.NewRequest, "This is a {Username} and {Username} Because {Curly}{Curly}" } }, + new Dictionary { { "Username", "HEY!" }, {"Curly","Bob"} }) + .Returns("This is a HEY! and HEY! Because BobBob") + .SetName("Double Curly"); + + yield return new TestCaseData( + new Dictionary { { NotificationType.NewRequest, "This is a {Username} and {Username} Because {Curly}{Curly}" } }, + new Dictionary { { "username", "HEY!" }, { "Curly", "Bob" } }) + .Returns("This is a {Username} and {Username} Because BobBob") + .SetName("Case sensitive"); + + yield return new TestCaseData( + new Dictionary { { NotificationType.NewRequest, "{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}{a}" } }, + new Dictionary { { "a", "b" } }) + .Returns("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb") + .SetName("Lots of curlys"); + + yield return new TestCaseData( + new Dictionary { { NotificationType.NewRequest, $"{{{new string('b', 10000)}}}" } }, + new Dictionary { { new string('b', 10000), "Hello" } }) + .Returns("Hello") + .SetName("Very long Curly"); } } diff --git a/PlexRequests.Core/NotificationMessageResolver.cs b/PlexRequests.Core/NotificationMessageResolver.cs index 5e26e9300..6dd4006c8 100644 --- a/PlexRequests.Core/NotificationMessageResolver.cs +++ b/PlexRequests.Core/NotificationMessageResolver.cs @@ -34,6 +34,8 @@ namespace PlexRequests.Core { public class NotificationMessageResolver { + private const char StartChar = (char)123; + private const char EndChar = (char)125; public string ParseMessage(T notification, NotificationType type) where T : NotificationSettings { var notificationToParse = notification.Message.FirstOrDefault(x => x.Key == type).Value; @@ -75,13 +77,13 @@ namespace PlexRequests.Core continue; } - if (c == 123) // Start of curly '{' + if (c == StartChar) // Start of curly '{' { insideCurly = true; continue; } - if (c == 125) // End of curly '}' + if (c == EndChar) // End of curly '}' { fields.Add(currentWord); // We have finished the curly, add the word into the list currentWord = string.Empty;