[英]How do I improve QuickCheck and Parsec debugging?
I am using Haskell and Parsec to parse a file format. 我正在使用Haskell和Parsec来解析文件格式。 My parsing function looks something like:
我的解析函数类似于:
parseInput :: String -> Model
parseInput input = ...
data Model = Model { mNumV :: Int, mNumF :: Int, ... }
In order to test this, I am using QuickCheck. 为了测试这个,我使用的是QuickCheck。 I have defined an
Arbitrary
instance that generates a String
representing the contents of a formatted file: 我已经定义了一个
Arbitrary
实例,它生成一个表示格式化文件内容的String
:
instance Arbitrary File where
arbitrary = ...
data File = File { fContents :: String, fNumV :: Int, fNumF :: Int, ... }
One of my properties might check to determine if mNumV == fNumV
after parsing the arbitrary String
. 我的一个属性可能会在解析任意
String
后检查以确定mNumV == fNumV
。 This works well - when it works. 这很有效 - 当它工作时。
But if something fails, Parsec throws an error similar to: 但如果出现问题,Parsec会抛出类似于的错误:
*** Failed (after 1 test):
Exception:
(line 302, column 3):
unexpected "\n"
expecting space
This is useful - however, after the test fails the contents of the arbitrary file disappear. 这很有用 - 但是,在测试失败后,任意文件的内容都会消失。 I can't go in and reference line 302.
我不能进去参考302行。
The only alternative that I can see is to print the fContents
of each arbitrary file after each test - but that seems like a terrible idea. 我能看到的唯一选择是在每次测试后打印每个任意文件的
fContents
- 但这似乎是一个糟糕的主意。 The same goes for routing every arbitrary file to a file on disk for later reference. 将每个任意文件路由到磁盘上的文件以供以后参考也是如此。
Is there a common way around this? 这有什么共同的方法吗?
您可以使用whenFail
在失败时打印有问题的字符串(或将其转储到文件中)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.