Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>[my program] scans the declarations for variable names that start prop_, and builds a test suite containing them. Is there some other pure Haskell way I could do this instead, without directly munging source files?</p> </blockquote> <p>Yes, there is! Using <a href="http://hackage.haskell.org/packages/archive/language-haskell-extract/0.2.1/doc/html/Language-Haskell-Extract.html" rel="nofollow">the <code>language-haskell-extract</code> package</a>.</p> <pre><code>{-# LANGUAGE TemplateHaskell #-} import Language.Haskell.Extract import Test.QuickCheck prop_foo xs = reverse (reverse xs) == (xs :: [Int]) prop_bar = 2 + 2 == 4 properties = $(functionExtractorMap "^prop_" [|\name prop -&gt; putStrLn name &gt;&gt; quickCheck prop|]) main = sequence_ properties </code></pre> <p>Running this, we get:</p> <pre><code>prop_foo +++ OK, passed 100 tests. prop_bar +++ OK, passed 100 tests. </code></pre> <hr> <p>However, before you go reinventing the wheel I would also recommend you take a look at <a href="http://hackage.haskell.org/package/test-framework-th" rel="nofollow">the <code>test-framework-th</code> package</a>, which does pretty much exactly this, but also supports HUnit and has a nice test runner (with colors!).</p> <pre><code>{-# LANGUAGE TemplateHaskell #-} import Test.Framework.Providers.HUnit import Test.Framework.Providers.QuickCheck2 import Test.Framework.TH import Test.HUnit import Test.QuickCheck prop_bar = 1+1 == 2 case_foo = 2+2 @?= 4 main = $(defaultMainGenerator) </code></pre> <p>Output:</p> <pre><code>Main: bar: [OK, passed 100 tests] foo: [OK] Properties Test Cases Total Passed 1 1 2 Failed 0 0 0 Total 1 1 2 </code></pre> <p>There's also a <code>testGroupGenerator</code> which is useful if you want to combine tests from multiple files.</p>
 

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