Skip to content

Instantly share code, notes, and snippets.

@laser
Created April 22, 2015 20:12
Show Gist options
  • Save laser/9905a302c3cd5617595b to your computer and use it in GitHub Desktop.
Save laser/9905a302c3cd5617595b to your computer and use it in GitHub Desktop.
runhaskell AParserSpec.hs
import Test.Hspec
import Test.QuickCheck
import AParser
import Control.Applicative
main :: IO ()
main = hspec $ do
describe "first" $ do
it "should work" $ do
first (*2) (10, 10) `shouldBe` (20, 10)
first (Just) (10, 10) `shouldBe` (Just 10, 10)
describe "Parser Functor instance" $ do
it "should implement fmap" $ do
let p = Parser $ \s -> Just (10, s)
let p2 = fmap (*2) p
let r = runParser p2 "foo"
r `shouldBe` Just (20, "foo")
describe "Parser Applicative instance" $ do
-- pure f represents the parser which consumes no input
-- and successfully returns a result of f
it "should implement pure" $ do
let f = 10
let p = runParser (pure f) "flarp"
p `shouldBe` Just (10, "flarp")
it "should implement compose" $ do
let f = (*2)
let p = pure f
let p2 = pure 10
let p3 = p <*> p2
runParser p3 "flarp" `shouldBe` Just (20, "flarp")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment