@@ -1,49 +1,62 @@
1+{-# OPTIONS_GHC -W #-}
12module Tests.Compiler (compilerTests) where
23
3−import Data.Functor ((<$>))
4−import Data.Traversable (traverse)
5−import System.FilePath ((</>))
6−import System.FilePath.Find (find, (==?), extension)
4+import Data.Traversable (traverse)
5+
6+import System.Exit
7+import System.FilePath ((</>))
8+import System.FilePath.Find (find, (==?), extension)
9+import System.Process (readProcessWithExitCode)
10+
711import Test.Framework
812import Test.Framework.Providers.HUnit (testCase)
9−import Test.HUnit (Assertion, assertFailure, assertBool)
10−import Text.Parsec (ParseError)
11−
12−import Elm.Internal.Utils as Elm
13+import Test.HUnit (Assertion, assertFailure, assertBool)
1314
1415compilerTests :: Test
1516compilerTests =
1617 buildTest $ do
17− goods <- mkTests goodCompile =<< getElms "good"
18− bads <- mkTests badCompile =<< getElms "bad"
18+ goods <- testIf isSuccess =<< getElms "good"
19+ bads <- testIf isFailure =<< getElms "bad"
1920 return $ testGroup "Compile Tests"
2021 [ testGroup "Good Tests" goods
2122 , testGroup "Bad Tests" bads
2223 ]
24+
25+-- COLLECT FILES
26+
27+getElms :: FilePath -> IO [FilePath]
28+getElms filePath =
29+ find (return True) (extension ==? ".elm") (testsDir </> filePath)
30+
31+testsDir :: FilePath
32+testsDir = "tests" </> "compiler" </> "test-files"
33+
34+
35+-- RUN COMPILER
36+
37+testIf :: (ExitCode -> String -> Assertion) -> [FilePath] -> IO [Test]
38+testIf handler filePaths =
39+ traverse setupTest filePaths
2340 where
24− getElms :: FilePath -> IO [FilePath]
25− getElms fname = find (return True) (extension ==? ".elm") (testsDir </> fname)
26−
27− mkTests :: (Either String String -> Assertion) -> [FilePath] -> IO [Test]
28− mkTests h = traverse setupTest
29− where setupTest f = testCase f . mkCompileTest h <$> readFile f
30−
31− testsDir :: FilePath
32− testsDir = "tests" </> "compiler" </> "test-files"
33−
34−goodCompile :: Either String String -> Assertion
35−goodCompile result =
36− case result of
37− Left err -> assertFailure err
38− Right _ -> assertBool "" True
39−
40−badCompile :: Either String String -> Assertion
41−badCompile result =
42− case result of
43− Left _ -> assertBool "" True
44− Right _ -> assertFailure "Compilation succeeded but should have failed"
45−
46−mkCompileTest :: ((Either String String) -> Assertion) -- ^ Handler
47− -> String -- ^ File Contents
48− -> Assertion
49−mkCompileTest handle = handle . Elm.compile
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 stderr))
45+
46+compiler :: FilePath
47+compiler = "dist" </> "build" </> "Elm" </> "elm"
48+
49+
50+-- CHECK RESULTS
51+
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