2{-# LANGUAGE ScopedTypeVariables #-}
3module Test.Compiler (compilerTests) where
5import Control.Exception (try, IOException)
7import qualified Data.Map as Map
8import Data.Maybe (isJust)
10import System.Directory (createDirectoryIfMissing)
11import System.Environment (lookupEnv)
12import System.FilePath ((</>), (<.>), dropFileName, joinPath, splitDirectories)
13import System.FilePath.Find (find, (==?), extension)
16import Test.Framework.Providers.HUnit (testCase)
17import Test.HUnit (Assertion, assertFailure, assertBool, assertEqual)
19import qualified Elm.Compiler as Compiler
20import qualified Elm.Package as Package
22writeExpectedJsEnvVarName :: String
23writeExpectedJsEnvVarName =
24 "ELM_WRITE_NEW_EXPECTED"
30 do goods <- testIf isSuccess =<< getElms "good"
31 bads <- testIf isFailure =<< getElms "bad"
32 expectedJsTests <- testMatchesExpected "good" "good-expected-js"
34 testGroup "Compile Tests"
35 [ testGroup "Good Tests" goods
36 , testGroup "Bad Tests" bads
37 , testGroup "Expected JS Tests" expectedJsTests
43getElms :: FilePath -> IO [FilePath]
47 (extension ==? ".elm")
48 (testsDir </> filePath)
53 "tests" </> "test-files"
55convertToExpectedJsFilePath :: String -> FilePath -> FilePath
56convertToExpectedJsFilePath expectedJsDir filePath =
59 </> joinPath (drop 3 (splitDirectories filePath))
65compileString :: FilePath -> String -> Either String Compiler.Result
66compileString filePath source =
68 Compiler.Context Package.coreName True False []
69 (dealiaser, _warnings, result) =
70 Compiler.compile context source Map.empty
72 concatMap (Compiler.errorToString dealiaser filePath source) errors
74 either (Left . formatErrors) Right result
77readFileOrErrorStr :: FilePath -> IO String
78readFileOrErrorStr filePath =
79 do strOrExc <- try $ readFile filePath
84 Left (e :: IOException) ->
91 :: (Either String Compiler.Result -> Assertion)
94testIf handleResult filePaths =
96 do source <- readFile filePath
97 let formattedResult = compileString filePath source
98 return $ testCase filePath (handleResult formattedResult)
100 traverse doTest filePaths
103doMatchesExpectedTest :: String -> FilePath -> IO Test
104doMatchesExpectedTest expectedJsDir filePath =
105 do source <- readFile filePath
106 expectedJs <- readFileOrErrorStr (convertToExpectedJsFilePath expectedJsDir filePath)
108 let formattedResult = compileString filePath source
109 let assertion = matchesExpected expectedJs formattedResult
111 return $ testCase filePath assertion
114doWriteNewExpectedTest :: String -> FilePath -> IO Test
115doWriteNewExpectedTest expectedJsDir filePath =
116 do source <- readFile filePath
117 let formattedResult = compileString filePath source
118 let expectedFilePath = convertToExpectedJsFilePath expectedJsDir filePath
120 case formattedResult of
121 Right (Compiler.Result _ _ js) ->
122 do createDirectoryIfMissing True (dropFileName expectedFilePath)
123 -- Force the evaluation of `js` before `writeFile`
124 -- so that if an error is raised we do not unintentionally write an empty file.
125 -- NB: `js` can be an empty string in the case of NoExpressions.elm
126 assertBool "" (length js >= 0)
127 writeFile expectedFilePath js
128 assertFailure ("Wrote new expected js: " ++ expectedFilePath)
131 assertFailure "Compile failed. Could not write new expected js."
132 return $ testCase filePath assertion
135testMatchesExpected :: String -> String -> IO [Test]
136testMatchesExpected elmDir expectedJsDir =
137 do elmFiles <- getElms elmDir
138 envVar <- lookupEnv writeExpectedJsEnvVarName
141 traverse (doWriteNewExpectedTest expectedJsDir) elmFiles
143 traverse (doMatchesExpectedTest expectedJsDir) elmFiles
148isSuccess :: Either String a -> Assertion
154 Left errorMessages ->
155 assertFailure errorMessages
158isFailure :: Either a b -> Assertion
162 assertFailure "Compilation succeeded but should have failed"
168matchesExpected :: String -> Either String Compiler.Result -> Assertion
169matchesExpected expectedJs result =
171 Right (Compiler.Result _ _ js) ->
172 assertEqual matchFailureMessage expectedJs js
174 Left errorMessages ->
175 assertFailure $ "Compile failed:\n" ++ errorMessages
178matchFailureMessage :: String
180 "Compiled JS did not match expected JS." ++
181 "\n If the change is intentional, rerun " ++
182 "the tests with environment variable: " ++ writeExpectedJsEnvVarName ++ "=1"
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list