Skip to content
Newer
Older
100644 169 lines (134 sloc) 5.19 KB
70e5121 @cdsmith Convert tests to HUnit with HPC
cdsmith authored Jul 2, 2011
1 {-# LANGUAGE ScopedTypeVariables #-}
2 {-# LANGUAGE OverloadedStrings #-}
3
4 module Main where
5
6 import Prelude hiding (lookup)
7
8 import Control.Concurrent
9 import Control.Exception
10 import Control.Monad
e8bfc50 @cdsmith Add some tests, including one exposing odd import behavior
cdsmith authored Jul 2, 2011
11 import qualified Data.ByteString.Lazy.Char8 as L
70e5121 @cdsmith Convert tests to HUnit with HPC
cdsmith authored Jul 2, 2011
12 import Data.Configurator
13 import Data.Configurator.Types
14 import Data.Functor
e8bfc50 @cdsmith Add some tests, including one exposing odd import behavior
cdsmith authored Jul 2, 2011
15 import Data.Int
70e5121 @cdsmith Convert tests to HUnit with HPC
cdsmith authored Jul 2, 2011
16 import Data.Maybe
4442de5 @cdsmith Expand test suite
cdsmith authored Jul 2, 2011
17 import Data.Text (Text)
e8bfc50 @cdsmith Add some tests, including one exposing odd import behavior
cdsmith authored Jul 2, 2011
18 import Data.Word
70e5121 @cdsmith Convert tests to HUnit with HPC
cdsmith authored Jul 2, 2011
19 import System.Directory
e8bfc50 @cdsmith Add some tests, including one exposing odd import behavior
cdsmith authored Jul 2, 2011
20 import System.Environment
70e5121 @cdsmith Convert tests to HUnit with HPC
cdsmith authored Jul 2, 2011
21 import System.IO
22 import Test.HUnit
23
24 main :: IO ()
25 main = runTestTT tests >> return ()
26
27 tests :: Test
28 tests = TestList [
e8bfc50 @cdsmith Add some tests, including one exposing odd import behavior
cdsmith authored Jul 2, 2011
29 "load" ~: loadTest,
30 "types" ~: typesTest,
31 "interp" ~: interpTest,
32 "import" ~: importTest,
70e5121 @cdsmith Convert tests to HUnit with HPC
cdsmith authored Jul 2, 2011
33 "reload" ~: reloadTest
34 ]
35
36 withLoad :: [Worth FilePath] -> (Config -> IO ()) -> IO ()
37 withLoad files t = do
38 mb <- try $ load files
39 case mb of
40 Left (err :: SomeException) -> assertFailure (show err)
41 Right cfg -> t cfg
42
43 withReload :: [Worth FilePath] -> ([Maybe FilePath] -> Config -> IO ()) -> IO ()
44 withReload files t = do
45 tmp <- getTemporaryDirectory
46 temps <- forM files $ \f -> do
47 exists <- doesFileExist (worth f)
48 if exists
49 then do
50 (p,h) <- openBinaryTempFile tmp "test.cfg"
51 L.hPut h =<< L.readFile (worth f)
52 hClose h
53 return (p <$ f, Just p)
54 else do
55 return (f, Nothing)
56 flip finally (mapM_ removeFile (catMaybes (map snd temps))) $ do
57 mb <- try $ autoReload autoConfig (map fst temps)
58 case mb of
59 Left (err :: SomeException) -> assertFailure (show err)
60 Right (cfg, tid) -> t (map snd temps) cfg >> killThread tid
61
62 takeMVarTimeout :: Int -> MVar a -> IO (Maybe a)
63 takeMVarTimeout millis v = do
64 w <- newEmptyMVar
65 tid <- forkIO $ do
66 putMVar w . Just =<< takeMVar v
67 forkIO $ do
68 threadDelay (millis * 1000)
69 killThread tid
70 tryPutMVar w Nothing
71 return ()
72 takeMVar w
73
74 loadTest :: Assertion
4442de5 @cdsmith Expand test suite
cdsmith authored Jul 2, 2011
75 loadTest = withLoad [Required "resources/pathological.cfg"] $ \cfg -> do
76 aa <- lookup cfg "aa"
77 assertEqual "int property" aa $ (Just 1 :: Maybe Int)
78
79 ab <- lookup cfg "ab"
80 assertEqual "string property" ab (Just "foo" :: Maybe Text)
81
82 acx <- lookup cfg "ac.x"
83 assertEqual "nested int" acx (Just 1 :: Maybe Int)
84
85 acy <- lookup cfg "ac.y"
86 assertEqual "nested bool" acy (Just True :: Maybe Bool)
87
88 ad <- lookup cfg "ad"
89 assertEqual "simple bool" ad (Just False :: Maybe Bool)
90
91 ae <- lookup cfg "ae"
92 assertEqual "simple int 2" ae (Just 1 :: Maybe Int)
93
94 af <- lookup cfg "af"
95 assertEqual "list property" af (Just (2,3) :: Maybe (Int,Int))
96
97 deep <- lookup cfg "ag.q-e.i_u9.a"
98 assertEqual "deep bool" deep (Just False :: Maybe Bool)
70e5121 @cdsmith Convert tests to HUnit with HPC
cdsmith authored Jul 2, 2011
99
e8bfc50 @cdsmith Add some tests, including one exposing odd import behavior
cdsmith authored Jul 2, 2011
100 typesTest :: Assertion
101 typesTest = withLoad [Required "resources/pathological.cfg"] $ \ cfg -> do
102 asInt <- lookup cfg "aa" :: IO (Maybe Int)
103 assertEqual "int" asInt (Just 1)
104
105 asInteger <- lookup cfg "aa" :: IO (Maybe Integer)
106 assertEqual "int" asInteger (Just 1)
107
108 asWord <- lookup cfg "aa" :: IO (Maybe Word)
109 assertEqual "int" asWord (Just 1)
110
111 asInt8 <- lookup cfg "aa" :: IO (Maybe Int8)
112 assertEqual "int8" asInt8 (Just 1)
113
114 asInt16 <- lookup cfg "aa" :: IO (Maybe Int16)
115 assertEqual "int16" asInt16 (Just 1)
116
117 asInt32 <- lookup cfg "aa" :: IO (Maybe Int32)
118 assertEqual "int32" asInt32 (Just 1)
119
120 asInt64 <- lookup cfg "aa" :: IO (Maybe Int64)
121 assertEqual "int64" asInt64 (Just 1)
122
123 asWord8 <- lookup cfg "aa" :: IO (Maybe Word8)
124 assertEqual "word8" asWord8 (Just 1)
125
126 asWord16 <- lookup cfg "aa" :: IO (Maybe Word16)
127 assertEqual "word16" asWord16 (Just 1)
128
129 asWord32 <- lookup cfg "aa" :: IO (Maybe Word32)
130 assertEqual "word32" asWord32 (Just 1)
131
132 asWord64 <- lookup cfg "aa" :: IO (Maybe Word64)
133 assertEqual "word64" asWord64 (Just 1)
134
135 asTextBad <- lookup cfg "aa" :: IO (Maybe Text)
136 assertEqual "word64" asTextBad Nothing
137
138 asTextGood <- lookup cfg "ab" :: IO (Maybe Text)
139 assertEqual "word64" asTextGood (Just "foo")
140
141 interpTest :: Assertion
142 interpTest = withLoad [Required "resources/pathological.cfg"] $ \ cfg -> do
143 home <- getEnv "HOME"
144 cfgHome <- lookup cfg "ba"
145 assertEqual "home interp" (Just home) cfgHome
146
147 importTest :: Assertion
148 importTest = withLoad [Required "resources/import.cfg"] $ \ cfg -> do
149 aa <- lookup cfg "x.aa" :: IO (Maybe Int)
150 assertEqual "simple" aa (Just 1)
151 acx <- lookup cfg "x.ac.x" :: IO (Maybe Int)
152 assertEqual "nested" acx (Just 1)
153
70e5121 @cdsmith Convert tests to HUnit with HPC
cdsmith authored Jul 2, 2011
154 reloadTest :: Assertion
4442de5 @cdsmith Expand test suite
cdsmith authored Jul 2, 2011
155 reloadTest = withReload [Required "resources/pathological.cfg"] $ \[Just f] cfg -> do
70e5121 @cdsmith Convert tests to HUnit with HPC
cdsmith authored Jul 2, 2011
156 aa <- lookup cfg "aa"
157 assertEqual "simple property 1" aa $ Just (1 :: Int)
158
159 dongly <- newEmptyMVar
160 wongly <- newEmptyMVar
161 subscribe cfg "dongly" $ \ _ _ -> putMVar dongly ()
162 subscribe cfg "wongly" $ \ _ _ -> putMVar wongly ()
163 L.appendFile f "\ndongly = 1"
164 r1 <- takeMVarTimeout 2000 dongly
165 assertEqual "notify happened" r1 (Just ())
166 r2 <- takeMVarTimeout 2000 wongly
167 assertEqual "notify not happened" r2 Nothing
168
Something went wrong with that request. Please try again.