summaryrefslogtreecommitdiff
path: root/src/Unlambda/Test.hs
blob: f049b59a4128e18dce8a79f46d84de1f0b217af7 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
module Unlambda.Test (
    parserTests,
    interpreterTests,
    tests,
    ioTests
    ) where


import Test.HUnit
import Text.Parsec.Error
import Control.Monad
import System.IO.Silently
import Unlambda.Types
import Unlambda.Parser
import Unlambda.Interpreter


instance Eq Text.Parsec.Error.ParseError




parser0 = (Right S) ~=? (parseUnlambda "s")

parser1 = (Right K) ~=? (parseUnlambda "k")

parser2 = (Right I) ~=? (parseUnlambda "i")

parser3 = (Right V) ~=? (parseUnlambda "v")

parser4 = (Right R) ~=? (parseUnlambda "r")

parser5 = (Right D) ~=? (parseUnlambda "d")

parser6 = (Right C) ~=? (parseUnlambda "c")

parser7 = (Right E) ~=? (parseUnlambda "e")

parser8 = (Right (App S K)) ~=? (parseUnlambda "`sk")

parser9 = (Right (Dot 'c')) ~=? (parseUnlambda ".c")

parser10 = (Right (Compare '?')) ~=? (parseUnlambda "??")

parser11 = (Right Bar) ~=? (parseUnlambda "|")

parser12 = (Right Reed) ~=? (parseUnlambda "@")



interpretString :: String -> IO (Maybe (String,UnlambdaTerm))
interpretString input =
    let t = parseUnlambda input
    in case t of
        Left _ -> return Nothing
        Right term -> do
            c <- capture (unlambda term)
            return (Just c)



interpreter0 = (liftM2 (~=?))
                    (return (Just ("\n", R)) )
                    (interpretString "``cir")

interpreter1 = (liftM2 (~=?))
                    (return (Just ("", I)) )
                    (interpretString "`c``s`kr``si`ki")

interpreter2 = (liftM2 (~=?))
                    (return (Just ("", Promise (App R I))) )
                    (interpretString "`d`ri")

interpreter3 = (liftM2 (~=?))
                    (return (Just ("\n", Promise I)) )
                    (interpretString "``dd`ri")



parserTests :: Test
parserTests = TestList [parser0, parser1, parser2, parser3, parser4, parser5, parser6, parser7, parser8
                       ,parser9, parser10, parser11, parser12]



interpreterTests :: IO Test
interpreterTests = do
    t0 <- interpreter0
    t1 <- interpreter1
    t2 <- interpreter2
    t3 <- interpreter3
    return (TestList [t0,t1,t2,t3])



tests :: Test
tests = parserTests



ioTests :: IO Test
ioTests = interpreterTests