Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fizruk committed Aug 23, 2024
1 parent 99e98c7 commit 798f112
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 9 deletions.
28 changes: 27 additions & 1 deletion free-foil-hm.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -111,5 +111,31 @@ test-suite doctests
, bifunctors
, containers
, doctest-parallel
, free-foil
, free-foil >=0.0.3
, free-foil-hm
default-language: Haskell2010

test-suite spec
type: exitcode-stdio-1.0
main-is: Spec.hs
other-modules:
HM.InterpretSpec
Paths_free_foil_hm
hs-source-dirs:
test
ghc-options: -Wall -threaded -rtsopts -with-rtsopts=-N
build-tools:
alex >=3.2.4
, happy >=1.19.9
build-tool-depends:
BNFC:bnfc >=2.9.4.1
build-depends:
array >=0.5.3.0
, base >=4.7 && <5
, bifunctors
, containers
, free-foil >=0.0.3
, free-foil-hm
, hspec
, hspec-discover
default-language: Haskell2010
14 changes: 13 additions & 1 deletion package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,17 @@ tests:
main: Main.hs
other-modules: []
dependencies:
- free-foil
- free-foil-hm
- doctest-parallel

spec:
main: Spec.hs
source-dirs: test
ghc-options:
- -threaded
- -rtsopts
- -with-rtsopts=-N
dependencies:
- free-foil-hm
- hspec
- hspec-discover
14 changes: 8 additions & 6 deletions src/HM/Interpret.hs
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
module HM.Interpret where

import Control.Monad.Foil (emptyNameMap, emptyScope)
import HM.Eval
import HM.Parser.Par
import HM.Syntax (toExpClosed)
import HM.Typecheck
import Control.Monad.Foil (emptyNameMap, emptyScope)
import HM.Eval
import HM.Parser.Par
import HM.Syntax (toExpClosed)
import HM.Typecheck

data Result
= Success String -- Output of evaluation.
| Failure ErrorKind String -- Error kind with message.
deriving (Show)

data ErrorKind
= ParsingError
| TypecheckingError
| EvaluationError
deriving (Show)

interpret :: String -> Result
interpret input =
Expand All @@ -22,7 +24,7 @@ interpret input =
Right e -> case inferType emptyNameMap e of
Left err -> Failure TypecheckingError ("Typechecking error: " ++ err)
Right _type -> case eval emptyScope e of
Left err -> Failure EvaluationError ("Evaluation error: " ++ err)
Left err -> Failure EvaluationError ("Evaluation error: " ++ err)
Right outExp -> Success (show outExp)
where
tokens = myLexer input
32 changes: 32 additions & 0 deletions test/HM/InterpretSpec.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module HM.InterpretSpec where

import Test.Hspec
import Control.Monad (forM_)
import HM.Interpret

isSuccess :: Result -> Bool
isSuccess Success{} = True
isSuccess _ = False

isTypeError :: Result -> Bool
isTypeError (Failure TypecheckingError _) = True
isTypeError _ = False

wellTypedPaths :: [FilePath]
wellTypedPaths = ["well-typed/1.lam", "well-typed/2.lam"]

illTypedPaths :: [FilePath]
illTypedPaths = ["ill-typed/1.lam", "ill-typed/2.lam"]

spec :: Spec
spec = do

describe "well-typed expressions" $ do
forM_ wellTypedPaths $ \path -> it path $ do
contents <- readFile ("test/files/" ++ path)
interpret contents `shouldSatisfy` isSuccess

describe "ill-typed expressions" $ do
forM_ illTypedPaths $ \path -> it path $ do
contents <- readFile ("test/files/" ++ path)
interpret contents `shouldSatisfy` isTypeError
1 change: 1 addition & 0 deletions test/Spec.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{-# OPTIONS_GHC -F -pgmF hspec-discover #-}
2 changes: 1 addition & 1 deletion test/files/ill-typed/2.lam
Original file line number Diff line number Diff line change
@@ -1 +1 @@
let x = true in (if iszero x then 1 else 0)
let x = true in (if iszero(x) then 1 else 0)

0 comments on commit 798f112

Please sign in to comment.