{-# LANGUAGE Safe #-}
module Test.Parser (tests) where
import Control.Monad (when)
import Text.Parsec
import Text.Parsec.String
import Base.CompileError
import Compilation.CompileInfo
import Parser.Common
import Test.Common
tests :: [IO (CompileInfo ())]
tests = [
checkParsesAs stringChar "\\'" '\'',
checkParsesAs stringChar "\\\"" '"',
checkParsesAs stringChar "\\?" '?',
checkParsesAs stringChar "\\\\" '\\',
checkParsesAs stringChar "\\a" '\a',
checkParsesAs stringChar "\\b" '\b',
checkParsesAs stringChar "\\f" '\f',
checkParsesAs stringChar "\\n" '\n',
checkParsesAs stringChar "\\r" '\r',
checkParsesAs stringChar "\\t" '\t',
checkParsesAs stringChar "\\v" '\v',
checkParsesAs stringChar "\n" '\n',
checkParsesAs stringChar "\\x0A" '\n',
checkParsesAs stringChar "\\012" '\n',
checkParseFail stringChar "\"",
checkParseFail stringChar "\\q",
checkParseFail stringChar "\\00",
checkParseFail stringChar "\\x0",
checkParsesAs regexChar "\\\\" "\\\\",
checkParsesAs regexChar "\\n" "\\n",
checkParsesAs regexChar "\n" "\n",
checkParsesAs regexChar "\\\"" "\"",
checkParseFail regexChar "\""
]
checkParsesAs p s m = return $ do
let parsed = readSingleWith p "(string)" s
check parsed
e <- parsed
when (e /= m) $
compileError $ show s ++ " does not parse as " ++ show m ++ ":\n" ++ show e
where
check c
| isCompileError c = compileError $ "Parse '" ++ s ++ "':\n" ++ show (getCompileError c)
| otherwise = return ()
checkParseFail p s = do
let parsed = readSingleWith p "(string)" s
return $ check parsed
where
check c
| isCompileError c = return ()
| otherwise = compileError $ "Parse '" ++ s ++ "': Expected failure but got\n" ++
show (getCompileSuccess c) ++ "\n"