// Mart Lubbers s4109503 // Charlie Gerhardus s3050009 module matcher_shallow import qualified Text from Text import class Text, instance Text String import StdEnv Contains :: a [a] -> Bool | == a Contains x xs = foldl (\b y . b || x == y) False xs ContainsString :: String String -> Bool ContainsString x y = 'Text'.indexOf x y <> -1 EqualTo :: a a -> Bool | == a EqualTo x y = x == y LessThen :: a a -> Bool | < a LessThen x y = y < x Not :: (a -> Bool) a -> Bool Not f x = not (f x) Is :: (a -> Bool) a -> Bool Is f x = f x (Or) infixl 2 :: (a -> Bool) (a -> Bool) -> (a -> Bool) (Or) f g = \x . f x || g x AssertThat :: String a (a -> Bool) -> [String] AssertThat id x f | f x = [id : " " : "PASS" : []] | otherwise = [id : " " : "FAIL": []] instance * [String] where (*) x y = x ++ ["\n":y] test :: [String] -> [String] test x = x ++ ["\n"] a1 = AssertThat "(2*2) (Is (EqualTo (2+2)))" (2*2) (Is(EqualTo (2+2))) a2 = AssertThat "(3*3) (EqualTo (3+3))" (3*3) (EqualTo (3+3)) a3 = AssertThat "(length [0..3]) is not 4" (length [0..3]) (Not(EqualTo 4)) a4 = AssertThat "[0..3] (Contains 2)" [0..3] (Contains 2) a5 = AssertThat "[0..3] (Contains 7)" [0..3] (Contains 7) a6 = AssertThat "[0..3] (Either (EqualTo [1]) (Contains 7))" [0..3] (EqualTo [1] Or (Contains 7)) a7 = AssertThat "'hello world' (ContainsString 'hello')" "hello world" (ContainsString "hello") a8 = AssertThat "'hello world' (ContainsString 'world')" "hello world" (ContainsString "world") a9 = AssertThat "Red, yellow and blue" "Who is afraid of red, yellow and blue" (ContainsString "Red") report = test (a1*a2*a3*a4*a5*a6*a7*a8*a9) instance <<< [String] where (<<<) file [] = file (<<<) file [s:ss] = file <<< s <<< ss Start :: !*World -> *World Start world # (file, world) = stdio world # file = file <<< report # (ok, world) = fclose file world | otherwise = world