From: Mart Lubbers Date: Fri, 27 Nov 2015 20:38:11 +0000 (+0100) Subject: a10 done except that tostring for lists behaves strange X-Git-Url: https://git.martlubbers.net/?a=commitdiff_plain;h=3f898f2dffcc1bbd79357ea526812b24038b2af5;p=ap2015.git a10 done except that tostring for lists behaves strange --- diff --git a/a10/mart/skeleton10.icl b/a10/mart/skeleton10.icl index 685874c..c38b398 100644 --- a/a10/mart/skeleton10.icl +++ b/a10/mart/skeleton10.icl @@ -7,42 +7,52 @@ from Text import class Text, instance Text String import StdOverloaded, StdBool, StdString, StdEnum, StdList :: TestState = {s :: Int, f :: Int, p :: [String]} -:: Test :== (TestState -> TestState) -:: Match a - = Is (Match a) - | Not (Match a) - | Either (Match a) (Match a) - | LessThen a - | EqualTo a - -eval :: a (Match a) -> Bool | ==, < a -eval x (Is y) = eval x y -eval x (EqualTo y) = x == y -eval x (LessThen y) = x < y -eval x (Not y) = not (eval x y) -eval x (Either y1 y2) = eval x y1 || eval x y2 - -show :: (Match a) [String] -> [String] | toString a -show (Is x) l = ["(Is ":show x [")":l]] -show (EqualTo x) l = ["(EqualTo ", toString x,")":l] -show (LessThen x) l = ["LessThen ", toString x,")":l] -show (Not x) l = ["Not ":show x [")":l]] -show (Either x1 x2) l = ["Either (":show x1 [")(":show x2 [")":l]]] - -assertThat :: String a (Match a) -> Test | ==, <, toString a -assertThat str x y = \st=:{f,s,p}.if (eval x y) - {st & s=s+1} - {st & f=f+1, p=p++[toString (f+1),": AssertThat ",str," ":show y [")\n"]]} +:: TestResult :== (Bool, [String]) +:: Test :== TestState -> TestState + +class Testable a | Eq, Ord, toString a + +instance * Test where + (*) t1 t2 = \st.let st`=t1 st in t2 st` + +EqualTo :: a -> (a -> TestResult) | Testable a +EqualTo q = \a.(q == a, ["(EqualTo ",toString q,")"]) + +LessThen :: a -> (a -> TestResult) | Testable a +LessThen q = \a.(q < a, ["(LessThen ",toString q,")"]) + +Not :: (a -> TestResult) -> (a -> TestResult) | Testable a +Not x = \r.let (v,p)=x r in (not v, ["(Not ":p++[")"]]) + +Is :: (a -> TestResult) -> (a -> TestResult) +Is x = \r.let (v,p)=x r in (v, ["(Is ":p++[")"]]) + +Either :: (a -> TestResult) (a -> TestResult) -> (a -> TestResult) +Either x1 x2 = \r.let (v1,p1)=x1 r in let(v2,p2)=x2 r in + (v1 || v2, ["(Either ":p1++[" ":p2++[")"]]]) + +Contains :: a -> ([a] -> TestResult) | Testable a +Contains x = \r.(isMember x r, ["(Contains ",toString x,")"]) + +ContainsString :: String -> (String -> TestResult) +ContainsString s = \r.('Text'.indexOf s r <> -1, + ["(ContainsString ",toString s,")"]) + +AssertThat :: String a (a -> TestResult) -> Test +AssertThat msg q t = \st=:{s,f,p}.case t q of + (True, _) = {st & s=s+1} + (_, p`) = {st & f=f+1, + p=p++[toString (f+1),". AssertThat ",msg," ":p`++["\n"]]} test :: Test -> String test t = 'Text'.concat ["passes=", toString s, ", fails=", toString f, "\n":p] where {f,s,p}=t {s=0, f=0, p=[]} -instance * Test where - (*) s1 s2 = \st.let st`=s1 st in s2 st` - -Start = test (t1 * t2 * t3) +Start = test (a1 * a2 * a3 * a4 * a5 * a6) where - t1 = assertThat "(2*2)" (2*2) (Is (EqualTo (2+2))) - t2 = assertThat "(3*3)" (3*3) (Is (EqualTo (3+3))) - t3 = assertThat "(length [0..3])" (length [0..3]) (Not (EqualTo 4)) + a1 = AssertThat "(2*2)" (2*2) (Is (EqualTo (2+2))) + a2 = AssertThat "(3*3)" (3*3) (Is (EqualTo (3+3))) + a3 = AssertThat "(length [0..3])" (length [0..3]) (Not (EqualTo 4)) + a4 = AssertThat "[0..3]" [0..3] (Contains 2) + a5 = AssertThat "[0..3]" [0..3] (Contains 7) + a6 = AssertThat "[0..3]" [0..3] (Either (EqualTo [1]) (Contains 7))