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



我在C#中使用FsCheck,我想通过拥有100个ArrayList来生成ArrayList的任意ArrayList来执行PropertyBasedTest。我有这个 ArrayList,其中每个元素都定义了任意(它们无法更改( -

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

如何获取此数组列表的任意值?

基于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);
}
}

最新更新