summaryrefslogtreecommitdiff
path: root/src/Test.hs
blob: 404d96500cd200040da3546fc05bb53c9aa75e80 (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
70
71
72
73
74
75
76
77
78
module Test (
    Test(..),
    TestStep(..),
    SourceLine(..),

    ProcName(..), textProcName, unpackProcName,
    NodeName(..), textNodeName, unpackNodeName,

    MonadEval(..),
    VarName(..), textVarName, unpackVarName,
    Expr(..), eval,
    Regex,
) where

import Control.Monad

import Data.List
import Data.Text (Text)
import qualified Data.Text as T

import Text.Regex.TDFA
import Text.Regex.TDFA.Text

import Process

data Test = Test
    { testName :: Text
    , testSteps :: [TestStep]
    }

data TestStep = Spawn ProcName NodeName
              | Send ProcName (Expr Text)
              | Expect SourceLine ProcName (Expr Regex) [VarName]
              | Wait

newtype SourceLine = SourceLine Text

newtype NodeName = NodeName Text
    deriving (Eq, Ord)

textNodeName :: NodeName -> Text
textNodeName (NodeName name) = name

unpackNodeName :: NodeName -> String
unpackNodeName (NodeName tname) = T.unpack tname


class MonadFail m => MonadEval m where
  lookupStringVar :: VarName -> m Text


data VarName = VarName [Text]
    deriving (Eq, Ord)

textVarName :: VarName -> Text
textVarName (VarName name) = T.concat $ intersperse (T.singleton '.') name

unpackVarName :: VarName -> String
unpackVarName = T.unpack . textVarName


data Expr a where
    StringVar :: VarName -> Expr Text
    StringLit :: Text -> Expr Text
    Concat :: [Expr Text] -> Expr Text
    Regex :: [Expr Text] -> Expr Regex

eval :: MonadEval m => Expr a -> m a
eval (StringVar var) = lookupStringVar var
eval (StringLit str) = return str
eval (Concat xs) = T.concat <$> mapM eval xs
eval (Regex xs) = do
    parts <- forM xs $ \case
        StringLit str -> return str
        expr -> T.concatMap (\c -> T.pack ['\\', c]) <$> eval expr
    case compile defaultCompOpt defaultExecOpt $ T.concat $ concat [[T.singleton '^'], parts, [T.singleton '$']] of
        Left err -> fail err
        Right re -> return re