C# 如何限制FsCheck中的输入值?
我有一个FsCheck属性,如下所示:C# 如何限制FsCheck中的输入值?,c#,fscheck,C#,Fscheck,我有一个FsCheck属性,如下所示: [Property] public Property ConversionCanBeInversedForAllUnits(double input, string unit1, string unit2) { ... } FsCheck将向其提供随机值。我想将字符串的输入限制为某个固定集的成员 显而易见的解决方案是: [Property] public Property ConversionCanBeInversedForAllUnits(doubl
[Property]
public Property ConversionCanBeInversedForAllUnits(double input, string unit1, string unit2)
{ ... }
FsCheck将向其提供随机值。我想将字符串的输入限制为某个固定集的成员
显而易见的解决方案是:
[Property]
public Property ConversionCanBeInversedForAllUnits(double input, int unit1Int, int unit2Int)
{
string unit1 = units[unit1Int % units.Lenght];
string unit2 = units[unit2Int % units.Lenght];
input = math.clamp(min, input, max);
}
我不认为用这种方式是缩进的
第二次尝试没有成功。
首先,我增加了一些类
public class MyTestDataCreator
{
private static string[] lengthUnits = new string[] { "m", "mm" };
public static Arbitrary<string> lenghtunits()
{
// Does not compile.
return FsCheck.Gen.Choose(0, lengthUnits.Length).Select(index => lengthUnits[index]);
}
public Gen<double> DoubleGen()
{
return FsCheck.Gen.Choose(0, 1000);
}
}
那就剩下我一个例外了
Message: System.Reflection.TargetInvocationException : Exception has been thrown by the target of an invocation.
---- System.Exception : No instances found on type Prop.MyTestDataCreator. Check that the type is public and has public static members with the right signature.
这就引出了一个问题:什么是正确的签名?lengthunits()
具有正确的签名:返回一个任意的
实例和一个没有参数的方法
通过调用ToArbitrary()
,尝试将Gen
转换为Arbitrary
如果您想编写收缩器,任意
将非常有用。任意=生成器+收缩器
Message: System.Reflection.TargetInvocationException : Exception has been thrown by the target of an invocation.
---- System.Exception : No instances found on type Prop.MyTestDataCreator. Check that the type is public and has public static members with the right signature.