on ed48cd2 help sign in
elk/tests/Test/Compiler.hs
88 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
17compilerTests :: Test
18compilerTests =
19 buildTest $ do
20 goods <- testIf isSuccess =<< getElms "good"
21 bads <- testIf isFailure =<< getElms "bad"
22 return $
23 testGroup "Compile Tests"
24 [ testGroup "Good Tests" goods
25 , testGroup "Bad Tests" bads
26 ]
29-- GATHER ELM FILES
31getElms :: FilePath -> IO [FilePath]
32getElms filePath =
33 find
34 (return True)
35 (extension ==? ".elm")
36 (testsDir </> filePath)
39testsDir :: FilePath
40testsDir =
41 "tests" </> "test-files"
44-- RUN COMPILER
46testIf
47 :: (Either String Compiler.Result -> Assertion)
48 -> [FilePath]
49 -> IO [Test]
50testIf handleResult filePaths =
51 traverse setupTest filePaths
52 where
53 setupTest filePath = do
54 source <- readFile filePath
56 let context =
57 Compiler.Context "elm-lang" "core" True False
58 let (dealiaser, _warnings, result) =
59 Compiler.compile context source Map.empty
60 let formatErrors errors =
61 concatMap (Compiler.errorToString dealiaser filePath source) errors
62 let formattedResult =
63 either (Left . formatErrors) Right result
65 return $ testCase filePath (handleResult formattedResult)
68-- CHECK RESULTS
70isSuccess :: Either String a -> Assertion
71isSuccess result =
72 case result of
73 Right _ ->
74 assertBool "" True
76 Left errorMessages ->
77 assertFailure errorMessages
80isFailure :: 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