hakyll/tests/TestSuite/Util.hs

114 lines
3.9 KiB
Haskell
Raw Normal View History

2012-11-19 13:59:55 +00:00
--------------------------------------------------------------------------------
-- | Test utilities
module TestSuite.Util
( fromAssertions
2012-11-20 10:36:45 +00:00
, newTestStore
, cleanTestStore
2012-11-19 13:59:55 +00:00
, withTestStore
, newTestProvider
, testCompiler
, testCompilerDone
2012-11-21 19:38:13 +00:00
, withTestConfiguration
) where
2011-05-25 09:24:33 +00:00
2012-11-19 13:59:55 +00:00
--------------------------------------------------------------------------------
import Data.Monoid (mempty)
import qualified Data.Set as S
2012-11-19 13:59:55 +00:00
import System.Directory (removeDirectoryRecursive)
import Test.Framework
import Test.Framework.Providers.HUnit
import Test.HUnit hiding (Test)
2012-11-20 10:50:22 +00:00
import Text.Printf (printf)
2011-05-25 09:24:33 +00:00
2012-11-19 13:59:55 +00:00
--------------------------------------------------------------------------------
import Hakyll.Core.Compiler.Internal
2012-11-21 19:38:13 +00:00
import Hakyll.Core.Configuration
2012-11-19 13:59:55 +00:00
import Hakyll.Core.Identifier
import qualified Hakyll.Core.Logger as Logger
import Hakyll.Core.Provider
import Hakyll.Core.Store (Store)
import qualified Hakyll.Core.Store as Store
--------------------------------------------------------------------------------
fromAssertions :: String -- ^ Name
-> [Assertion] -- ^ Cases
-> [Test] -- ^ Result tests
2012-11-20 10:50:22 +00:00
fromAssertions name =
2012-11-24 12:34:50 +00:00
zipWith testCase [printf "[%2d] %s" n name | n <- [1 :: Int ..]]
2011-05-25 09:24:33 +00:00
2012-11-19 13:59:55 +00:00
2012-11-20 10:36:45 +00:00
--------------------------------------------------------------------------------
newTestStore :: IO Store
newTestStore = Store.new True "_teststore"
--------------------------------------------------------------------------------
cleanTestStore :: IO ()
cleanTestStore = removeDirectoryRecursive "_teststore"
2012-11-19 13:59:55 +00:00
--------------------------------------------------------------------------------
withTestStore :: (Store -> IO a) -> IO a
withTestStore f = do
2012-11-20 10:36:45 +00:00
store <- newTestStore
2012-11-19 13:59:55 +00:00
result <- f store
2012-11-20 10:36:45 +00:00
cleanTestStore
2012-11-19 13:59:55 +00:00
return result
--------------------------------------------------------------------------------
newTestProvider :: Store -> IO Provider
newTestProvider store = newProvider store (const False) "tests/data"
--------------------------------------------------------------------------------
testCompiler :: Store -> Provider -> Identifier -> Compiler a
-> IO (CompilerResult a)
testCompiler store provider underlying compiler = do
2012-12-29 09:41:05 +00:00
logger <- Logger.new Logger.Error
2012-11-19 13:59:55 +00:00
let read' = CompilerRead
{ compilerUnderlying = underlying
, compilerProvider = provider
, compilerUniverse = S.empty
2012-11-19 13:59:55 +00:00
, compilerRoutes = mempty
, compilerStore = store
, compilerLogger = logger
}
result <- runCompiler compiler read'
Logger.flush logger
return result
--------------------------------------------------------------------------------
testCompilerDone :: Store -> Provider -> Identifier -> Compiler a -> IO a
testCompilerDone store provider underlying compiler = do
result <- testCompiler store provider underlying compiler
case result of
CompilerDone x _ -> return x
CompilerError e -> error $
"TestSuite.Util.testCompilerDone: compiler " ++ show underlying ++
" threw: " ++ e
CompilerRequire i _ -> error $
"TestSuite.Util.testCompilerDone: compiler " ++ show underlying ++
" requires: " ++ show i
2012-11-21 19:38:13 +00:00
--------------------------------------------------------------------------------
withTestConfiguration :: (Configuration -> IO a) -> IO a
withTestConfiguration f = do
x <- f config
removeDirectoryRecursive $ destinationDirectory config
removeDirectoryRecursive $ storeDirectory config
return x
where
config = defaultConfiguration
{ destinationDirectory = "_testsite"
, storeDirectory = "_teststore"
, providerDirectory = "tests/data"
}