@@ -1,15 +1,18 @@
1{-# OPTIONS_GHC -W #-}
1{-# OPTIONS_GHC -W #-}
2+{-# LANGUAGE ScopedTypeVariables #-}
2module Test.Compiler (compilerTests) where
3module Test.Compiler (compilerTests) where
5+import Control.Exception (try, IOException)
4import qualified Data.Map as Map
7import qualified Data.Map as Map
5import Data.Traversable (traverse)
8import Data.Traversable (traverse)
7−import System.FilePath ((</>))
10+import System.FilePath ((</>), joinPath, splitDirectories)
8import System.FilePath.Find (find, (==?), extension)
11import System.FilePath.Find (find, (==?), extension)
10import Test.Framework
13import Test.Framework
11import Test.Framework.Providers.HUnit (testCase)
14import Test.Framework.Providers.HUnit (testCase)
12−import Test.HUnit (Assertion, assertFailure, assertBool)
15+import Test.HUnit (Assertion, assertFailure, assertBool, assertEqual)
14import qualified Elm.Compiler as Compiler
17import qualified Elm.Compiler as Compiler
15import qualified Elm.Package as Package
18import qualified Elm.Package as Package
@@ -19,10 +22,12 @@ compilerTests =
19 buildTest $ do
22 buildTest $ do
20 goods <- testIf isSuccess =<< getElms "good"
23 goods <- testIf isSuccess =<< getElms "good"
21 bads <- testIf isFailure =<< getElms "bad"
24 bads <- testIf isFailure =<< getElms "bad"
25+ expectedJsTests <- testMatchesExpected "good" "good-expected-js"
23 testGroup "Compile Tests"
27 testGroup "Compile Tests"
24 [ testGroup "Good Tests" goods
28 [ testGroup "Good Tests" goods
25 , testGroup "Bad Tests" bads
29 , testGroup "Bad Tests" bads
30+ , testGroup "Expected JS Tests" expectedJsTests
@@ -43,6 +48,18 @@ testsDir =
44-- RUN COMPILER
49-- RUN COMPILER
51+compileString :: FilePath -> String -> Either String Compiler.Result
52+compileString filePath source =
54+ Compiler.Context Package.coreName True False []
55+ (dealiaser, _warnings, result) =
56+ Compiler.compile context source Map.empty
57+ formatErrors errors =
58+ concatMap (Compiler.errorToString dealiaser filePath source) errors
60+ either (Left . formatErrors) Right result
47 :: (Either String Compiler.Result -> Assertion)
64 :: (Either String Compiler.Result -> Assertion)
48 -> [FilePath]
65 -> [FilePath]
@@ -52,19 +69,34 @@ testIf handleResult filePaths =
53 setupTest filePath = do
70 setupTest filePath = do
54 source <- readFile filePath
71 source <- readFile filePath
57− Compiler.Context Package.coreName 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 =
72+ let formattedResult = compileString filePath source
63− either (Left . formatErrors) Right result
65 return $ testCase filePath (handleResult formattedResult)
74 return $ testCase filePath (handleResult formattedResult)
77+testMatchesExpected :: String -> String -> IO [Test]
78+testMatchesExpected elmDir expectedJsDir =
79+ traverse doTest =<< getElms elmDir
81+ readFileOrErrorStr filePath = do
82+ strOrExc <- try $ readFile filePath
85+ Left (e :: IOException) -> return (show e)
88+ source <- readFile filePath
90+ let expectedFilePath =
91+ testsDir </> expectedJsDir </> joinPath (drop 3 (splitDirectories filePath)) ++ ".js"
92+ expectedJs <- readFileOrErrorStr expectedFilePath
94+ let formattedResult = compileString filePath source
95+ assertion = matchesExpected expectedJs formattedResult
97+ return $ testCase filePath assertion
68-- CHECK RESULTS
100-- CHECK RESULTS
70isSuccess :: Either String a -> Assertion
102isSuccess :: Either String a -> Assertion
@@ -86,3 +118,19 @@ isFailure result =
86 Left _ ->
118 Left _ ->
87 assertBool "" True
119 assertBool "" True
122+matchesExpected :: String -> Either String Compiler.Result -> Assertion
123+matchesExpected expectedJs result =
125+ Right (Compiler.Result _ _ js) ->
126+ assertEqual matchFailureMessage expectedJs js
129+ assertFailure "compile failed; could not match expected output"
132+matchFailureMessage :: String
133+matchFailureMessage =
134+ "Compiled JS did not match expected JS." ++
135+ "\n If the change is intentional, rerun " ++
136+ "the tests with environment variable: ELM_WRITE_NEW_EXPECTED=1"