module Test.Falderal.Formatter.Haskell (format) where -- -- Test.Falderal.Formatter.Haskell -- Haskell compiler for Falderal -- import Test.Falderal.Common -- -- Formatting function which compiles a Falderal file to Haskell source. -- -- XXX tests should be partitioned before this is called, but this should -- be able to handle multiple Haskell-implemented functionalities. -- format _ blocks = (prelude blocks) ++ (formatBlocks blocks) ++ postlude formatBlocks (test@(Test id [(HaskellTest moduleName functionName)] desc text expectation _):rest) = let fn = moduleName ++ "." ++ functionName in " (" ++ (show id) ++ ", " ++ fn ++ ", " ++ (show text) ++ "),\n" ++ (formatBlocks rest) formatBlocks (_:rest) = formatBlocks rest formatBlocks [] = "" gatherImports ((Test id [(HaskellTest moduleName functionName)] _ _ _ _):rest) mNames | moduleName `elem` mNames = gatherImports rest mNames | otherwise = "import qualified " ++ moduleName ++ "\n" ++ gatherImports rest (moduleName:mNames) gatherImports (_:rest) mNames = gatherImports rest mNames gatherImports [] _ = "" prelude blocks = "-- This file was automatically generated by Test.Falderal.Formatter.Haskell\n\ \-- Edit at your own risk!\n\ \\n\ \import qualified Control.Exception as Exc\n\ \" ++ (gatherImports blocks []) ++ "\ \\n\ \runFun testFun inputText = do\n\ \ Exc.catch (Exc.evaluate (Left $! (testFun inputText)))\n\ \ (\\exception -> return (Right (show (exception :: Exc.SomeException))))\n\ \\n\ \getKind (Left _) = \"output\"\n\ \getKind (Right _) = \"exception\"\n\ \getText (Left x) = x\n\ \getText (Right x) = x\n\ \report [] = do\n\ \ return ()\n\ \report ((-1,_,_):rest) =\n\ \ report rest\n\ \report ((id,fun,input):rest) = do\n\ \ result <- runFun (fun) input\n\ \ numLines <- return $ length $ lines $ getText result\n\ \ putStrLn (getKind result)\n\ \ putStrLn (show id)\n\ \ putStrLn (show numLines)\n\ \ case numLines of\n\ \ 0 -> do\n\ \ report rest\n\ \ _ -> do\n\ \ putStrLn (getText result)\n\ \ report rest\n\ \\n\ \main = report [\n" postlude = " (-1,id,\"\")\n\ \ ]\n"