如何为FsCheck测试生成空字符串

编程入门 行业动态 更新时间:2024-10-26 22:22:27
本文介绍了如何为FsCheck测试生成空字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

使用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测试生成空字符串

本文发布于:2023-10-08 13:54:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1472825.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:何为   测试   空字符串   FsCheck

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!