File: Util.hs

package info (click to toggle)
haskell-bimap 0.5.0-3
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 108 kB
  • sloc: haskell: 745; makefile: 6
file content (53 lines) | stat: -rw-r--r-- 1,405 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
{-# LANGUAGE TemplateHaskell #-}
module Test.Util (
    extractTests,
) where

import Control.Arrow
import Data.List
import Language.Haskell.TH
import Language.Haskell.TH.Syntax
import Test.QuickCheck
import Text.Printf


{-
Use 'propertyNames' to extract all QuickCheck test names from
a file.
-}
fileProperties :: FilePath -> IO [String]
fileProperties = fmap propertyNames . readFile

{-
Find all the tokens in a file that
  1) are the first token on a line, and
  2) begin with "prop_".
-}
propertyNames :: String -> [String]
propertyNames = 
    lines >>> map firstToken >>> filter isProperty >>> nub
    where
    firstToken = fst . head . lex
    isProperty = isPrefixOf "prop_"

resultIsSuccess Success {} = True
resultIsSuccess _ = False

mkCheck' name = [| printf "%-25s : " name
                   >> quickCheckResult $(varE (mkName name))
                   >>= return . resultIsSuccess |]
mkChecks' [] = undefined
mkChecks' [name] = mkCheck' name
mkChecks' (name:ns) = [| do
                          this <- $(mkCheck' name)
                          rest <- $(mkChecks' ns)
                          return $ this && rest |]


{-
Extract the names of QuickCheck tests from a file, and splice in
a sequence of calls to them. The module doing the splicing must
also import the file being processed.
-}
extractTests :: FilePath -> Q Exp
extractTests = (mkChecks' =<<) . runIO . fileProperties