on db277d9 help sign in
elk/tests/Test/Compiler.hs
88 lines 2.1 kB history raw
1{-# OPTIONS_GHC -W #-}
2module Test.Compiler (compilerTests) where
4import Control.Arrow (second)
5import qualified Data.Map as Map
6import Data.Traversable (traverse)
8import System.FilePath ((</>))
9import System.FilePath.Find (find, (==?), extension)
11import Test.Framework
12import Test.Framework.Providers.HUnit (testCase)
13import Test.HUnit (Assertion, assertFailure, assertBool)
15import qualified Elm.Compiler as Compiler
16import qualified Elm.Compiler.Module as Module
19compilerTests :: Test
20compilerTests =
21 buildTest $ do
22 goods <- testIf isSuccess =<< getElms "good"
23 bads <- testIf isFailure =<< getElms "bad"
24 return $
25 testGroup "Compile Tests"
26 [ testGroup "Good Tests" goods
27 , testGroup "Bad Tests" bads
28 ]
31-- GATHER ELM FILES
33getElms :: FilePath -> IO [FilePath]
34getElms filePath =
35 find
36 (return True)
37 (extension ==? ".elm")
38 (testsDir </> filePath)
41testsDir :: FilePath
42testsDir =
43 "tests" </> "test-files"
46-- RUN COMPILER
48testIf
49 :: (([Compiler.Warning], Either String (Module.Interface, String)) -> Assertion)
50 -> [FilePath]
51 -> IO [Test]
52testIf handleResult filePaths =
53 traverse setupTest filePaths
54 where
55 setupTest filePath = do
56 source <- readFile filePath
58 let result =
59 Compiler.compile "elm-lang" "core" True source Map.empty
60 let formatErrors errors =
61 concatMap (Compiler.errorToString filePath source) errors
62 let formattedResult =
63 second (either (Left . formatErrors) Right) result
65 return $ testCase filePath (handleResult formattedResult)
68-- CHECK RESULTS
70isSuccess :: ([Compiler.Warning], Either String a) -> Assertion
71isSuccess (_, result) =
72 case result of
73 Right _ ->
74 assertBool "" True
76 Left errorMessages ->
77 assertFailure errorMessages
80isFailure :: ([Compiler.Warning], Either a b) -> Assertion
81isFailure (_, result) =
82 case result of
83 Right _ ->
84 assertFailure "Compilation succeeded but should have failed"
86 Left _ ->
87 assertBool "" True