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

import System.FilePath

import Base.CompilerError
import Base.TrackedErrors
import Parser.SourceFile
import Test.Common


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

checkParseSuccess :: String -> ((FilePath,String) -> TrackedErrors a) -> IO (TrackedErrors ())
checkParseSuccess :: String
-> ((String, String) -> TrackedErrors a) -> IO (TrackedErrors ())
checkParseSuccess String
f (String, String) -> TrackedErrors a
p = do
  String
contents <- String -> IO String
loadFile String
f
  let parsed :: TrackedErrors a
parsed = (String, String) -> TrackedErrors a
p (String
f,String
contents)
  TrackedErrors () -> IO (TrackedErrors ())
forall (m :: * -> *) a. Monad m => a -> m a
return (TrackedErrors () -> IO (TrackedErrors ()))
-> TrackedErrors () -> IO (TrackedErrors ())
forall a b. (a -> b) -> a -> b
$ TrackedErrors a -> TrackedErrors ()
forall (m :: * -> *) a.
ErrorContextM m =>
TrackedErrorsT Identity a -> m ()
check TrackedErrors a
parsed
  where
    check :: TrackedErrorsT Identity a -> m ()
check TrackedErrorsT Identity a
c
      | TrackedErrorsT Identity a -> Bool
forall (t :: (* -> *) -> * -> *) a.
(ErrorContextT t, ErrorContextM (t Identity)) =>
t Identity a -> Bool
isCompilerError TrackedErrorsT Identity a
c = String -> m ()
forall (m :: * -> *) a. ErrorContextM m => String -> m a
compilerErrorM (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]
++ CompilerMessage -> String
forall a. Show a => a -> String
show (TrackedErrorsT Identity a -> CompilerMessage
forall a. TrackedErrors a -> CompilerMessage
getCompilerError TrackedErrorsT Identity a
c)
      | Bool
otherwise = () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()