on ea343fc help sign in
elk/tests/Test/Compiler.hs
87 lines 2.0 kB history raw
1{-# OPTIONS_GHC -W #-}
2module Test.Compiler (compilerTests) where
4import qualified Data.Map as Map
5import Data.Traversable (traverse)
7import System.FilePath ((</>))
8import System.FilePath.Find (find, (==?), extension)
10import Test.Framework
11import Test.Framework.Providers.HUnit (testCase)
12import Test.HUnit (Assertion, assertFailure, assertBool)
14import qualified Elm.Compiler as Compiler
15import qualified Elm.Compiler.Module as Module
18compilerTests :: Test
19compilerTests =
20 buildTest $ do
21 goods <- testIf isSuccess =<< getElms "good"
22 bads <- testIf isFailure =<< getElms "bad"
23 return $
24 testGroup "Compile Tests"
25 [ testGroup "Good Tests" goods
26 , testGroup "Bad Tests" bads
27 ]
30-- GATHER ELM FILES
32getElms :: FilePath -> IO [FilePath]
33getElms filePath =
34 find
35 (return True)
36 (extension ==? ".elm")
37 (testsDir </> filePath)
40testsDir :: FilePath
41testsDir =
42 "tests" </> "test-files"
45-- RUN COMPILER
47testIf
48 :: (Either String (Module.Interface, String) -> Assertion)
49 -> [FilePath]
50 -> IO [Test]
51testIf handleResult filePaths =
52 traverse setupTest filePaths
53 where
54 setupTest filePath = do
55 source <- readFile filePath
57 let (dealiaser, _warnings, result) =
58 Compiler.compile "elm-lang" "core" True source Map.empty
59 let formatErrors errors =
60 concatMap (Compiler.errorToString dealiaser filePath source) errors
61 let formattedResult =
62 either (Left . formatErrors) Right result
64 return $ testCase filePath (handleResult formattedResult)
67-- CHECK RESULTS
69isSuccess :: Either String a -> Assertion
70isSuccess result =
71 case result of
72 Right _ ->
73 assertBool "" True
75 Left errorMessages ->
76 assertFailure errorMessages
79isFailure :: Either a b -> Assertion
80isFailure result =
81 case result of
82 Right _ ->
83 assertFailure "Compilation succeeded but should have failed"
85 Left _ ->
86 assertBool "" True