DocTest 0.0.1 → 0.0.2
raw patch · 2 files changed
+22/−57 lines, 2 filesdep +processdep −plugins
Dependencies added: process
Dependencies removed: plugins
Files
- DocTest.cabal +2/−2
- Test/DocTest.hs +20/−55
DocTest.cabal view
@@ -1,5 +1,5 @@ name: DocTest-version: 0.0.1+version: 0.0.2 stability: experimental synopsis: Test interactive Haskell examples description: DocTest checks examples in source code comments.@@ -18,7 +18,7 @@ , haskell-src , directory , filepath- , plugins+ , process build-type: Simple extra-source-files:
Test/DocTest.hs view
@@ -1,44 +1,11 @@ -- Copyright (c) 2009 Simon Hengel <simon.hengel@web.de> module Test.DocTest where -import Control.Exception import Test.HUnit-import System.Plugins-import System.IO import System.FilePath import System.Directory import Test.DocTest.Util---loadTest :: FilePath -> FilePath -> IO Test-loadTest file dir = do- mv <- load file [dir] [] "docTest"- case mv of- LoadFailure msg -> fail ("error while loading " ++ file ++ ": " ++ (concat msg))- LoadSuccess _ v -> return v---compileModule filename dir = do- status <- makeAll filename ["-i" ++ dir]- case status of- MakeFailure errors -> fail (concat errors)- MakeSuccess _ file -> return file----- Example:------ > makeTest (DocTest "Fib.hs - line 6: " "Fib" "fib 10" "55")--- "docTest = TestCase (assertEqual \"Fib.hs - line 6: \" (show (fib 10)) \"55\")"--makeTest (DocTest source _ expression result) = (- "docTest = TestCase (assertEqual \"" ++- source ++ "\" " ++- "(show (" ++ expression ++ "))" ++ " \"" ++- (escape result) ++ "\")"- )--escape :: String -> String-escape str = replace "\"" "\\\"" $ replace "\\" "\\\\" str+import System.Process data DocTest = DocTest {@@ -50,34 +17,32 @@ deriving (Show) -writeModule test moduleName handle = do- hPutStrLn handle ("module " ++ moduleName ++ " where")- hPutStrLn handle "import Test.HUnit"- hPutStrLn handle ("import " ++ (_module test))- hPutStrLn handle (makeTest test)+_testModule :: DocTest -> String+_testModule (DocTest source _module expression result) =+ "import " ++ _module ++ "\n" +++ "main = do\n" +++ " putStr (show (" ++ expression ++ "))\n" +runDocTest test baseDir = do+ ret <- readProcess "runhaskell" ["-i" ++ baseDir] (_testModule test)+ return (TestCase (assertEqual (source test) (result test) ret)) + doTest :: FilePath -> DocTest -> IO Test doTest directory test = do- let moduleBaseName = replace "." "_" (_module test)- (filename, handle) <- openTempFile directory (moduleBaseName ++ ".hs")-- putStrLn filename- let testModuleName = takeBaseName filename-- --withFile filename WriteMode (writeModule test testModuleName)- writeModule test testModuleName handle- hClose handle-- canonicalModulePath <- canonicalizePath $ source test- putStrLn canonicalModulePath- let baseDir = packageBaseDir canonicalModulePath (_module test)- putStrLn baseDir+ runDocTest test baseDir - obj_file <- compileModule filename baseDir- loadTest obj_file directory+-- Maps a given source file and a corresponding module name to the base+-- directory of the package.+--+-- Example:+-- > packageBaseDir "/foo/bar/MyPackage/MyModule.hs" "MyPackage.MyModule"+-- "/foo/bar/"++-- > packageBaseDir "foo" "bar"+-- "foo*** Exception: Prelude.undefined packageBaseDir :: FilePath -> String -> FilePath packageBaseDir moduleSrcFile moduleName = stripPostfix (dropExtension moduleSrcFile) (replace "." [pathSeparator] moduleName)