Debian Codegraph @

test/hs/Test/Ganeti/TestHelper.hs

raw
1 {-# LANGUAGE TemplateHaskell #-}
2
3 {-| Unittest helpers for TemplateHaskell components.
4
5 -}
6
7 {-
8
9 Copyright (C) 2011, 2012, 2013 Google Inc.
10 All rights reserved.
11
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions are
14 met:
15
16 1. Redistributions of source code must retain the above copyright notice,
17 this list of conditions and the following disclaimer.
18
19 2. Redistributions in binary form must reproduce the above copyright
20 notice, this list of conditions and the following disclaimer in the
21 documentation and/or other materials provided with the distribution.
22
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
24 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
27 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34
35 -}
36
37 module Test.Ganeti.TestHelper
38 ( testSuite
39 , genArbitrary
40 ) where
41
42 import Data.List (stripPrefix, isPrefixOf)
43 import Data.Maybe (fromMaybe)
44 import Test.Framework
45 import Test.Framework.Providers.HUnit
46 import Test.Framework.Providers.QuickCheck2
47 import Test.HUnit (Assertion)
48 import Test.QuickCheck
49 import Language.Haskell.TH
50 import Ganeti.THH.Compat
51
52 -- | Test property prefix.
53 propPrefix :: String
54 propPrefix = "prop_"
55
56 -- | Test case prefix.
57 casePrefix :: String
58 casePrefix = "case_"
59
60 -- | Test case prefix without underscore.
61 case2Pfx :: String
62 case2Pfx = "case"
63
64 -- | Tries to drop a prefix from a string.
65 simplifyName :: String -> String -> String
66 simplifyName pfx string = fromMaybe string (stripPrefix pfx string)
67
68 -- | Builds a test from a QuickCheck property.
69 runProp :: Testable prop => String -> prop -> Test
70 runProp = testProperty . simplifyName propPrefix
71
72 -- | Builds a test for a HUnit test case.
73 runCase :: String -> Assertion -> Test
74 runCase = testCase . simplifyName casePrefix
75
76 -- | Runs the correct test provider for a given test, based on its
77 -- name (not very nice, but...).
78 run :: Name -> Q Exp
79 run name =
80 let str = nameBase name
81 nameE = varE name
82 strE = litE (StringL str)
83 in case () of
84 _ | propPrefix `isPrefixOf` str -> [| runProp $strE $nameE |]
85 | casePrefix `isPrefixOf` str -> [| runCase $strE $nameE |]
86 | case2Pfx `isPrefixOf` str ->
87 [| (testCase . simplifyName case2Pfx) $strE $nameE |]
88 | otherwise -> fail $ "Unsupported test function name '" ++ str ++ "'"
89
90 -- | Convert slashes in a name to underscores.
91 mapSlashes :: String -> String
92 mapSlashes = map (\c -> if c == '/' then '_' else c)
93
94 -- | Builds a test suite.
95 testSuite :: String -> [Name] -> Q [Dec]
96 testSuite tsname tdef = do
97 let fullname = mkName $ "test" ++ mapSlashes tsname
98 tests <- mapM run tdef
99 sigtype <- [t| Test |]
100 body <- [| testGroup $(litE $ stringL tsname) $(return $ ListE tests) |]
101 return [ SigD fullname sigtype
102 , ValD (VarP fullname) (NormalB body) []
103 ]
104
105 -- | Builds an arbitrary value for a given constructor. This doesn't
106 -- use the actual types of the fields, since we expect arbitrary
107 -- instances for all of the types anyway, we only care about the
108 -- number of fields.
109 mkConsArbitrary :: (Name, [a]) -> Exp
110 mkConsArbitrary (name, types) =
111 let infix_arb a = InfixE (Just a) (VarE '(<*>)) (Just (VarE 'arbitrary))
112 constr = AppE (VarE 'pure) (ConE name)
113 in foldl (\a _ -> infix_arb a) constr types
114
115 -- | Extracts the name and the types from a constructor.
116 conInfo :: Con -> (Name, [Type])
117 conInfo (NormalC name t) = (name, map snd t)
118 conInfo (RecC name t) = (name, map (\(_, _, x) -> x) t)
119 conInfo (InfixC t1 name t2) = (name, [snd t1, snd t2])
120 conInfo (ForallC _ _ subcon) = conInfo subcon
121
122 -- | Builds an arbitrary instance for a regular data type (i.e. not Bounded).
123 mkRegularArbitrary :: Name -> [Con] -> Q [Dec]
124 mkRegularArbitrary name cons = do
125 expr <- case cons of
126 [] -> fail "Can't make Arbitrary instance for an empty data type"
127 [x] -> return $ mkConsArbitrary (conInfo x)
128 xs -> appE (varE 'oneof) $
129 listE (map (return . mkConsArbitrary . conInfo) xs)
130 return [gntInstanceD [] (AppT (ConT ''Arbitrary) (ConT name))
131 [ValD (VarP 'arbitrary) (NormalB expr) []]]
132
133 -- | Builds a default Arbitrary instance for a type. This requires
134 -- that all members are of types that already have Arbitrary
135 -- instances, and that the arbitrary instances are well behaved
136 -- (w.r.t. recursive data structures, or similar concerns). In that
137 -- sense, this is not appropriate for all data types, just those that
138 -- are simple but very repetitive or have many simple fields.
139 genArbitrary :: Name -> Q [Dec]
140 genArbitrary name = do
141 r <- reify name
142 case r of
143 TyConI (DataD _ _ _ _ cons _) ->
144 mkRegularArbitrary name cons
145 TyConI (NewtypeD _ _ _ _ con _) ->
146 mkRegularArbitrary name [con]
147 TyConI (TySynD _ _ (ConT tn)) -> genArbitrary tn
148 _ -> fail $ "Invalid type in call to genArbitrary for " ++ show name
149 ++ ", type " ++ show r