{- -----------------------------------------------------------------------------
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.SourceFile (tests) where

import System.FilePath

import Base.CompileError
import Base.CompileInfo
import Parser.SourceFile
import Test.Common


tests :: [IO (CompileInfo ())]
tests :: [IO (CompileInfo ())]
tests = [
    String
-> ((String, String)
    -> CompileInfo ([Pragma SourcePos], [AnyCategory SourcePos]))
-> IO (CompileInfo ())
forall a.
String
-> ((String, String) -> CompileInfo a) -> IO (CompileInfo ())
checkParseSuccess (String
"testfiles" String -> String -> String
</> String
"public.0rp")   (String, String)
-> CompileInfo ([Pragma SourcePos], [AnyCategory SourcePos])
forall (m :: * -> *).
CompileErrorM m =>
(String, String) -> m ([Pragma SourcePos], [AnyCategory SourcePos])
parsePublicSource,
    String
-> ((String, String)
    -> CompileInfo
         ([Pragma SourcePos], [AnyCategory SourcePos],
          [DefinedCategory SourcePos]))
-> IO (CompileInfo ())
forall a.
String
-> ((String, String) -> CompileInfo a) -> IO (CompileInfo ())
checkParseSuccess (String
"testfiles" String -> String -> String
</> String
"internal.0rx") (String, String)
-> CompileInfo
     ([Pragma SourcePos], [AnyCategory SourcePos],
      [DefinedCategory SourcePos])
forall (m :: * -> *).
CompileErrorM m =>
(String, String)
-> m ([Pragma SourcePos], [AnyCategory SourcePos],
      [DefinedCategory SourcePos])
parseInternalSource,
    String
-> ((String, String)
    -> CompileInfo ([Pragma SourcePos], [IntegrationTest SourcePos]))
-> IO (CompileInfo ())
forall a.
String
-> ((String, String) -> CompileInfo a) -> IO (CompileInfo ())
checkParseSuccess (String
"testfiles" String -> String -> String
</> String
"test.0rt")     (String, String)
-> CompileInfo ([Pragma SourcePos], [IntegrationTest SourcePos])
forall (m :: * -> *).
CompileErrorM m =>
(String, String)
-> m ([Pragma SourcePos], [IntegrationTest SourcePos])
parseTestSource
  ]

checkParseSuccess :: String -> ((FilePath,String) -> CompileInfo a) -> IO (CompileInfo ())
checkParseSuccess :: String
-> ((String, String) -> CompileInfo a) -> IO (CompileInfo ())
checkParseSuccess String
f (String, String) -> CompileInfo a
p = do
  String
contents <- String -> IO String
loadFile String
f
  let parsed :: CompileInfo a
parsed = (String, String) -> CompileInfo a
p (String
f,String
contents)
  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 => 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 = 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 ()