【问题标题】:How do I make use of FsCheck custom generators from C#?如何使用 C# 中的 FsCheck 自定义生成器?
【发布时间】:2018-01-19 20:22:11
【问题描述】:

我有以下代码:

var gen = from x in Arb.Generate<int>()
from int y in Gen.Choose(5, 10)
where x > 5
select new tuple { Fst = x, Snd = y };

我可以跑

Prop.ForAll<tuple>(c =>
      Console.WriteLine($"{c.Fst}, {c.Snd}")
).Check(Configuration.Default);

我看到了所有构造生成器和定义属性的方法。

但我只是没有足够快地找到如何将它们一起使用。

【问题讨论】:

    标签: fscheck


    【解决方案1】:

    您需要向 FsCheck 注册自定义生成器。见FSCheck docs

    简而言之,创建一个类来保存您的自定义生成器。有一个返回 Arbitrary&lt;T&gt; 的公共静态方法,其中 T 是您正在生成的类型。

    在您的示例中,您需要将生成器包装在对 Arb.From(...) 的调用中。

    public class MyGenerators {
        public static Arbitrary<tuple> Tuple() {
            return Arb.From(from x in Arb.Generate<int>()
                            from int y in Gen.Choose(5, 10)
                            where x > 5
                            select new tuple { Fst = x, Snd = y });
        }
    }
    

    最后,在运行测试之前致电Arb.Register&lt;MyGenerators&gt;()

    【讨论】:

      猜你喜欢
      • 2020-01-06
      • 1970-01-01
      • 2016-07-21
      • 2016-07-19
      • 1970-01-01
      • 1970-01-01
      • 2012-10-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多