Skip to content

Commit 6ea99ca

Browse files
committed
Check the right kind of error is thrown in tests
Add a basic mechanism for declaring what kind of errors a test should throw, and add a few of these declarations to failing examples.
1 parent ba3f76f commit 6ea99ca

File tree

4 files changed

+53
-10
lines changed

4 files changed

+53
-10
lines changed

examples/failing/365.purs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
-- @shouldFailWith CycleInDeclaration
12
module Main where
23

34
import Prelude

examples/failing/Object.purs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
-- @shouldFailWith PropertyIsMissing
12
module Main where
23

34
import Prelude

examples/failing/OverlappingInstances2.purs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1+
-- @shouldFailWith OverlappingInstances
12
module OverlappingInstances where
23

34
import Prelude
45

56
data A = A | B
67

78
instance eqA1 :: Eq A where
8-
(==) A A = true
9-
(==) B B = true
10-
(==) _ _ = false
11-
(/=) x y = not (x == y)
9+
eq A A = true
10+
eq B B = true
11+
eq _ _ = false
1212

1313
instance eqA2 :: Eq A where
14-
(==) _ _ = true
15-
(/=) _ _ = false
14+
eq _ _ = true
1615

1716
instance ordA :: Ord A where
1817
compare A B = LT

tests/Main.hs

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,30 @@
1818
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
1919
{-# LANGUAGE FlexibleInstances #-}
2020

21+
-- Failing tests can specify the kind of error that should be thrown with a
22+
-- @shouldFailWith declaration. For example:
23+
--
24+
-- "-- @shouldFailWith TypesDoNotUnify"
25+
--
26+
-- will cause the test to fail unless that module fails to compile with exactly
27+
-- one TypesDoNotUnify error.
28+
--
29+
-- If a module is expected to produce multiple type errors, then use multiple
30+
-- @shouldFailWith lines; for example:
31+
--
32+
-- -- @shouldFailWith TypesDoNotUnify
33+
-- -- @shouldFailWith TypesDoNotUnify
34+
-- -- @shouldFailWith TransitiveExportError
35+
2136
module Main (main) where
2237

2338
import qualified Language.PureScript as P
2439
import qualified Language.PureScript.CodeGen.JS as J
2540
import qualified Language.PureScript.CoreFn as CF
2641

27-
import Data.List (isSuffixOf)
42+
import Data.Char (isSpace)
43+
import Data.Maybe (mapMaybe)
44+
import Data.List (isSuffixOf, sort, stripPrefix)
2845
import Data.Traversable (traverse)
2946
import Data.Time.Clock (UTCTime())
3047

@@ -33,6 +50,7 @@ import qualified Data.Map as M
3350
import Control.Monad
3451
import Control.Monad.IO.Class (liftIO)
3552
import Control.Applicative
53+
import Control.Arrow ((>>>))
3654

3755
import Control.Monad.Reader
3856
import Control.Monad.Writer
@@ -146,11 +164,35 @@ assertCompiles inputFiles foreigns = do
146164

147165
assertDoesNotCompile :: [FilePath] -> M.Map P.ModuleName (FilePath, P.ForeignJS) -> IO ()
148166
assertDoesNotCompile inputFiles foreigns = do
149-
putStrLn $ "Assert " ++ last inputFiles ++ " does not compile"
167+
let testFile = last inputFiles
168+
putStrLn $ "Assert " ++ testFile ++ " does not compile"
169+
shouldFailWith <- getShouldFailWith testFile
150170
assert inputFiles foreigns $ \e ->
151171
case e of
152-
Left errs -> putStrLn (P.prettyPrintMultipleErrors False errs) >> return Nothing
153-
Right _ -> return $ Just "Should not have compiled"
172+
Left errs -> do
173+
putStrLn (P.prettyPrintMultipleErrors False errs)
174+
if null shouldFailWith
175+
then return Nothing
176+
else return $ checkShouldFailWith shouldFailWith errs
177+
Right _ ->
178+
return $ Just "Should not have compiled"
179+
180+
where
181+
getShouldFailWith =
182+
readFile
183+
>>> fmap ( lines
184+
>>> mapMaybe (stripPrefix "-- @shouldFailWith ")
185+
>>> map trim
186+
)
187+
188+
checkShouldFailWith expected errs =
189+
let actual = map P.errorCode $ P.runMultipleErrors errs
190+
in if sort expected == sort actual
191+
then Nothing
192+
else Just $ "Expected these errors: " ++ show expected ++ ", but got these: " ++ show actual
193+
194+
trim =
195+
dropWhile isSpace >>> reverse >>> dropWhile isSpace >>> reverse
154196

155197
findNodeProcess :: IO (Maybe String)
156198
findNodeProcess = runMaybeT . msum $ map (MaybeT . findExecutable) names

0 commit comments

Comments
 (0)