hakyll/tests/TestSuite/Util.hs

104 lines
3.7 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
2012-11-19 13:59:55 +00:00
, newTestProvider
, testCompiler
, testCompilerDone
2013-01-06 17:33:00 +00:00
, testConfiguration
, cleanTestEnv
) 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 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
2013-01-06 17:33:00 +00:00
import Hakyll.Core.Util.File
2012-11-19 13:59:55 +00:00
--------------------------------------------------------------------------------
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
2013-01-06 17:33:00 +00:00
newTestStore = Store.new True $ storeDirectory testConfiguration
2012-11-19 13:59:55 +00:00
--------------------------------------------------------------------------------
newTestProvider :: Store -> IO Provider
newTestProvider store = newProvider store (const $ return False) $
2013-01-06 17:33:00 +00:00
providerDirectory testConfiguration
2012-11-19 13:59:55 +00:00
--------------------------------------------------------------------------------
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
2013-01-06 17:33:00 +00:00
{ compilerConfig = testConfiguration
, compilerUnderlying = underlying
2012-11-19 13:59:55 +00:00
, 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
--------------------------------------------------------------------------------
2013-01-06 17:33:00 +00:00
testConfiguration :: Configuration
testConfiguration = defaultConfiguration
{ destinationDirectory = "_testsite"
, storeDirectory = "_teststore"
, tmpDirectory = "_testtmp"
, providerDirectory = "tests/data"
}
--------------------------------------------------------------------------------
cleanTestEnv :: IO ()
cleanTestEnv = do
removeDirectory $ destinationDirectory testConfiguration
removeDirectory $ storeDirectory testConfiguration
removeDirectory $ tmpDirectory testConfiguration