From 983d2cf364079209791774b524fcf67bf240181e Mon Sep 17 00:00:00 2001 From: Benjamin Michaelis Date: Fri, 9 Sep 2022 13:52:03 -0700 Subject: [PATCH] #WIP add regex --- .../ConsoleAssertTests.cs | 20 ++++++++++++++++ .../ConsoleAssert.cs | 24 ++++++++++++++++++- 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/IntelliTect.TestTools.Console.Tests/ConsoleAssertTests.cs b/IntelliTect.TestTools.Console.Tests/ConsoleAssertTests.cs index 164a670..cc335c0 100644 --- a/IntelliTect.TestTools.Console.Tests/ConsoleAssertTests.cs +++ b/IntelliTect.TestTools.Console.Tests/ConsoleAssertTests.cs @@ -72,6 +72,26 @@ await ConsoleAssert.ExpectAsync(view, async () => }, NormalizeOptions.None); } + [TestMethod] + [DataRow("The Chicago Cubs", "^The")] + [DataRow("This is the end", "end$")] + [DataRow("The end", "^The end$")] + [DataRow("Can you hear me roar?", "roar")] + [DataRow("abcsdfkjalsdjf", "abc*")] + [DataRow("abcccc", "abc+")] + [DataRow("ab", "abc?")] + [DataRow("abc", "abc?")] + [DataRow(@"Error Hello World +Hello World", @"^Error (Hello World{Environment.NewLine})+$")] + public void ConsoleTester_HelloWorldRegex_Input(string output, string pattern) + { + pattern = pattern.Replace("{Environment.NewLine}", $"{Environment.NewLine}"); + ConsoleAssert.ExpectLikeRegex(pattern, () => + { + System.Console.WriteLine(output); + }); + } + [TestMethod] public void ConsoleTester_HelloWorld_TrimLF() { diff --git a/IntelliTect.TestTools.Console/ConsoleAssert.cs b/IntelliTect.TestTools.Console/ConsoleAssert.cs index 7d23f31..62053d4 100644 --- a/IntelliTect.TestTools.Console/ConsoleAssert.cs +++ b/IntelliTect.TestTools.Console/ConsoleAssert.cs @@ -359,6 +359,29 @@ public static Task ExpectLikeAsync(string expected, "The values are not like (using wildcards) each other"); } + /// + /// Performs a unit test on a console-based method. A "view" of + /// what a user would see in their console is provided as a string, + /// where their input (including line-breaks) is surrounded by double + /// less-than/greater-than signs, like so: "Input please: <<Input>>" + /// + /// Expected "view" to be seen on the console, + /// including both input and output + /// Method to be run + /// Whether differences in line ending styles should be ignored. + /// The escape character for the wildcard caracters. Default is '\'. + public static string ExpectLikeRegex(string expectedRegex, + Action action, + NormalizeOptions normalizeLineEndings = NormalizeOptions.Default, + char escapeCharacter = '\\') + { + return Expect(expectedRegex, + action, + (pattern, output) => Regex.IsMatch(output, pattern), + normalizeLineEndings, + "The values are not like (using wildcards) each other"); + } + /// /// Normalizes all line endings of the input string into /// @@ -406,7 +429,6 @@ private static string Execute(string givenInput, ) { string output = Execute(givenInput, action); - return CompareOutput(output, expectedOutput, normalizeOptions, areEquivalentOperator, equivalentOperatorErrorMessage); }