{- -----------------------------------------------------------------------------
Copyright 2019 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.DefinedCategory (tests) where

import System.FilePath
import Text.Parsec

import Base.CompileError
import Base.CompileInfo
import Parser.DefinedCategory ()
import Test.Common
import Types.DefinedCategory


tests :: [IO (CompileInfo ())]
tests :: [IO (CompileInfo ())]
tests = [
    String -> IO (CompileInfo ())
checkParseSuccess (String
"testfiles" String -> String -> String
</> String
"definitions.0rx"),
    String -> IO (CompileInfo ())
checkParseSuccess (String
"testfiles" String -> String -> String
</> String
"internal_inheritance.0rx"),
    String -> IO (CompileInfo ())
checkParseSuccess (String
"testfiles" String -> String -> String
</> String
"internal_params.0rx"),
    String -> IO (CompileInfo ())
checkParseSuccess (String
"testfiles" String -> String -> String
</> String
"internal_filters.0rx")
  ]

checkParseSuccess :: String -> IO (CompileInfo ())
checkParseSuccess :: String -> IO (CompileInfo ())
checkParseSuccess String
f = do
  String
contents <- String -> IO String
loadFile String
f
  let parsed :: CompileInfo [DefinedCategory SourcePos]
parsed = String -> String -> CompileInfo [DefinedCategory SourcePos]
forall a. ParseFromSource a => String -> String -> CompileInfo [a]
readMulti String
f String
contents :: CompileInfo [DefinedCategory SourcePos]
  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 [DefinedCategory SourcePos] -> CompileInfo ()
forall (m :: * -> *) a. CompileErrorM m => CompileInfo a -> m ()
check CompileInfo [DefinedCategory SourcePos]
parsed
  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
f 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 ()

checkParseFail :: String -> IO (CompileInfo ())
checkParseFail :: String -> IO (CompileInfo ())
checkParseFail String
f = do
  String
contents <- String -> IO String
loadFile String
f
  let parsed :: CompileInfo [DefinedCategory SourcePos]
parsed = String -> String -> CompileInfo [DefinedCategory SourcePos]
forall a. ParseFromSource a => String -> String -> CompileInfo [a]
readMulti String
f String
contents :: CompileInfo [DefinedCategory SourcePos]
  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 [DefinedCategory SourcePos] -> CompileInfo ()
forall (m :: * -> *) a.
(CompileErrorM m, Show a) =>
CompileInfo a -> m ()
check CompileInfo [DefinedCategory SourcePos]
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 = 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
f String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
": Expected 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) String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"\n"