685874c407ab53ef4a2c82c2ecffb9ae288b5916
[ap2015.git] / a10 / mart / skeleton10.icl
1 // Mart Lubbers s4109503
2 // Charlie Gerhardus s3050009
3 module skeleton10
4
5 import qualified Text
6 from Text import class Text, instance Text String
7 import StdOverloaded, StdBool, StdString, StdEnum, StdList
8
9 :: TestState = {s :: Int, f :: Int, p :: [String]}
10 :: Test :== (TestState -> TestState)
11 :: Match a
12 = Is (Match a)
13 | Not (Match a)
14 | Either (Match a) (Match a)
15 | LessThen a
16 | EqualTo a
17
18 eval :: a (Match a) -> Bool | ==, < a
19 eval x (Is y) = eval x y
20 eval x (EqualTo y) = x == y
21 eval x (LessThen y) = x < y
22 eval x (Not y) = not (eval x y)
23 eval x (Either y1 y2) = eval x y1 || eval x y2
24
25 show :: (Match a) [String] -> [String] | toString a
26 show (Is x) l = ["(Is ":show x [")":l]]
27 show (EqualTo x) l = ["(EqualTo ", toString x,")":l]
28 show (LessThen x) l = ["LessThen ", toString x,")":l]
29 show (Not x) l = ["Not ":show x [")":l]]
30 show (Either x1 x2) l = ["Either (":show x1 [")(":show x2 [")":l]]]
31
32 assertThat :: String a (Match a) -> Test | ==, <, toString a
33 assertThat str x y = \st=:{f,s,p}.if (eval x y)
34 {st & s=s+1}
35 {st & f=f+1, p=p++[toString (f+1),": AssertThat ",str," ":show y [")\n"]]}
36
37 test :: Test -> String
38 test t = 'Text'.concat ["passes=", toString s, ", fails=", toString f, "\n":p]
39 where {f,s,p}=t {s=0, f=0, p=[]}
40
41 instance * Test where
42 (*) s1 s2 = \st.let st`=s1 st in s2 st`
43
44 Start = test (t1 * t2 * t3)
45 where
46 t1 = assertThat "(2*2)" (2*2) (Is (EqualTo (2+2)))
47 t2 = assertThat "(3*3)" (3*3) (Is (EqualTo (3+3)))
48 t3 = assertThat "(length [0..3])" (length [0..3]) (Not (EqualTo 4))