shallow uitgebreid met list stuff
[ap2015.git] / a10 / charlie / matcher_shallow.icl
1 module matcher_shallow
2
3 import StdEnv
4
5 Contains :: a [a] -> Bool | == a
6 Contains x xs = foldl (\b y . b || x == y) False xs
7
8 EqualTo :: a a -> Bool | == a
9 EqualTo x y = x == y
10
11 LessThen :: a a -> Bool | < a
12 LessThen x y = y < x
13
14 Not :: (a -> Bool) a -> Bool
15 Not f x = not (f x)
16
17 Is :: (a -> Bool) a -> Bool
18 Is f x = f x
19
20 (Or) infixl 2 :: (a -> Bool) (a -> Bool) -> (a -> Bool)
21 (Or) f g = \x . f x || g x
22
23 AssertThat :: String a (a -> Bool) -> [String]
24 AssertThat id x f | f x = [id : " " : "PASS" : []]
25 | otherwise = [id : " " : "FAIL": []]
26
27 instance * [String] where
28 (*) x y = x ++ ["\n":y]
29
30 test :: [String] -> [String]
31 test x = x ++ ["\n"]
32
33 a1 = AssertThat "(2*2) (Is (EqualTo (2+2)))" (2*2) (Is(EqualTo (2+2)))
34 a2 = AssertThat "(3*3) (EqualTo (3+3))" (3*3) (EqualTo (3+3))
35 a3 = AssertThat "(length [0..3]) is not 4" (length [0..3]) (Not(EqualTo 4))
36 a4 = AssertThat "[0..3] (Contains 2)" [0..3] (Contains 2)
37 a5 = AssertThat "[0..3] (Contains 7)" [0..3] (Contains 7)
38 a6 = AssertThat "[0..3] (Either (EqualTo [1]) (Contains 7))" [0..3] (EqualTo [1] Or (Contains 7))
39
40 report = test (a1*a2*a3*a4*a5*a6)
41
42 instance <<< [String] where
43 (<<<) file [] = file
44 (<<<) file [s:ss] = file <<< s <<< ss
45
46 Start :: !*World -> *World
47 Start world
48 # (file, world) = stdio world
49 # file = file <<< report
50 # (ok, world) = fclose file world
51 | otherwise = world