add UnitTest for Regex

This commit is contained in:
francesco 2020-08-29 14:58:49 +02:00
parent fe7d313e66
commit ebcb1c5781
7 changed files with 394 additions and 4 deletions

View File

@ -5,6 +5,8 @@ VisualStudioVersion = 16.0.29806.167
MinimumVisualStudioVersion = 10.0.40219.1 MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LoggingClient", "LoggingClient\LoggingClient.csproj", "{2622C2FC-3522-4D6F-B021-F63A243E77F1}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LoggingClient", "LoggingClient\LoggingClient.csproj", "{2622C2FC-3522-4D6F-B021-F63A243E77F1}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LoggingClientUnitTest", "LoggingClientUnitTest\LoggingClientUnitTest.csproj", "{6B9F572F-AE97-4A52-89AD-4453BC51E038}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU 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}.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.ActiveCfg = Release|Any CPU
{2622C2FC-3522-4D6F-B021-F63A243E77F1}.Release|Any CPU.Build.0 = 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 EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE

View File

@ -10,8 +10,8 @@ namespace LoggingClient.Validators
{ {
public class IntRangeValidationRule : ValidationRule public class IntRangeValidationRule : ValidationRule
{ {
public int MinimumLength { get; set; } public int MinimumLength { get; set; } = 1;
public int MaximumLength { get; set; } public int MaximumLength { get; set; } = 8;
public string ErrorMessage { get; set; } public string ErrorMessage { get; set; }
public override ValidationResult Validate(object value, public override ValidationResult Validate(object value,
CultureInfo cultureInfo) CultureInfo cultureInfo)

View File

@ -10,8 +10,8 @@ namespace LoggingClient.Validators
{ {
public class StringRangeValidationRule : ValidationRule public class StringRangeValidationRule : ValidationRule
{ {
public int MinimumLength { get; set; } public int MinimumLength { get; set; } = 1;
public int MaximumLength { get; set; } public int MaximumLength { get; set; } = 50;
public string ErrorMessage { get; set; } public string ErrorMessage { get; set; }

View File

@ -0,0 +1,75 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\packages\MSTest.TestAdapter.2.1.1\build\net45\MSTest.TestAdapter.props" Condition="Exists('..\packages\MSTest.TestAdapter.2.1.1\build\net45\MSTest.TestAdapter.props')" />
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{6B9F572F-AE97-4A52-89AD-4453BC51E038}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>LoggingClientUnitTest</RootNamespace>
<AssemblyName>LoggingClientUnitTest</AssemblyName>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">15.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
<ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages</ReferencePath>
<IsCodedUITest>False</IsCodedUITest>
<TestProjectType>UnitTest</TestProjectType>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\MSTest.TestFramework.2.1.1\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.dll</HintPath>
</Reference>
<Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\MSTest.TestFramework.2.1.1\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll</HintPath>
</Reference>
<Reference Include="PresentationFramework" />
<Reference Include="System" />
<Reference Include="System.Core" />
</ItemGroup>
<ItemGroup>
<Compile Include="RegEx.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\LoggingClient\LoggingClient.csproj">
<Project>{2622c2fc-3522-4d6f-b021-f63a243e77f1}</Project>
<Name>LoggingClient</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>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}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\MSTest.TestAdapter.2.1.1\build\net45\MSTest.TestAdapter.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\MSTest.TestAdapter.2.1.1\build\net45\MSTest.TestAdapter.props'))" />
<Error Condition="!Exists('..\packages\MSTest.TestAdapter.2.1.1\build\net45\MSTest.TestAdapter.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\MSTest.TestAdapter.2.1.1\build\net45\MSTest.TestAdapter.targets'))" />
</Target>
<Import Project="..\packages\MSTest.TestAdapter.2.1.1\build\net45\MSTest.TestAdapter.targets" Condition="Exists('..\packages\MSTest.TestAdapter.2.1.1\build\net45\MSTest.TestAdapter.targets')" />
</Project>

View File

@ -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")]

View File

@ -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);
}
}
}

View File

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="MSTest.TestAdapter" version="2.1.1" targetFramework="net472" />
<package id="MSTest.TestFramework" version="2.1.1" targetFramework="net472" />
</packages>