From 5abf50a9896f7fd297b58533dd7024ae55bd153f Mon Sep 17 00:00:00 2001 From: Vladimir Pecanac Date: Tue, 1 Dec 2026 00:35:36 +0100 Subject: [PATCH] Update EscapingBackSlashInCSharp to .NET 11 and MSTest 4.5.3 Target net11.0, update test packages, fix the string interpolation test calling the wrong method, add regex helper methods and tests. --- .../EscapingBackSlash/BackSlashMethods.cs | 23 ++++++++++++- .../EscapingBackSlash.csproj | 2 +- .../EscapingBackSlash/Program.cs | 6 +++- .../EscapingBackSlashInCSharp/Tests/Tests.cs | 34 ++++++++++++++++++- .../Tests/Tests.csproj | 10 +++--- 5 files changed, 66 insertions(+), 9 deletions(-) diff --git a/csharp-basic-topics/EscapingBackSlashInCSharp/EscapingBackSlash/BackSlashMethods.cs b/csharp-basic-topics/EscapingBackSlashInCSharp/EscapingBackSlash/BackSlashMethods.cs index e0a494a708..82356a1a55 100644 --- a/csharp-basic-topics/EscapingBackSlashInCSharp/EscapingBackSlash/BackSlashMethods.cs +++ b/csharp-basic-topics/EscapingBackSlashInCSharp/EscapingBackSlash/BackSlashMethods.cs @@ -1,4 +1,6 @@ -public static class BackslashMethods +using System.Text.RegularExpressions; + +public static class BackslashMethods { public static string UsingDoubleBackslash() { @@ -29,4 +31,23 @@ public static string UsingRawStringLiterals() { return """C:\Users\User\Documents"""; } + + public static bool ContainsBackslash(string input) + { + string pattern = "\\\\"; + + return Regex.IsMatch(input, pattern); + } + + public static bool ContainsBackslashUsingVerbatimPattern(string input) + { + string pattern = @"\\"; + + return Regex.IsMatch(input, pattern); + } + + public static string EscapeForRegex(string text) + { + return Regex.Escape(text); + } } \ No newline at end of file diff --git a/csharp-basic-topics/EscapingBackSlashInCSharp/EscapingBackSlash/EscapingBackSlash.csproj b/csharp-basic-topics/EscapingBackSlashInCSharp/EscapingBackSlash/EscapingBackSlash.csproj index c76b054ecf..e5703b1640 100644 --- a/csharp-basic-topics/EscapingBackSlashInCSharp/EscapingBackSlash/EscapingBackSlash.csproj +++ b/csharp-basic-topics/EscapingBackSlashInCSharp/EscapingBackSlash/EscapingBackSlash.csproj @@ -1,7 +1,7 @@ Exe - net8.0 + net11.0 enable enable diff --git a/csharp-basic-topics/EscapingBackSlashInCSharp/EscapingBackSlash/Program.cs b/csharp-basic-topics/EscapingBackSlashInCSharp/EscapingBackSlash/Program.cs index 4e4646ed05..2f9941b27a 100644 --- a/csharp-basic-topics/EscapingBackSlashInCSharp/EscapingBackSlash/Program.cs +++ b/csharp-basic-topics/EscapingBackSlashInCSharp/EscapingBackSlash/Program.cs @@ -3,4 +3,8 @@ Console.WriteLine(BackslashMethods.UsingUnicodeEscapeSequence()); Console.WriteLine(BackslashMethods.UsingStringFormat()); Console.WriteLine(BackslashMethods.UsingStringInterpolation()); -Console.WriteLine(BackslashMethods.UsingRawStringLiterals()); \ No newline at end of file +Console.WriteLine(BackslashMethods.UsingRawStringLiterals()); +Console.WriteLine(BackslashMethods.UsingDoubleBackslash().Length); +Console.WriteLine(BackslashMethods.ContainsBackslash(@"C:\Users\User\Documents")); +Console.WriteLine(BackslashMethods.ContainsBackslashUsingVerbatimPattern(@"C:\Users\User\Documents")); +Console.WriteLine(BackslashMethods.EscapeForRegex(@"C:\Users\User\Documents")); \ No newline at end of file diff --git a/csharp-basic-topics/EscapingBackSlashInCSharp/Tests/Tests.cs b/csharp-basic-topics/EscapingBackSlashInCSharp/Tests/Tests.cs index ce1bd3361c..5ba000cb8a 100644 --- a/csharp-basic-topics/EscapingBackSlashInCSharp/Tests/Tests.cs +++ b/csharp-basic-topics/EscapingBackSlashInCSharp/Tests/Tests.cs @@ -36,6 +36,38 @@ public void GivenStringFormatMethod_WhenCallingMethod_ThenReturnFormattedFilePat [TestMethod] public void GivenStringInterpolation_WhenCallingMethod_ThenReturnInterpolatedFilePath() { - Assert.AreEqual(PathUsingDirSeparator, BackslashMethods.UsingStringFormat()); + Assert.AreEqual(PathUsingDirSeparator, BackslashMethods.UsingStringInterpolation()); + } + + [TestMethod] + public void GivenRawStringLiteral_WhenCallingMethod_ThenReturnFilePathWithRawString() + { + Assert.AreEqual(ExpectedPath, BackslashMethods.UsingRawStringLiterals()); + } + + [TestMethod] + public void GivenDoubleBackslash_WhenCountingCharacters_ThenEachPairIsOneCharacter() + { + Assert.AreEqual(23, BackslashMethods.UsingDoubleBackslash().Length); + } + + [TestMethod] + public void GivenRegexPattern_WhenInputHasBackslash_ThenMatch() + { + Assert.IsTrue(BackslashMethods.ContainsBackslash(ExpectedPath)); + Assert.IsTrue(BackslashMethods.ContainsBackslashUsingVerbatimPattern(ExpectedPath)); + } + + [TestMethod] + public void GivenRegexPattern_WhenInputHasNoBackslash_ThenNoMatch() + { + Assert.IsFalse(BackslashMethods.ContainsBackslash("C:/Users/User/Documents")); + Assert.IsFalse(BackslashMethods.ContainsBackslashUsingVerbatimPattern("C:/Users/User/Documents")); + } + + [TestMethod] + public void GivenBackslash_WhenEscapingForRegex_ThenBackslashIsDoubled() + { + Assert.AreEqual(@"C:\\Users", BackslashMethods.EscapeForRegex(@"C:\Users")); } } \ No newline at end of file diff --git a/csharp-basic-topics/EscapingBackSlashInCSharp/Tests/Tests.csproj b/csharp-basic-topics/EscapingBackSlashInCSharp/Tests/Tests.csproj index 6ae0ba9a9a..40c2776251 100644 --- a/csharp-basic-topics/EscapingBackSlashInCSharp/Tests/Tests.csproj +++ b/csharp-basic-topics/EscapingBackSlashInCSharp/Tests/Tests.csproj @@ -1,15 +1,15 @@ - net8.0 + net11.0 enable enable false - - - - + + + +