1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
{-# LANGUAGE ViewPatterns #-}
-- | Use "runhaskell Setup.hs test" or "cabal test" to run these tests.
module Main where
import Language.Haskell.Exts
import Test.Tasty hiding (defaultMain)
import Test.Tasty.Golden
import Test.Tasty.Golden.Manage
import System.FilePath
import System.IO
import Control.Monad.Trans
import Control.Applicative
import Extensions
import Text.Show.Pretty
main :: IO ()
main = do
sources <- getTestFiles examplesDir
defaultMain $ testGroup "Tests" $
[ parserTests sources
, exactPrinterTests sources
, prettyPrinterTests sources
, prettyParserTests sources
, extensionProperties
, commentsTests examplesDir
]
-- | Where all the tests are to be found
examplesDir :: FilePath
examplesDir = "tests/examples"
getTestFiles :: MonadIO m => FilePath -> m [FilePath]
getTestFiles dir = liftIO $ findByExtension [".hs", ".lhs"] dir
parserTests :: [FilePath] -> TestTree -- {{{
parserTests sources = testGroup "Parser tests" $ do
-- list monad
file <- sources
let
out = file <.> "parser" <.> "out"
golden = file <.> "parser" <.> "golden"
run = do
ast <-
parseUTF8FileWithComments
(defaultParseMode { parseFilename = file })
file
writeBinaryFile out $ ppShow ast ++ "\n"
return $ goldenVsFile (takeBaseName file) golden out run
-- }}}
exactPrinterTests :: [FilePath] -> TestTree -- {{{
exactPrinterTests sources = testGroup "Exact printer tests" $ do
-- list monad
file <- sources
let
out = file <.> "exactprinter" <.> "out"
golden = file <.> "exactprinter" <.> "golden"
run = do
contents <- readUTF8File file
let
-- parse
mbAst =
parseFileContentsWithComments
(defaultParseMode { parseFilename = file })
contents
-- try to pretty-print; summarize the test result
result =
case mbAst of
f@ParseFailed{} -> show f
ParseOk ast ->
let
printed = uncurry exactPrint ast
in
if printed == contents
then "Match"
else printed
writeBinaryFile out $ result ++ "\n"
return $ goldenVsFile (takeBaseName file) golden out run
-- }}}
prettyPrinterTests :: [FilePath] -> TestTree -- {{{
prettyPrinterTests sources = testGroup "Pretty printer tests" $ do
-- list monad
file <- sources
let
out = file <.> "prettyprinter" <.> "out"
golden = file <.> "prettyprinter" <.> "golden"
run = do
contents <- readUTF8File file
let
-- parse
mbAst =
parseFileContentsWithMode
(defaultParseMode { parseFilename = file })
contents
-- try to pretty-print; summarize the test result
result =
case mbAst of
f@ParseFailed{} -> show f
ParseOk ast -> prettyPrint ast
writeBinaryFile out $ result ++ "\n"
return $ goldenVsFile (takeBaseName file) golden out run
-- }}}
prettyParserTests :: [FilePath] -> TestTree -- {{{
prettyParserTests sources = testGroup "Pretty-parser tests" $ do
-- list monad
file <- sources
let
out = file <.> "prettyparser" <.> "out"
golden = file <.> "prettyparser" <.> "golden"
run = do
contents <- readUTF8File file
let
-- parse
parse1Result :: ParseResult (Module SrcSpanInfo)
parse1Result =
parseFileContentsWithMode
(defaultParseMode { parseFilename = file })
contents
prettyResult :: ParseResult String
prettyResult = prettyPrint <$> parse1Result
parse2Result :: ParseResult (ParseResult (Module SrcSpanInfo))
parse2Result = parseFileContents <$> prettyResult
-- Even the un-annotated AST contains certain locations.
-- Obviously, they may differ, so we have to erase them.
eraseLocs :: Module l -> Module ()
eraseLocs = (() <$)
summary =
case liftA3 (,,) parse1Result prettyResult parse2Result of
f@ParseFailed{} -> show f
ParseOk (eraseLocs -> ast1, pretty, mbAst2) ->
case mbAst2 of
f@ParseFailed{} ->
"Failed to parse output of pretty-printer:\n" ++
show f ++ "\n" ++
"The pretty-printer output follows.\n\n" ++
pretty
ParseOk (eraseLocs -> ast2) ->
if ast1 == ast2
then "Match"
else
"Roundtrip test failed\n\n" ++
"AST 1:\n\n" ++
show ast1 ++ "\n\n" ++
"AST 2:\n\n" ++
show ast2 ++ "\n"
writeBinaryFile out $ summary ++ "\n"
return $ goldenVsFile (takeBaseName file) golden out run
-- }}}
commentsTests :: FilePath -> TestTree -- {{{
commentsTests dir = testGroup "Comments tests" $ do
let file = dir ++ "/HaddockComments.hs"
out = file <.> "comments" <.> "out"
golden = file <.> "comments" <.> "golden"
run = do
contents <- readUTF8File file
let
-- parse
parse1Result :: ParseResult (Module SrcSpanInfo,[Comment])
parse1Result =
parseFileContentsWithComments
(defaultParseMode { parseFilename = file })
contents
withC = case parse1Result of
ParseOk res -> ParseOk $ associateHaddock res
ParseFailed sloc msg -> ParseFailed sloc msg
writeBinaryFile out $ show withC ++ "\n"
return $ goldenVsFile (takeBaseName file) golden out run
-- UTF8 utils {{{
readUTF8File :: FilePath -> IO String
readUTF8File fp = openFile fp ReadMode >>= \h -> do
hSetEncoding h utf8
hGetContents h
parseUTF8FileWithComments :: ParseMode -> FilePath -> IO (ParseResult (Module SrcSpanInfo, [Comment]))
parseUTF8FileWithComments p fp = readUTF8File fp >>= (return . parseFileContentsWithComments p)
-- }}}
|