@@ -1,62 +1,73 @@
11{-# OPTIONS_GHC -W #-}
22module Tests.Compiler (compilerTests) where
33
4+import qualified Data.Map as Map
45import Data.Traversable (traverse)
56
6−import System.Exit
77import System.FilePath ((</>))
88import System.FilePath.Find (find, (==?), extension)
9−import System.Process (readProcessWithExitCode)
109
1110import Test.Framework
1211import Test.Framework.Providers.HUnit (testCase)
1312import Test.HUnit (Assertion, assertFailure, assertBool)
1413
14+import qualified Elm.Compiler as Compiler
15+import qualified Elm.Compiler.Module as Module
16+
17+
1518compilerTests :: Test
1619compilerTests =
1720 buildTest $ do
1821 goods <- testIf isSuccess =<< getElms "good"
1922 bads <- testIf isFailure =<< getElms "bad"
20− return $ testGroup "Compile Tests"
21− [ testGroup "Good Tests" goods
22− , testGroup "Bad Tests" bads
23− ]
23+ return $
24+ testGroup "Compile Tests"
25+ [ testGroup "Good Tests" goods
26+ , testGroup "Bad Tests" bads
27+ ]
2428
25−-- COLLECT FILES
29+
30+-- GATHER ELM FILES
2631
2732getElms :: FilePath -> IO [FilePath]
2833getElms filePath =
29− find (return True) (extension ==? ".elm") (testsDir </> filePath)
34+ find
35+ (return True)
36+ (extension ==? ".elm")
37+ (testsDir </> filePath)
38+
3039
3140testsDir :: FilePath
32−testsDir = "tests" </> "compiler" </> "test-files"
41+testsDir =
42+ "tests" </> "compiler" </> "test-files"
3343
3444
3545-- RUN COMPILER
3646
37−testIf :: (ExitCode -> String -> Assertion) -> [FilePath] -> IO [Test]
38−testIf handler filePaths =
47+testIf
48+ :: (Either String (Module.Interface, String) -> Assertion)
49+ -> [FilePath]
50+ -> IO [Test]
51+testIf handleResult filePaths =
3952 traverse setupTest filePaths
4053 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)))
45−
46−compiler :: FilePath
47−compiler = "dist" </> "build" </> "elm" </> "elm"
54+ setupTest filePath =
55+ do source <- readFile filePath
56+ let result = Compiler.compile "elm-lang" "core" source Map.empty
57+ return (testCase filePath (handleResult result))
4858
4959
5060-- CHECK RESULTS
5161
52−isSuccess :: ExitCode -> String -> Assertion
53−isSuccess exitCode stderr =
54− case exitCode of
55− ExitSuccess -> assertBool "" True
56− ExitFailure _ -> assertFailure stderr
57−
58−isFailure :: ExitCode -> String -> Assertion
59−isFailure exitCode _stderr =
60− case exitCode of
61− ExitSuccess -> assertFailure "Compilation succeeded but should have failed"
62− ExitFailure _ -> assertBool "" True
62+isSuccess :: Either String a -> Assertion
63+isSuccess result =
64+ case result of
65+ Right _ -> assertBool "" True
66+ Left msg -> assertFailure msg
67+
68+
69+isFailure :: Either a b -> Assertion
70+isFailure result =
71+ case result of
72+ Left _ -> assertFailure "Compilation succeeded but should have failed"
73+ Right _ -> assertBool "" True