FsCheck:在 C# 中生成 ArrayList 的 Arbitrary,其元素作为 Arbitraries

FsCheck: Generate Arbitrary of a ArrayList with its elements as Arbitraries in C#

我在 C# 中使用 FsCheck,我想通过拥有 100 个 ArrayList 来生成 ArrayList 的 Arbitrary 以进行 PropertyBasedTesting。我有这个 ArrayList,其中每个元素都定义了 Arbitraries(它们不能更改)-

例如 System.Collections.ArrayList a = new System.Collections.ArrayList(); a.Add(Gen.Choose(1, 55)); a.Add(Arb.Generate<int>()); a.Add(Arb.Generate<string>())

如何获取此 ArrayList 的 Arbitrary?

基于 Mark Seemann 编写的 the example link,我创建了一个完整的编译示例。与 link 相比,它并没有提供太多额外功能,但不会有在未来被破坏的风险。

using System.Collections;
using FsCheck;
using FsCheck.Xunit;
using Xunit;
public class ArrayListArbritary
{
    public static Arbitrary<ArrayList> ArrayList() =>
        (from e1 in Gen.Choose(1, 15)
         from e2 in Arb.Generate<int>()
         from e3 in Arb.Generate<string>()
         select CreateArrayList(e1, e2, e3))
        .ToArbitrary();

    private static ArrayList CreateArrayList(params object[] elements) => new ArrayList(elements);
}

public class Tests
{
    public Tests()
    {
        Arb.Register<ArrayListArbritary>();
    }

    [Property]
    public void TestWithArrayList(ArrayList arrayList)
    {
        Assert.Equal(3, arrayList.Count);
    }
}