8 import Test.QuickCheck.Batch
12 import System.IO.Unsafe
18 import Language.Haskell.Extension
20 newtype CmdOptions = CmdOptions {cmdOptions :: [String]}
23 instance Arbitrary CmdOptions where
24 arbitrary = resize 30 $ liftM CmdOptions arbitrary
25 coarbitrary = undefined
27 instance Arbitrary Char where
28 arbitrary = elements $ ['a'..'z']++['A'..'Z']
29 coarbitrary = undefined
31 data Options = Options
35 instance Arbitrary Options where
36 arbitrary = elements [Options,Options_GHC]
37 coarbitrary = undefined
39 -- Test that OPTIONS are correctly extracted from a buffer
40 -- with comments and garbage.
41 prop_optionsIdentity lowercase options cmds
43 all (all (not.null).cmdOptions) cmds ==>
44 concatMap cmdOptions cmds == map unLoc (getOptions buffer "somefile")
45 where buffer = unsafePerformIO $ stringToStringBuffer str
46 str = concatMap mkPragma cmds ++
47 "\n @#@# garbage #@#@ \n"
48 mkPragma (CmdOptions cmd)
49 = unlines [ "-- Pragma: "
50 , unwords $ ["{-#", pragma]++cmd++["#-}"]
51 , "{- End of pragma -}" ]
52 pragma = (if lowercase then map toLower else map toUpper) $
55 Options_GHC -> "OPTIONS_GHC"
57 newtype Extensions = Extensions [Extension]
60 instance Arbitrary Extensions where
61 arbitrary = resize 30 $ liftM Extensions arbitrary
62 coarbitrary = undefined
64 extensions :: [Extension]
65 extensions = [ OverlappingInstances
66 , UndecidableInstances
70 , MultiParamTypeClasses
71 , NoMonomorphismRestriction
72 , FunctionalDependencies
75 , PolymorphicComponents
76 , ExistentialQuantification
83 , TypeSynonymInstances
85 , ForeignFunctionInterface
93 , GeneralizedNewtypeDeriving
95 , RestrictedTypeSynonyms
98 -- derive Enum for Extension?
99 instance Arbitrary Extension where
100 arbitrary = elements extensions
101 coarbitrary = undefined
103 -- Test that we can parse all known extensions.
104 prop_languageParse lowercase (Extensions exts)
105 = not (null exts) ==>
106 not (isBottom (getOptions buffer "somefile"))
107 where buffer = unsafePerformIO $ stringToStringBuffer str
108 str = unlines [ "-- Pragma: "
109 , unwords $ ["{-#", pragma, ppExts exts "" , "#-}"]
110 , "{- End of pragma -}"
113 ppExts (x:xs) = shows x . showChar ',' . ppExts xs
115 pragma = (if lowercase then map toLower else map toUpper)
118 -- Test that invalid extensions cause exceptions.
119 prop_languageError lowercase ext
121 ext `notElem` map show extensions ==>
122 isBottom (foldr seq () (getOptions buffer "somefile"))
123 where buffer = unsafePerformIO $ stringToStringBuffer str
124 str = unlines [ "-- Pragma: "
125 , unwords $ ["{-#", pragma, ext , "#-}"]
126 , "{- End of pragma -}"
128 pragma = (if lowercase then map toLower else map toUpper)