2017-07-06 39 views
1

我有一个属性,我想测试一个Stuff的集合,其中一个Stuff满足某个属性。我有一种方法可以生成满足属性的Stuff,并且可以生成不包含Stuff的方法。将两台发电机组合成一个任意的FsCheck

今天,我在做这样的事情(是的,我在C#中使用FsCheck):

IEnumerable<Stuff> GetStuffCollection(int input) 
{ 
    yield return GenerateStuffSatisfyingProperty(input); 
    yield return GenerateStuffNotSatisfyingProperty(input); 
} 

[Fact] 
public void PropertyForCollectionHolds() 
{ 
    Prop.ForAll(Arb.Choose(1,5), input => 
    { 
     var collection = GetStuffCollection(input); 

     return collection.SatisfiesProperty(); 
    }).VerboseCheckThrowOnFailure(); 
} 

但这种硬编码的顺序,即其Stuff满足属性的集合;我也想仲裁这一点。

一种方法是嵌套Prop.ForAll调用;外一个生成的东西,其确定顺序,和一个内其一是一个I具有上述而是将参数传递控制排序到集合助洗剂:

IEnumerable<Stuff> GetStuffCollection(int input, bool first) 
{ 
    if (first) 
    { 
     yield return GenerateStuffSatisfyingProperty(input); 
     yield return GenerateStuffNotSatisfyingProperty(input); 
    } 
    else 
    { 
     yield return GenerateStuffNotSatisfyingProperty(input); 
     yield return GenerateStuffSatisfyingProperty(input); 
    } 
} 

[Fact] 
public void PropertyForCollectionHolds() 
{ 
    Prop.ForAll(Arb.Default.Bool(), first => 
     Prop.ForAll(Arb.Choose(1,5), input => 
     { 
      var collection = GetStuffCollection(input, first); 

      return collection.SatisfiesProperty(); 
     }).VerboseCheckThrowOnFailure() 
    ).VerboseCheckThrowOnFailure(); 
} 

但这感觉klunky和旋绕。是否有一种更简单和/或更为惯用的方式来实现同样的事情,即测试两个仲裁结果的笛卡儿积?

回答

0

您可以使用Gen.Shuffle来生成不同的顺序序列:

var gen = from input in Gen.Choose(1, 5) 
      let sc = GetStuffCollection(input) 
      from shuffled in Gen.Shuffle(sc) 
      select shuffled 

然后

Prop.ForAll(gen, collection => { ... })