diff --git a/MultiAdmin/ServerIO/StringSections.cs b/MultiAdmin/ServerIO/StringSections.cs index d324730..1642139 100644 --- a/MultiAdmin/ServerIO/StringSections.cs +++ b/MultiAdmin/ServerIO/StringSections.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Text; using MultiAdmin.ConsoleTools; @@ -45,6 +46,12 @@ public StringSections(StringSection[] sections) public static StringSections FromString(string fullString, int sectionLength, ColoredMessage leftIndicator = null, ColoredMessage rightIndicator = null, ColoredMessage sectionBase = null) { + int rightIndicatorLength = rightIndicator?.Length ?? 0; + int totalIndicatorLength = (leftIndicator?.Length ?? 0) + rightIndicatorLength; + + if (fullString.Length > sectionLength && sectionLength <= totalIndicatorLength) + throw new ArgumentException($"{nameof(sectionLength)} must be greater than the total length of {nameof(leftIndicator)} and {nameof(rightIndicator)}", nameof(sectionLength)); + List sections = new List(); if (string.IsNullOrEmpty(fullString)) @@ -65,12 +72,12 @@ public static StringSections FromString(string fullString, int sectionLength, Co curSecBuilder.Append(fullString[i]); // If the section is less than the smallest possible section size, skip processing - if (curSecBuilder.Length < sectionLength - ((leftIndicator?.Length ?? 0) + (rightIndicator?.Length ?? 0))) continue; + if (curSecBuilder.Length < sectionLength - totalIndicatorLength) continue; // Decide what the left indicator text should be accounting for the leftmost section ColoredMessage leftIndicatorSection = sections.Count > 0 ? leftIndicator : null; // Decide what the right indicator text should be accounting for the rightmost section - ColoredMessage rightIndicatorSection = i < fullString.Length - (1 + (rightIndicator?.Length ?? 0)) ? rightIndicator : null; + ColoredMessage rightIndicatorSection = i < fullString.Length - (1 + rightIndicatorLength) ? rightIndicator : null; // Check the section length against the final section length if (curSecBuilder.Length >= sectionLength - ((leftIndicatorSection?.Length ?? 0) + (rightIndicatorSection?.Length ?? 0))) diff --git a/MultiAdminTests/ServerIO/StringSectionsTests.cs b/MultiAdminTests/ServerIO/StringSectionsTests.cs index 7e5abb3..15d4b2a 100644 --- a/MultiAdminTests/ServerIO/StringSectionsTests.cs +++ b/MultiAdminTests/ServerIO/StringSectionsTests.cs @@ -1,4 +1,6 @@ +using System; using Microsoft.VisualStudio.TestTools.UnitTesting; +using MultiAdmin.ConsoleTools; using MultiAdmin.ServerIO; namespace MultiAdminTests.ServerIO @@ -6,32 +8,63 @@ namespace MultiAdminTests.ServerIO [TestClass] public class StringSectionsTests { + private struct FromStringTemplate + { + public readonly string testString; + public readonly string[] expectedSections; + + public readonly int sectionLength; + public readonly ColoredMessage leftIndictator; + public readonly ColoredMessage rightIndictator; + + public FromStringTemplate(string testString, string[] expectedSections, int sectionLength, ColoredMessage leftIndictator = null, ColoredMessage rightIndictator = null) + { + this.testString = testString; + this.expectedSections = expectedSections; + + this.sectionLength = sectionLength; + this.leftIndictator = leftIndictator; + this.rightIndictator = rightIndictator; + } + } + [TestMethod] public void FromStringTest() { - string[] expectedSections = + try { - "te", - "st", - " s", - "tr", - "in", - "g" + StringSections.FromString("test string", 2, new ColoredMessage("."), new ColoredMessage(".")); + Assert.Fail("This case should not be allowed, no further characters can be output because of the prefix and suffix"); + } + catch (ArgumentException) + { + // Expected behaviour + } + + FromStringTemplate[] sectionTests = + { + new FromStringTemplate("test string", new string[] {"te", "st", " s", "tr", "in", "g"}, 2), + new FromStringTemplate("test string", new string[] {"tes..", ".t ..", ".st..", ".ring"}, 5, new ColoredMessage("."), new ColoredMessage("..")) }; - StringSections sections = StringSections.FromString("test string", 2); + for (int i = 0; i < sectionTests.Length; i++) + { + FromStringTemplate sectionTest = sectionTests[i]; + + StringSections sections = StringSections.FromString(sectionTest.testString, sectionTest.sectionLength, sectionTest.leftIndictator, sectionTest.rightIndictator); - Assert.IsNotNull(sections); - Assert.IsNotNull(sections.Sections); + Assert.IsNotNull(sections); + Assert.IsNotNull(sections.Sections); - Assert.IsTrue(sections.Sections.Length == expectedSections.Length, $"Expected sections length \"{expectedSections.Length}\", got \"{sections.Sections.Length}\""); + Assert.IsTrue(sections.Sections.Length == sectionTest.expectedSections.Length, $"Failed at index {i}: Expected sections length \"{sectionTest.expectedSections.Length}\", got \"{sections.Sections.Length}\""); - for (int i = 0; i < expectedSections.Length; i++) - { - string expected = expectedSections[i]; - string result = sections.Sections[i].Text?.text; + for (int j = 0; j < sectionTest.expectedSections.Length; j++) + { + string expected = sectionTest.expectedSections[j]; + string result = sections.Sections[j].Section.GetText(); - Assert.AreEqual(expected, result, $"Failed at section index {i}: Expected section text to be \"{expected ?? "null"}\", got \"{result ?? "null"}\""); + Assert.AreEqual(expected, result, $"Failed at index {i}: Failed at section index {j}: Expected section text to be \"{expected ?? "null"}\", got \"{result ?? "null"}\""); + } } } }