在 AutoFixture ISpecimenBuilder 中使用 RegEx 时,为什么我总是得到相同的值?
When using a RegEx in AutoFixture ISpecimenBuilder, Why do I always get back the same value?
我的构建器设置为处理参数或 属性。这在未来可能会改变,但现在这是我在我的构建器中的内容:
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 class 继承自 SemanticType,这是一个为我的值类型提供基础的项目。
每当我按如下方式使用 AutoFixture 时:
var fixture = new Fixture();
fixture.Customizations.Add(new UserNameBuilder());
var userName = fixture.Create<UserName>();
我总是得到值“......”我以为我每次都会得到不同的值。我看到的是预期的行为吗?
如果可能的话,favor negated character classes instead of the dot
, and try to 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
原来的正则表达式没问题。 – 是 引擎 尽可能多地重复 dot
:
^[a-zA-Z0-9_.]{6,30}$
我的构建器设置为处理参数或 属性。这在未来可能会改变,但现在这是我在我的构建器中的内容:
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 class 继承自 SemanticType,这是一个为我的值类型提供基础的项目。
每当我按如下方式使用 AutoFixture 时:
var fixture = new Fixture();
fixture.Customizations.Add(new UserNameBuilder());
var userName = fixture.Create<UserName>();
我总是得到值“......”我以为我每次都会得到不同的值。我看到的是预期的行为吗?
如果可能的话,favor negated character classes instead of the dot
, and try to 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
原来的正则表达式没问题。 – 是 引擎 尽可能多地重复 dot
:
^[a-zA-Z0-9_.]{6,30}$