gpt4 book ai didi

unit-testing - 如何在 AutoFixture 中创建样本生成器以始终返回与 Regex 匹配的字符串?

转载 作者:行者123 更新时间:2023-12-04 02:21:31 24 4
gpt4 key购买 nike

我正在尝试在使用 AutoFixture 的单元测试中使用约定。我有一个密码值对象,如下所示:

public class Password : SemanticType<string>
{
private int _daysPasswordValid;

public Password(string password) : base(IsValid, password)
{
Guard.NotNullOrEmpty(() => password, password);
Guard.IsValid(() => password, password, IsValid, "Invalid Password");

DateCreated = DateTime.Now;
}

static bool IsValid(string candidate)
{
//password cannot contain be whitespace
if (string.IsNullOrWhiteSpace(candidate))
return false;

const string passwordPattern = @"^(?=.*[A-Z].*[A-Z])(?=.*[!@#$&=()-*])(?=.*[0-9].*[0-9])(?=.*[a-z].*[a-z].*[a-z]).{20}$";

var match = Regex.Match(candidate, passwordPattern);

return match.Success;
}

public DateTime DateCreated { get; private set; }

public int DaysPasswordValid
{
get { return _daysPasswordValid; }
set
{
const int minimunDays = 0;
const int maximumDays = 90;

if (value <= maximumDays && value > minimunDays)
_daysPasswordValid = value;
else
throw new ArgumentOutOfRangeException(
"Password must be valid more than {0} and no more than {1} days.".Fmt(minimunDays, maximumDays));
}
}

public static implicit operator string(Password password)
{
return password.Value;
}

public static explicit operator Password(string value)
{
return new Password(value);
}
}

我希望能够基于 Password 类创建一个固定装置,并使用有效密码创建固定装置,换句话说,一个与 Password 类中的 RegEx 模式匹配的密码。我一直在查看 AutoFixture 中可用的 RegularExpressionGenerator,但还没有完全发挥作用。到目前为止,这是构建器:

public class ValidPasswordBuilder : ISpecimenBuilder
{
public ValidPasswordBuilder(string regularExpressionRequest)
{
PasswordRegularExpression = regularExpressionRequest;
}

public string PasswordRegularExpression { get; private set; }

public object Create(object request, ISpecimenContext context)
{
var pi = request as ParameterInfo;
if (pi != null && pi.Name == "password" && pi.ParameterType == typeof(string))
{
var generator = new RegularExpressionGenerator();
var regExRequest = new RegularExpressionRequest(PasswordRegularExpression);
var result = generator.Create(regExRequest, context);
return result.ToString();
}

return new NoSpecimen(request);
}
}

这是测试:

[Fact]
public void ValidPasswordMatches()
{
var fixture = new Fixture();
fixture.Customizations.Add(new ValidPasswordBuilder(PasswordPattern));
Action a = () => fixture.Create<Password>();
a.ShouldNotThrow<ArgumentException>();
}

我可以按照上面的设置运行测试,它通过了,但是如果我调试它,我会在密码类中的 IsValid 函数中收到一个错误(通过 Guard 子句),表明从构建器返回的密码是:

Ploeh.AutoFixture.Kernel.NoSpecimen

我似乎从来没有得到与 RegEx 模式匹配的结果。我觉得我很接近,但需要一些帮助来克服困难。

谢谢!

最佳答案

看起来,您使用的模式不受支持:

^(?=.*[A-Z].*[A-Z])(?=.*[!@#$&=()-*])(?=.*[0-9].*[0-9])(?=.*[a-z].*[a-z].*[a-z]).{20}$

Regular expression visualization

尝试使用不同的正则表达式。

编辑:或尝试做 thisthis , 为了将 AutoFixture 配置为使用不同的正则表达式,而不更改 Password 类。


AutoFixture 将正则表达式转换为 Automatons通过应用 dk.brics.automaton 的算法.

您可以使用不同的模式或使用不同的引擎将正则表达式反转为自动机。例如,您可以使用 Rex引擎。

尽管如此,即使使用 Rex 也不支持您的正则表达式,因为 Rex 目前不支持以下结构:

锚定\G、\b、\B、命名组、前瞻、后视、尽可能少的量词、反向引用、条件交替、替换。


如果你想用 AutoFixture 试试 Rex,你可以使用下面的生成器:

internal class RexRegularExpressionGenerator : ISpecimenBuilder
{
public object Create(object request, ISpecimenContext context)
{
if (request == null)
return new NoSpecimen();

var regularExpressionRequest = request as RegularExpressionRequest;
if (regularExpressionRequest == null)
return new NoSpecimen();

var pattern = regularExpressionRequest.Pattern;

try
{
var regex = RexEngine
.GenerateMembers(
new RexSettings(pattern) { k = 1 })
.FirstOrDefault();

if (Regex.IsMatch(regex, pattern))
return regex;
}
catch (ArgumentException)
{
return new NoSpecimen(request);
}

return new NoSpecimen(request);
}
}

据我所知,您只能将 Rex 下载为 .NET 应用程序 (.exe) 文件,然后您可以像项目中的任何其他托管程序集一样引用它。

关于unit-testing - 如何在 AutoFixture 中创建样本生成器以始终返回与 Regex 匹配的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28706286/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com