{-# LANGUAGE OverloadedStrings #-}


module Text.TestCommonParsers (spec) where


import Data.Text (Text, append)
import Control.Applicative
import Control.Monad
import Test.Hspec.Attoparsec
import Test.Hspec
import Text.CommonParsers


spec :: Spec
spec = do
  describe "identifier" $ do
    it "parse `hello world`" $
      ("hello world" :: Text) ~> identifier `shouldParse` "hello" 

    it "numbers" $
      ("h3110 world" :: Text) ~> identifier `shouldParse` "h3110" 

    it "parse `+` `-` `*` `/`" $ do
      ("+" :: Text) ~> identifier `shouldParse` "+"
      ("-" :: Text) ~> identifier `shouldParse` "-"
      ("/" :: Text) ~> identifier `shouldParse` "/"
      ("*" :: Text) ~> identifier `shouldParse` "*"

  describe "whitespace" $ do
    it "skips whitespace" $
      ("   hello" :: Text) ~> (ignored *> identifier) 
        `shouldParse` "hello"

    it "everything but whitespace" $
      ("hi m8" :: Text) ~> (append <$> identifier <*> (ignored *> identifier)) 
        `shouldParse` "him8"

    it "comments" $
      ("hello ;; to the \nworld\n;; asdfasf" :: Text) ~> 
        (append <$> identifier <*> (ignored *> identifier) <* ignored) 
        `shouldParse` "helloworld"

  describe "inParens" $
    it "should parse `(hello)` -> `hello`" $ do
      ("(hello)" :: Text) ~> inParens identifier `shouldParse` "hello"
      ("( hello )" :: Text) ~> inParens identifier `shouldParse` "hello"
      

