{- -----------------------------------------------------------------------------
Copyright 2020 Kevin P. Barry

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
----------------------------------------------------------------------------- -}

-- Author: Kevin P. Barry [ta0kira@gmail.com]

{-# LANGUAGE Safe #-}

module Test.Parser (tests) where

import Control.Monad (when)

import Base.CompileError
import Base.CompileInfo
import Parser.Common
import Test.Common
import Text.Parsec.String


tests :: [IO (CompileInfo ())]
tests :: [IO (CompileInfo ())]
tests = [
    Parser Char -> [Char] -> Char -> IO (CompileInfo ())
forall a.
(Eq a, Show a) =>
Parser a -> [Char] -> a -> IO (CompileInfo ())
checkParsesAs Parser Char
stringChar [Char]
"\\'" Char
'\'',
    Parser Char -> [Char] -> Char -> IO (CompileInfo ())
forall a.
(Eq a, Show a) =>
Parser a -> [Char] -> a -> IO (CompileInfo ())
checkParsesAs Parser Char
stringChar [Char]
"\\\"" Char
'"',
    Parser Char -> [Char] -> Char -> IO (CompileInfo ())
forall a.
(Eq a, Show a) =>
Parser a -> [Char] -> a -> IO (CompileInfo ())
checkParsesAs Parser Char
stringChar [Char]
"\\?" Char
'?',
    Parser Char -> [Char] -> Char -> IO (CompileInfo ())
forall a.
(Eq a, Show a) =>
Parser a -> [Char] -> a -> IO (CompileInfo ())
checkParsesAs Parser Char
stringChar [Char]
"\\\\" Char
'\\',
    Parser Char -> [Char] -> Char -> IO (CompileInfo ())
forall a.
(Eq a, Show a) =>
Parser a -> [Char] -> a -> IO (CompileInfo ())
checkParsesAs Parser Char
stringChar [Char]
"\\a" Char
'\a',
    Parser Char -> [Char] -> Char -> IO (CompileInfo ())
forall a.
(Eq a, Show a) =>
Parser a -> [Char] -> a -> IO (CompileInfo ())
checkParsesAs Parser Char
stringChar [Char]
"\\b" Char
'\b',
    Parser Char -> [Char] -> Char -> IO (CompileInfo ())
forall a.
(Eq a, Show a) =>
Parser a -> [Char] -> a -> IO (CompileInfo ())
checkParsesAs Parser Char
stringChar [Char]
"\\f" Char
'\f',
    Parser Char -> [Char] -> Char -> IO (CompileInfo ())
forall a.
(Eq a, Show a) =>
Parser a -> [Char] -> a -> IO (CompileInfo ())
checkParsesAs Parser Char
stringChar [Char]
"\\n" Char
'\n',
    Parser Char -> [Char] -> Char -> IO (CompileInfo ())
forall a.
(Eq a, Show a) =>
Parser a -> [Char] -> a -> IO (CompileInfo ())
checkParsesAs Parser Char
stringChar [Char]
"\\r" Char
'\r',
    Parser Char -> [Char] -> Char -> IO (CompileInfo ())
forall a.
(Eq a, Show a) =>
Parser a -> [Char] -> a -> IO (CompileInfo ())
checkParsesAs Parser Char
stringChar [Char]
"\\t" Char
'\t',
    Parser Char -> [Char] -> Char -> IO (CompileInfo ())
forall a.
(Eq a, Show a) =>
Parser a -> [Char] -> a -> IO (CompileInfo ())
checkParsesAs Parser Char
stringChar [Char]
"\\v" Char
'\v',
    Parser Char -> [Char] -> Char -> IO (CompileInfo ())
forall a.
(Eq a, Show a) =>
Parser a -> [Char] -> a -> IO (CompileInfo ())
checkParsesAs Parser Char
stringChar [Char]
"\n" Char
'\n',
    Parser Char -> [Char] -> Char -> IO (CompileInfo ())
forall a.
(Eq a, Show a) =>
Parser a -> [Char] -> a -> IO (CompileInfo ())
checkParsesAs Parser Char
stringChar [Char]
"\\x0A" Char
'\n',
    Parser Char -> [Char] -> Char -> IO (CompileInfo ())
forall a.
(Eq a, Show a) =>
Parser a -> [Char] -> a -> IO (CompileInfo ())
checkParsesAs Parser Char
stringChar [Char]
"\\012" Char
'\n',
    Parser Char -> [Char] -> IO (CompileInfo ())
forall a. Show a => Parser a -> [Char] -> IO (CompileInfo ())
checkParseFail Parser Char
stringChar [Char]
"\"",
    Parser Char -> [Char] -> IO (CompileInfo ())
forall a. Show a => Parser a -> [Char] -> IO (CompileInfo ())
checkParseFail Parser Char
stringChar [Char]
"\\q",
    Parser Char -> [Char] -> IO (CompileInfo ())
forall a. Show a => Parser a -> [Char] -> IO (CompileInfo ())
checkParseFail Parser Char
stringChar [Char]
"\\00",
    Parser Char -> [Char] -> IO (CompileInfo ())
forall a. Show a => Parser a -> [Char] -> IO (CompileInfo ())
checkParseFail Parser Char
stringChar [Char]
"\\x0",

    Parser [Char] -> [Char] -> [Char] -> IO (CompileInfo ())
forall a.
(Eq a, Show a) =>
Parser a -> [Char] -> a -> IO (CompileInfo ())
checkParsesAs Parser [Char]
regexChar [Char]
"\\\\" [Char]
"\\\\",
    Parser [Char] -> [Char] -> [Char] -> IO (CompileInfo ())
forall a.
(Eq a, Show a) =>
Parser a -> [Char] -> a -> IO (CompileInfo ())
checkParsesAs Parser [Char]
regexChar [Char]
"\\n" [Char]
"\\n",
    Parser [Char] -> [Char] -> [Char] -> IO (CompileInfo ())
forall a.
(Eq a, Show a) =>
Parser a -> [Char] -> a -> IO (CompileInfo ())
checkParsesAs Parser [Char]
regexChar [Char]
"\n" [Char]
"\n",
    Parser [Char] -> [Char] -> [Char] -> IO (CompileInfo ())
forall a.
(Eq a, Show a) =>
Parser a -> [Char] -> a -> IO (CompileInfo ())
checkParsesAs Parser [Char]
regexChar [Char]
"\\\"" [Char]
"\"",
    Parser [Char] -> [Char] -> IO (CompileInfo ())
forall a. Show a => Parser a -> [Char] -> IO (CompileInfo ())
checkParseFail Parser [Char]
regexChar [Char]
"\""
  ]

checkParsesAs :: (Eq a, Show a) => Parser a -> [Char] -> a -> IO (CompileInfo ())
checkParsesAs :: Parser a -> [Char] -> a -> IO (CompileInfo ())
checkParsesAs Parser a
p [Char]
s a
m = CompileInfo () -> IO (CompileInfo ())
forall (m :: * -> *) a. Monad m => a -> m a
return (CompileInfo () -> IO (CompileInfo ()))
-> CompileInfo () -> IO (CompileInfo ())
forall a b. (a -> b) -> a -> b
$ do
  let parsed :: CompileInfo a
parsed = Parser a -> [Char] -> [Char] -> CompileInfo a
forall a. Parser a -> [Char] -> [Char] -> CompileInfo a
readSingleWith Parser a
p [Char]
"(string)" [Char]
s
  CompileInfo a -> CompileInfo ()
forall (m :: * -> *) a. CompileErrorM m => CompileInfo a -> m ()
check CompileInfo a
parsed
  a
e <- CompileInfo a
parsed
  Bool -> CompileInfo () -> CompileInfo ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (a
e a -> a -> Bool
forall a. Eq a => a -> a -> Bool
/= a
m) (CompileInfo () -> CompileInfo ())
-> CompileInfo () -> CompileInfo ()
forall a b. (a -> b) -> a -> b
$
    [Char] -> CompileInfo ()
forall (m :: * -> *) a. CompileErrorM m => [Char] -> m a
compileErrorM ([Char] -> CompileInfo ()) -> [Char] -> CompileInfo ()
forall a b. (a -> b) -> a -> b
$ [Char] -> [Char]
forall a. Show a => a -> [Char]
show [Char]
s [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
" does not parse as " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ a -> [Char]
forall a. Show a => a -> [Char]
show a
m [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
":\n" [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ a -> [Char]
forall a. Show a => a -> [Char]
show a
e
  where
    check :: CompileInfo a -> m ()
check CompileInfo a
c
      | CompileInfo a -> Bool
forall a. CompileInfo a -> Bool
isCompileError CompileInfo a
c = [Char] -> m ()
forall (m :: * -> *) a. CompileErrorM m => [Char] -> m a
compileErrorM ([Char] -> m ()) -> [Char] -> m ()
forall a b. (a -> b) -> a -> b
$ [Char]
"Parse '" [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
s [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
"':\n" [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ CompileMessage -> [Char]
forall a. Show a => a -> [Char]
show (CompileInfo a -> CompileMessage
forall a. CompileInfo a -> CompileMessage
getCompileError CompileInfo a
c)
      | Bool
otherwise = () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()

checkParseFail :: Show a => Parser a -> [Char] -> IO (CompileInfo ())
checkParseFail :: Parser a -> [Char] -> IO (CompileInfo ())
checkParseFail Parser a
p [Char]
s = do
  let parsed :: CompileInfo a
parsed = Parser a -> [Char] -> [Char] -> CompileInfo a
forall a. Parser a -> [Char] -> [Char] -> CompileInfo a
readSingleWith Parser a
p [Char]
"(string)" [Char]
s
  CompileInfo () -> IO (CompileInfo ())
forall (m :: * -> *) a. Monad m => a -> m a
return (CompileInfo () -> IO (CompileInfo ()))
-> CompileInfo () -> IO (CompileInfo ())
forall a b. (a -> b) -> a -> b
$ CompileInfo a -> CompileInfo ()
forall (m :: * -> *) a.
(CompileErrorM m, Show a) =>
CompileInfo a -> m ()
check CompileInfo a
parsed
  where
    check :: CompileInfo a -> m ()
check CompileInfo a
c
      | CompileInfo a -> Bool
forall a. CompileInfo a -> Bool
isCompileError CompileInfo a
c = () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
      | Bool
otherwise = [Char] -> m ()
forall (m :: * -> *) a. CompileErrorM m => [Char] -> m a
compileErrorM ([Char] -> m ()) -> [Char] -> m ()
forall a b. (a -> b) -> a -> b
$ [Char]
"Parse '" [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
s [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
"': Expected failure but got\n" [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++
                                   a -> [Char]
forall a. Show a => a -> [Char]
show (CompileInfo a -> a
forall a. CompileInfo a -> a
getCompileSuccess CompileInfo a
c) [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
"\n"