on fdcaaed help sign in
elk/tests/compiler/Tests/Compiler.hs
62 lines 1.8 kB history raw
1{-# OPTIONS_GHC -W #-}
2module Tests.Compiler (compilerTests) where
4import Data.Traversable (traverse)
6import System.Exit
7import System.FilePath ((</>))
8import System.FilePath.Find (find, (==?), extension)
9import System.Process (readProcessWithExitCode)
11import Test.Framework
12import Test.Framework.Providers.HUnit (testCase)
13import Test.HUnit (Assertion, assertFailure, assertBool)
15compilerTests :: Test
16compilerTests =
17 buildTest $ do
18 goods <- testIf isSuccess =<< getElms "good"
19 bads <- testIf isFailure =<< getElms "bad"
20 return $ testGroup "Compile Tests"
21 [ testGroup "Good Tests" goods
22 , testGroup "Bad Tests" bads
23 ]
25-- COLLECT FILES
27getElms :: FilePath -> IO [FilePath]
28getElms filePath =
29 find (return True) (extension ==? ".elm") (testsDir </> filePath)
31testsDir :: FilePath
32testsDir = "tests" </> "compiler" </> "test-files"
35-- RUN COMPILER
37testIf :: (ExitCode -> String -> Assertion) -> [FilePath] -> IO [Test]
38testIf handler filePaths =
39 traverse setupTest filePaths
40 where
41 setupTest filePath = do
42 let args = [ "--make", "--src-dir=" ++ testsDir, filePath ]
43 (exitCode, stdout, stderr) <- readProcessWithExitCode compiler args ""
44 return (testCase filePath (handler exitCode (stdout ++ stderr)))
46compiler :: FilePath
47compiler = "dist" </> "build" </> "elm" </> "elm"
50-- CHECK RESULTS
52isSuccess :: ExitCode -> String -> Assertion
53isSuccess exitCode stderr =
54 case exitCode of
55 ExitSuccess -> assertBool "" True
56 ExitFailure _ -> assertFailure stderr
58isFailure :: ExitCode -> String -> Assertion
59isFailure exitCode _stderr =
60 case exitCode of
61 ExitSuccess -> assertFailure "Compilation succeeded but should have failed"
62 ExitFailure _ -> assertBool "" True