-
Notifications
You must be signed in to change notification settings - Fork 2
/
Tests.hs
71 lines (61 loc) · 1.71 KB
/
Tests.hs
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
-- | Testing module
module Tests
( runIlTests ) where
import Il.Lexer
import Il.Parser
import Defs.Util
import Defs.AST
import Analyzer
import Il.Typechecker
import Control.DeepSeq
import System.FilePath.Posix
import Control.Exception
import Data.List
import System.Directory
import Prelude hiding (lex, catch)
-- | Runs all Il tests
runIlTests :: IO ()
runIlTests = listFiles "Il/tests/" >>= runTestFiles
-- | Run all test files given by filenames
runTestFiles :: [FilePath] -> IO()
runTestFiles = mapM_ runTest
-- | Opens a test file and runs the test
runTest :: FilePath -> IO()
runTest name = do
yellowColor
putStrLn $ "Test File " ++ name
resetColor
contents <- readFile name
test contents
-- | Lists all filenames in a given directory
listFiles :: FilePath -> IO [FilePath]
listFiles path = do
allfiles <- getDirectoryContents path
let files = sort $ filter (\s -> last (split "/" s) `notElem` [".", ".."]) allfiles
return $ map (path </>) files
-- | Lexes, parses, analyzes and pretty prints test results
test :: String -> IO()
test src = do
let fns = (parse.lex) src
typechecking fns `catch` handler "\nTyping failed"
analysis fns `catch` handler "\nAnalysis failed"
-- | Tests analysis
analysis :: [Function] -> IO ()
analysis fns = do
printRecommendationFromAnalysis.analyze $ fns
greenColor
putStrLn "Passed"
resetColor
-- | Tests typechecking
typechecking :: [Function] -> IO ()
typechecking fns = mapM_ (\fn -> do
blueColor
deepseq (typecheckF fns fn) (putStrLn "Typed")
resetColor) fns
-- | Handles failing tests
handler :: String -> SomeException -> IO ()
handler msg e = do
redColor
putStrLn msg
print (e :: SomeException)
resetColor