Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The <a href="http://hackage.haskell.org/packages/archive/QuickCheck/2.1.0.2/doc/html/Test-QuickCheck-Monadic.html" rel="noreferrer">Test.QuickCheck.Monadic</a> module lets you test monadic code, even things that run in <code>IO</code>.</p> <p>A monadic property test is of type <code>PropertyM m a</code>, where <code>m</code> is the monad the test runs in and <code>a</code> is ultimately ignored. In the case of <code>PropertyM IO a</code>, you convert the monadic test to a <code>Property</code> by using <code>monadicIO</code>; for all other monads, you use <code>monadic</code> instead (which takes a function to run the monad, something <code>IO</code> doesn't have).</p> <p>In a monadic test, the value <code>return</code>ed out of the monad is ignored. To check an expression, use <code>assert</code>; <code>assert</code>ing a false value will fail the test. Use <code>run</code> to execute the code in the monad being tested.</p> <p>There are other monadic actions at your disposal. For example, <code>pick</code> will generate new test inputs out of a <code>Gen a</code>, and <code>pre</code> will check test preconditions. These are useful if the test inputs or preconditions themselves depend on values computed via the monad being tested, in which case the normal way of generating inputs or checking precontions won't work.</p> <p>Here's an example of testing some <code>IO</code> code: we check that after writing something to a temporary file, we can read that same data back. For demonstration purposes, we'll impose the precondition that we write at least one byte to the file. The two test properties do the same thing; one uses <code>pick</code> and <code>pre</code> unnecessarily while the other does not.</p> <pre><code>import System.Directory (removeFile) import System.IO (hGetContents, hPutStr, hSeek, openBinaryTempFile, SeekMode (..)) import Test.QuickCheck (arbitrary, Property, quickCheck, (==&gt;)) import Test.QuickCheck.Monadic (assert, monadicIO, pick, pre, run) -- Demonstrating pick and pre as well: prop_writeThenRead :: Property prop_writeThenRead = monadicIO $ do writtenData &lt;- pick arbitrary pre $ not (null writtenData) readData &lt;- run $ writeThenRead writtenData assert $ writtenData == readData -- A more idiomatic way to write the above: prop_writeThenRead2 :: [Char] -&gt; Property prop_writeThenRead2 writtenData = not (null writtenData) ==&gt; monadicIO test where test = do readData &lt;- run $ writeThenRead writtenData assert $ writtenData == readData writeThenRead :: [Char] -&gt; IO [Char] writeThenRead output = do (path, h) &lt;- openBinaryTempFile "/tmp" "quickcheck.tmp" removeFile path hPutStr h output hSeek h AbsoluteSeek 0 hGetContents h main :: IO () main = do quickCheck prop_writeThenRead quickCheck prop_writeThenRead2 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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