Skip to content

Instantly share code, notes, and snippets.

@paolino
Last active August 19, 2020 16:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paolino/fdadced749dedf10c762869291d5b737 to your computer and use it in GitHub Desktop.
Save paolino/fdadced749dedf10c762869291d5b737 to your computer and use it in GitHub Desktop.
pretty report inequality
{-
dependencies:
- base
- protolude
- tree-diff
-}
import Data.Typeable (Typeable)
import Data.TreeDiff (toExpr, exprDiff, ansiWlEditExpr, ToExpr)
import Text.Printf (printf)
import Test.HUnit (assertFailure)
-- | show a coloured difference of 2 values
prettyDifferences :: (ToExpr a) => a -> a -> String
prettyDifferences a1 a2 =
show $ ansiWlEditExpr $ exprDiff (toExpr a1) (toExpr a2)
infixl 4 @?=
-- | equality test which show pretty differences on fail
(@?=) :: (ToExpr a, Eq a, Typeable a) => a -> a -> IO ()
a1 @?= a2 =
if a1 == a2
then return ()
else assertFailure $ printf "Expected equality:\n%s" $
prettyDifferences a1 a2
shouldBe :: (ToExpr a, Eq a, Typeable a) => a -> a -> IO ()
shouldBe = (@?=)
@paolino
Copy link
Author

paolino commented Aug 19, 2020

*Test.ExpressiveShouldBe 
λ> import Test.HUnit
*Test.ExpressiveShouldBe Test.HUnit 
λ> import GHC.Generics
*Test.ExpressiveShouldBe Test.HUnit GHC.Generics
λ> :set -XDeriveGeneric 
*Test.ExpressiveShouldBe Test.HUnit GHC.Generics
λ> :set -XDeriveAnyClass 
*Test.ExpressiveShouldBe Test.HUnit GHC.Generics
λ> data A = A Int String Char [Int] deriving (Generic, ToExpr, Show, Eq)
*Test.ExpressiveShouldBe Test.HUnit GHC.Generics
λ> runTestTT $ TestList [TestLabel "CVD" $ TestCase $ shouldBe (A 5 "python" 'a' [1,2,3]) (A 2 "haskell" 'a' [1,4,3,5])]
### Failure in: 0:CVD                     
...................
Expected equality:
A -5
  +2
  -"python"
  +"haskell"
  'a'
  [1,-2,+4,3,+5]
Cases: 1  Tried: 1  Errors: 0  Failures: 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment