|
| 1 | +using System; |
1 | 2 | using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 3 | +using MultiAdmin.ConsoleTools; |
2 | 4 | using MultiAdmin.ServerIO; |
3 | 5 |
|
4 | 6 | namespace MultiAdminTests.ServerIO |
5 | 7 | { |
6 | 8 | [TestClass] |
7 | 9 | public class StringSectionsTests |
8 | 10 | { |
| 11 | + private struct FromStringTemplate |
| 12 | + { |
| 13 | + public readonly string testString; |
| 14 | + public readonly string[] expectedSections; |
| 15 | + |
| 16 | + public readonly int sectionLength; |
| 17 | + public readonly ColoredMessage leftIndictator; |
| 18 | + public readonly ColoredMessage rightIndictator; |
| 19 | + |
| 20 | + public FromStringTemplate(string testString, string[] expectedSections, int sectionLength, ColoredMessage leftIndictator = null, ColoredMessage rightIndictator = null) |
| 21 | + { |
| 22 | + this.testString = testString; |
| 23 | + this.expectedSections = expectedSections; |
| 24 | + |
| 25 | + this.sectionLength = sectionLength; |
| 26 | + this.leftIndictator = leftIndictator; |
| 27 | + this.rightIndictator = rightIndictator; |
| 28 | + } |
| 29 | + } |
| 30 | + |
9 | 31 | [TestMethod] |
10 | 32 | public void FromStringTest() |
11 | 33 | { |
12 | | - string[] expectedSections = |
| 34 | + try |
13 | 35 | { |
14 | | - "te", |
15 | | - "st", |
16 | | - " s", |
17 | | - "tr", |
18 | | - "in", |
19 | | - "g" |
| 36 | + StringSections.FromString("test string", 2, new ColoredMessage("."), new ColoredMessage(".")); |
| 37 | + Assert.Fail("This case should not be allowed, no further characters can be output because of the prefix and suffix"); |
| 38 | + } |
| 39 | + catch (ArgumentException) |
| 40 | + { |
| 41 | + // Expected behaviour |
| 42 | + } |
| 43 | + |
| 44 | + FromStringTemplate[] sectionTests = |
| 45 | + { |
| 46 | + new FromStringTemplate("test string", new string[] {"te", "st", " s", "tr", "in", "g"}, 2), |
| 47 | + new FromStringTemplate("test string", new string[] {"tes..", ".t ..", ".st..", ".ring"}, 5, new ColoredMessage("."), new ColoredMessage("..")) |
20 | 48 | }; |
21 | 49 |
|
22 | | - StringSections sections = StringSections.FromString("test string", 2); |
| 50 | + for (int i = 0; i < sectionTests.Length; i++) |
| 51 | + { |
| 52 | + FromStringTemplate sectionTest = sectionTests[i]; |
| 53 | + |
| 54 | + StringSections sections = StringSections.FromString(sectionTest.testString, sectionTest.sectionLength, sectionTest.leftIndictator, sectionTest.rightIndictator); |
23 | 55 |
|
24 | | - Assert.IsNotNull(sections); |
25 | | - Assert.IsNotNull(sections.Sections); |
| 56 | + Assert.IsNotNull(sections); |
| 57 | + Assert.IsNotNull(sections.Sections); |
26 | 58 |
|
27 | | - Assert.IsTrue(sections.Sections.Length == expectedSections.Length, $"Expected sections length \"{expectedSections.Length}\", got \"{sections.Sections.Length}\""); |
| 59 | + Assert.IsTrue(sections.Sections.Length == sectionTest.expectedSections.Length, $"Failed at index {i}: Expected sections length \"{sectionTest.expectedSections.Length}\", got \"{sections.Sections.Length}\""); |
28 | 60 |
|
29 | | - for (int i = 0; i < expectedSections.Length; i++) |
30 | | - { |
31 | | - string expected = expectedSections[i]; |
32 | | - string result = sections.Sections[i].Text?.text; |
| 61 | + for (int j = 0; j < sectionTest.expectedSections.Length; j++) |
| 62 | + { |
| 63 | + string expected = sectionTest.expectedSections[j]; |
| 64 | + string result = sections.Sections[j].Section.GetText(); |
33 | 65 |
|
34 | | - Assert.AreEqual(expected, result, $"Failed at section index {i}: Expected section text to be \"{expected ?? "null"}\", got \"{result ?? "null"}\""); |
| 66 | + Assert.AreEqual(expected, result, $"Failed at index {i}: Failed at section index {j}: Expected section text to be \"{expected ?? "null"}\", got \"{result ?? "null"}\""); |
| 67 | + } |
35 | 68 | } |
36 | 69 | } |
37 | 70 | } |
|
0 commit comments