module Kivuli.Evaluator where

import Language.Haskell.Interpreter
import Kivuli.Types
import Control.Monad.Catch (MonadMask)

evaluate :: (Functor m, MonadIO m, MonadMask m) => Test -> m TestResult
evaluate test = do
            let expressionToEvaluate = (head . expressions) test
                expected = expectedResult test

            result <- runInterpreter $ do
                              setImportsQ [("Prelude", Nothing)]
                              eval expressionToEvaluate

            case result of
              Right resultingVal -> if resultingVal == expected 
                                    then return (Pass) 
                                    else return (Fail $ "Expected " ++ expected ++ "\nActual " ++ resultingVal)
              Left (UnknownError errorMessage) -> return (Fail errorMessage)	 
              Left (WontCompile ghcErrors) -> return (Fail $ concatMap errMsg ghcErrors)
              Left (NotAllowed errorMessage) -> return (Fail errorMessage)
              Left (GhcException errorMessage) -> return (Fail errorMessage)
