Weired output when using newlines in error messages
Nick Smallbone
nick.smallbone at gmail.com
Tue Jan 24 15:31:15 GMT 2012
Hi Simon,
On 24 January 2012 13:45, Simon Hengel <sol at typeful.net> wrote:
> Hi,
> when I use a multi-line error message to indicate a failed test, I get
> wired output on shrinking.
>
> Here is an example:
>
> import Test.QuickCheck.Property
>
> prop_foo, prop_bar :: Int -> Result
> prop_foo n = failed {reason = "\nexpected: 23\n but got: " ++ show n}
> prop_bar n = failed {reason = "expected: 23, but got: " ++ show n}
What happens is that QuickCheck prints the error reason when it first
finds a counterexample, then starts shrinking. Whenever it
successfully shrinks the counterexample, it backspaces over the old
reason to erase it and then prints the new reason. However,
backspacing can only erase the current line so if you have a
multi-line error message then only the last line is erased each time.
The "normal" way to get an error message from a property is just to
use error: this will remove newlines from the message so that it fits
onto one line. But it should work to construct the Result directly!
I've pushed a couple of patches that fix this: during shrinking, the
message is squeezed to fit on one line, and at the end the whole error
message is printed in full. (Single-line error messages are printed as
part of the "*** Failed!" line, as before, but multi-line error
messages are printed separately.)
You might also like the printTestCase function. It includes a string
as part of the counterexample when a property fails. It copes fine
with newlines, and QuickCheck won't meddle with the formatting. So,
for example,
prop_foo n = printTestCase ("expected: 23\nbut got: " ++ show n) False
will give
> quickCheck prop_foo
*** Failed! Falsifiable (after 1 test and 1 shrink):
0
expected: 23
but got: 0
Nick
> And here is a ghci session, that illustrates the issue:
>
> ghci> quickCheck prop_foo
> *** Failed!
> expected: 23
> expected: 23
> expected: 23
> expected: 23
> but got: 0 (after 1 test and 2 shrinks):
> 0
>
> ghci> quickCheck prop_bar
> *** Failed! expected: 23, but got: 0 (after 1 test and 1 shrink):
> 0
>
> `quickcheck prop_foo` produces redundant output, `quickcheck prop_bar`
> works just fine.
>
> Is this a know issue?
>
> Cheers,
> Simon
>
> PS: What about setting up a proper issue tracker for QuickCheck (or
> maybe just use GitHub)? I would be happy to help with this.
>
> _______________________________________________
> QuickCheck mailing list
> QuickCheck at projects.haskell.org
> http://projects.haskell.org/cgi-bin/mailman/listinfo/quickcheck
More information about the QuickCheck
mailing list