on master help sign in
Begin testing custom imports and exports
commit28e4d328fd35a507fe45b70ac122fbc747fc5e5e authorEvan Czaplicki <info@elm-lang.org> dateSun 24 Aug 2014, 19:12 (12 years ago) parent14ef71a
7 files changed+84−36
Elm.cabal+1 tests/compiler/Tests/Compiler.hs+49−36 tests/compiler/test-files/Imports.elm+22 tests/compiler/test-files/bad/Imports/UseHiddenValues.elm+4 tests/compiler/test-files/good/OverlappingImports.elm → tests/compiler/test-files/good/Imports/OverlappingImports.elm tests/compiler/test-files/good/Imports/UseHiddenTypes.elm+4 tests/compiler/test-files/good/Imports/UseTypeConstructor.elm+4
Elm.cabal+1
@@ -291,6 +291,7 @@ Test-Suite compiler-tests
291291 mtl >= 2 && < 3,
292292 parsec >= 3.1.1 && < 3.5,
293293 pretty >= 1.0 && < 2.0,
294+ process,
294295 text >= 1 && < 2,
295296 transformers >= 0.2 && < 0.5,
296297 union-find >= 0.2 && < 0.3,
tests/compiler/Tests/Compiler.hs+49−36
@@ -1,49 +1,62 @@
1+{-# OPTIONS_GHC -W #-}
12module Tests.Compiler (compilerTests) where
23
3−import Data.Functor ((<$>))
4−import Data.Traversable (traverse)
5−import System.FilePath ((</>))
6−import System.FilePath.Find (find, (==?), extension)
4+import Data.Traversable (traverse)
5+
6+import System.Exit
7+import System.FilePath ((</>))
8+import System.FilePath.Find (find, (==?), extension)
9+import System.Process (readProcessWithExitCode)
10+
711import Test.Framework
812import Test.Framework.Providers.HUnit (testCase)
9−import Test.HUnit (Assertion, assertFailure, assertBool)
10−import Text.Parsec (ParseError)
11−
12−import Elm.Internal.Utils as Elm
13+import Test.HUnit (Assertion, assertFailure, assertBool)
1314
1415compilerTests :: Test
1516compilerTests =
1617 buildTest $ do
17− goods <- mkTests goodCompile =<< getElms "good"
18− bads <- mkTests badCompile =<< getElms "bad"
18+ goods <- testIf isSuccess =<< getElms "good"
19+ bads <- testIf isFailure =<< getElms "bad"
1920 return $ testGroup "Compile Tests"
2021 [ testGroup "Good Tests" goods
2122 , testGroup "Bad Tests" bads
2223 ]
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
2340 where
24− getElms :: FilePath -> IO [FilePath]
25− getElms fname = find (return True) (extension ==? ".elm") (testsDir </> fname)
26−
27− mkTests :: (Either String String -> Assertion) -> [FilePath] -> IO [Test]
28− mkTests h = traverse setupTest
29− where setupTest f = testCase f . mkCompileTest h <$> readFile f
30−
31− testsDir :: FilePath
32− testsDir = "tests" </> "compiler" </> "test-files"
33−
34−goodCompile :: Either String String -> Assertion
35−goodCompile result =
36− case result of
37− Left err -> assertFailure err
38− Right _ -> assertBool "" True
39−
40−badCompile :: Either String String -> Assertion
41−badCompile result =
42− case result of
43− Left _ -> assertBool "" True
44− Right _ -> assertFailure "Compilation succeeded but should have failed"
45−
46−mkCompileTest :: ((Either String String) -> Assertion) -- ^ Handler
47− -> String -- ^ File Contents
48− -> Assertion
49−mkCompileTest handle = handle . Elm.compile
41+ setupTest filePath = do
42+ let args = [ "--make", "--src-dir=" ++ testsDir, filePath ]
43+ (exitCode, _stdout, stderr) <- readProcessWithExitCode compiler args ""
44+ return (testCase filePath (handler exitCode stderr))
45+
46+compiler :: FilePath
47+compiler = "dist" </> "build" </> "Elm" </> "elm"
48+
49+
50+-- CHECK RESULTS
51+
52+isSuccess :: ExitCode -> String -> Assertion
53+isSuccess exitCode stderr =
54+ case exitCode of
55+ ExitSuccess -> assertBool "" True
56+ ExitFailure _ -> assertFailure stderr
57+
58+isFailure :: ExitCode -> String -> Assertion
59+isFailure exitCode _stderr =
60+ case exitCode of
61+ ExitSuccess -> assertFailure "Compilation succeeded but should have failed"
62+ ExitFailure _ -> assertBool "" True
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