Fleshed out the tests and infrastructure

This commit is contained in:
Jamie Willis 2021-08-23 09:06:11 +01:00
commit 96b481223e
No known key found for this signature in database
GPG Key ID: 8B5526D5B3B8F45E
4 changed files with 40 additions and 9 deletions

View File

@ -3,8 +3,11 @@ module Main where
import Interpreter ( eval )
import Parser ( parseExpr )
fromEither :: (e -> a) -> Either e a -> a
fromEither err = either err id
calc :: String -> (String -> Int) -> Int
calc e = eval (parseExpr e)
calc e = eval (fromEither error (parseExpr e))
main :: IO ()
main = do

View File

@ -1,5 +1,7 @@
module Parser where
import Prelude hiding (negate)
import WeakAST
import Miniparsec
@ -7,5 +9,5 @@ import Miniparsec
This function should turn a string into an expression.
-}
-- Should be a good issue for a newcomer?
parseExpr :: String -> Expr
parseExpr = error "Not implemented yet :("
parseExpr :: String -> Either String Expr
parseExpr = undefined

View File

@ -1,9 +1,11 @@
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeApplications, StandaloneDeriving, DeriveGeneric #-}
module Arbitrary where
import qualified WeakAST as Weak
import qualified StrongAST as Strong
import GHC.Generics
import Convert (strengthen)
import Test.Tasty.QuickCheck
@ -33,8 +35,25 @@ instance Arbitrary Weak.Expr where
]
where
subexpr = expr (n `div` 2)
shrink (Weak.Var v) = []
shrink e = genericShrink e
instance Arbitrary Strong.Expr where arbitrary = fmap strengthen arbitrary
instance Arbitrary Strong.Term where arbitrary = fmap strengthen arbitrary
instance Arbitrary Strong.Negate where arbitrary = fmap strengthen arbitrary
instance Arbitrary Strong.Atom where arbitrary = fmap strengthen arbitrary
instance Arbitrary Strong.Expr where
arbitrary = fmap strengthen arbitrary
shrink = genericShrink
instance Arbitrary Strong.Term where
arbitrary = fmap strengthen arbitrary
shrink = genericShrink
instance Arbitrary Strong.Negate where
arbitrary = fmap strengthen arbitrary
shrink = genericShrink
instance Arbitrary Strong.Atom where
arbitrary = fmap strengthen arbitrary
shrink (Strong.Num n) = map Strong.Num (shrink n)
shrink (Strong.Var v) = []
shrink (Strong.Parens x) = map Strong.Parens (shrink x)
deriving instance Generic Weak.Expr
deriving instance Generic Strong.Expr
deriving instance Generic Strong.Term
deriving instance Generic Strong.Negate

View File

@ -1,3 +1,4 @@
{-# LANGUAGE FlexibleInstances #-}
module Main where
import Arbitrary
@ -14,4 +15,10 @@ main = defaultMain $ testGroup "Conversions" [
--testProperty "parse . pretty = id" roundtrip
]
roundtrip e = parseExpr (pretty e) === e
newtype MakeNice a = MakeNice a deriving Eq
instance Pretty e => Show (MakeNice (Either String e)) where
show (MakeNice (Right e)) = pretty e
show (MakeNice (Left err)) = err
roundtrip e = MakeNice (parseExpr (pretty e)) === MakeNice (Right e)