2017-04-17 54 views
3

我正在写一个FSCheck生成器创建具有以下属性字符串:如何定义一个FSCheck发电机,以便它可以被发现

  • 他们都是非空
  • 修剪它们不会影响长度
  • 它们不包含空格。

这里是我的生成代码:

namespace Example 

open FsCheck.Arb 

module public Generation = 

    let hasChars (s : string) = 
     (isNull s |> not) 
     && s.Length > 0 

    let isTrimmed (s : string) = 
     s.Trim().Length = s.Length 

    let isContinuous (s : string) = 
     s 
     |> Seq.exists ((=) ' ') 
     |> not 

    [<AbstractClass; Sealed>] 
    type public Generators = class end 

    type public ContinuousString = ContinuousString of string with 
     member x.Get = match x with ContinuousString r -> r 
     override x.ToString() = x.Get 

    type public Generators with 

     static member ContinuousString() = 
      Default.String() 
      |> filter hasChars 
      |> filter isTrimmed 
      |> filter isContinuous 
      |> convert ContinuousString string 

而这里的目的是验证生成的测试:

[<Property(Arbitrary=[| typeof<ContinuousString> |], MaxTest=2)>] 
let ``A continuous string contains no spaces`` (s: ContinuousString) = 
    s.Get.Contains " " |> not 

当我运行这个测试,我得到:

System.Exception: No instances found on type Example.Generation+ContinuousString. Check that the type is public and has public static members with the right signature. 

据我可以告诉看FSCheck的源代码,我有de成员罚款应该被发现过滤器发现,并且该方法似乎类似于类似的内置例如NonEmptyString

我错过了什么?谢谢!

+1

我认为'typeof '应该是'typeof '。此外,我不认为你需要定义它两次:'类型公共Generators =类结束'定义没有为你做,AFAIK。 – rmunn

+0

另外:'isNull s |> not'可能只是''。另外:''ContinuousString.Get'是一个属性,但名字就像一个方法。另外:您实际上不需要'Get',您可以将测试函数的参数定义为'(ContinuousString s)'而不是'(s:ContinuousString)'。 –

+0

太好了,谢谢@rmunn!修正的'typeof <>'固定它。空的类型就在那里,所以我尽可能地复制了FSCheck自己的代码 - 我没有想到它会产生效果。如果您想将您的评论转换为答案,我会将其标记为已接受。再次感谢! – Kit

回答

5

您正在传递FsCheck错误的类型。你应该通过你的Generators班,而不是你的ContinuousString DU。也就是说,这样的:

[<Property(Arbitrary=[| typeof<ContinuousString> |], MaxTest=2)>] 
let ``A continuous string contains no spaces`` (s: ContinuousString) = 
    s.Get.Contains " " |> not 

应该是:

[<Property(Arbitrary=[| typeof<Generators> |], MaxTest=2)>] 
let ``A continuous string contains no spaces`` (s: ContinuousString) = 
    s.Get.Contains " " |> not 

的FsCheck错误消息想告诉你这还有:

检查类型是公众和具有公共具有正确签名的静态成员。

您创建的类型与您要找的匹配的是Generators

相关问题