Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoiding build dependency on QuickCheck when declaring an Arbitrary instance

Assume I have a Haskell module named Foo, defined in src/Foo.hs. Assume also that Foo exports a type Bar.

Now I want to write unit tests for Bar (for the whole Foo module, actually), so I throw a couple of QuickCheck properties into test/FooTest.hs; but hey, now I need to define an Arbitrary instance for Bar.

And there's the rub: in -Wall -Werror mode, ghc requires instance declarations to appear in one of two places: in the same file where the type is defined, or where the class is defined. But I don't want to clutter my Foo module with a build dependency on QuickCheck, and I obviously cannot add an instance of Bar to QuickCheck.

So how do I make my datatype an instance of Arbitrary, for the purpose of unit testing only, without introducing a dependency on QuickCheck for users of my module and without tossing-Wall -Werror out of the window?

like image 690
Arek' Fu Avatar asked Dec 19 '22 15:12

Arek' Fu


1 Answers

Within the test suite, create a newtype which wraps Bar and define the Arbitrary instance for the newtype.

like image 106
danidiaz Avatar answered Dec 29 '22 10:12

danidiaz