首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何限制FsCheck中的输入值?

如何限制FsCheck中的输入值?
EN

Stack Overflow用户
提问于 2019-12-13 03:40:28
回答 1查看 101关注 0票数 0

我有一个FsCheck属性,如下所示:

代码语言:javascript
复制
[Property]
public Property ConversionCanBeInversedForAllUnits(double input, string unit1, string unit2) 
{ ... }

FsCheck会给它提供随机值。我希望将字符串的输入限制为某个固定集合的成员。

显而易见的解决方案是:

代码语言:javascript
复制
[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);
}

我不认为这样使用是刻意的。

第二次尝试没有成功。首先,我添加了一些类

代码语言:javascript
复制
  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);
    }
}

并将该属性更改为

代码语言:javascript
复制
[Property(Arbitrary = new Type[] { typeof(MyTestDataCreator) })]

这就给我留下了一个例外

代码语言:javascript
复制
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.

这就引出了一个问题:什么是正确的签名?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-12-13 17:22:36

lengthunits()有正确的签名:返回一个Arbitrary实例和一个不带参数的方法。

尝试通过在Gen<T>上调用ToArbitrary()将其转换为Arbitrary<T>

如果你想写缩略器,Arbitrary会变得很有用。任意=生成器+收缩器。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59311673

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档