on master help sign in
Begin testing custom imports and exports
28e4d32Evan Czaplicki
Elm.cabal+1
@@ -291,6 +291,7 @@ Test-Suite compiler-tests
291 mtl >= 2 && < 3,
291 mtl >= 2 && < 3,
292 parsec >= 3.1.1 && < 3.5,
292 parsec >= 3.1.1 && < 3.5,
293 pretty >= 1.0 && < 2.0,
293 pretty >= 1.0 && < 2.0,
294+ process,
294 text >= 1 && < 2,
295 text >= 1 && < 2,
295 transformers >= 0.2 && < 0.5,
296 transformers >= 0.2 && < 0.5,
296 union-find >= 0.2 && < 0.3,
297 union-find >= 0.2 && < 0.3,
tests/compiler/Tests/Compiler.hs+49−36
@@ -1,49 +1,62 @@
1+{-# OPTIONS_GHC -W #-}
1module Tests.Compiler (compilerTests) where
2module Tests.Compiler (compilerTests) where
2
3
3−import Data.Functor ((<$>))
4−import Data.Traversable (traverse)
4+import Data.Traversable (traverse)
5+
5−import System.FilePath ((</>))
6+import System.Exit
7+import System.FilePath ((</>))
6−import System.FilePath.Find (find, (==?), extension)
8+import System.FilePath.Find (find, (==?), extension)
9+import System.Process (readProcessWithExitCode)
10+
7import Test.Framework
11import Test.Framework
8import Test.Framework.Providers.HUnit (testCase)
12import Test.Framework.Providers.HUnit (testCase)
9−import Test.HUnit (Assertion, assertFailure, assertBool)
13+import Test.HUnit (Assertion, assertFailure, assertBool)
10−import Text.Parsec (ParseError)
11−
12−import Elm.Internal.Utils as Elm
13
14
14compilerTests :: Test
15compilerTests :: Test
15compilerTests =
16compilerTests =
16 buildTest $ do
17 buildTest $ do
17− goods <- mkTests goodCompile =<< getElms "good"
18+ goods <- testIf isSuccess =<< getElms "good"
18− bads <- mkTests badCompile =<< getElms "bad"
19+ bads <- testIf isFailure =<< getElms "bad"
19 return $ testGroup "Compile Tests"
20 return $ testGroup "Compile Tests"
20 [ testGroup "Good Tests" goods
21 [ testGroup "Good Tests" goods
21 , testGroup "Bad Tests" bads
22 , testGroup "Bad Tests" bads
22 ]
23 ]
24+
25+-- COLLECT FILES
26+
27+getElms :: FilePath -> IO [FilePath]
28+getElms filePath =
29+ find (return True) (extension ==? ".elm") (testsDir </> filePath)
30+
31+testsDir :: FilePath
32+testsDir = "tests" </> "compiler" </> "test-files"
33+
34+
35+-- RUN COMPILER
36+
37+testIf :: (ExitCode -> String -> Assertion) -> [FilePath] -> IO [Test]
38+testIf handler filePaths =
39+ traverse setupTest filePaths
23 where
40 where
24− getElms :: FilePath -> IO [FilePath]
41+ setupTest filePath = do
25− getElms fname = find (return True) (extension ==? ".elm") (testsDir </> fname)
42+ let args = [ "--make", "--src-dir=" ++ testsDir, filePath ]
26−
43+ (exitCode, _stdout, stderr) <- readProcessWithExitCode compiler args ""
27− mkTests :: (Either String String -> Assertion) -> [FilePath] -> IO [Test]
44+ return (testCase filePath (handler exitCode stderr))
28− mkTests h = traverse setupTest
45+
29− where setupTest f = testCase f . mkCompileTest h <$> readFile f
30−
31− testsDir :: FilePath
46+compiler :: FilePath
32− testsDir = "tests" </> "compiler" </> "test-files"
47+compiler = "dist" </> "build" </> "Elm" </> "elm"
33−
48+
49+
50+-- CHECK RESULTS
51+
34−goodCompile :: Either String String -> Assertion
52+isSuccess :: ExitCode -> String -> Assertion
35−goodCompile result =
53+isSuccess exitCode stderr =
36− case result of
54+ case exitCode of
37− Left err -> assertFailure err
55+ ExitSuccess -> assertBool "" True
38− Right _ -> assertBool "" True
56+ ExitFailure _ -> assertFailure stderr
39−
57+
40−badCompile :: Either String String -> Assertion
58+isFailure :: ExitCode -> String -> Assertion
41−badCompile result =
59+isFailure exitCode _stderr =
42− case result of
60+ case exitCode of
43− Left _ -> assertBool "" True
44− Right _ -> assertFailure "Compilation succeeded but should have failed"
61+ ExitSuccess -> assertFailure "Compilation succeeded but should have failed"
45−
62+ ExitFailure _ -> assertBool "" True
46−mkCompileTest :: ((Either String String) -> Assertion) -- ^ Handler
47− -> String -- ^ File Contents
48− -> Assertion
49−mkCompileTest handle = handle . Elm.compile
tests/compiler/test-files/Imports.elmnew file+22
@@ -0,0 +1,22 @@
1+module Imports (Point, BooleanExpr(Not), true, false, adt, func) where
2+
3+type Point = { x : Float, y : Float }
4+
5+data BooleanExpr
6+ = T
7+ | F
8+ | Not BooleanExpr
9+ | And BooleanExpr BooleanExpr
10+ | Or BooleanExpr BooleanExpr
11+
12+true = T
13+
14+false = F
15+
16+adt : HiddenADT
17+adt = HiddenADT
18+
19+func : HiddenADT -> Int
20+func adt = 42
21+
22+data HiddenADT = HiddenADT
tests/compiler/test-files/bad/Imports/UseHiddenValues.elmnew file+4
@@ -0,0 +1,4 @@
1+
2+import Imports (BooleanExpr(..))
3+
4+bool = Or (Not T) F
tests/compiler/test-files/good/OverlappingImports.elm → tests/compiler/test-files/good/Imports/OverlappingImports.elmrenamed

The content is the same.

tests/compiler/test-files/good/Imports/UseHiddenTypes.elmnew file+4
@@ -0,0 +1,4 @@
1+
2+import Imports (adt, func)
3+
4+fortyTwo = func adt
tests/compiler/test-files/good/Imports/UseTypeConstructor.elmnew file+4
@@ -0,0 +1,4 @@
1+
2+import Imports (BooleanExpr(..), true, false)
3+
4+bool = Not true