// Subjects the SimpleParser class to a series of tests, returning True if it passes. public static bool ExecuteUnitTest() { // Note that the collection of tests below is NOT exhaustive, but they do give an // indication of the basic functionality of the SimpleParser class. They should be // helpful in validating after any future code refactoring (e.g. SimpleParser could // be rewritten to use exclusively base-1 indexing internally, and this test might // assist in verifying the various +1's and -1's were tacked on correctly. // Some of these tests may be repetitive.. I added things in several times without // checking to see if they superceeded parts that were already written. // But hey, it's not like we need to worry about having *too many* tests here! SimpleParser p = default(SimpleParser); bool caught = false; UTO("TESTING THE SIMPLEPARSER CLASS ---"); UTO("Testing constructors..."); p = new SimpleParser(); if (p.Text != "" || p.Cursor != 0) { return(false); } p = new SimpleParser("Hello"); if (p.Text != "Hello" || p.Cursor != 0) { return(false); } UTO("Passed" + Environment.NewLine); UTO("Testing basic functionality..."); p.Text = "abCdeFghijklmnop123aa1123"; if (p.Extract("C", "F") != "de") { return(false); } if (p.Cursor != 6) { return(false); } p.SeekNext("i"); if (p.Cursor != 9) { return(false); } p.SeekPrev("F"); if (p.Cursor != 5) { return(false); } if (p.Peek("f", "i") != "gh") { return(false); } if (p.Cursor != 5) { return(false); } if (p.SeekAndExtract("123", "1", "3") != "12") { return(false); } if (p.Cursor != 25) { return(false); } if (p.TrySeekPrev("Z")) { return(false); } if (p.Cursor != 25) { return(false); } // Some testing of SeekPrev on the right bound p.Cursor = p.Text.Length; if (!p.TrySeekPrev("3")) { return(false); } if (p.Cursor != p.Text.Length - 1) { return(false); } p.Text += "Y"; p.Cursor = p.Text.Length; if (!p.TrySeekPrev("Y")) { return(false); } if (p.Cursor != p.Text.Length - 1) { return(false); } // Testing of small string p.Text = "A"; if (!p.TrySeekNext("A")) { return(false); } if (!p.TrySeekPrev("A")) { return(false); } UTO("Passed" + Environment.NewLine); UTO("Testing case insensitivity..."); p.Text = "abc123CdeFghi"; p.CaseSensitive = true; if (p.Extract("C", "F") != "de") { return(false); } p.CaseSensitive = false; p.Cursor = 0; if (p.Extract("C", "F") != "123Cde") { return(false); } UTO("Passed" + Environment.NewLine); UTO("Testing cursor property edges..."); p.Text = "abcdef"; p.Cursor = 0; p.Cursor = p.Text.Length - 1; if (p.TryExtract("a", "b") != "") { return(false); } if (p.Cursor != p.Text.Length - 1) { return(false); } p.Cursor += 1; // cursor is allowed to be to the right of the last character UTO("Passed" + Environment.NewLine); UTO("Testing cursor right edge exception..."); p.Text = "abcdef"; caught = false; try { p.Cursor = p.Text.Length + 1; } catch (ArgumentOutOfRangeException) { caught = true; } if (!caught) { return(false); } UTO("Passed" + Environment.NewLine); UTO("Testing cursor left edge exception..."); p.Text = "abcdef"; caught = false; try { p.Cursor = -1; } catch (ArgumentOutOfRangeException) { caught = true; } if (!caught) { return(false); } UTO("Passed" + Environment.NewLine); UTO("Testing that SeekNext generates exception if called when cursor is at right edge of text..."); p.Text = "abc"; p.Cursor = 3; caught = false; try { p.SeekNext("a"); } catch (SimpleParser.SymbolNotFoundException) { caught = true; } if (!caught) { return(false); } // Now make sure it doesn't throw and exception using the other method p.Cursor = 3; caught = false; try { if (p.TrySeekNext("a") != false) { return(false); } } catch (SimpleParser.SymbolNotFoundException) { caught = true; } if (caught) { return(false); } UTO("Passed" + Environment.NewLine); UTO("Testing that SeekPrev generates exception if called when cursor is at left edge of text..."); p.Text = "abc"; p.Cursor = 0; caught = false; try { p.SeekPrev("a"); } catch (SimpleParser.SymbolNotFoundException) { caught = true; } if (!caught) { return(false); } // Now make sure it doesn't throw and exception using the other method p.Cursor = 0; caught = false; try { if (p.TrySeekPrev("a") != false) { return(false); } } catch (SimpleParser.SymbolNotFoundException) { caught = true; } if (caught) { return(false); } UTO("Passed" + Environment.NewLine); UTO("Testing extraction with zero-length arguments..."); p.Text = "abc"; p.Cursor = 0; if (p.Extract("", "a") != "") { return(false); } p.Cursor = 0; if (p.Extract("", "abc") != "") { return(false); } p.Cursor = 0; if (p.Extract("", "b") != "a") { return(false); } p.Cursor = 0; if (p.Extract("", "c") != "ab") { return(false); } p.Cursor = 0; if (p.Extract("", "") != "abc") { return(false); } p.Text = ""; if (p.Extract("", "") != "") { return(false); } p.Cursor = 0; if (p.CanExtract("a", "")) { return(false); } UTO("Passed" + Environment.NewLine); UTO("ALL TESTS PASSED!"); return(true); }