some additions to test helpers

This commit is contained in:
Joel Kaasinen
2021-01-28 11:08:17 +02:00
parent 43315919c4
commit 97db6d8ed2
2 changed files with 16 additions and 2 deletions
+5
View File
@@ -44,6 +44,11 @@ hasElements expected actual = counterexample' (" Expected elements (in any orde
++ "\n Was: " ++ show actual)
(sort expected == sort actual)
hasElementsDuplicates expected actual =
counterexample' (" Expected elements (in any order, duplicates allowed): " ++ show expected
++ "\n Was: " ++ show actual)
(nub (sort expected) == nub (sort actual))
was f actual = counterexample' (" Was: "++show actual) (f actual)
-- helpers
+11 -2
View File
@@ -1,7 +1,7 @@
{-# LANGUAGE TemplateHaskell, DeriveLift, StandaloneDeriving #-}
module Mooc.Th (testing, testing', timeLimit,
isDefined, withDefined, hasType, importsOnly, show',
isDefined, withDefined, hasType, hasType', importsOnly, show',
reifyType, DataType(..), FieldType(..), Constructor(..),
withInstance, withInstance1, classContains, defineInstance)
where
@@ -60,6 +60,15 @@ hasType s qtyp = do
then [|\k -> counterexample s (k $(varOrCon $ mkName s))|]
else [|\k -> counterexample ("The type of '"++s++"'\n Expected: "++expected++"\n Was: "++actual) False|]
hasType' :: String -> String -> Q Exp
hasType' s expected = do
mt <- getType s
let actual = case mt of Nothing -> "nothing"
Just t -> showType t
if (actual==expected)
then [|\k -> counterexample s (k $(varOrCon $ mkName s))|]
else [|\k -> counterexample ("The type of '"++s++"'\n Expected: "++expected++"\n Was: "++actual) False|]
-- testing types
data FieldType = SimpleType String | WeirdType
@@ -132,7 +141,7 @@ classContains cln varn = do
Just cl -> do
info <- reify cl
case info of
(ClassOpI _ _ parent) -> let pn = nameBase parent in [|counterexample ("Function "++varn++ "is in the wrong class!") (pn ?== cln)|]
(ClassOpI _ _ parent) -> let pn = nameBase parent in [|counterexample ("Function "++varn++" is in the wrong class!") (pn ?== cln)|]
_ -> [|counterexample ("Function "++varn++" is not a method of class "++cln) (property False)|]
lookupConstructor :: String -> Q (Maybe Type)