2017-04-17 52 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

相關問題