Skip to content

Commit

Permalink
Improve StringSection test, optimize, & add safety
Browse files Browse the repository at this point in the history
  • Loading branch information
Dankrushen committed Dec 1, 2019
1 parent 4dd2e2a commit 3bca12a
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 18 deletions.
11 changes: 9 additions & 2 deletions MultiAdmin/ServerIO/StringSections.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text;
using MultiAdmin.ConsoleTools;
Expand Down Expand Up @@ -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<StringSection> sections = new List<StringSection>();

if (string.IsNullOrEmpty(fullString))
Expand All @@ -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)))
Expand Down
65 changes: 49 additions & 16 deletions MultiAdminTests/ServerIO/StringSectionsTests.cs
Original file line number Diff line number Diff line change
@@ -1,37 +1,70 @@
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MultiAdmin.ConsoleTools;
using MultiAdmin.ServerIO;

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"}\"");
}
}
}
}
Expand Down

0 comments on commit 3bca12a

Please sign in to comment.