Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here's a fragment of the <code>.cabal</code> file I used for one of my recent <a href="https://github.com/scvalex/ltc/blob/master/ltc.cabal">libraries</a>.</p> <pre><code>... Library Build-depends: base &gt;= 4 &amp;&amp; &lt; 5, bytestring, directory, filepath, hslogger, SHA, zlib Ghc-options: -Wall Exposed-modules: Ltc.Store Test-suite reference Hs-Source-Dirs: Test, . Main-Is: ReferenceProps.hs Type: exitcode-stdio-1.0 Build-Depends: base &gt;= 4 &amp;&amp; &lt; 5, bytestring, directory, filepath, hslogger, SHA, zlib Ghc-Options: -Wall Build-Depends: test-framework, test-framework-hunit, test-framework-quickcheck2, HUnit, QuickCheck </code></pre> <p>As we can see the cabal file defines a library and a testsuite. The library defines the modules it exports, the packages it depends on, and sets some custom GHC options.</p> <p>We can easily build and package the library for distribution with:</p> <pre><code>% cabal configure % cabal build % cabal sdist </code></pre> <p>The testsuite looks a lot like the the library: first off, it has the same dependencies as the library (see the first <code>Build-Depends</code> line), and it then adds some extra test dependencies (see the second <code>Build-Depends</code> line). The testsuite here is a combination of HUnit and QuickCheck tests, and it uses <a href="http://hackage.haskell.org/package/test-framework">Test-Framework</a> as the runner. The <em>test</em> proper is <code>Test/ReferenceProps.hs</code>. It's a <code>exitcode-stdio</code> type test. This means that cabal will say that the tests pass if <code>ReferenceProps</code> exits with code 0. Otherwise, it will say the tests failed.</p> <p>The testsuite looks like <a href="https://github.com/scvalex/ltc/blob/master/Test/ReferenceProps.hs">this</a> (but, here, we're going to use some simple tests for list reversals):</p> <pre><code>import Data.Monoid import Test.Framework import Test.Framework.Providers.HUnit import Test.Framework.Providers.QuickCheck2 import Test.HUnit import Test.QuickCheck main :: IO () main = defaultMainWithOpts [ testCase "rev" testRev , testProperty "listRevRevId" propListRevRevId ] mempty testRev :: Assertion testRev = reverse [1, 2, 3] @?= [3, 2, 1] propListRevRevId :: [Int] -&gt; Property propListRevRevId xs = not (null xs) ==&gt; reverse (reverse xs) == xs </code></pre> <p>The <code>main</code> is just a harness. You can also set various options for <code>test-framework</code> by replacing the <code>mempty</code>. The function <code>testRev</code> is a HUnit test, and <code>propListRevRevId</code> is a QuickCheck test; see the <a href="http://hackage.haskell.org/package/HUnit">relevant</a> <a href="http://hackage.haskell.org/package/QuickCheck">docs</a> on how to write these.</p> <p>Finally, we can run the tests:</p> <pre><code>% cabal configure --enable-tests % cabal test </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload