@@ -1,49 +1,62 @@
1module Tests.Compiler (compilerTests) where
2module Tests.Compiler (compilerTests) where
3−import Data.Functor ((<$>))
4−import Data.Traversable (traverse)
4+import Data.Traversable (traverse)
5−import System.FilePath ((</>))
6+import System.Exit
7+import System.FilePath ((</>))
6−import System.FilePath.Find (find, (==?), extension)
8+import System.FilePath.Find (find, (==?), extension)
9+import System.Process (readProcessWithExitCode)
7import Test.Framework
11import Test.Framework
8import Test.Framework.Providers.HUnit (testCase)
12import Test.Framework.Providers.HUnit (testCase)
9−import Test.HUnit (Assertion, assertFailure, assertBool)
13+import Test.HUnit (Assertion, assertFailure, assertBool)
10−import Text.Parsec (ParseError)
12−import Elm.Internal.Utils as Elm
14compilerTests :: Test
15compilerTests :: Test
15compilerTests =
16compilerTests =
16 buildTest $ do
17 buildTest $ do
17− goods <- mkTests goodCompile =<< getElms "good"
18+ goods <- testIf isSuccess =<< getElms "good"
18− bads <- mkTests badCompile =<< getElms "bad"
19+ bads <- testIf isFailure =<< getElms "bad"
19 return $ testGroup "Compile Tests"
20 return $ testGroup "Compile Tests"
20 [ testGroup "Good Tests" goods
21 [ testGroup "Good Tests" goods
21 , testGroup "Bad Tests" bads
22 , testGroup "Bad Tests" bads
27+getElms :: FilePath -> IO [FilePath]
29+ find (return True) (extension ==? ".elm") (testsDir </> filePath)
32+testsDir = "tests" </> "compiler" </> "test-files"
37+testIf :: (ExitCode -> String -> Assertion) -> [FilePath] -> IO [Test]
38+testIf handler filePaths =
39+ traverse setupTest filePaths
24− getElms :: FilePath -> IO [FilePath]
41+ setupTest filePath = do
25− getElms fname = find (return True) (extension ==? ".elm") (testsDir </> fname)
42+ let args = [ "--make", "--src-dir=" ++ testsDir, filePath ]
26−
43+ (exitCode, _stdout, stderr) <- readProcessWithExitCode compiler args ""
27− mkTests :: (Either String String -> Assertion) -> [FilePath] -> IO [Test]
44+ return (testCase filePath (handler exitCode stderr))
28− mkTests h = traverse setupTest
45+
29− where setupTest f = testCase f . mkCompileTest h <$> readFile f
31− testsDir :: FilePath
46+compiler :: FilePath
32− testsDir = "tests" </> "compiler" </> "test-files"
47+compiler = "dist" </> "build" </> "Elm" </> "elm"
34−goodCompile :: Either String String -> Assertion
52+isSuccess :: ExitCode -> String -> Assertion
35−goodCompile result =
53+isSuccess exitCode stderr =
36− case result of
54+ case exitCode of
37− Left err -> assertFailure err
55+ ExitSuccess -> assertBool "" True
38− Right _ -> assertBool "" True
56+ ExitFailure _ -> assertFailure stderr
40−badCompile :: Either String String -> Assertion
58+isFailure :: ExitCode -> String -> Assertion
41−badCompile result =
59+isFailure exitCode _stderr =
42− case result of
60+ case exitCode of
43− Left _ -> assertBool "" True
44− Right _ -> assertFailure "Compilation succeeded but should have failed"
61+ ExitSuccess -> assertFailure "Compilation succeeded but should have failed"
45−
62+ ExitFailure _ -> assertBool "" True
46−mkCompileTest :: ((Either String String) -> Assertion) -- ^ Handler
47− -> String -- ^ File Contents
49−mkCompileTest handle = handle . Elm.compile