~jojo/Carth

ref: 5f4549698c6783d9dbed5f2bf6b12d537a529314 Carth/test/SystemSpec.hs -rw-r--r-- 1.6 KiB
5f454969JoJo Test that examples and benchmarks compile 1 year, 2 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
{-# LANGUAGE LambdaCase #-}

module SystemSpec where

import Data.Data
import Data.Functor
import Control.Monad
import System.Directory
import System.FilePath
import Test.Hspec

import Parse
import Check
import Compile
import Monomorphize
import Conf

spec :: Spec
spec = do
    describe "Examples compile" $ do
        let d = "examples"
        fs <- runIO $ listDirectory d <&> filter isSourceFile
        forM_ fs $ \f -> do
            it (dropExtension f) $ shouldReturn (compile' (d </> f)) True
    describe "Benchmarks compile" $ do
        let d = "test/bench"
        fs <- runIO $ listDirectory d <&> filter isSourceFile
        forM_ fs $ \f -> do
            it (dropExtension f) $ shouldReturn (compile' (d </> f)) True
    describe "Bad programs don't typecheck" $ do
        let d = "test/tests/bad"
        fs <- runIO $ listDirectory d <&> filter isSourceFile
        forM_ fs $ \f -> do
            expectedErr <- runIO $ fmap (drop 3 . head . lines) (readFile (d </> f))
            result <- runIO $ parse (d </> f)
            it (dropExtension f) $ shouldSatisfy (fmap typecheck result) $ \case
                Right (Left e) -> show (toConstr e) == expectedErr
                _ -> False

isSourceFile :: FilePath -> Bool
isSourceFile f = let e = takeExtension f in e == ".carth" || e == ".org"

compile' :: FilePath -> IO Bool
compile' f =
    let cfg = defaultCompileConfig f (dropExtension f)
    in  Parse.parse f >>= \case
            Left _ -> pure False
            Right ast -> case typecheck ast of
                Left _ -> pure False
                Right ann -> compile f cfg (monomorphize ann) $> True