on master help sign in
Compiler tests: remove where clauses
71d95c5Laszlo Pandy
tests/Test/Compiler.hs+57−49
@@ -39,7 +39,7 @@ compilerTests =
39 ]
39 ]
40
40
41
41
42−-- GATHER ELM FILES
42+-- GATHER FILES
43
43
44getElms :: FilePath -> IO [FilePath]
44getElms :: FilePath -> IO [FilePath]
45getElms filePath =
45getElms filePath =
@@ -53,8 +53,15 @@ testsDir :: FilePath
53testsDir =
53testsDir =
54 "tests" </> "test-files"
54 "tests" </> "test-files"
55
55
56+convertToExpectedJsFilePath :: String -> FilePath -> FilePath
57+convertToExpectedJsFilePath expectedJsDir filePath =
58+ testsDir
59+ </> expectedJsDir
60+ </> joinPath (drop 3 (splitDirectories filePath))
61+ <.> ".js"
62+
56
63
57−-- RUN COMPILER
64+-- TEST HELPERS
58
65
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
69
76
70
77
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+
71testIf
91testIf
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
77− where
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)
100+ in
101+ traverse doTest filePaths
102+
81
103
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+
82− return $ testCase filePath (handleResult formattedResult)
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
83
130
84
131
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
90 then
137 then
91− traverse writeNewExpected elmFiles
138+ traverse (doWriteNewExpectedTest expectedJsDir) elmFiles
92 else
139 else
93− traverse doTest elmFiles
94− where
95− getExpectedFilePath filePath =
96− testsDir
97− </> expectedJsDir
140+ traverse (doMatchesExpectedTest expectedJsDir) elmFiles
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
133
141
134
142
135-- CHECK RESULTS
143-- CHECK RESULTS