我正在编写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
Run Code Online (Sandbox Code Playgroud)
这是一个旨在验证这一代的测试:
[<Property(Arbitrary=[| typeof<ContinuousString> |], MaxTest=2)>]
let ``A continuous string contains no spaces`` (s: ContinuousString) =
s.Get.Contains " " |> not
Run Code Online (Sandbox Code Playgroud)
当我运行此测试时,我得到:
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.
Run Code Online (Sandbox Code Playgroud)
据我所知,查看FSCheck源代码,我已定义的成员应该由发现过滤器找到,并且该方法似乎类似于类似的内置类型,例如NonEmptyString.
我错过了什么?谢谢!
你传递的FsCheck是错误的类型.你应该把它传给你的Generators班级,而不是你的ContinuousStringDU.即,这个:
[<Property(Arbitrary=[| typeof<ContinuousString> |], MaxTest=2)>]
let ``A continuous string contains no spaces`` (s: ContinuousString) =
s.Get.Contains " " |> not
Run Code Online (Sandbox Code Playgroud)
本来应该:
[<Property(Arbitrary=[| typeof<Generators> |], MaxTest=2)>]
let ``A continuous string contains no spaces`` (s: ContinuousString) =
s.Get.Contains " " |> not
Run Code Online (Sandbox Code Playgroud)
FsCheck错误消息也试图告诉你:
检查类型是否为公共类型,并且具有正确签名的公共静态成员.
您创建的匹配其所需类型的类型Generators.