aboutsummaryrefslogtreecommitdiffstats
path: root/test/TestUtils.hs
blob: ff533101bf57c1b83ab6281d6a8711527e91a29a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
{-# OPTIONS_GHC -Wno-missing-export-lists #-}
module TestUtils where

import qualified Data.Map as M
import qualified Data.Bifunctor as B
import Utils

testConfig :: Config
testConfig = Config {
    configScriptFileName = Nothing,
    configVerboseMode = False,
    configShowHelp = False,
    configPrintEvaled = PrintEvaledOff,
    configPrintCallStack = False,
    configUseREPL = False
}

testRunL :: Env -> LContext a -> IO (Either LException (a, LState))
testRunL env = runL LState {
    stateConfig = testConfig,
    stateEnv = env,
    stateDepth = 0,
    statePure = Impure
}

expectSuccessL :: Env -> LContext a -> IO (a, LState)
expectSuccessL env lc =
     do res <- testRunL env lc
        case res of
            Left ex -> error $ "unexpected error: " ++ (foldStackMessage ex)
            Right val -> return val

expectErrorL :: Show a => Env -> LContext a -> IO String
expectErrorL env lc =
     do res <- testRunL env lc
        case res of
            Left ex -> return $ foldStackMessage ex
            Right (val, _) -> error $ "unexpected success: " ++ show val

makeEnv :: [(String, AST)] -> Env
makeEnv = M.fromList . map (B.second Regular)

ast :: ASTNode -> AST
ast node = makeNonsenseAST node

astInteger :: Integer -> AST
astInteger a = ast $ ASTInteger a
astDouble :: Double -> AST
astDouble a = ast $ ASTDouble a
astSymbol :: String -> AST
astSymbol a = ast $ ASTSymbol a
astBoolean :: Bool -> AST
astBoolean a = ast $ ASTBoolean a
astString :: String -> AST
astString a = ast $ ASTString a
astTag :: String -> AST
astTag a = ast $ ASTTag (computeTagN a) a
astVector :: [AST] -> AST
astVector a = ast $ ASTVector a
astFunctionCall :: [AST] -> AST
astFunctionCall a = ast $ ASTFunctionCall a
astHashMap :: M.Map AST AST -> AST
astHashMap a = ast $ ASTHashMap a
astRecord :: String -> LRecord -> AST
astRecord a hmap = ast $ ASTRecord (computeTagN a) a hmap
astUnit :: AST
astUnit = ast $ ASTUnit
astHole :: AST
astHole = ast $ ASTHole