c439f86fa0536f41e77717968cc71ceac50400f1
[ap2015.git] / a10 / charlie / matcher_shallow.icl
1 module matcher_shallow
2
3 import qualified Text
4 from Text import class Text, instance Text String
5 import StdEnv
6
7 Contains :: a [a] -> Bool | == a
8 Contains x xs = foldl (\b y . b || x == y) False xs
9
10 ContainsString :: String String -> Bool
11 ContainsString x y = 'Text'.indexOf x y <> -1
12
13 EqualTo :: a a -> Bool | == a
14 EqualTo x y = x == y
15
16 LessThen :: a a -> Bool | < a
17 LessThen x y = y < x
18
19 Not :: (a -> Bool) a -> Bool
20 Not f x = not (f x)
21
22 Is :: (a -> Bool) a -> Bool
23 Is f x = f x
24
25 (Or) infixl 2 :: (a -> Bool) (a -> Bool) -> (a -> Bool)
26 (Or) f g = \x . f x || g x
27
28 AssertThat :: String a (a -> Bool) -> [String]
29 AssertThat id x f | f x = [id : " " : "PASS" : []]
30 | otherwise = [id : " " : "FAIL": []]
31
32 instance * [String] where
33 (*) x y = x ++ ["\n":y]
34
35 test :: [String] -> [String]
36 test x = x ++ ["\n"]
37
38 a1 = AssertThat "(2*2) (Is (EqualTo (2+2)))" (2*2) (Is(EqualTo (2+2)))
39 a2 = AssertThat "(3*3) (EqualTo (3+3))" (3*3) (EqualTo (3+3))
40 a3 = AssertThat "(length [0..3]) is not 4" (length [0..3]) (Not(EqualTo 4))
41 a4 = AssertThat "[0..3] (Contains 2)" [0..3] (Contains 2)
42 a5 = AssertThat "[0..3] (Contains 7)" [0..3] (Contains 7)
43 a6 = AssertThat "[0..3] (Either (EqualTo [1]) (Contains 7))" [0..3] (EqualTo [1] Or (Contains 7))
44 a7 = AssertThat "'hello world' (ContainsString 'hello')" "hello world" (ContainsString "hello")
45 a8 = AssertThat "'hello world' (ContainsString 'world')" "hello world" (ContainsString "world")
46 a9 = AssertThat "Red, yellow and blue" "Who is afraid of red, yellow and blue" (ContainsString "Red")
47
48 report = test (a1*a2*a3*a4*a5*a6*a7*a8*a9)
49
50 instance <<< [String] where
51 (<<<) file [] = file
52 (<<<) file [s:ss] = file <<< s <<< ss
53
54 Start :: !*World -> *World
55 Start world
56 # (file, world) = stdio world
57 # file = file <<< report
58 # (ok, world) = fclose file world
59 | otherwise = world