Merge branch 'master' of github.com:dopefishh/ap2015
[ap2015.git] / a10 / charlie / matcher_shallow.icl
diff --git a/a10/charlie/matcher_shallow.icl b/a10/charlie/matcher_shallow.icl
new file mode 100644 (file)
index 0000000..b27db7e
--- /dev/null
@@ -0,0 +1,61 @@
+// Mart Lubbers s4109503\r
+// Charlie Gerhardus s3050009\r
+module matcher_shallow\r
+\r
+import qualified Text\r
+from Text import class Text, instance Text String\r
+import StdEnv\r
+\r
+Contains :: a [a] -> Bool | == a\r
+Contains x xs = foldl (\b y . b || x == y) False xs\r
+\r
+ContainsString :: String String -> Bool\r
+ContainsString x y = 'Text'.indexOf x y <> -1\r
+\r
+EqualTo :: a a -> Bool | == a\r
+EqualTo x y = x == y\r
+\r
+LessThen :: a a -> Bool | < a\r
+LessThen x y = y < x\r
+\r
+Not :: (a -> Bool) a -> Bool\r
+Not f x = not (f x)\r
+\r
+Is :: (a -> Bool) a -> Bool\r
+Is f x = f x\r
+\r
+(Or) infixl 2 :: (a -> Bool) (a -> Bool) -> (a -> Bool)\r
+(Or) f g = \x . f x || g x\r
+\r
+AssertThat :: String a (a -> Bool) -> [String]\r
+AssertThat id x f | f x = [id : " " : "PASS" : []]\r
+                  | otherwise = [id : " " : "FAIL": []]\r
+\r
+instance * [String] where\r
+       (*) x y = x ++ ["\n":y]\r
+\r
+test :: [String] -> [String]\r
+test x = x ++ ["\n"]\r
+\r
+a1 = AssertThat "(2*2) (Is (EqualTo (2+2)))" (2*2) (Is(EqualTo (2+2)))\r
+a2 = AssertThat "(3*3) (EqualTo (3+3))" (3*3) (EqualTo (3+3))\r
+a3 = AssertThat "(length [0..3]) is not 4" (length [0..3]) (Not(EqualTo 4))\r
+a4 = AssertThat "[0..3] (Contains 2)" [0..3] (Contains 2)\r
+a5 = AssertThat "[0..3] (Contains 7)" [0..3] (Contains 7)\r
+a6 = AssertThat "[0..3] (Either (EqualTo [1]) (Contains 7))" [0..3] (EqualTo [1] Or (Contains 7))\r
+a7 = AssertThat  "'hello world' (ContainsString 'hello')" "hello world" (ContainsString "hello")\r
+a8 = AssertThat  "'hello world' (ContainsString 'world')" "hello world" (ContainsString "world")\r
+a9 = AssertThat  "Red, yellow and blue" "Who is afraid of red, yellow and blue" (ContainsString "Red")\r
+\r
+report = test (a1*a2*a3*a4*a5*a6*a7*a8*a9)\r
+\r
+instance <<< [String] where\r
+       (<<<) file [] = file\r
+       (<<<) file [s:ss] = file <<< s <<< ss\r
+\r
+Start :: !*World -> *World\r
+Start world\r
+       # (file, world) = stdio world\r
+       # file = file <<< report\r
+       # (ok, world) = fclose file world\r
+       | otherwise = world
\ No newline at end of file