[Haskell-cafe] Need advise with using Quickcheck

coot at coot.me coot at coot.me
Sat Jun 12 11:00:43 UTC 2021


I think you're looking for something like this:

```

prop_parser :: Foo -> Property

prop_parser foo  =

   case parserFoo pretty of

      Checked foo' [] | foo == foo' -> property True

      a  -> counterexample (show a ++ "\n" ++ show pretty) False

  where

    pretty = prettyFoo foo

```

Sent with ProtonMail Secure Email.

‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐

On Saturday, June 12th, 2021 at 12:49, Han Joosten <han.joosten.han at gmail.com> wrote:

> Thanks for this respons, Rubén. I might not have explained what I am looking for very well. I already have instance Arbitrary Foo That is not the problem. The problem is, that when a counterexample is found, it is dumped to the output with the `show` function. I would like to be able to use another function for that, say show'. In order to do so, I need to obtain the counterexample itself in some way, so I can use it to generate a useful message after the tests are done. 
> 

> Op vr 11 jun. 2021 om 23:31 schreef Ruben Astudillo <ruben.astud at gmail.com>:
> 

> > Hello Han
> > 

> > On 11-06-21 17:16, Han Joosten wrote:
> > 

> > > [..]
> > 

> > > *parseFoo :: Text -> Guarded Foo*
> > 

> > > *prettyFoo :: Foo -> Text*
> > 

> > >
> > 

> > > The idea is that for all foo :: Foo, the following holds:
> > 

> > >     *parseFoo . prettyFoo $ foo*  should be equal to  *Checked foo []*
> > 

> > >
> > 

> > > Now, if there is a counterexample, I would like to see prettyFoo foo as
> > 

> > > output, folowed by the show of the Errors part.
> > 

> > It seems you know what property should hold for `parseFoo` and `prettyFoo`.
> > 

> > Per the QuickCheck manual [1] this could be written as
> > 

> >     import Test.QuickCheck
> > 

> >     propOnAllFooText :: Property
> > 

> >     propOnAllFooText = forAll (arbitrary :: Gen Foo) $ \exFoo ->
> > 

> >         parseFoo (prettyFoo exFoo) == Checked exFoo []
> > 

> > What you are missing is a way to generate the test cases (the `arbitrary'
> > 

> > generator above). You need a way to declare this instance
> > 

> >     instance Arbitrary Foo where
> > 

> >         -- arbitrary :: Gen Foo
> > 

> >         arbitrary = <something>
> > 

> > You can see example on how to define it on the manual [1]. Good luck.
> > 

> > [1]: http://www.cse.chalmers.se/~rjmh/QuickCheck/manual.html
> > 

> > --
> > 

> > -- Rubén
> > 

> > -- pgp: 4EE9 28F7 932E F4AD
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20210612/23f80213/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 509 bytes
Desc: OpenPGP digital signature
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20210612/23f80213/attachment.sig>


More information about the Haskell-Cafe mailing list