我正在编写FSCheck生成器以创建具有以下属性的字符串:

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

  • 这是我的生成器代码:
    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源代码,应该通过发现过滤器找到我定义的成员,该方法似乎类似于类似的内置对象(例如NonEmptyString)。

    我错过了什么?谢谢!

    最佳答案

    您传递的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错误消息也试图告诉您以下信息:

    检查类型是否为public,并且具有带有正确签名的public static成员。

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

    关于f# - 如何定义FSCheck生成器以便可以发现它,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43453621/

    10-13 04:46