{- -----------------------------------------------------------------------------
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]

module Test.Pragma (tests) where

import Control.Monad (when)
import Text.Parsec
import Text.Parsec.String
import Text.Regex.TDFA -- Not safe!

import Base.CompileError
import Base.CompileInfo
import Parser.Pragma
import Test.Common
import Types.Pragma


tests :: [IO (CompileInfo ())]
tests :: [IO (CompileInfo ())]
tests = [
    String
-> Parser [Pragma SourcePos]
-> ([Pragma SourcePos] -> Bool)
-> IO (CompileInfo ())
checkParsesAs String
"$ModuleOnly$" ((Pragma SourcePos -> [Pragma SourcePos])
-> ParsecT String () Identity (Pragma SourcePos)
-> Parser [Pragma SourcePos]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Pragma SourcePos -> [Pragma SourcePos] -> [Pragma SourcePos]
forall a. a -> [a] -> [a]
:[]) ParsecT String () Identity (Pragma SourcePos)
pragmaModuleOnly)
      (\[Pragma SourcePos]
e -> case [Pragma SourcePos]
e of
                  [PragmaVisibility [SourcePos]
_ CodeVisibility
ModuleOnly] -> Bool
True
                  [Pragma SourcePos]
_ -> Bool
False),

    String
-> Parser [Pragma SourcePos]
-> ([Pragma SourcePos] -> Bool)
-> IO (CompileInfo ())
checkParsesAs String
"$TestsOnly$" ((Pragma SourcePos -> [Pragma SourcePos])
-> ParsecT String () Identity (Pragma SourcePos)
-> Parser [Pragma SourcePos]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Pragma SourcePos -> [Pragma SourcePos] -> [Pragma SourcePos]
forall a. a -> [a] -> [a]
:[]) ParsecT String () Identity (Pragma SourcePos)
pragmaTestsOnly)
      (\[Pragma SourcePos]
e -> case [Pragma SourcePos]
e of
                  [PragmaVisibility [SourcePos]
_ CodeVisibility
TestsOnly] -> Bool
True
                  [Pragma SourcePos]
_ -> Bool
False),

    String
-> Parser [Pragma SourcePos]
-> ([Pragma SourcePos] -> Bool)
-> IO (CompileInfo ())
checkParsesAs String
"$SourceContext$" ((Pragma SourcePos -> [Pragma SourcePos])
-> ParsecT String () Identity (Pragma SourcePos)
-> Parser [Pragma SourcePos]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Pragma SourcePos -> [Pragma SourcePos] -> [Pragma SourcePos]
forall a. a -> [a] -> [a]
:[]) ParsecT String () Identity (Pragma SourcePos)
pragmaSourceContext)
      (\[Pragma SourcePos]
e -> case [Pragma SourcePos]
e of
                  [PragmaSourceContext SourcePos
_] -> Bool
True
                  [Pragma SourcePos]
_ -> Bool
False),

    String
-> Parser [Pragma SourcePos]
-> ([Pragma SourcePos] -> Bool)
-> IO (CompileInfo ())
checkParsesAs String
"$NoTrace$" ((Pragma SourcePos -> [Pragma SourcePos])
-> ParsecT String () Identity (Pragma SourcePos)
-> Parser [Pragma SourcePos]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Pragma SourcePos -> [Pragma SourcePos] -> [Pragma SourcePos]
forall a. a -> [a] -> [a]
:[]) ParsecT String () Identity (Pragma SourcePos)
pragmaNoTrace)
      (\[Pragma SourcePos]
e -> case [Pragma SourcePos]
e of
                  [PragmaTracing [SourcePos]
_ TraceType
NoTrace] -> Bool
True
                  [Pragma SourcePos]
_ -> Bool
False),

    String
-> Parser [Pragma SourcePos]
-> ([Pragma SourcePos] -> Bool)
-> IO (CompileInfo ())
checkParsesAs String
"$TraceCreation$" ((Pragma SourcePos -> [Pragma SourcePos])
-> ParsecT String () Identity (Pragma SourcePos)
-> Parser [Pragma SourcePos]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Pragma SourcePos -> [Pragma SourcePos] -> [Pragma SourcePos]
forall a. a -> [a] -> [a]
:[]) ParsecT String () Identity (Pragma SourcePos)
pragmaTraceCreation)
      (\[Pragma SourcePos]
e -> case [Pragma SourcePos]
e of
                  [PragmaTracing [SourcePos]
_ TraceType
TraceCreation] -> Bool
True
                  [Pragma SourcePos]
_ -> Bool
False),

    String
-> Parser [Pragma SourcePos]
-> ([Pragma SourcePos] -> Bool)
-> IO (CompileInfo ())
checkParsesAs String
"$Comment[ \"this is a pragma with args\" ]$" ((Pragma SourcePos -> [Pragma SourcePos])
-> ParsecT String () Identity (Pragma SourcePos)
-> Parser [Pragma SourcePos]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Pragma SourcePos -> [Pragma SourcePos] -> [Pragma SourcePos]
forall a. a -> [a] -> [a]
:[]) ParsecT String () Identity (Pragma SourcePos)
pragmaComment)
      (\[Pragma SourcePos]
e -> case [Pragma SourcePos]
e of
                  [PragmaComment [SourcePos]
_ String
"this is a pragma with args"] -> Bool
True
                  [Pragma SourcePos]
_ -> Bool
False),

    String
-> Parser [Pragma SourcePos]
-> ([Pragma SourcePos] -> Bool)
-> IO (CompileInfo ())
checkParsesAs String
"$ExprLookup[ \nMODULE_PATH /*comment*/\n ]$" ((Pragma SourcePos -> [Pragma SourcePos])
-> ParsecT String () Identity (Pragma SourcePos)
-> Parser [Pragma SourcePos]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Pragma SourcePos -> [Pragma SourcePos] -> [Pragma SourcePos]
forall a. a -> [a] -> [a]
:[]) ParsecT String () Identity (Pragma SourcePos)
pragmaExprLookup)
      (\[Pragma SourcePos]
e -> case [Pragma SourcePos]
e of
                  [PragmaExprLookup [SourcePos]
_ String
"MODULE_PATH"] -> Bool
True
                  [Pragma SourcePos]
_ -> Bool
False),

    String
-> Parser [Pragma SourcePos]
-> ([Pragma SourcePos] -> Bool)
-> IO (CompileInfo ())
checkParsesAs String
"/*only comments*/" ([ParsecT String () Identity (Pragma SourcePos)]
-> Parser [Pragma SourcePos]
forall a. [Parser a] -> Parser [a]
parsePragmas [ParsecT String () Identity (Pragma SourcePos)
pragmaModuleOnly,ParsecT String () Identity (Pragma SourcePos)
pragmaTestsOnly])
      (\[Pragma SourcePos]
e -> case [Pragma SourcePos]
e of
                  [] -> Bool
True
                  [Pragma SourcePos]
_ -> Bool
False),

    String
-> Parser [Pragma SourcePos]
-> ([Pragma SourcePos] -> Bool)
-> IO (CompileInfo ())
checkParsesAs String
"$ModuleOnly$  // comment" ([ParsecT String () Identity (Pragma SourcePos)]
-> Parser [Pragma SourcePos]
forall a. [Parser a] -> Parser [a]
parsePragmas [ParsecT String () Identity (Pragma SourcePos)
pragmaTestsOnly,ParsecT String () Identity (Pragma SourcePos)
pragmaModuleOnly])
      (\[Pragma SourcePos]
e -> case [Pragma SourcePos]
e of
                  [PragmaVisibility [SourcePos]
_ CodeVisibility
ModuleOnly] -> Bool
True
                  [Pragma SourcePos]
_ -> Bool
False),

    String
-> Parser [Pragma SourcePos]
-> ([Pragma SourcePos] -> Bool)
-> IO (CompileInfo ())
checkParsesAs String
"$TestsOnly$  /*comment*/" ([ParsecT String () Identity (Pragma SourcePos)]
-> Parser [Pragma SourcePos]
forall a. [Parser a] -> Parser [a]
parsePragmas [ParsecT String () Identity (Pragma SourcePos)
pragmaModuleOnly,ParsecT String () Identity (Pragma SourcePos)
pragmaTestsOnly])
      (\[Pragma SourcePos]
e -> case [Pragma SourcePos]
e of
                  [PragmaVisibility [SourcePos]
_ CodeVisibility
TestsOnly] -> Bool
True
                  [Pragma SourcePos]
_ -> Bool
False),

    String
-> Parser [Pragma SourcePos]
-> ([Pragma SourcePos] -> Bool)
-> IO (CompileInfo ())
checkParsesAs String
"$TestsOnly$\n$TestsOnly$\n$ModuleOnly$" ([ParsecT String () Identity (Pragma SourcePos)]
-> Parser [Pragma SourcePos]
forall a. [Parser a] -> Parser [a]
parsePragmas [ParsecT String () Identity (Pragma SourcePos)
pragmaModuleOnly,ParsecT String () Identity (Pragma SourcePos)
pragmaTestsOnly])
      (\[Pragma SourcePos]
e -> case [Pragma SourcePos]
e of
                  [PragmaVisibility [SourcePos]
_ CodeVisibility
TestsOnly,
                   PragmaVisibility [SourcePos]
_ CodeVisibility
TestsOnly,
                   PragmaVisibility [SourcePos]
_ CodeVisibility
ModuleOnly] -> Bool
True
                  [Pragma SourcePos]
_ -> Bool
False),

    String
-> String
-> ParsecT String () Identity (Pragma SourcePos)
-> IO (CompileInfo ())
checkParseError String
"$ModuleOnly[ extra ]$" String
"does not allow arguments" ParsecT String () Identity (Pragma SourcePos)
pragmaModuleOnly,

    String
-> String
-> ParsecT String () Identity (Pragma SourcePos)
-> IO (CompileInfo ())
checkParseError String
"$TestsOnly[ extra ]$" String
"does not allow arguments" ParsecT String () Identity (Pragma SourcePos)
pragmaTestsOnly,

    String
-> String
-> ParsecT String () Identity (Pragma SourcePos)
-> IO (CompileInfo ())
checkParseError String
"$Comment$" String
"requires arguments" ParsecT String () Identity (Pragma SourcePos)
pragmaComment,

    String
-> String
-> ParsecT String () Identity (Pragma SourcePos)
-> IO (CompileInfo ())
checkParseError String
"$ExprLookup[ \"bad stuff\" ]$" String
"macro name" ParsecT String () Identity (Pragma SourcePos)
pragmaExprLookup
  ]

checkParsesAs :: String -> Parser [Pragma SourcePos] -> ([Pragma SourcePos] -> Bool) -> IO (CompileInfo ())
checkParsesAs :: String
-> Parser [Pragma SourcePos]
-> ([Pragma SourcePos] -> Bool)
-> IO (CompileInfo ())
checkParsesAs String
s Parser [Pragma SourcePos]
p [Pragma SourcePos] -> Bool
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 [Pragma SourcePos]
parsed = Parser [Pragma SourcePos]
-> String -> String -> CompileInfo [Pragma SourcePos]
forall a. Parser a -> String -> String -> CompileInfo a
readSingleWith Parser [Pragma SourcePos]
p String
"(string)" String
s
  CompileInfo [Pragma SourcePos] -> CompileInfo ()
forall (m :: * -> *) a. CompileErrorM m => CompileInfo a -> m ()
check CompileInfo [Pragma SourcePos]
parsed
  [Pragma SourcePos]
e <- CompileInfo [Pragma SourcePos]
parsed
  Bool -> CompileInfo () -> CompileInfo ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ [Pragma SourcePos] -> Bool
m [Pragma SourcePos]
e) (CompileInfo () -> CompileInfo ())
-> CompileInfo () -> CompileInfo ()
forall a b. (a -> b) -> a -> b
$
    String -> CompileInfo ()
forall (m :: * -> *) a. CompileErrorM m => String -> m a
compileErrorM (String -> CompileInfo ()) -> String -> CompileInfo ()
forall a b. (a -> b) -> a -> b
$ String
"No match in '" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
s String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"':\n" String -> String -> String
forall a. [a] -> [a] -> [a]
++ [Pragma SourcePos] -> String
forall a. Show a => a -> String
show [Pragma SourcePos]
e
  where
    check :: CompileInfo a -> m ()
check CompileInfo a
c
      | CompileInfo a -> Bool
forall a. CompileInfo a -> Bool
isCompileError CompileInfo a
c = String -> m ()
forall (m :: * -> *) a. CompileErrorM m => String -> m a
compileErrorM (String -> m ()) -> String -> m ()
forall a b. (a -> b) -> a -> b
$ String
"Parse '" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
s String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"':\n" String -> String -> String
forall a. [a] -> [a] -> [a]
++ CompileMessage -> String
forall a. Show a => a -> String
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 ()

checkParseError :: String -> String -> Parser (Pragma SourcePos) -> IO (CompileInfo ())
checkParseError :: String
-> String
-> ParsecT String () Identity (Pragma SourcePos)
-> IO (CompileInfo ())
checkParseError String
s String
m ParsecT String () Identity (Pragma SourcePos)
p = 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 (Pragma SourcePos)
parsed = ParsecT String () Identity (Pragma SourcePos)
-> String -> String -> CompileInfo (Pragma SourcePos)
forall a. Parser a -> String -> String -> CompileInfo a
readSingleWith ParsecT String () Identity (Pragma SourcePos)
p String
"(string)" String
s
  CompileInfo (Pragma SourcePos) -> CompileInfo ()
forall (f :: * -> *) a.
(CompileErrorM f, Show a) =>
CompileInfo a -> f ()
check CompileInfo (Pragma SourcePos)
parsed
  where
    check :: CompileInfo a -> f ()
check CompileInfo a
c
      | CompileInfo a -> Bool
forall a. CompileInfo a -> Bool
isCompileError CompileInfo a
c = do
          let text :: String
text = CompileMessage -> String
forall a. Show a => a -> String
show (CompileInfo a -> CompileMessage
forall a. CompileInfo a -> CompileMessage
getCompileError CompileInfo a
c)
          Bool -> f () -> f ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ String
text String -> String -> Bool
forall source source1 target.
(RegexMaker Regex CompOption ExecOption source,
 RegexContext Regex source1 target) =>
source1 -> source -> target
=~ String
m) (f () -> f ()) -> f () -> f ()
forall a b. (a -> b) -> a -> b
$
            String -> f ()
forall (m :: * -> *) a. CompileErrorM m => String -> m a
compileErrorM (String -> f ()) -> String -> f ()
forall a b. (a -> b) -> a -> b
$ String
"Expected pattern " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String -> String
forall a. Show a => a -> String
show String
m String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" in error output but got\n" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
text
      | Bool
otherwise =
          String -> f ()
forall (m :: * -> *) a. CompileErrorM m => String -> m a
compileErrorM (String -> f ()) -> String -> f ()
forall a b. (a -> b) -> a -> b
$ String
"Expected write failure but got\n" String -> String -> String
forall a. [a] -> [a] -> [a]
++ a -> String
forall a. Show a => a -> String
show (CompileInfo a -> a
forall a. CompileInfo a -> a
getCompileSuccess CompileInfo a
c)