@@ -39,7 +39,7 @@ compilerTests =
42−-- GATHER ELM FILES
42+-- GATHER FILES
44getElms :: FilePath -> IO [FilePath]
44getElms :: FilePath -> IO [FilePath]
45getElms filePath =
45getElms filePath =
@@ -53,8 +53,15 @@ testsDir :: FilePath
54 "tests" </> "test-files"
54 "tests" </> "test-files"
56+convertToExpectedJsFilePath :: String -> FilePath -> FilePath
57+convertToExpectedJsFilePath expectedJsDir filePath =
60+ </> joinPath (drop 3 (splitDirectories filePath))
57−-- RUN COMPILER
64+-- TEST HELPERS
59compileString :: FilePath -> String -> Either String Compiler.Result
66compileString :: FilePath -> String -> Either String Compiler.Result
60compileString filePath source =
67compileString filePath source =
@@ -68,18 +75,58 @@ compileString filePath source =
68 either (Left . formatErrors) Right result
75 either (Left . formatErrors) Right result
78+readFileOrErrorStr :: FilePath -> IO String
79+readFileOrErrorStr filePath =
80+ do strOrExc <- try $ readFile filePath
85+ Left (e :: IOException) ->
72 :: (Either String Compiler.Result -> Assertion)
92 :: (Either String Compiler.Result -> Assertion)
73 -> [FilePath]
93 -> [FilePath]
74 -> IO [Test]
94 -> IO [Test]
75testIf handleResult filePaths =
95testIf handleResult filePaths =
76− traverse setupTest filePaths
78− setupTest filePath =
96+ let doTest filePath =
79− do source <- readFile filePath
97+ do source <- readFile filePath
80− let formattedResult = compileString filePath source
98+ let formattedResult = compileString filePath source
99+ return $ testCase filePath (handleResult formattedResult)
101+ traverse doTest filePaths
104+doMatchesExpectedTest :: String -> FilePath -> IO Test
105+doMatchesExpectedTest expectedJsDir filePath =
106+ do source <- readFile filePath
107+ expectedJs <- readFileOrErrorStr (convertToExpectedJsFilePath expectedJsDir filePath)
109+ let formattedResult = compileString filePath source
110+ let assertion = matchesExpected expectedJs formattedResult
82− return $ testCase filePath (handleResult formattedResult)
112+ return $ testCase filePath assertion
115+doWriteNewExpectedTest :: String -> FilePath -> IO Test
116+doWriteNewExpectedTest expectedJsDir filePath =
117+ do source <- readFile filePath
118+ let formattedResult = compileString filePath source
119+ let expectedFilePath = convertToExpectedJsFilePath expectedJsDir filePath
121+ case formattedResult of
122+ Right (Compiler.Result _ _ js) ->
123+ do createDirectoryIfMissing True (dropFileName expectedFilePath)
124+ writeFile expectedFilePath js
125+ assertFailure ("Wrote new expected js: " ++ expectedFilePath)
128+ assertFailure "Compile failed. Could not write new expected js."
129+ return $ testCase filePath assertion
85testMatchesExpected :: String -> String -> IO [Test]
132testMatchesExpected :: String -> String -> IO [Test]
@@ -88,48 +135,9 @@ testMatchesExpected elmDir expectedJsDir =
88 envVar <- lookupEnv writeExpectedJsEnvVarName
135 envVar <- lookupEnv writeExpectedJsEnvVarName
89 if isJust envVar
136 if isJust envVar
91− traverse writeNewExpected elmFiles
138+ traverse (doWriteNewExpectedTest expectedJsDir) elmFiles
93− traverse doTest elmFiles
95− getExpectedFilePath filePath =
97− </> expectedJsDir
140+ traverse (doMatchesExpectedTest expectedJsDir) elmFiles
98− </> joinPath (drop 3 (splitDirectories filePath))
101− readFileOrErrorStr filePath =
102− do strOrExc <- try $ readFile filePath
107− Left (e :: IOException) ->
111− do source <- readFile filePath
112− expectedJs <- readFileOrErrorStr (getExpectedFilePath filePath)
114− let formattedResult = compileString filePath source
115− let assertion = matchesExpected expectedJs formattedResult
117− return $ testCase filePath assertion
119− writeNewExpected filePath =
120− do source <- readFile filePath
121− let formattedResult = compileString filePath source
122− let expectedFilePath = getExpectedFilePath filePath
124− case formattedResult of
125− Right (Compiler.Result _ _ js) ->
126− do createDirectoryIfMissing True (dropFileName expectedFilePath)
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
135-- CHECK RESULTS
143-- CHECK RESULTS