[TestCase] public void AddContentsAndClear() { var buffer = new StringWrapBuffer(4, 7); Assert.AreEqual(buffer.Contents.Count, 0); //0 strings after creation Assert.AreEqual(buffer.Clear().Count, 0); //0 strings after clear buffer.Add(null); buffer.Add(""); buffer.Add(" "); //leading discarded separators are removed from strings that begin a new line Assert.AreEqual(buffer.Contents.Count, 0); //0 strings after empty adds buffer.Add("Hello. "); Assert.AreEqual(buffer.Contents.Count, 1); //1 string after add Assert.AreEqual(buffer.Contents[0], "Hello. "); //first string matches input Assert.AreEqual(buffer.Clear()[0], "Hello. "); //first string of cleared contents matches input Assert.AreEqual(buffer.Contents.Count, 0); //0 strings after clear }
public void Print(bool requireMorePrompt) { if (requireMorePrompt) { buffer.EnsureReservedSpace(false); } DisplayLines(buffer.Clear(), requireMorePrompt, true); if (interruptPlayer) { //todo interrupt interruptPlayer = false; } }
[TestCase] public void BufferOverflow() { var buffer = new StringWrapBuffer(1, 4); buffer.Add("abcde"); Assert.AreEqual(buffer.Contents.Count, 1); //only the overflow should remain Assert.AreEqual(buffer.Contents[0], "e"); List <string> bufferOverflow = new List <string>(); buffer.BufferFull += list => { foreach (string s in list) { bufferOverflow.Add(s); } }; buffer.Add("fghijk"); //adding atop the "e" Assert.AreEqual(buffer.Contents[0], "ijk"); //only the overflow should remain Assert.AreEqual(bufferOverflow.Count, 1); Assert.AreEqual(bufferOverflow[0], "efgh"); //this list should have received the full buffer's contents buffer = new StringWrapBuffer(3, 4); buffer.BufferFull += list => { foreach (string s in list) { bufferOverflow.Add(s); } }; bufferOverflow.Clear(); buffer.Add("one two four five "); Assert.AreEqual(buffer.Contents.Count, 1); Assert.AreEqual(buffer.Contents[0], "five"); //only "five" should remain Assert.AreEqual(bufferOverflow.Count, 3); Assert.AreEqual(bufferOverflow[0], "one"); Assert.AreEqual(bufferOverflow[2], "four"); buffer.MaxLines = -1; buffer.Clear(); bufferOverflow.Clear(); buffer.Add("one two four five "); buffer.MaxLines = 4; Assert.AreEqual(4, buffer.Contents.Count); buffer.MaxLines = 3; Assert.AreEqual(1, buffer.Contents.Count); }
[TestCase] public void LineOverflow() { var buffer = new StringWrapBuffer(2, 10); //with this constructor: space is discarded, hyphen is retained buffer.Add("Hello. Goodbye. "); Assert.AreEqual(buffer.Contents.Count, 2); //2 strings after add & wrap Assert.AreEqual(buffer.Contents[0], "Hello."); //wrap should happen at edge of discarded characters Assert.AreEqual(buffer.Contents[1], "Goodbye. "); //spaces not used for wrapping should be unaffected buffer = new StringWrapBuffer(2, 10, new char[] { '+' }, null); //+ is retained buffer.Add("Hello.+Goodbye.+"); Assert.AreEqual(buffer.Contents.Count, 2); //2 strings after add & wrap Assert.AreEqual(buffer.Contents[0], "Hello.+"); //chosen + should be retained buffer.Clear(); buffer.Add("abcdefghijklm"); Assert.AreEqual(buffer.Contents.Count, 2); //2 strings after add & wrap Assert.AreEqual(buffer.Contents[0], "abcdefghij"); //max length used when no separators present Assert.AreEqual(buffer.Contents[1], "klm"); buffer = new StringWrapBuffer(-1, 5); buffer.Add("abcde "); Assert.AreEqual(buffer.Contents.Count, 1); //discarded spaces should not create new line Assert.AreEqual(buffer.Contents[0], "abcde"); buffer = new StringWrapBuffer(-1, 20); buffer.Add("0 1 2 3 4 5 6 7 8 9 "); buffer.MaxLength = 12; Assert.AreEqual(buffer.Contents.Count, 2); //should wrap onto 2nd line Assert.AreEqual(buffer.Contents[0], "0 1 2 3 4 5"); Assert.AreEqual(buffer.Contents[1], "6 7 8 9 "); buffer.MaxLength = 3; Assert.AreEqual(buffer.Contents.Count, 3); //should discard spaces and end up with 3 lines Assert.AreEqual(buffer.Contents[0], "0 1 2 3 4 5"); //the MaxLength value of 3 should not affect previous lines Assert.AreEqual(buffer.Contents[1], "6 7"); Assert.AreEqual(buffer.Contents[2], "8 9"); //the trailing space should be discarded too buffer = new StringWrapBuffer(-1, 1, null, new char[] { '!' }); //! is discarded buffer.Add("abc!!!!hijklm"); //expected lines: a b c h i j k l m Assert.AreEqual(buffer.Contents.Count, 9); Assert.AreEqual(buffer.Contents[2], "c"); Assert.AreEqual(buffer.Contents[3], "h"); Assert.Throws(typeof(ArgumentOutOfRangeException), () => { buffer.MaxLength = 0; }); }