git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Merge pull request #2119 from hdgarrood/require-main

Check that passing tests have a Main module

commit51ccb1db3e803ea72a5ed289d1d023d605bbf602 authorPhil Freeman <paf31@cantab.net> dateFri 13 May 2016, 09:54 (10 years ago) parent7620ec3c6d2465
2 files changed+62−52
examples/passing/ExportedInstanceDeclarations.purs+45−44 tests/TestCompiler.hs+17−8
examples/passing/ExportedInstanceDeclarations.purs+45−44
@@ -1,44 +1,45 @@
1−-- Tests that instances for non-exported classes / types do not appear in the
2−-- result of `exportedDeclarations`.
3−module ExportedInstanceDeclarations
4− ( Const(..)
5− , class Foo
6− , foo
7− ) where
8−
9−import Prelude
10−import Control.Monad.Eff.Console (log)
11−
12−data Const a b = Const a
13−
14−class Foo a where
15− foo :: a
16−
17−data NonexportedType = NonexportedType
18−
19−class NonexportedClass a where
20− notExported :: a
21−
22−-- There are three places that a nonexported type or type class can occur,
23−-- leading an instance to count as non-exported:
24−-- * Constraints
25−-- * The type class itself
26−-- * The instance types
27−
28−-- Case 1: constraints
29−instance nonExportedFoo :: (NonexportedClass a) => Foo a where
30− foo = notExported
31−
32−-- Another instance of case 1:
33−instance nonExportedFoo2 :: (Foo NonexportedType) => Foo (a -> a) where
34− foo = id
35−
36−-- Case 2: type class
37−instance nonExportedNonexportedType :: NonexportedClass (Const Int a) where
38− notExported = Const 0
39−
40−-- Case 3: instance types
41−instance constFoo :: Foo (Const NonexportedType b) where
42− foo = Const NonexportedType
43−
44−main = log "Done"
1+-- Tests that instances for non-exported classes / types do not appear in the
2+-- result of `exportedDeclarations`.
3+module Main
4+ ( Const(..)
5+ , class Foo
6+ , foo
7+ , main
8+ ) where
9+
10+import Prelude
11+import Control.Monad.Eff.Console (log)
12+
13+data Const a b = Const a
14+
15+class Foo a where
16+ foo :: a
17+
18+data NonexportedType = NonexportedType
19+
20+class NonexportedClass a where
21+ notExported :: a
22+
23+-- There are three places that a nonexported type or type class can occur,
24+-- leading an instance to count as non-exported:
25+-- * Constraints
26+-- * The type class itself
27+-- * The instance types
28+
29+-- Case 1: constraints
30+instance nonExportedFoo :: (NonexportedClass a) => Foo a where
31+ foo = notExported
32+
33+-- Another instance of case 1:
34+instance nonExportedFoo2 :: (Foo NonexportedType) => Foo (a -> a) where
35+ foo = id
36+
37+-- Case 2: type class
38+instance nonExportedNonexportedType :: NonexportedClass (Const Int a) where
39+ notExported = Const 0
40+
41+-- Case 3: instance types
42+instance constFoo :: Foo (Const NonexportedType b) where
43+ foo = Const NonexportedType
44+
45+main = log "Done"
tests/TestCompiler.hs+17−8
@@ -176,10 +176,12 @@ compile
176176 :: [(P.Module, P.ExternsFile)]
177177 -> [FilePath]
178178 -> M.Map P.ModuleName FilePath
179+ -> ([P.Module] -> IO ())
179180 -> IO (Either P.MultipleErrors [P.ExternsFile])
180−compile supportExterns inputFiles foreigns = silence $ runTest $ do
181+compile supportExterns inputFiles foreigns check = silence $ runTest $ do
181182 fs <- liftIO $ readInput inputFiles
182183 ms <- P.parseModulesFromFiles id fs
184+ liftIO (check (map snd ms))
183185 let actions = makeActions foreigns
184186 case ms of
185187 [singleModule] -> pure <$> P.rebuildModule actions (map snd supportExterns) (snd singleModule)
@@ -189,10 +191,11 @@ assert
189191 :: [(P.Module, P.ExternsFile)]
190192 -> [FilePath]
191193 -> M.Map P.ModuleName FilePath
194+ -> ([P.Module] -> IO ())
192195 -> (Either P.MultipleErrors [P.ExternsFile] -> IO (Maybe String))
193196 -> Expectation
194−assert supportExterns inputFiles foreigns f = do
195− e <- compile supportExterns inputFiles foreigns
197+assert supportExterns inputFiles foreigns check f = do
198+ e <- compile supportExterns inputFiles foreigns check
196199 maybeErr <- f e
197200 maybe (return ()) expectationFailure maybeErr
198201
@@ -201,8 +204,8 @@ assertCompiles
201204 -> [FilePath]
202205 -> M.Map P.ModuleName FilePath
203206 -> Expectation
204−assertCompiles supportExterns inputFiles foreigns = do
205− assert supportExterns inputFiles foreigns $ \e ->
207+assertCompiles supportExterns inputFiles foreigns =
208+ assert supportExterns inputFiles foreigns checkMain $ \e ->
206209 case e of
207210 Left errs -> return . Just . P.prettyPrintMultipleErrors False $ errs
208211 Right _ -> do
@@ -217,6 +220,10 @@ assertCompiles supportExterns inputFiles foreigns = do
217220 | otherwise -> return $ Just $ "Test did not finish with 'Done':\n\n" <> out
218221 Just (ExitFailure _, _, err) -> return $ Just err
219222 Nothing -> return $ Just "Couldn't find node.js executable"
223+ where
224+ checkMain ms =
225+ unless (any ((== P.moduleNameFromString "Main") . P.getModuleName) ms)
226+ (fail "Main module missing")
220227
221228assertDoesNotCompile
222229 :: [(P.Module, P.ExternsFile)]
@@ -224,10 +231,10 @@ assertDoesNotCompile
224231 -> M.Map P.ModuleName FilePath
225232 -> [String]
226233 -> Expectation
227−assertDoesNotCompile supportExterns inputFiles foreigns shouldFailWith = do
228− assert supportExterns inputFiles foreigns $ \e ->
234+assertDoesNotCompile supportExterns inputFiles foreigns shouldFailWith =
235+ assert supportExterns inputFiles foreigns noPreCheck $ \e ->
229236 case e of
230− Left errs -> do
237+ Left errs ->
231238 return $ if null shouldFailWith
232239 then Just $ "shouldFailWith declaration is missing (errors were: "
233240 ++ show (map P.errorCode (P.runMultipleErrors errs))
@@ -237,6 +244,8 @@ assertDoesNotCompile supportExterns inputFiles foreigns shouldFailWith = do
237244 return $ Just "Should not have compiled"
238245
239246 where
247+ noPreCheck = const (return ())
248+
240249 checkShouldFailWith expected errs =
241250 let actual = map P.errorCode $ P.runMultipleErrors errs
242251 in if sort expected == sort actual