mirror of
https://github.com/j-mie6/design-patterns-for-parser-combinators.git
synced 2024-10-26 14:57:39 +03:00
Fleshed out the tests and infrastructure
This commit is contained in:
commit
96b481223e
@ -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
|
||||
|
@ -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
|
@ -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
|
@ -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)
|
Loading…
Reference in New Issue
Block a user