1

我在 C# 中使用 FsCheck,我想通过拥有 100 个 ArrayList 来生成任意的 ArrayList 来执行 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 的任意值?

4

1 回答 1

1

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

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);
    }
}
于 2018-09-05T14:41:40.043 回答