// execute test cases for Scanner.IsSpace property public static void TestIsSpace() { var n = 0; var text = " \t\0\a\r\n1A"; var source = new ScannerStringSource(text); var results = new bool[] { true, true, true, true, false, false, false, false }; var test = new ScannerTestClass(source); foreach (var c in text) { TestCase( $"IsSpace at '{test.CharCode(test.CharCurrent)}'", () => test.IsSpace, results[n] ); source.Advance(1); ++n; } Console.WriteLine(); }
// execute test cases for Scanner.HasCharacters function public static void TestHasCharacters() { var text = "0123456789"; var source = new ScannerStringSource(text); var data = new int[] { 1, 4, 8, 20 }; var test = new ScannerTestClass(source); var n = 0; var results1 = new bool[] { true, true, true, false }; foreach (var dv in data) { TestCase( "HasCharacters({0})", (d) => test.HasCharacters(d), results1[n], dv ); ++n; } n = 0; source.Advance(5); var results2 = new bool[] { true, true, false, false }; foreach (var dv in data) { TestCase( "HasCharacters({0})", (d) => test.HasCharacters(d), results2[n], dv ); ++n; } Console.WriteLine(); }
// execute test cases for Scanner.IsBreak property public static void TestIsBreak() { var n = 0; var text = "\r \nZ\r\n\n\r"; var source = new ScannerStringSource(text); var results = new int[] { 1, 0, 1, 0, 2, 2 }; var test = new ScannerTestClass(source); while (!source.IsEnd) { TestCase( $"IsBreak at '{test.CharCode(test.CharCurrent)}'", () => test.IsBreak, results[n] ); source.Advance(results[n] == 0 ? 1 : results[n]); ++n; } Console.WriteLine(); }
// execute test cases for Scanner.SkipToToken functions public static void TestSkipToToken() { var text = " \t\bA\r\nB"; var source = new ScannerStringSource(text); var test = new ScannerTestClass(source); TestCase( "SkipToToken(false)", (d) => { var result = test.SkipToToken(false); return(new { Result = result, Current = test.CharCurrent }); }, new { Result = true, Current = 'A' }, 0, (a, b) => a.Result == b.Result && a.Current == b.Current, (v) => $"{{{v.Result}, '{v.Current}'}}" ); source.Advance(1); TestCase( "SkipToToken(false) at EOL", (d) => { var pos = source.Position; var result = test.SkipToToken(false); return(new { Result = result, Position = pos }); }, new { Result = false, Position = 4 }, 0, (a, b) => a.Result == b.Result && a.Position == b.Position, (v) => $"{{{v.Result}, {v.Position}}}" ); TestCase( "SkipToToken(true)", (d) => { var result = test.SkipToToken(); return(new { Result = result, Current = test.CharCurrent }); }, new { Result = true, Current = 'B' }, 0, (a, b) => a.Result == b.Result && a.Current == b.Current, (v) => $"{{{v.Result}, '{v.Current}'}}" ); source.Advance(1); TestCase( "SkipToToken(true) at EOF", (d) => { var pos = source.Position; var result = test.SkipToToken(); return(new { Result = result, Position = pos }); }, new { Result = false, Position = 7 }, 0, (a, b) => a.Result == b.Result && a.Position == b.Position, (v) => $"{{{v.Result}, {v.Position}}}" ); Console.WriteLine(); }
// execute test cases for ScannerStringSource class static void TestScannerStringSource() { var source = "abcdEFGH"; IScannerSource src = new ScannerStringSource(source); TestCase( "Initial Position", () => src.Position, 0 ); TestCase( "Initial Length", () => src.Length, source.Length ); for (var n = 0; n < 4; ++n) { TestCase( "CharCurrent", () => { var result = src.CharCurrent; src.Advance(); return(result); }, source[n] ); } TestCase( "Position after Advance()", () => src.Position, 4 ); for (var n = 0; n < 4; ++n) { TestCase( "CharAt({0})", (d) => src.CharAt(n), source[src.Position + n], n ); } TestCase( "IsEnd value", () => { src.Advance(4); return(src.IsEnd); }, true ); TestCase( "TokenToString(<{0}>)", (d) => src.TokenToString(d), source.Substring(2, 4), new SourceToken(2, 4) ); Console.WriteLine(); }