{-# LANGUAGE OverloadedStrings #-}
module Language.Fortran.Extras.Test where

import qualified Data.ByteString.Lazy.Char8    as BC
import           Language.Fortran.Analysis      ( Analysis )
import           Language.Fortran.AST           ( A0
                                                , ProgramFile
                                                )
import           Language.Fortran.Version       ( FortranVersion(..) )

import           Language.Fortran.Extras.Analysis
                                                ( versionedExpandedProgramAnalysis
                                                )
import           Language.Fortran.Extras.ProgramFile
                                                ( versionedExpandedProgramFile )
import           Language.Fortran.Util.Files
                                                ( flexReadFile )

getTestProgramFile :: String -> IO (ProgramFile A0)
getTestProgramFile :: String -> IO (ProgramFile A0)
getTestProgramFile String
p = do
  ByteString
cts <- String -> IO ByteString
flexReadFile String
p
  FortranVersion
-> [String] -> String -> ByteString -> IO (ProgramFile A0)
versionedExpandedProgramFile FortranVersion
Fortran77Legacy [] String
p ByteString
cts

getTestProgramFileIncludes :: String -> [String] -> IO (ProgramFile A0)
getTestProgramFileIncludes :: String -> [String] -> IO (ProgramFile A0)
getTestProgramFileIncludes String
p [String]
incls = do
  ByteString
cts <- String -> IO ByteString
flexReadFile String
p
  FortranVersion
-> [String] -> String -> ByteString -> IO (ProgramFile A0)
versionedExpandedProgramFile FortranVersion
Fortran77Legacy [String]
incls String
p ByteString
cts

getTestProgramAnalysis :: String -> IO (ProgramFile (Analysis A0))
getTestProgramAnalysis :: String -> IO (ProgramFile (Analysis A0))
getTestProgramAnalysis String
p = do
  ByteString
cts <- String -> IO ByteString
flexReadFile String
p
  FortranVersion
-> [String]
-> String
-> ByteString
-> IO (ProgramFile (Analysis A0))
versionedExpandedProgramAnalysis FortranVersion
Fortran77Legacy [] String
p ByteString
cts

getTestProgramAnalysisIncludes
  :: String -> [String] -> IO (ProgramFile (Analysis A0))
getTestProgramAnalysisIncludes :: String -> [String] -> IO (ProgramFile (Analysis A0))
getTestProgramAnalysisIncludes String
p [String]
incls = do
  ByteString
cts <- String -> IO ByteString
flexReadFile String
p
  FortranVersion
-> [String]
-> String
-> ByteString
-> IO (ProgramFile (Analysis A0))
versionedExpandedProgramAnalysis FortranVersion
Fortran77Legacy [String]
incls String
p ByteString
cts

-- | Utility function to compare file content
compareFile :: FilePath -> FilePath -> IO Bool
compareFile :: String -> String -> IO Bool
compareFile String
expected String
actual = do
  ByteString
c1 <- String -> IO ByteString
BC.readFile String
expected
  ByteString
c2 <- String -> IO ByteString
BC.readFile String
actual
  ByteString -> ByteString -> IO Bool
compareByteString ByteString
c1 ByteString
c2

compareByteString :: BC.ByteString -> BC.ByteString -> IO Bool
compareByteString :: ByteString -> ByteString -> IO Bool
compareByteString ByteString
expected ByteString
actual = if ByteString
expected ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
== ByteString
actual
  then Bool -> IO Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
  else do
    ByteString -> IO A0
BC.putStrLn ByteString
"<<<<<<< EXPECTED"
    ByteString -> IO A0
BC.putStrLn ByteString
expected
    ByteString -> IO A0
BC.putStrLn ByteString
">>>>>>> ACTUAL"
    ByteString -> IO A0
BC.putStrLn ByteString
actual
    Bool -> IO Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False