Statistics
| Branch: | Tag: | Revision:

root / htools / Ganeti / HTools / QCHelper.hs @ 95f6c931

History | View | Annotate | Download (1.8 kB)

1
{-# LANGUAGE TemplateHaskell #-}
2

    
3
{-| Unittest helpers for ganeti-htools
4

    
5
-}
6

    
7
{-
8

    
9
Copyright (C) 2011, 2012 Google Inc.
10

    
11
This program is free software; you can redistribute it and/or modify
12
it under the terms of the GNU General Public License as published by
13
the Free Software Foundation; either version 2 of the License, or
14
(at your option) any later version.
15

    
16
This program is distributed in the hope that it will be useful, but
17
WITHOUT ANY WARRANTY; without even the implied warranty of
18
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19
General Public License for more details.
20

    
21
You should have received a copy of the GNU General Public License
22
along with this program; if not, write to the Free Software
23
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24
02110-1301, USA.
25

    
26
-}
27

    
28
module Ganeti.HTools.QCHelper
29
  ( testSuite
30
  ) where
31

    
32
import Data.List (stripPrefix)
33
import Data.Maybe (fromMaybe)
34
import Test.QuickCheck
35
import Test.Framework
36
import Test.Framework.Providers.QuickCheck2
37
import Language.Haskell.TH
38

    
39
-- | Tries to drop a prefix from a string.
40
simplifyName :: String -> String -> String
41
simplifyName pfx string = fromMaybe string (stripPrefix pfx string)
42

    
43
-- | Builds a test from a property and given arguments.
44
run :: Testable prop => String -> String -> prop -> Test
45
run pfx name = testProperty (simplifyName ("prop_" ++ pfx ++ "_") name)
46

    
47
-- | Builds a test suite.
48
testSuite :: String -> [Name] -> Q [Dec]
49
testSuite tsname tdef = do
50
  let fullname = mkName $ "test" ++ tsname
51
  tests <- mapM (\n -> [| run tsname
52
                          $(litE . StringL . nameBase $ n) $(varE n) |])
53
           tdef
54
  sigtype <- [t| (String, [Test]) |]
55
  return [ SigD fullname sigtype
56
         , ValD (VarP fullname) (NormalB (TupE [LitE (StringL tsname),
57
                                                ListE tests])) []
58
         ]