property-matchers 0.6.0.0 → 0.7.0.0
raw patch · 4 files changed
+44/−20 lines, 4 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
+ PropertyMatchers: failPretty :: HasCallStack => Doc () -> Doc () -> IO x
- PropertyMatchers: PropertyFailed :: !CallStack -> Doc ann -> actual -> PropertyException
+ PropertyMatchers: PropertyFailed :: !CallStack -> Doc ann -> Either (Doc ann) actual -> PropertyException
Files
- CHANGELOG.md +3/−0
- property-matchers.cabal +1/−1
- src/PropertyMatchers.hs +37/−18
- test/PropertyMatchersSpec.hs +3/−1
CHANGELOG.md view
@@ -1,5 +1,8 @@ # Revision history for property-matchers +## 0.7+* Use the `Show` constraint on `equals` to produce a better printed `actual`, not just `expected`.+ ## 0.6 * Add `Show` constraint to `equals` and other comparison properties to improve error messages in cases where rtti-derived printing is not as good.
property-matchers.cabal view
@@ -1,7 +1,7 @@ cabal-version: 2.4 name: property-matchers-version: 0.6.0.0+version: 0.7.0.0 synopsis: A library for tests, based on transforming and writing properties description: This package provides ways to write properties for testing such that they compose nicely and are easy to debug.
src/PropertyMatchers.hs view
@@ -29,6 +29,7 @@ ( Assertion , Boolish(..) , fail+ , failPretty , succeed , branch , PropertyException(..)@@ -83,6 +84,7 @@ import qualified "prettyprinter" Prettyprinter.Render.String as PP import "recover-rtti" Debug.RecoverRTTI (anythingToString) import Data.IORef+import qualified Prettyprinter.Render.Text as PP -- $setup -- >>> :set -XOverloadedStrings -XTypeApplications@@ -128,21 +130,22 @@ ------------------------------------------------------------------------------- anythingToTextPretty :: a -> TL.Text-anythingToTextPretty = Pretty.Simple.pStringOpt opts . anythingToString- where- opts = Pretty.Simple.defaultOutputOptionsNoColor- { Pretty.Simple.outputOptionsIndentAmount = 2- , Pretty.Simple.outputOptionsPageWidth = 120- , Pretty.Simple.outputOptionsCompact = True- , Pretty.Simple.outputOptionsCompactParens = True- , Pretty.Simple.outputOptionsInitialIndent = 0- }+anythingToTextPretty = Pretty.Simple.pStringOpt prettyOpts . anythingToString +prettyOpts :: Pretty.Simple.OutputOptions+prettyOpts = Pretty.Simple.defaultOutputOptionsNoColor+ { Pretty.Simple.outputOptionsIndentAmount = 2+ , Pretty.Simple.outputOptionsPageWidth = 120+ , Pretty.Simple.outputOptionsCompact = True+ , Pretty.Simple.outputOptionsCompactParens = True+ , Pretty.Simple.outputOptionsInitialIndent = 0+ }+ -- | The exception thrown by properties of type `Assertion` by default. -- Other non-async exceptions will work fine too. -- data PropertyException- = forall actual ann. PropertyFailed !CallStack (PP.Doc ann) actual+ = forall actual ann. PropertyFailed !CallStack (PP.Doc ann) (Either (PP.Doc ann) actual) | forall actual. NoBranchMatched !CallStack actual deriving (Typeable) instance PP.Pretty PropertyException where@@ -165,7 +168,7 @@ <> PP.softline <> PP.unAnnotate expected ) where- prettyActual = anythingToTextPretty actual+ prettyActual = either (PP.renderLazy . PP.layoutSmart PP.defaultLayoutOptions) anythingToTextPretty actual prettyExnWithoutCallStack (NoBranchMatched _cs actual) = PP.group ( PP.line'@@ -195,8 +198,24 @@ -- :} -- Left Actual value 42 but expected something else fail :: HasCallStack => PP.Doc ann -> a -> IO x-fail expected actual = throwIO (PropertyFailed (popCallStack callStack) expected actual)+fail expected actual = throwIO $+ PropertyFailed (popCallStack callStack) expected (Right actual) +-- | Failed property. Includes documentation of the failed property,+-- as well as the asserted-on value for printing to the user.+-- Accepts pretty-printable values, for the cases when this is possible to do.+--+-- >>> let actual = 42+-- >>> :{+-- actual+-- & failPretty "something else"+-- & failureMessage+-- :}+-- Left Actual value 42 but expected something else+failPretty :: (HasCallStack) => PP.Doc () -> PP.Doc () -> IO x+failPretty expected actual = throwIO $+ PropertyFailed (popCallStack callStack) expected (Left actual)+ -- | Always-successful property, equivalent to @\_ -> pure ()@. -- -- >>> let actual = 42@@ -474,42 +493,42 @@ equals :: (HasCallStack, Show a, Eq a) => a -> Prop a equals expected actual | expected == actual = succeed actual- | otherwise = fail (PP.viaShow expected) actual+ | otherwise = failPretty (PP.viaShow expected) (PP.viaShow actual) -- | The property of not being equal to some expected value. -- notEquals :: (HasCallStack, Show a, Eq a) => a -> Prop a notEquals expected actual | expected /= actual = succeed actual- | otherwise = fail ("not equal to " <> PP.viaShow expected) actual+ | otherwise = failPretty ("not equal to " <> PP.viaShow expected) (PP.viaShow actual) -- | The property of being less than some expected value. -- lt :: (HasCallStack, Show a, Ord a) => a -> Prop a lt expected actual | actual < expected = succeed actual- | otherwise = fail ("less than " <> PP.viaShow expected) actual+ | otherwise = failPretty ("less than " <> PP.viaShow expected) (PP.viaShow actual) -- | The property of being greater than some expected value. -- gt :: (HasCallStack, Show a, Ord a) => a -> Prop a gt expected actual | actual > expected = succeed actual- | otherwise = fail ("greater than " <> PP.viaShow expected) actual+ | otherwise = failPretty ("greater than " <> PP.viaShow expected) (PP.viaShow actual) -- | The property of being less than or equal to some expected value. -- lte :: (HasCallStack, Show a, Ord a) => a -> Prop a lte expected actual | actual <= expected = succeed actual- | otherwise = fail ("less than/equal to " <> PP.viaShow expected) actual+ | otherwise = failPretty ("less than/equal to " <> PP.viaShow expected) (PP.viaShow actual) -- | The property of being greater than or equal to some expected value. -- gte :: (HasCallStack, Show a, Ord a) => a -> Prop a gte expected actual | actual >= expected = succeed actual- | otherwise = fail ("greater than/equal to " <> PP.viaShow expected) actual+ | otherwise = failPretty ("greater than/equal to " <> PP.viaShow expected) (PP.viaShow actual) -- | Sugar for tupling. -- The intended use is something like
test/PropertyMatchersSpec.hs view
@@ -29,6 +29,8 @@ import Debug.RecoverRTTI (anythingToString) import System.IO.Unsafe (unsafePerformIO) import Data.Text (Text)+import qualified Data.Text.Lazy as TL+import qualified Prettyprinter.Render.String as PP data AndOrTree = And AndOrTree AndOrTree | Or AndOrTree AndOrTree | Fail String | Error String | Succeed deriving stock (Generic, Show)@@ -50,7 +52,7 @@ Just failure -> error (show failure) pattern PropertyFailed :: Text -> String -> P.PropertyException-pattern PropertyFailed msg actual <- P.PropertyFailed _ (PP.renderStrict . PP.layoutCompact -> msg) (anythingToString -> actual)+pattern PropertyFailed msg actual <- P.PropertyFailed _ (PP.renderStrict . PP.layoutCompact -> msg) (either (PP.renderString . PP.layoutCompact) anythingToString -> actual) _PropertyFailed :: Fold P.PropertyException (Text, String) _PropertyFailed = to (\case