blob: 13279e99ede61e2670f394f9253486081710bc9d (
plain)
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
|
module Minilang.Lambda.ParserSpec where
import Data.Text (Text, pack)
import qualified Data.Text as Text
import Minilang.Lambda.Parser (AST (..), initialChars, parse, restChars)
import Test.Hspec (Spec, parallel, shouldBe)
import Test.Hspec.QuickCheck (prop)
import Test.QuickCheck (Arbitrary (..), NonEmptyList (..), elements, listOf, (==>))
spec :: Spec
spec = parallel $ do
prop "parses an identifier as a variable" $ \(Identifier ident) ->
parse ident `shouldBe` Right (Sym ident)
prop "parses a lambda-expression as an abstraction" $ \(Identifier ident) (Identifier body) ->
parse ("(lam (" <> ident <> ") " <> body <> ")") `shouldBe` Right (Abs [ident] (Sym body))
prop "parses a lambda-expression with multiple bindings as an abstraction" $ \(NonEmpty idents) (Identifier body) ->
let vars = unIdent <$> idents
abs = Abs vars (Sym body)
in parse ("(lam (" <> Text.unwords vars <> ") " <> body <> ")") `shouldBe` Right abs
prop "parses an application" $ \(Identifier ident1) (Identifier ident2) ->
parse ("(" <> ident1 <> " " <> ident2 <> ")") `shouldBe` Right (App (Sym ident1) (Sym ident2) [])
prop "parses multiple applications" $ \(NonEmpty idents) ->
(length idents >= 2) ==>
let vars = unIdent <$> idents
(a : b : rest) = vars
app = App (Sym a) (Sym b) (Sym <$> rest)
in parse ("(" <> Text.unwords vars <> ")") `shouldBe` Right app
newtype Identifier = Identifier {unIdent :: Text}
deriving (Eq, Show)
instance Arbitrary Identifier where
arbitrary =
Identifier . pack
<$> ((:) <$> elements initialChars <*> listOf (elements restChars))
|