@@ -39,7 +39,7 @@ compilerTests =
3939 ]
4040
4141
42−-- GATHER ELM FILES
42+-- GATHER FILES
4343
4444getElms :: FilePath -> IO [FilePath]
4545getElms filePath =
@@ -53,8 +53,15 @@ testsDir :: FilePath
5353testsDir =
5454 "tests" </> "test-files"
5555
56+convertToExpectedJsFilePath :: String -> FilePath -> FilePath
57+convertToExpectedJsFilePath expectedJsDir filePath =
58+ testsDir
59+ </> expectedJsDir
60+ </> joinPath (drop 3 (splitDirectories filePath))
61+ <.> ".js"
62+
5663
57−-- RUN COMPILER
64+-- TEST HELPERS
5865
5966compileString :: FilePath -> String -> Either String Compiler.Result
6067compileString filePath source =
@@ -68,18 +75,58 @@ compileString filePath source =
6875 either (Left . formatErrors) Right result
6976
7077
78+readFileOrErrorStr :: FilePath -> IO String
79+readFileOrErrorStr filePath =
80+ do strOrExc <- try $ readFile filePath
81+ case strOrExc of
82+ Right s ->
83+ return s
84+
85+ Left (e :: IOException) ->
86+ return (show e)
87+
88+
89+-- RUN TESTS
90+
7191testIf
7292 :: (Either String Compiler.Result -> Assertion)
7393 -> [FilePath]
7494 -> IO [Test]
7595testIf handleResult filePaths =
76− traverse setupTest filePaths
77− where
78− setupTest filePath =
79− do source <- readFile filePath
80− let formattedResult = compileString filePath source
96+ let doTest filePath =
97+ do source <- readFile filePath
98+ let formattedResult = compileString filePath source
99+ return $ testCase filePath (handleResult formattedResult)
100+ in
101+ traverse doTest filePaths
102+
81103
82− return $ testCase filePath (handleResult formattedResult)
104+doMatchesExpectedTest :: String -> FilePath -> IO Test
105+doMatchesExpectedTest expectedJsDir filePath =
106+ do source <- readFile filePath
107+ expectedJs <- readFileOrErrorStr (convertToExpectedJsFilePath expectedJsDir filePath)
108+
109+ let formattedResult = compileString filePath source
110+ let assertion = matchesExpected expectedJs formattedResult
111+
112+ return $ testCase filePath assertion
113+
114+
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
120+ let assertion =
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)
126+
127+ _ ->
128+ assertFailure "Compile failed. Could not write new expected js."
129+ return $ testCase filePath assertion
83130
84131
85132testMatchesExpected :: String -> String -> IO [Test]
@@ -88,48 +135,9 @@ testMatchesExpected elmDir expectedJsDir =
88135 envVar <- lookupEnv writeExpectedJsEnvVarName
89136 if isJust envVar
90137 then
91− traverse writeNewExpected elmFiles
138+ traverse (doWriteNewExpectedTest expectedJsDir) elmFiles
92139 else
93− traverse doTest elmFiles
94− where
95− getExpectedFilePath filePath =
96− testsDir
97− </> expectedJsDir
98− </> joinPath (drop 3 (splitDirectories filePath))
99− <.> ".js"
100−
101− readFileOrErrorStr filePath =
102− do strOrExc <- try $ readFile filePath
103− case strOrExc of
104− Right s ->
105− return s
106−
107− Left (e :: IOException) ->
108− return (show e)
109−
110− doTest filePath =
111− do source <- readFile filePath
112− expectedJs <- readFileOrErrorStr (getExpectedFilePath filePath)
113−
114− let formattedResult = compileString filePath source
115− let assertion = matchesExpected expectedJs formattedResult
116−
117− return $ testCase filePath assertion
118−
119− writeNewExpected filePath =
120− do source <- readFile filePath
121− let formattedResult = compileString filePath source
122− let expectedFilePath = getExpectedFilePath filePath
123− let assertion =
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)
129−
130− _ ->
131− assertFailure "Compile failed. Could not write new expected js."
132− return $ testCase filePath assertion
140+ traverse (doMatchesExpectedTest expectedJsDir) elmFiles
133141
134142
135143-- CHECK RESULTS