Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

Expand All @@ -10,11 +10,10 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="xunit" Version="2.5.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" />
<PackageReference Include="coverlet.collector" Version="10.1.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.11.0" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="4.0.1" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public void GivenValidPassword_WhenValidatePasswordWithRegularRegExIsInvoked_The
var result = PasswordValidator.ValidatePasswordWithRegularRegEx(ValidPassword);

// Assert
result.Should().BeTrue();
Assert.True(result);
}

[Fact]
Expand All @@ -23,7 +23,7 @@ public void GivenInvalidPassword_WhenValidatePasswordWithRegularRegExIsInvoked_T
var result = PasswordValidator.ValidatePasswordWithRegularRegEx(InvalidPassword);

// Assert
result.Should().BeFalse();
Assert.False(result);
}
#endregion

Expand All @@ -35,7 +35,7 @@ public void GivenValidPassword_WhenValidatePasswordWithCompiledRegExIsInvoked_Th
var result = PasswordValidator.ValidatePasswordWithCompiledRegEx(ValidPassword);

// Assert
result.Should().BeTrue();
Assert.True(result);
}

[Fact]
Expand All @@ -45,7 +45,7 @@ public void GivenInvalidPassword_WhenValidatePasswordWithCompiledRegExIsInvoked_
var result = PasswordValidator.ValidatePasswordWithCompiledRegEx(InvalidPassword);

// Assert
result.Should().BeFalse();
Assert.False(result);
}
#endregion

Expand All @@ -57,7 +57,7 @@ public void GivenValidPassword_WhenValidatePasswordWithSourceGeneratedRegExIsInv
var result = PasswordValidator.ValidatePasswordWithSourceGeneratedRegEx(ValidPassword);

// Assert
result.Should().BeTrue();
Assert.True(result);
}

[Fact]
Expand All @@ -67,7 +67,73 @@ public void GivenInvalidPassword_WhenValidatePasswordWithSourceGeneratedRegExIsI
var result = PasswordValidator.ValidatePasswordWithSourceGeneratedRegEx(InvalidPassword);

// Assert
result.Should().BeFalse();
Assert.False(result);
}
#endregion
}

#region ValidatePasswordWithStaticRegEx
[Fact]
public void GivenValidPassword_WhenValidatePasswordWithStaticRegExIsInvoked_ThenTrueIsReturned()
{
// Act
var result = PasswordValidator.ValidatePasswordWithStaticRegEx(ValidPassword);

// Assert
Assert.True(result);
}

[Fact]
public void GivenInvalidPassword_WhenValidatePasswordWithStaticRegExIsInvoked_ThenFalseIsReturned()
{
// Act
var result = PasswordValidator.ValidatePasswordWithStaticRegEx(InvalidPassword);

// Assert
Assert.False(result);
}
#endregion

#region ValidatePasswordWithNewCompiledRegEx
[Fact]
public void GivenValidPassword_WhenValidatePasswordWithNewCompiledRegExIsInvoked_ThenTrueIsReturned()
{
// Act
var result = PasswordValidator.ValidatePasswordWithNewCompiledRegEx(ValidPassword);

// Assert
Assert.True(result);
}

[Fact]
public void GivenInvalidPassword_WhenValidatePasswordWithNewCompiledRegExIsInvoked_ThenFalseIsReturned()
{
// Act
var result = PasswordValidator.ValidatePasswordWithNewCompiledRegEx(InvalidPassword);

// Assert
Assert.False(result);
}
#endregion

#region ValidatePasswordWithSourceGeneratedRegExProperty
[Fact]
public void GivenValidPassword_WhenValidatePasswordWithSourceGeneratedRegExPropertyIsInvoked_ThenTrueIsReturned()
{
// Act
var result = PasswordValidator.ValidatePasswordWithSourceGeneratedRegExProperty(ValidPassword);

// Assert
Assert.True(result);
}

[Fact]
public void GivenInvalidPassword_WhenValidatePasswordWithSourceGeneratedRegExPropertyIsInvoked_ThenFalseIsReturned()
{
// Act
var result = PasswordValidator.ValidatePasswordWithSourceGeneratedRegExProperty(InvalidPassword);

// Assert
Assert.False(result);
}
#endregion
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.12" />
<PackageReference Include="BenchmarkDotNet" Version="0.15.8" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,35 @@ public static bool ValidatePasswordWithRegularRegEx(string password)
return regex.IsMatch(password);
}

public static bool ValidatePasswordWithStaticRegEx(string password)
=> Regex.IsMatch(
password,
@"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^\da-zA-Z]).{8,}$");

public static bool ValidatePasswordWithCompiledRegEx(string password)
=> Regex.IsMatch(
password,
@"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^\da-zA-Z]).{8,}$",
RegexOptions.Compiled);

public static bool ValidatePasswordWithNewCompiledRegEx(string password)
{
var regex = new Regex(
@"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^\da-zA-Z]).{8,}$",
RegexOptions.Compiled);

return regex.IsMatch(password);
}

public static bool ValidatePasswordWithSourceGeneratedRegEx(string password)
=> PasswordRegEx().IsMatch(password);

public static bool ValidatePasswordWithSourceGeneratedRegExProperty(string password)
=> PasswordRegExProperty.IsMatch(password);

[GeneratedRegex(@"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^\da-zA-Z]).{8,}$")]
private static partial Regex PasswordRegEx();

[GeneratedRegex(@"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^\da-zA-Z]).{8,}$")]
private static partial Regex PasswordRegExProperty { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,23 @@ public class RegexBenchmarks
private const string Password = "c0d3-MaZ3-Pa55w0rd";

[Benchmark(Baseline = true)]
public void RegularRegex()
public bool RegularRegex()
=> PasswordValidator.ValidatePasswordWithRegularRegEx(Password);

[Benchmark]
public void CompiledRegex()
public bool StaticRegex()
=> PasswordValidator.ValidatePasswordWithStaticRegEx(Password);

[Benchmark]
public bool CompiledRegex()
=> PasswordValidator.ValidatePasswordWithCompiledRegEx(Password);

[Benchmark]
public void SourceGeneratedRegex()
public bool NewCompiledRegex()
=> PasswordValidator.ValidatePasswordWithNewCompiledRegEx(Password);

[Benchmark]
public bool SourceGeneratedRegex()
=> PasswordValidator.ValidatePasswordWithSourceGeneratedRegEx(Password);

private class StyleConfig : ManualConfig
Expand Down
Loading