{-# OPTIONS -fglasgow-exts -fth -fno-monomorphism-restriction #-} -- |Tests for the Logic module module QuicQuid.LogicTest(t) where import Data.Maybe import qualified Data.Map as M import QuicQuid.Test import QuicQuid.Term import QuicQuid.Logic t = do run $ testUnify run $ testSubstitute testUnify :: Test testUnify = tests "unification" chk unifyTests where chk :: (String, String, [(String, Term)]) -> Assertion chk (pattern,target,answer) = assertEqual "Unification" answer (M.toList . fromJust $ uni pattern target) uni t1 t2 = let Just p1 = parse t1; Just p2 = parse t2 in unify1 p1 p2 unifyTests = [t3 "random 1234" "random 1234" [] ,t3 "resource{url:?url,content:?content}" "resource{content:?C,url:\"http...\"}" [("content",Var "C"),("url",Str "http...")] ,t3 "hash[sha256 ?hash, ?obj]" "hash[sha256 \"81dd\",?Doc]" [("hash",Str "81dd"),("obj",Var "Doc")] ,t3 "echo[?i,v(?r)]" "echo[someText,?out]" [("i",Str "someText"),("r",Str "out")] ,t3 " ?q " "match [?X, IBM] " [("q",App (Str "match")(Arr [Var "X",Str "IBM"]))] ,t3 "?x" " 34" [("x",Num 34)] ,t3 "?y[z,gg] [?y ,e][?n]" "x[z , gg][x,e][f]" [("n",Str "f"),("y",Str "x")] ,t3 "person?x" "person{name:joe, age:18}" [("x",Obj $ M.fromList [("age",Num 18.0),("name",Str "joe")])] ] -- Not currently supported (generic maps with term keys). -- ,t3 "person{age:?AGE, ?Z:?N}" "person{name :joe, age : 18}" [("AGE",Num 18),("N",Str "joe"),("Z",Str "name")] testSubstitute = tests "substitute" chk [t3 [("x",Num 33)] "?x" "33" ,t3 [("X",Num 33)] "match[?X,IBM]" "match[33,IBM]" ,t3 [("R",Str "ABC")] "integer ?R" "integer ABC" ] where chk (binding,target,result) = assertEqual "substitute" (substitute (M.fromList binding) (fromJust.parse $ target)) (fromJust.parse $ result)