From dab75d98c8238b816e475d26b4c67c6e8fa13d75 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Thu, 16 Aug 2018 06:16:09 +0100 Subject: [PATCH] lib: expectEq', expectParseEq: pretty-print compared values (#812) --- hledger-lib/Hledger/Utils/Test.hs | 8 +++++++- hledger-lib/hledger-lib.cabal | 6 +++++- hledger-lib/package.yaml | 1 + 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/hledger-lib/Hledger/Utils/Test.hs b/hledger-lib/Hledger/Utils/Test.hs index 4bab8e632..9882daaa5 100644 --- a/hledger-lib/Hledger/Utils/Test.hs +++ b/hledger-lib/Hledger/Utils/Test.hs @@ -7,6 +7,7 @@ import Control.Exception import Control.Monad import Control.Monad.State.Strict (StateT) --import Control.Monad.State.Strict (evalStateT) +import Data.CallStack import Data.Functor.Identity import Data.List import qualified Data.Text as T @@ -102,7 +103,12 @@ runEasyTests args easytests = (do expectParseEq :: (Monoid st, Eq a, Show a) => StateT st (ParsecT CustomErr T.Text Identity) a -> T.Text -> a -> EasyTest.Test () expectParseEq parser input expected = do let ep = runIdentity $ parseWithState mempty parser input - either (fail.("parse error at "++).parseErrorPretty) (flip expectEq expected) ep + either (fail.("parse error at "++).parseErrorPretty) (flip expectEq' expected) ep + +-- | Like easytest's expectEq, but pretty-prints the values in failure output. +expectEq' :: (Eq a, Show a, HasCallStack) => a -> a -> EasyTest.Test () +expectEq' x y = if x == y then ok else crash $ + "expected:\n" <> T.pack (pshow x) <> "\nbut got:\n" <> T.pack (pshow y) <> "\n" -- | Run some hunit tests, returning True if there was a problem. -- With arguments, runs only tests whose names contain the first argument diff --git a/hledger-lib/hledger-lib.cabal b/hledger-lib/hledger-lib.cabal index d825a2a40..00618e45a 100644 --- a/hledger-lib/hledger-lib.cabal +++ b/hledger-lib/hledger-lib.cabal @@ -2,7 +2,7 @@ -- -- see: https://github.com/sol/hpack -- --- hash: b52d450888004e007b3689cfe42d916ab8e8af3bc91a6a374ff022a719e86611 +-- hash: 297d8918f59c94ceae6e61bc5df3a67247c22d5cd135ae9f9ffc8fd84cf144d7 name: hledger-lib version: 1.10.99 @@ -111,6 +111,7 @@ library , base-compat-batteries >=0.10.1 && <0.11 , blaze-markup >=0.5.1 , bytestring + , call-stack , cmdargs >=0.10 , containers , csv @@ -209,6 +210,7 @@ test-suite doctests , base-compat-batteries >=0.10.1 && <0.11 , blaze-markup >=0.5.1 , bytestring + , call-stack , cmdargs >=0.10 , containers , csv @@ -308,6 +310,7 @@ test-suite easytests , base-compat-batteries >=0.10.1 && <0.11 , blaze-markup >=0.5.1 , bytestring + , call-stack , cmdargs >=0.10 , containers , csv @@ -407,6 +410,7 @@ test-suite hunittests , base-compat-batteries >=0.10.1 && <0.11 , blaze-markup >=0.5.1 , bytestring + , call-stack , cmdargs >=0.10 , containers , csv diff --git a/hledger-lib/package.yaml b/hledger-lib/package.yaml index 8ebd84751..8c5c9f062 100644 --- a/hledger-lib/package.yaml +++ b/hledger-lib/package.yaml @@ -45,6 +45,7 @@ dependencies: - array - blaze-markup >=0.5.1 - bytestring +- call-stack - cmdargs >=0.10 - containers - csv