其他分享
首页 > 其他分享> > 我在AutoFixture ISpecimenBuilder中使用RegEx时,为什么总是返回相同的值?

我在AutoFixture ISpecimenBuilder中使用RegEx时,为什么总是返回相同的值?

作者:互联网

我的构建器设置为处理参数或属性.将来可能会改变,但是现在这就是我在构建器中拥有的:

public class UserNameBuilder : ISpecimenBuilder
{
    public object Create(object request, ISpecimenContext context)
    {
       var propertyInfo = request as PropertyInfo;
        if (propertyInfo != null && propertyInfo.Name == "UserName" && propertyInfo.PropertyType == typeof(string))
        {
            return GetUserName();
        }

        var parameterInfo = request as ParameterInfo;
        if (parameterInfo != null && parameterInfo.Name == "userName" && parameterInfo.ParameterType == typeof(string))
        {
            return GetUserName();
        }

        return new NoSpecimen(request);
    }

    static object GetUserName()
    {
        var fixture = new Fixture();
        return new SpecimenContext(fixture).Resolve(new RegularExpressionRequest(@"^[a-zA-Z0-9_.]{6,30}$"));
    }
}

我的UserName对象是ValueType对象,如下所示:

public class UserName : SemanticType<string>
{
    private static readonly Regex ValidPattern = new Regex(@"^[a-zA-Z0-9_.]{6,30}$");

    public UserName(string userName) : base(IsValid, userName)
    {
        Guard.NotNull(() => userName, userName);
        Guard.IsValid(() => userName, userName, IsValid, "Invalid username");
    }

    public static bool IsValid(string candidate)
    {
        return ValidPattern.IsMatch(candidate);
    }

    public static bool TryParse(string candidate, out UserName userName)
    {
        userName = null;

        try
        {
            userName = new UserName(candidate);
            return true;
        }
        catch (ArgumentException ex)
        {
            return false;
        }
    }
}

UserName类继承自SemanticType,SemanticType是一个为我的值类型提供基础的项目.

每当我如下使用AutoFixture时:

var fixture = new Fixture();
fixture.Customizations.Add(new UserNameBuilder());

var userName = fixture.Create<UserName>();

我总是得到“ ……”的值,我以为每次都会得到不同的值.我看到的是预期的行为吗?

解决方法:

如果可能,请输入favor negated character classes instead of the dot,然后尝试use the dot sparingly

^([a-zA-Z0-9]|[._](?![.])){6,30}$

上面的正则表达式匹配的文本也与有问题的原始文本匹配,例如nik_s.bax_vanis.

它还使AutoFixture也生成不同的文本(对不起,我的F#):

// PM> Install-Package Unquote
// PM> Install-Package AutoFixture
// PM> Install-Package FsCheck.Xunit

open FsCheck
open FsCheck.Xunit
open Ploeh.AutoFixture
open Ploeh.AutoFixture.Kernel
open Swensen.Unquote

[<Property>]
let ``Generated strings from RegEx are not all the same`` (PositiveInt count) =
    let fixture = new Fixture()
    let context = new SpecimenContext(fixture)

    let results = seq {
        for i in 1 .. count do
            yield context.Resolve(
                new RegularExpressionRequest("^([a-zA-Z0-9]|[._](?![.])){6,30}$")) }

    let threshold = count / 10
    results |> Seq.distinct |> Seq.length >! threshold

原始的正则表达式很好. –引擎会多次重复点:

^[a-zA-Z0-9_.]{6,30}$

标签:autofixture,unit-testing,c,regex
来源: https://codeday.me/bug/20191119/2038906.html