2{-# LANGUAGE ScopedTypeVariables #-}
3module Test.Compiler (compilerTests) where
5import Control.Exception (try, IOException)
7import qualified Data.Map as Map
8import Data.Traversable (traverse)
10import System.FilePath ((</>), joinPath, splitDirectories)
11import System.FilePath.Find (find, (==?), extension)
14import Test.Framework.Providers.HUnit (testCase)
15import Test.HUnit (Assertion, assertFailure, assertBool, assertEqual)
17import qualified Elm.Compiler as Compiler
18import qualified Elm.Package as Package
23 goods <- testIf isSuccess =<< getElms "good"
24 bads <- testIf isFailure =<< getElms "bad"
25 expectedJsTests <- testMatchesExpected "good" "good-expected-js"
27 testGroup "Compile Tests"
28 [ testGroup "Good Tests" goods
29 , testGroup "Bad Tests" bads
30 , testGroup "Expected JS Tests" expectedJsTests
36getElms :: FilePath -> IO [FilePath]
40 (extension ==? ".elm")
41 (testsDir </> filePath)
46 "tests" </> "test-files"
51compileString :: FilePath -> String -> Either String Compiler.Result
52compileString filePath source =
54 Compiler.Context Package.coreName True False []
55 (dealiaser, _warnings, result) =
56 Compiler.compile context source Map.empty
58 concatMap (Compiler.errorToString dealiaser filePath source) errors
60 either (Left . formatErrors) Right result
64 :: (Either String Compiler.Result -> Assertion)
67testIf handleResult filePaths =
68 traverse setupTest filePaths
70 setupTest filePath = do
71 source <- readFile filePath
72 let formattedResult = compileString filePath source
74 return $ testCase filePath (handleResult formattedResult)
77testMatchesExpected :: String -> String -> IO [Test]
78testMatchesExpected 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
102isSuccess :: Either String a -> Assertion
108 Left errorMessages ->
109 assertFailure errorMessages
112isFailure :: Either a b -> Assertion
116 assertFailure "Compilation succeeded but should have failed"
122matchesExpected :: String -> Either String Compiler.Result -> Assertion
123matchesExpected expectedJs result =
125 Right (Compiler.Result _ _ js) ->
126 assertEqual matchFailureMessage expectedJs js
129 assertFailure "compile failed; could not match expected output"
132matchFailureMessage :: String
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"
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