{- -----------------------------------------------------------------------------
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 Types.IntegrationTest (
  ExpectedResult(..),
  IntegrationTest(..),
  IntegrationTestHeader(..),
  OutputPattern(..),
  OutputScope(..),
  getExcludePattern,
  getRequirePattern,
  isExpectCompilerError,
  isExpectCompiles,
  isExpectRuntimeError,
  isExpectRuntimeSuccess,
) where

import Types.TypeCategory
import Types.DefinedCategory
import Types.Procedure


data IntegrationTestHeader c =
  IntegrationTestHeader {
    IntegrationTestHeader c -> [c]
ithContext :: [c],
    IntegrationTestHeader c -> String
ithTestName :: String,
    IntegrationTestHeader c -> [String]
ithArgs :: [String],
    IntegrationTestHeader c -> Maybe Integer
ithTimeout :: Maybe Integer,
    IntegrationTestHeader c -> ExpectedResult c
ithResult :: ExpectedResult c
  }

data IntegrationTest c =
  IntegrationTest {
    IntegrationTest c -> IntegrationTestHeader c
itHeader :: IntegrationTestHeader c,
    IntegrationTest c -> [AnyCategory c]
itCategory :: [AnyCategory c],
    IntegrationTest c -> [DefinedCategory c]
itDefinition :: [DefinedCategory c],
    IntegrationTest c -> [TestProcedure c]
itTests :: [TestProcedure c]
  }

data ExpectedResult c =
  ExpectCompilerError {
    ExpectedResult c -> [c]
eceContext :: [c],
    ExpectedResult c -> [OutputPattern]
eceRequirePattern :: [OutputPattern],
    ExpectedResult c -> [OutputPattern]
eceExcludePattern :: [OutputPattern]
  } |
  ExpectCompiles {
    eceContext :: [c],
    eceRequirePattern :: [OutputPattern],
    eceExcludePattern :: [OutputPattern]
  } |
  ExpectRuntimeError {
    ExpectedResult c -> [c]
ereContext :: [c],
    ExpectedResult c -> [OutputPattern]
ereRequirePattern :: [OutputPattern],
    ExpectedResult c -> [OutputPattern]
ereExcludePattern :: [OutputPattern]
  } |
  ExpectRuntimeSuccess {
    ExpectedResult c -> [c]
ersContext :: [c],
    ExpectedResult c -> [OutputPattern]
ersRequirePattern :: [OutputPattern],
    ExpectedResult c -> [OutputPattern]
ersExcludePattern :: [OutputPattern]
  }

data OutputPattern =
  OutputPattern {
    OutputPattern -> OutputScope
opScope :: OutputScope,
    OutputPattern -> String
opPattern :: String
  }
  deriving (OutputPattern -> OutputPattern -> Bool
(OutputPattern -> OutputPattern -> Bool)
-> (OutputPattern -> OutputPattern -> Bool) -> Eq OutputPattern
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: OutputPattern -> OutputPattern -> Bool
$c/= :: OutputPattern -> OutputPattern -> Bool
== :: OutputPattern -> OutputPattern -> Bool
$c== :: OutputPattern -> OutputPattern -> Bool
Eq,Eq OutputPattern
Eq OutputPattern
-> (OutputPattern -> OutputPattern -> Ordering)
-> (OutputPattern -> OutputPattern -> Bool)
-> (OutputPattern -> OutputPattern -> Bool)
-> (OutputPattern -> OutputPattern -> Bool)
-> (OutputPattern -> OutputPattern -> Bool)
-> (OutputPattern -> OutputPattern -> OutputPattern)
-> (OutputPattern -> OutputPattern -> OutputPattern)
-> Ord OutputPattern
OutputPattern -> OutputPattern -> Bool
OutputPattern -> OutputPattern -> Ordering
OutputPattern -> OutputPattern -> OutputPattern
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: OutputPattern -> OutputPattern -> OutputPattern
$cmin :: OutputPattern -> OutputPattern -> OutputPattern
max :: OutputPattern -> OutputPattern -> OutputPattern
$cmax :: OutputPattern -> OutputPattern -> OutputPattern
>= :: OutputPattern -> OutputPattern -> Bool
$c>= :: OutputPattern -> OutputPattern -> Bool
> :: OutputPattern -> OutputPattern -> Bool
$c> :: OutputPattern -> OutputPattern -> Bool
<= :: OutputPattern -> OutputPattern -> Bool
$c<= :: OutputPattern -> OutputPattern -> Bool
< :: OutputPattern -> OutputPattern -> Bool
$c< :: OutputPattern -> OutputPattern -> Bool
compare :: OutputPattern -> OutputPattern -> Ordering
$ccompare :: OutputPattern -> OutputPattern -> Ordering
$cp1Ord :: Eq OutputPattern
Ord,Int -> OutputPattern -> ShowS
[OutputPattern] -> ShowS
OutputPattern -> String
(Int -> OutputPattern -> ShowS)
-> (OutputPattern -> String)
-> ([OutputPattern] -> ShowS)
-> Show OutputPattern
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [OutputPattern] -> ShowS
$cshowList :: [OutputPattern] -> ShowS
show :: OutputPattern -> String
$cshow :: OutputPattern -> String
showsPrec :: Int -> OutputPattern -> ShowS
$cshowsPrec :: Int -> OutputPattern -> ShowS
Show)

data OutputScope = OutputAny | OutputCompiler | OutputStderr | OutputStdout deriving (OutputScope -> OutputScope -> Bool
(OutputScope -> OutputScope -> Bool)
-> (OutputScope -> OutputScope -> Bool) -> Eq OutputScope
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: OutputScope -> OutputScope -> Bool
$c/= :: OutputScope -> OutputScope -> Bool
== :: OutputScope -> OutputScope -> Bool
$c== :: OutputScope -> OutputScope -> Bool
Eq,Eq OutputScope
Eq OutputScope
-> (OutputScope -> OutputScope -> Ordering)
-> (OutputScope -> OutputScope -> Bool)
-> (OutputScope -> OutputScope -> Bool)
-> (OutputScope -> OutputScope -> Bool)
-> (OutputScope -> OutputScope -> Bool)
-> (OutputScope -> OutputScope -> OutputScope)
-> (OutputScope -> OutputScope -> OutputScope)
-> Ord OutputScope
OutputScope -> OutputScope -> Bool
OutputScope -> OutputScope -> Ordering
OutputScope -> OutputScope -> OutputScope
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: OutputScope -> OutputScope -> OutputScope
$cmin :: OutputScope -> OutputScope -> OutputScope
max :: OutputScope -> OutputScope -> OutputScope
$cmax :: OutputScope -> OutputScope -> OutputScope
>= :: OutputScope -> OutputScope -> Bool
$c>= :: OutputScope -> OutputScope -> Bool
> :: OutputScope -> OutputScope -> Bool
$c> :: OutputScope -> OutputScope -> Bool
<= :: OutputScope -> OutputScope -> Bool
$c<= :: OutputScope -> OutputScope -> Bool
< :: OutputScope -> OutputScope -> Bool
$c< :: OutputScope -> OutputScope -> Bool
compare :: OutputScope -> OutputScope -> Ordering
$ccompare :: OutputScope -> OutputScope -> Ordering
$cp1Ord :: Eq OutputScope
Ord,Int -> OutputScope -> ShowS
[OutputScope] -> ShowS
OutputScope -> String
(Int -> OutputScope -> ShowS)
-> (OutputScope -> String)
-> ([OutputScope] -> ShowS)
-> Show OutputScope
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [OutputScope] -> ShowS
$cshowList :: [OutputScope] -> ShowS
show :: OutputScope -> String
$cshow :: OutputScope -> String
showsPrec :: Int -> OutputScope -> ShowS
$cshowsPrec :: Int -> OutputScope -> ShowS
Show)

isExpectCompiles :: ExpectedResult c -> Bool
isExpectCompiles :: ExpectedResult c -> Bool
isExpectCompiles (ExpectCompiles [c]
_ [OutputPattern]
_ [OutputPattern]
_) = Bool
True
isExpectCompiles ExpectedResult c
_                      = Bool
False

isExpectCompilerError :: ExpectedResult c -> Bool
isExpectCompilerError :: ExpectedResult c -> Bool
isExpectCompilerError (ExpectCompilerError [c]
_ [OutputPattern]
_ [OutputPattern]
_) = Bool
True
isExpectCompilerError ExpectedResult c
_                          = Bool
False

isExpectRuntimeError :: ExpectedResult c -> Bool
isExpectRuntimeError :: ExpectedResult c -> Bool
isExpectRuntimeError (ExpectRuntimeError [c]
_ [OutputPattern]
_ [OutputPattern]
_) = Bool
True
isExpectRuntimeError ExpectedResult c
_                          = Bool
False

isExpectRuntimeSuccess :: ExpectedResult c -> Bool
isExpectRuntimeSuccess :: ExpectedResult c -> Bool
isExpectRuntimeSuccess (ExpectRuntimeSuccess [c]
_ [OutputPattern]
_ [OutputPattern]
_) = Bool
True
isExpectRuntimeSuccess ExpectedResult c
_                            = Bool
False

getRequirePattern :: ExpectedResult c -> [OutputPattern]
getRequirePattern :: ExpectedResult c -> [OutputPattern]
getRequirePattern (ExpectCompiles [c]
_ [OutputPattern]
rs [OutputPattern]
_)       = [OutputPattern]
rs
getRequirePattern (ExpectCompilerError [c]
_ [OutputPattern]
rs [OutputPattern]
_)   = [OutputPattern]
rs
getRequirePattern (ExpectRuntimeError [c]
_ [OutputPattern]
rs [OutputPattern]
_)   = [OutputPattern]
rs
getRequirePattern (ExpectRuntimeSuccess [c]
_ [OutputPattern]
rs [OutputPattern]
_) = [OutputPattern]
rs

getExcludePattern :: ExpectedResult c -> [OutputPattern]
getExcludePattern :: ExpectedResult c -> [OutputPattern]
getExcludePattern (ExpectCompiles [c]
_ [OutputPattern]
_ [OutputPattern]
es)       = [OutputPattern]
es
getExcludePattern (ExpectCompilerError [c]
_ [OutputPattern]
_ [OutputPattern]
es)   = [OutputPattern]
es
getExcludePattern (ExpectRuntimeError [c]
_ [OutputPattern]
_ [OutputPattern]
es)   = [OutputPattern]
es
getExcludePattern (ExpectRuntimeSuccess [c]
_ [OutputPattern]
_ [OutputPattern]
es) = [OutputPattern]
es