【问题标题】:FsCheck: Generate Arbitrary of a ArrayList with its elements as Arbitraries in C#FsCheck:在 C# 中生成 ArrayList 的任意元素,其元素为任意元素
【发布时间】:2019-01-24 17:57:18
【问题描述】:
我在 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 的任意值?
【问题讨论】:
标签:
fscheck
property-based-testing
【解决方案1】:
基于 Mark Seemann 链接的the example,我创建了一个完整的编译示例。与链接相比,它并没有提供太多额外的功能,但将来不会有被破坏的风险。
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);
}
}