使用Haskell QuickCheck测试库的F#版本 FsCheck 从C# ,我发现随机字符串生成器不会生成空字符串。
Using FsCheck, the F# version of the Haskell QuickCheck test library, to generate tests from C#, I found that the random string generator does not generate the null string.
using FsCheck.Fluent; Spec.ForAny<string>(s => s != null).QuickCheck(); // always pass此外,似乎没有设计处理空字符串的方法,但我没有做到从文档中将其固定下来。例如,仅在两个字符串(其中之一为null)之间进行选择将不起作用:
Furthermore, there seems not to handle null strings by design, but I have not managed to pin it down from the documentation. For example, just picking between two strings, one of them null, won't work:
var strings = Any.ValueIn<string>(null, "non-null string"); Spec.For(strings, s => true).QuickCheck(); // throws null ref exception字符串似乎是一种特殊情况,因为它可以处理定制的
And strings seem to be a special case, because it handles custom-made objects such as
class Thing {}当与空值混合时:
var objects = Any.ValueIn(null, new Thing()); Spec.For(objects, s => true).QuickCheck(); // pass推荐答案
我试图对此进行深入研究并且似乎您在FsCheck中发现了一个错误。
I tried to dig a bit into this and it appears that you have discovered a bug in FsCheck.
问题似乎出在文件Arbitrary.fs中,并且实际上仅与字符串有关。我必须替换掉它,他们在字符串上调用ToCharArray
It appears that the problem is in file Arbitrary.fs and is really only string-related. I had to replace this, where they call ToCharArray on the string
static member String() = { new Arbitrary<string>() with override x.Generator = Gen.map (fun chars -> new String(List.toArray chars)) generate override x.Shrinker s = s.ToCharArray() |> Array.toList |> shrink |> Seq.map (fun chars -> new String(List.toArray chars)) }具有此
static member String() = { new Arbitrary<string>() with override x.Generator = Gen.map (fun chars -> new String(List.toArray chars)) generate override x.Shrinker s = match s with | null -> seq {yield null;} | _ -> s.ToCharArray() |> Array.toList |> shrink |> Seq.map (fun chars -> new String(List.toArray chars)) }您可能想与fscheck开发人员在此处进行讨论,同时还要检查我的修复程序是否运作良好-也许有更好的方法来实现它,但是对于已经知道代码的人来说,它会更简单。
You may want to raise this with fscheck developers here and also check if my fix works well - there is probably a better way to implement it, but it would be simpler for someone, who already knows the code.
更多推荐
如何为FsCheck测试生成空字符串
发布评论