From ebcb1c578130a8c079fc1a12336bfd428a94829b Mon Sep 17 00:00:00 2001 From: francesco Date: Sat, 29 Aug 2020 14:58:49 +0200 Subject: [PATCH] add UnitTest for Regex --- LoggingClient/LoggingClient.sln | 6 + .../Validators/IntRangeValidationRule.cs | 4 +- .../Validators/StringRangeValidationRule.cs | 4 +- .../LoggingClientUnitTest.csproj | 75 +++++ .../Properties/AssemblyInfo.cs | 20 ++ LoggingClient/LoggingClientUnitTest/RegEx.cs | 284 ++++++++++++++++++ .../LoggingClientUnitTest/packages.config | 5 + 7 files changed, 394 insertions(+), 4 deletions(-) create mode 100644 LoggingClient/LoggingClientUnitTest/LoggingClientUnitTest.csproj create mode 100644 LoggingClient/LoggingClientUnitTest/Properties/AssemblyInfo.cs create mode 100644 LoggingClient/LoggingClientUnitTest/RegEx.cs create mode 100644 LoggingClient/LoggingClientUnitTest/packages.config diff --git a/LoggingClient/LoggingClient.sln b/LoggingClient/LoggingClient.sln index 19d7900..5fd76d9 100644 --- a/LoggingClient/LoggingClient.sln +++ b/LoggingClient/LoggingClient.sln @@ -5,6 +5,8 @@ VisualStudioVersion = 16.0.29806.167 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LoggingClient", "LoggingClient\LoggingClient.csproj", "{2622C2FC-3522-4D6F-B021-F63A243E77F1}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LoggingClientUnitTest", "LoggingClientUnitTest\LoggingClientUnitTest.csproj", "{6B9F572F-AE97-4A52-89AD-4453BC51E038}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -15,6 +17,10 @@ Global {2622C2FC-3522-4D6F-B021-F63A243E77F1}.Debug|Any CPU.Build.0 = Debug|Any CPU {2622C2FC-3522-4D6F-B021-F63A243E77F1}.Release|Any CPU.ActiveCfg = Release|Any CPU {2622C2FC-3522-4D6F-B021-F63A243E77F1}.Release|Any CPU.Build.0 = Release|Any CPU + {6B9F572F-AE97-4A52-89AD-4453BC51E038}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6B9F572F-AE97-4A52-89AD-4453BC51E038}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6B9F572F-AE97-4A52-89AD-4453BC51E038}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6B9F572F-AE97-4A52-89AD-4453BC51E038}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/LoggingClient/LoggingClient/Validators/IntRangeValidationRule.cs b/LoggingClient/LoggingClient/Validators/IntRangeValidationRule.cs index 487f8e2..212ce20 100644 --- a/LoggingClient/LoggingClient/Validators/IntRangeValidationRule.cs +++ b/LoggingClient/LoggingClient/Validators/IntRangeValidationRule.cs @@ -10,8 +10,8 @@ namespace LoggingClient.Validators { public class IntRangeValidationRule : ValidationRule { - public int MinimumLength { get; set; } - public int MaximumLength { get; set; } + public int MinimumLength { get; set; } = 1; + public int MaximumLength { get; set; } = 8; public string ErrorMessage { get; set; } public override ValidationResult Validate(object value, CultureInfo cultureInfo) diff --git a/LoggingClient/LoggingClient/Validators/StringRangeValidationRule.cs b/LoggingClient/LoggingClient/Validators/StringRangeValidationRule.cs index 2e285ff..48874ba 100644 --- a/LoggingClient/LoggingClient/Validators/StringRangeValidationRule.cs +++ b/LoggingClient/LoggingClient/Validators/StringRangeValidationRule.cs @@ -10,8 +10,8 @@ namespace LoggingClient.Validators { public class StringRangeValidationRule : ValidationRule { - public int MinimumLength { get; set; } - public int MaximumLength { get; set; } + public int MinimumLength { get; set; } = 1; + public int MaximumLength { get; set; } = 50; public string ErrorMessage { get; set; } diff --git a/LoggingClient/LoggingClientUnitTest/LoggingClientUnitTest.csproj b/LoggingClient/LoggingClientUnitTest/LoggingClientUnitTest.csproj new file mode 100644 index 0000000..a4ccd9a --- /dev/null +++ b/LoggingClient/LoggingClientUnitTest/LoggingClientUnitTest.csproj @@ -0,0 +1,75 @@ + + + + + + Debug + AnyCPU + {6B9F572F-AE97-4A52-89AD-4453BC51E038} + Library + Properties + LoggingClientUnitTest + LoggingClientUnitTest + v4.7.2 + 512 + {3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + 15.0 + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) + $(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages + False + UnitTest + + + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\packages\MSTest.TestFramework.2.1.1\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.dll + + + ..\packages\MSTest.TestFramework.2.1.1\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll + + + + + + + + + + + + + + + {2622c2fc-3522-4d6f-b021-f63a243e77f1} + LoggingClient + + + + + + + This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. + + + + + + \ No newline at end of file diff --git a/LoggingClient/LoggingClientUnitTest/Properties/AssemblyInfo.cs b/LoggingClient/LoggingClientUnitTest/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..6f83403 --- /dev/null +++ b/LoggingClient/LoggingClientUnitTest/Properties/AssemblyInfo.cs @@ -0,0 +1,20 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +[assembly: AssemblyTitle("LoggingClientUnitTest")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("LoggingClientUnitTest")] +[assembly: AssemblyCopyright("Copyright © 2020")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +[assembly: ComVisible(false)] + +[assembly: Guid("6b9f572f-ae97-4a52-89ad-4453bc51e038")] + +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/LoggingClient/LoggingClientUnitTest/RegEx.cs b/LoggingClient/LoggingClientUnitTest/RegEx.cs new file mode 100644 index 0000000..d121591 --- /dev/null +++ b/LoggingClient/LoggingClientUnitTest/RegEx.cs @@ -0,0 +1,284 @@ +using System; +using System.Globalization; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using LoggingClient.Validators; + +namespace LoggingClientUnitTest +{ + [TestClass] + public class RegEx + { + [TestMethod] + public void TestCustomerNumberRegex_True() + { + // arrange + var customerNumberValidationRule = new CustomerNumberValidationRule(); + // act + var result = customerNumberValidationRule.Validate("CU12345", CultureInfo.CurrentCulture); + // assert + Assert.IsTrue(result.IsValid); + } + [TestMethod] + public void TestCustomerNumberRegex_False() + { + // arrange + var customerNumberValidationRule = new CustomerNumberValidationRule(); + // act + var result = customerNumberValidationRule.Validate("CU123456", CultureInfo.CurrentCulture); + // assert + Assert.IsFalse(result.IsValid); + } + + [TestMethod] + public void TestCustomerNumberRegex_lower_False() + { + // arrange + var customerNumberValidationRule = new CustomerNumberValidationRule(); + // act + var result = customerNumberValidationRule.Validate("cu12345", CultureInfo.CurrentCulture); + // assert + Assert.IsFalse(result.IsValid); + } + + [TestMethod] + public void TestCustomerNumberRegex_wrongChar_False() + { + // arrange + var customerNumberValidationRule = new CustomerNumberValidationRule(); + // act + var result = customerNumberValidationRule.Validate("ab12345", CultureInfo.CurrentCulture); + // assert + Assert.IsFalse(result.IsValid); + } + + [TestMethod] + public void TestCustomerNumberRegex_wrongCharUpper_False() + { + // arrange + var customerNumberValidationRule = new CustomerNumberValidationRule(); + // act + var result = customerNumberValidationRule.Validate("ZZ12345", CultureInfo.CurrentCulture); + // assert + Assert.IsFalse(result.IsValid); + } + + [TestMethod] + public void TestCustomerNumberRegex_onlyDigit_False() + { + // arrange + var customerNumberValidationRule = new CustomerNumberValidationRule(); + // act + var result = customerNumberValidationRule.Validate("0012345", CultureInfo.CurrentCulture); + // assert + Assert.IsFalse(result.IsValid); + } + + [TestMethod] + public void TestEmailRegex_True() + { + // arrange + var emailValidationRule = new EmailValidationRule(); + // act + var result = emailValidationRule.Validate("test.test@test.com", CultureInfo.CurrentCulture); + // assert + Assert.IsTrue(result.IsValid); + } + [TestMethod] + public void TestEmailRegex_False() + { + // arrange + var emailValidationRule = new EmailValidationRule(); + // act + var result = emailValidationRule.Validate("test.test@.com", CultureInfo.CurrentCulture); + // assert + Assert.IsFalse(result.IsValid); + } + + [TestMethod] + public void TestEmailRegex_2_False() + { + // arrange + var emailValidationRule = new EmailValidationRule(); + // act + var result = emailValidationRule.Validate("test..test@a.com", CultureInfo.CurrentCulture); + // assert + Assert.IsFalse(result.IsValid); + } + + [TestMethod] + public void TestEmailRegex_3_False() + { + // arrange + var emailValidationRule = new EmailValidationRule(); + // act + var result = emailValidationRule.Validate("test.test@a_com", CultureInfo.CurrentCulture); + // assert + Assert.IsFalse(result.IsValid); + } + + [TestMethod] + public void TestIntRangeValidation_True() + { + // arrange + var intRangeValidationRule = new IntRangeValidationRule(); + // act + var result = intRangeValidationRule.Validate("8", CultureInfo.CurrentCulture); + // assert + Assert.IsTrue(result.IsValid); + } + + [TestMethod] + public void TestIntRangeValidation_False() + { + // arrange + var intRangeValidationRule = new IntRangeValidationRule(); + // act + var result = intRangeValidationRule.Validate("100", CultureInfo.CurrentCulture); + // assert + Assert.IsFalse(result.IsValid); + } + + [TestMethod] + public void TestPasswordRegex_True() + { + // arrange + var passwordValidationRule = new PasswordValidationRule(); + // act + var result = passwordValidationRule.Validate("Test1234?", CultureInfo.CurrentCulture); + // assert + Assert.IsTrue(result.IsValid); + } + + [TestMethod] + public void TestPasswordRegex_noLenght_False() + { + // arrange + var passwordValidationRule = new PasswordValidationRule(); + // act + var result = passwordValidationRule.Validate("Test12!", CultureInfo.CurrentCulture); + // assert + Assert.IsFalse(result.IsValid); + } + + [TestMethod] + public void TestPasswordRegex_nolower_False() + { + // arrange + var passwordValidationRule = new PasswordValidationRule(); + // act + var result = passwordValidationRule.Validate("TWST1234!", CultureInfo.CurrentCulture); + // assert + Assert.IsFalse(result.IsValid); + } + + [TestMethod] + public void TestPasswordRegex_noUpper_False() + { + // arrange + var passwordValidationRule = new PasswordValidationRule(); + // act + var result = passwordValidationRule.Validate("test1234!", CultureInfo.CurrentCulture); + // assert + Assert.IsFalse(result.IsValid); + } + + [TestMethod] + public void TestPasswordRegex_noSpecials_False() + { + // arrange + var passwordValidationRule = new PasswordValidationRule(); + // act + var result = passwordValidationRule.Validate("test1234", CultureInfo.CurrentCulture); + // assert + Assert.IsFalse(result.IsValid); + } + + [TestMethod] + public void TestStringRangeValidation_True() + { + // arrange + var stringRangeValidationRule = new StringRangeValidationRule(); + // act + var result = stringRangeValidationRule.Validate("Test", CultureInfo.CurrentCulture); + // assert + Assert.IsTrue(result.IsValid); + } + + [TestMethod] + public void TestStringRangeValidation_False() + { + // arrange + var stringRangeValidationRule = new StringRangeValidationRule(); + // act + var result = stringRangeValidationRule.Validate("", CultureInfo.CurrentCulture); + // assert + Assert.IsFalse(result.IsValid); + } + + [TestMethod] + public void TestUrlRegex_subdomain_True() + { + // arrange + var urlValidationRule = new UrlValidationRule(); + // act + var result = urlValidationRule.Validate("www.google.com", CultureInfo.CurrentCulture); + // assert + Assert.IsTrue(result.IsValid); + } + + [TestMethod] + public void TestUrlRegex_prefix_True() + { + // arrange + var urlValidationRule = new UrlValidationRule(); + // act + var result = urlValidationRule.Validate("https://google.com", CultureInfo.CurrentCulture); + // assert + Assert.IsTrue(result.IsValid); + } + + [TestMethod] + public void TestUrlRegex_prefix_subdomain_True() + { + // arrange + var urlValidationRule = new UrlValidationRule(); + // act + var result = urlValidationRule.Validate("https://www.google.com", CultureInfo.CurrentCulture); + // assert + Assert.IsTrue(result.IsValid); + } + + [TestMethod] + public void TestUrlRegex_prefix_subdomain_endpoint_True() + { + // arrange + var urlValidationRule = new UrlValidationRule(); + // act + var result = urlValidationRule.Validate("https://www.google.com/passwords", CultureInfo.CurrentCulture); + // assert + Assert.IsTrue(result.IsValid); + } + + [TestMethod] + public void TestUrlRegex_prefix_subdomain_endpoint_query_True() + { + // arrange + var urlValidationRule = new UrlValidationRule(); + // act + var result = urlValidationRule.Validate("https://www.google.com/passwords?lernen.zbw=rafr1", CultureInfo.CurrentCulture); + // assert + Assert.IsTrue(result.IsValid); + } + + [TestMethod] + public void TestUrlRegex_False() + { + // arrange + var urlValidationRule = new UrlValidationRule(); + // act + var result = urlValidationRule.Validate("google", CultureInfo.CurrentCulture); + // assert + Assert.IsFalse(result.IsValid); + } + } +} diff --git a/LoggingClient/LoggingClientUnitTest/packages.config b/LoggingClient/LoggingClientUnitTest/packages.config new file mode 100644 index 0000000..7079f81 --- /dev/null +++ b/LoggingClient/LoggingClientUnitTest/packages.config @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file