on 9087bb7 help sign in
elk/tests/Test/Compiler.hs
72 lines 1.9 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
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 :: (([Compiler.Warning], Either [Compiler.Error] (Module.Interface, String)) -> Assertion)
48 -> [FilePath]
49 -> IO [Test]
50testIf handleResult filePaths =
51 traverse setupTest filePaths
52 where
53 setupTest filePath =
54 do source <- readFile filePath
55 let result = Compiler.compile "elm-lang" "core" True source Map.empty
56 return (testCase filePath (handleResult result))
59-- CHECK RESULTS
61isSuccess :: ([Compiler.Warning], Either [Compiler.Error] a) -> Assertion
62isSuccess (_, result) =
63 case result of
64 Right _ -> assertBool "" True
65 Left errors -> assertFailure $ (unlines $ map (Compiler.errorToString "" "") errors)
68isFailure :: ([Compiler.Warning], Either a b) -> Assertion
69isFailure (_, result) =
70 case result of
71 Right _ -> assertFailure "Compilation succeeded but should have failed"
72 Left _ -> assertBool "" True