module Data.Portray.Diff.HUnit (assertNoDiff, (@?-), (@-?)) where
import GHC.Stack (HasCallStack)
import Text.PrettyPrint.HughesPJ (($+$), colon, empty, render, text)
import Test.HUnit (Assertion, assertFailure)
import Data.Portray.Diff (Diff(..))
import Data.Portray.Pretty (portrayalToDoc)
assertNoDiff :: (HasCallStack, Diff a) => String -> a -> a -> Assertion
assertNoDiff :: String -> a -> a -> Assertion
assertNoDiff String
msg a
a a
b = case a -> a -> Maybe Portrayal
forall a. Diff a => a -> a -> Maybe Portrayal
diff a
a a
b of
Maybe Portrayal
Nothing -> () -> Assertion
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
Just Portrayal
d -> String -> Assertion
forall a. HasCallStack => String -> IO a
assertFailure (String -> Assertion) -> (Doc -> String) -> Doc -> Assertion
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Doc -> String
render (Doc -> Assertion) -> Doc -> Assertion
forall a b. (a -> b) -> a -> b
$
(if String -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null String
msg then Doc
empty else String -> Doc
text String
msg Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> Doc
colon) Doc -> Doc -> Doc
$+$
Portrayal -> Doc
portrayalToDoc Portrayal
d
(@?-) :: (HasCallStack, Diff a) => a -> a -> Assertion
a
a @?- :: a -> a -> Assertion
@?- a
b = String -> a -> a -> Assertion
forall a. (HasCallStack, Diff a) => String -> a -> a -> Assertion
assertNoDiff String
"" a
a a
b
(@-?) :: (HasCallStack, Diff a) => a -> a -> Assertion
a
a @-? :: a -> a -> Assertion
@-? a
b = String -> a -> a -> Assertion
forall a. (HasCallStack, Diff a) => String -> a -> a -> Assertion
assertNoDiff String
"" a
b a
a