{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE ImplicitParams #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TypeSynonymInstances #-}

-- | This package provides the ability to run a Checklist of several
-- "checks" during a single test.  A "bad" check does not immediately
-- result in a test failure; at the end of the test (passed or failed
-- due to primary testing), all failed checks are reported (and any
-- failed checks will result in an overall test failure at the end.
--
-- This type of checking can be very useful when needing to test
-- various aspects of an operation that is complex to setup, has
-- multiple effects, or where the checks are related such that knowing
-- about the multiple failures makes debugging easier.
--
-- An alternative approach is to have some sort of common preparation
-- code and use a separate test for each item.  This module simply
-- provides a convenient method to collate related items under the
-- aegis of a single test.
--
-- This package also provides the 'checkValues' function which can be
-- used to check a number of derived values from a single input value
-- via a checklist.  This can be used to independently verify a number
-- of record fields of a data structure or to validate related
-- operations performed from a single input.
--
-- See the documentation for 'check' and 'checkValues' for examples of
-- using this library.  The tests in the source package also provide
-- additional examples of usage.

module Test.Tasty.Checklist
  (
    -- * Checklist testing context
    withChecklist
  , CanCheck
  -- * Performing or Disabling checks
  , check
  , discardCheck
  -- * Type-safe multi-check specifications
  -- $checkValues
  -- $setup
  , checkValues
  , DerivedVal(Val, Got, Observe)
  -- * Error reporting
  , CheckResult
  , ChecklistFailures
  -- * Displaying tested values
  , TestShow(testShow)
  , testShowList
  , multiLineDiff
  )
where

import           Control.Exception ( evaluate )
import           Control.Monad ( join, unless )
import           Control.Monad.Catch
import           Control.Monad.IO.Class ( MonadIO, liftIO )
import           Data.IORef
import qualified Data.List as List
import qualified Data.Parameterized.Context as Ctx
import           Data.Text ( Text )
import qualified Data.Text as T
import           System.IO ( hFlush, hPutStrLn, stdout, stderr )


-- | The ChecklistFailures exception is thrown if any checks have
-- failed during testing.

data ChecklistFailures = ChecklistFailures Text [CheckResult]

-- | The internal 'CheckResult' captures the failure information for a check

data CheckResult = CheckFailed Text Text  -- check name from user, fail message

instance Exception ChecklistFailures

instance Show CheckResult where
  show :: CheckResult -> String
show (CheckFailed Text
what Text
msg) =
    String
"Failed check of " forall a. Semigroup a => a -> a -> a
<> Text -> String
T.unpack Text
what forall a. Semigroup a => a -> a -> a
<> String
" with: " forall a. Semigroup a => a -> a -> a
<> Text -> String
T.unpack Text
msg

instance Show ChecklistFailures where
  show :: ChecklistFailures -> String
show (ChecklistFailures Text
topMsg [CheckResult]
fails) =
    String
"ERROR: " forall a. Semigroup a => a -> a -> a
<> Text -> String
T.unpack Text
topMsg forall a. Semigroup a => a -> a -> a
<> String
"\n  " forall a. Semigroup a => a -> a -> a
<>
    forall a. Show a => a -> String
show (forall (t :: * -> *) a. Foldable t => t a -> Int
length [CheckResult]
fails) forall a. Semigroup a => a -> a -> a
<> String
" checks failed in this checklist:\n  -" forall a. Semigroup a => a -> a -> a
<>
    forall a. [a] -> [[a]] -> [a]
List.intercalate String
"\n  -" (forall a. Show a => a -> String
show forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [CheckResult]
fails)

-- | A convenient Constraint to apply to functions that will perform
-- checks (i.e. call 'check' one or more times)

type CanCheck = (?checker :: IORef [CheckResult])


-- | This should be used to wrap the test that contains checks.  This
-- initializes the environment needed for the checks to run, and on
-- exit from the test, reports any (and all) failed checks as a test
-- failure.

withChecklist :: (MonadIO m, MonadMask m)
              => Text -> (CanCheck => m a) -> m a
withChecklist :: forall (m :: * -> *) a.
(MonadIO m, MonadMask m) =>
Text -> (CanCheck => m a) -> m a
withChecklist Text
topMsg CanCheck => m a
t = do
  IORef [CheckResult]
checks <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a. a -> IO (IORef a)
newIORef forall a. Monoid a => a
mempty
  a
r <- (let ?checker = IORef [CheckResult]
checks in CanCheck => m a
t)
       forall (m :: * -> *) a b. MonadCatch m => m a -> m b -> m a
`onException` (forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$
                       do [CheckResult]
cs <- forall a. [a] -> [a]
List.reverse forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. IORef a -> IO a
readIORef IORef [CheckResult]
checks
                          forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (forall (t :: * -> *) a. Foldable t => t a -> Bool
null [CheckResult]
cs) forall a b. (a -> b) -> a -> b
$ do
                            Handle -> IO ()
hFlush Handle
stdout
                            Handle -> String -> IO ()
hPutStrLn Handle
stderr String
""
                            let pfx :: String
pfx = String
"        WARN "
                            forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (Handle -> String -> IO ()
hPutStrLn Handle
stderr forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String
pfx forall a. Semigroup a => a -> a -> a
<>) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> String
show) [CheckResult]
cs
                            Handle -> IO ()
hFlush Handle
stderr
                     )

  -- If t failed, never get here:
  forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ do
    [CheckResult]
collected <- forall a. [a] -> [a]
List.reverse forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. IORef a -> IO a
readIORef IORef [CheckResult]
checks
    forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (forall (t :: * -> *) a. Foldable t => t a -> Bool
null [CheckResult]
collected) forall a b. (a -> b) -> a -> b
$
      forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwM (Text -> [CheckResult] -> ChecklistFailures
ChecklistFailures Text
topMsg [CheckResult]
collected)
  forall (m :: * -> *) a. Monad m => a -> m a
return a
r

-- | This is used to run a check within the code.  The first argument
-- is the "name" of this check, the second is a function that takes a
-- value and returns 'True' if the value is OK, or 'False' if the
-- value fails the check.  The last argument is the value to check.
--
-- >>> :set -XOverloadedStrings
-- >>> import Test.Tasty
-- >>> import Test.Tasty.HUnit
-- >>> :{
-- >>> defaultMain $ testCase "odd numbers" $ withChecklist "odds" $ do
-- >>>  let three = 3 :: Int
-- >>>  check "three is odd" odd three
-- >>>  check "two is odd" odd (2 :: Int)
-- >>>  check "7 + 3 is odd" odd $ 7 + three
-- >>>  check "7 is odd" odd (7 :: Int)
-- >>> :}
-- odd numbers: FAIL
--   Exception: ERROR: odds
--     2 checks failed in this checklist:
--     -Failed check of two is odd with: 2
--     -Failed check of 7 + 3 is odd with: 10
-- <BLANKLINE>
-- 1 out of 1 tests failed (...s)
-- *** Exception: ExitFailure 1
--
-- Any check failures are also printed to stdout (and omitted from the
-- above for clarity).  This is so that those failures are reported
-- even if a more standard test assertion is used that prevents
-- completion of the checklist.  Thus, if an @assertEqual "values"
-- three 7@ had been added to the above, that would have been the only
-- actual (and immediate) fail for the test, but any failing 'check's
-- appearing before that @assertEqual@ would still have printed.

check :: (CanCheck, TestShow a, MonadIO m)
      => Text -> (a -> Bool) -> a -> m ()
check :: forall a (m :: * -> *).
(CanCheck, TestShow a, MonadIO m) =>
Text -> (a -> Bool) -> a -> m ()
check = forall (m :: * -> *) a.
(CanCheck, MonadIO m) =>
(a -> String) -> Text -> (a -> Bool) -> a -> m ()
checkShow forall v. TestShow v => v -> String
testShow

checkShow :: (CanCheck, MonadIO m)
          => (a -> String) -> Text -> (a -> Bool) -> a -> m ()
checkShow :: forall (m :: * -> *) a.
(CanCheck, MonadIO m) =>
(a -> String) -> Text -> (a -> Bool) -> a -> m ()
checkShow a -> String
showit Text
what a -> Bool
eval a
val = do
  Bool
r <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a. a -> IO a
evaluate (a -> Bool
eval a
val)
  forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless Bool
r forall a b. (a -> b) -> a -> b
$ do
    let chk :: CheckResult
chk = Text -> Text -> CheckResult
CheckFailed Text
what forall a b. (a -> b) -> a -> b
$ String -> Text
T.pack forall a b. (a -> b) -> a -> b
$ a -> String
showit a
val
    forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a. IORef a -> (a -> a) -> IO ()
modifyIORef CanCheck
?checker (CheckResult
chkforall a. a -> [a] -> [a]
:)


-- | Sometimes checks are provided in common testing code, often in
-- setup/preparation for the main tests.  In some cases, the check is
-- not applicable for that particular test.  This function can be used
-- to discard any pending failures for the associated named check.
--
-- This is especially useful when a common code block is used to
-- perform a set of checks: if a few of the common checks are not
-- appropriate for the current situation, 'discardCheck' can be used
-- to throw away the results of those checks by matching on the check
-- name.

discardCheck :: (CanCheck, MonadIO m) => Text -> m ()
discardCheck :: forall (m :: * -> *). (CanCheck, MonadIO m) => Text -> m ()
discardCheck Text
what = do
  let isCheck :: Text -> CheckResult -> Bool
isCheck Text
n (CheckFailed Text
n' Text
_) = Text
n forall a. Eq a => a -> a -> Bool
== Text
n'
  forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a. IORef a -> (a -> a) -> IO ()
modifyIORef CanCheck
?checker (forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> CheckResult -> Bool
isCheck Text
what))

----------------------------------------------------------------------

-- $checkValues
--
-- Implementing a number of discrete 'check' calls can be tedious,
-- especially when they are validating different aspects of the same
-- result value.  To facilitate this, the 'checkValues' function can
-- be used along with a type-safe list of checks to perform.
--
-- To demonstrate this, first consider the following sample program,
-- which has code that generates a complex @Struct@ value, along with
-- tests for various fields in that @Struct@.

-- $setup
-- >>> :set -XPatternSynonyms
-- >>> :set -XOverloadedStrings
-- >>>
-- >>> import Data.Parameterized.Context ( pattern Empty, pattern (:>) )
-- >>> import Test.Tasty.Checklist
-- >>> import Test.Tasty
-- >>> import Test.Tasty.HUnit
-- >>>
-- >>> :{
-- >>> data Struct = MyStruct { foo :: Int, bar :: Char, baz :: String }
-- >>>
-- >>> instance Show Struct where
-- >>>    show s = baz s <> " is " <> show (foo s) <> [bar s]
-- >>> instance TestShow Struct where testShow = show
-- >>>
-- >>> someFun :: Int -> Struct
-- >>> someFun n = MyStruct (n * 6)
-- >>>               (if n * 6 == 42 then '!' else '?')
-- >>>               "The answer to the universe"
-- >>>
-- >>> oddAnswer :: Struct -> Bool
-- >>> oddAnswer = odd . foo
-- >>>
-- >>> test = testCase "someFun result" $
-- >>>    withChecklist "results for someFun" $
-- >>>    someFun 3 `checkValues`
-- >>>         (Empty
-- >>>         :> Val "foo" foo 42
-- >>>         :> Val "baz field" baz "The answer to the universe"
-- >>>         :> Val "shown" show "The answer to the universe is 42!"
-- >>>         :> Val "odd answer" oddAnswer False
-- >>>         :> Got "even answer" (not . oddAnswer)
-- >>>         :> Val "double-checking foo" foo 42
-- >>>         )
-- >>> :}
--
-- This code will be used below to demonstrate various advanced
-- checklist capabilities.

-- | The 'checkValues' is a checklist that tests various values that
-- can be derived from the input value.  The input value is provided,
-- along with an 'Data.Parameterized.Context.Assignment' list of
-- extraction functions and the expected result value (and name) of
-- that extraction.  Each extraction is performed as a check within
-- the checklist.
--
-- This is convenient to gather together a number of validations on a
-- single datatype and represent them economically.
--
-- One example is testing the fields of a record structure, given the
-- above code:
--
-- >>> defaultMain test
-- someFun result: FAIL
--   Exception: ERROR: results for someFun
--     3 checks failed in this checklist:
--     -Failed check of foo on input <<The answer to the universe is 18?>>
--           expected:    42
--           failed with: 18
--     -Failed check of shown on input <<The answer to the universe is 18?>>
--           expected:    "The answer to the universe is 42!"
--           failed with: "The answer to the universe is 18?"
--     -Failed check of double-checking foo on input <<The answer to the universe is 18?>>
--           expected:    42
--           failed with: 18
-- <BLANKLINE>
-- 1 out of 1 tests failed (...s)
-- *** Exception: ExitFailure 1
--
-- In this case, several of the values checked were correct, but more
-- than one was wrong.  Helpfully, this test output lists /all/ the
-- wrong answers for the single input provided.

checkValues :: CanCheck
            => TestShow dType
            => dType -> Ctx.Assignment (DerivedVal dType) idx ->  IO ()
checkValues :: forall dType (idx :: Ctx (*)).
(CanCheck, TestShow dType) =>
dType -> Assignment (DerivedVal dType) idx -> IO ()
checkValues dType
got Assignment (DerivedVal dType) idx
expF =
  forall (m :: * -> *) a. Monad m => m (m a) -> m a
join forall a b. (a -> b) -> a -> b
$ forall a. a -> IO a
evaluate forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall {k} (m :: * -> *) (ctx :: Ctx k) (f :: k -> *).
Applicative m =>
(forall (tp :: k). Index ctx tp -> f tp -> m ())
-> Assignment f ctx -> m ()
Ctx.traverseWithIndex_ (forall dType (idx :: Ctx (*)) valType.
(CanCheck, TestShow dType) =>
dType -> Index idx valType -> DerivedVal dType valType -> IO ()
chkValue dType
got) Assignment (DerivedVal dType) idx
expF


chkValue :: CanCheck
         => TestShow dType
         => dType -> Ctx.Index idx valType -> DerivedVal dType valType -> IO ()
chkValue :: forall dType (idx :: Ctx (*)) valType.
(CanCheck, TestShow dType) =>
dType -> Index idx valType -> DerivedVal dType valType -> IO ()
chkValue dType
got Index idx valType
_idx = \case
  (Val Text
txt dType -> valType
fld valType
v) ->
    let r :: valType
r = dType -> valType
fld dType
got
        msg :: Text
msg = Text
txt forall a. Semigroup a => a -> a -> a
<> Text
" on input <<" forall a. Semigroup a => a -> a -> a
<> Text
ti forall a. Semigroup a => a -> a -> a
<> Text
">>\n"
              forall a. Semigroup a => a -> a -> a
<> Text
"        " forall a. Semigroup a => a -> a -> a
<> Text
"expected:    " forall a. Semigroup a => a -> a -> a
<> Text
tv forall a. Semigroup a => a -> a -> a
<> Text
"\n"
              forall a. Semigroup a => a -> a -> a
<> Text
"        " forall a. Semigroup a => a -> a -> a
<> Text
"failed"
        ti :: Text
ti = String -> Text
T.pack (forall v. TestShow v => v -> String
testShow dType
got)
        tv :: Text
tv = String -> Text
T.pack (forall v. TestShow v => v -> String
testShow valType
v)
    in forall a (m :: * -> *).
(CanCheck, TestShow a, MonadIO m) =>
Text -> (a -> Bool) -> a -> m ()
check Text
msg (valType
v forall a. Eq a => a -> a -> Bool
==) valType
r
  (Observe Text
txt dType -> valType
fld valType
v valType -> valType -> String
observationReport) ->
    let r :: valType
r = dType -> valType
fld dType
got
        msg :: Text
msg = Text
txt forall a. Semigroup a => a -> a -> a
<> Text
" observation failure"
    in forall (m :: * -> *) a.
(CanCheck, MonadIO m) =>
(a -> String) -> Text -> (a -> Bool) -> a -> m ()
checkShow (valType -> valType -> String
observationReport valType
v) Text
msg (valType
v forall a. Eq a => a -> a -> Bool
==) valType
r
  (Got Text
txt dType -> Bool
fld) ->
    let r :: Bool
r = dType -> Bool
fld dType
got
        msg :: Text
msg = Text
txt forall a. Semigroup a => a -> a -> a
<> Text
" on input <<" forall a. Semigroup a => a -> a -> a
<> Text
ti forall a. Semigroup a => a -> a -> a
<> Text
">>"
        ti :: Text
ti = String -> Text
T.pack (forall v. TestShow v => v -> String
testShow dType
got)
    in forall a (m :: * -> *).
(CanCheck, TestShow a, MonadIO m) =>
Text -> (a -> Bool) -> a -> m ()
check Text
msg (Bool
True forall a. Eq a => a -> a -> Bool
==) Bool
r

-- | Each entry in the 'Data.Parameterized.Context.Assignment' list
-- for 'checkValues' should be one of these 'DerivedVal' values.
--
-- The @i@ type parameter is the input type, and the @d@ is the value
-- derived from that input type.

data DerivedVal i d where

  -- | Val allows specification of a description string, an extraction
  -- function, and the expected value to be extracted.  The
  -- 'checkValues' function will add a Failure if the expected value is
  -- not obtained.
  Val :: (TestShow d, Eq d) => Text -> (i -> d) -> d -> DerivedVal i d

  -- | Got allows specification of a description string and an
  -- extraction function.  The 'checkValues' function will add a
  -- Failure if the extraction result is False.
  --
  -- > Val "what" f True === Got "what" f
  --
  Got :: Text -> (i -> Bool) -> DerivedVal i Bool

  -- | Observe performs the same checking as Val except the TestShow
  -- information for the actual and expected values are not as useful
  -- (e.g. they are lengthy, multi-line, or gibberish) so instead this
  -- allows the specification of a function that will take the
  -- supplied expected value and the result of the extraction function
  -- (the actual), respectively, and generate its own description of
  -- the failure.
  --
  Observe :: (Eq d) => Text -> (i -> d) -> d -> (d -> d -> String) -> DerivedVal i d

----------------------------------------------------------------------

-- | The 'TestShow' class is defined to provide a way for the various
-- data objects tested by this module to be displayed when tests fail.
-- The default 'testShow' will use a 'Show' instance, but this can be
-- overridden if there are alternate ways to display a particular
-- object (e.g. pretty-printing, etc.)

class TestShow v where
  testShow :: v -> String
  default testShow :: Show v => v -> String
  testShow = forall a. Show a => a -> String
show

-- Some TestShow instances using Show for regular datatypes
instance TestShow ()
instance TestShow Bool
instance TestShow Int
instance TestShow Integer
instance TestShow Float
instance TestShow Char
instance TestShow String

instance (TestShow a, TestShow b) => TestShow (a,b) where
  testShow :: (a, b) -> String
testShow (a
a,b
b) = String
"(" forall a. Semigroup a => a -> a -> a
<> forall v. TestShow v => v -> String
testShow a
a forall a. Semigroup a => a -> a -> a
<> String
", " forall a. Semigroup a => a -> a -> a
<> forall v. TestShow v => v -> String
testShow b
b forall a. Semigroup a => a -> a -> a
<> String
")"
instance (TestShow a, TestShow b, TestShow c) => TestShow (a,b,c) where
  testShow :: (a, b, c) -> String
testShow (a
a,b
b,c
c) = String
"(" forall a. Semigroup a => a -> a -> a
<> forall v. TestShow v => v -> String
testShow a
a forall a. Semigroup a => a -> a -> a
<> String
", " forall a. Semigroup a => a -> a -> a
<> forall v. TestShow v => v -> String
testShow b
b forall a. Semigroup a => a -> a -> a
<> String
", " forall a. Semigroup a => a -> a -> a
<> forall v. TestShow v => v -> String
testShow c
c forall a. Semigroup a => a -> a -> a
<> String
")"

-- | A helper function for defining a testShow for lists of items.
--
-- > instance TestShow [Int] where testShow = testShowList

testShowList :: TestShow v => [v] -> String
testShowList :: forall v. TestShow v => [v] -> String
testShowList  [v]
l = String
"[ " forall a. Semigroup a => a -> a -> a
<> (forall a. [a] -> [[a]] -> [a]
List.intercalate String
", " (forall v. TestShow v => v -> String
testShow forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [v]
l)) forall a. Semigroup a => a -> a -> a
<> String
" ]"


-- | The multiLineDiff is another helper function that can be used to
-- format a line-by-line difference display of two Text
-- representations.  This is provided as a convenience function to
-- help format large text regions for easier comparison.

multiLineDiff :: T.Text -> T.Text -> String
multiLineDiff :: Text -> Text -> String
multiLineDiff Text
expected Text
actual =
  let dl :: (a, a) -> a
dl (a
e,a
a) = if a
e forall a. Eq a => a -> a -> Bool
== a
a then forall {a}. (Semigroup a, IsString a) => a -> a
db a
e else forall {a}. (Semigroup a, IsString a) => a -> a -> a
de a
" ↱" a
e forall a. Semigroup a => a -> a -> a
<> a
"\n    " forall a. Semigroup a => a -> a -> a
<> forall {a}. (Semigroup a, IsString a) => a -> a -> a
da a
" ↳" a
a
      db :: a -> a
db a
b = a
"|        > " forall a. Semigroup a => a -> a -> a
<> a
b
      de :: a -> a -> a
de a
m a
e = a
"|" forall a. Semigroup a => a -> a -> a
<> a
m forall a. Semigroup a => a -> a -> a
<> a
"expect> " forall a. Semigroup a => a -> a -> a
<> a
e
      da :: a -> a -> a
da a
m a
a = a
"|" forall a. Semigroup a => a -> a -> a
<> a
m forall a. Semigroup a => a -> a -> a
<> a
"actual> " forall a. Semigroup a => a -> a -> a
<> a
a
      el :: [Text]
el = Text -> Text
visible forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> [Text]
T.lines Text
expected
      al :: [Text]
al = Text -> Text
visible forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> [Text]
T.lines Text
actual
      visible :: Text -> Text
visible = Text -> Text -> Text -> Text
T.replace Text
" " Text
"␠"
                forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text -> Text -> Text
T.replace Text
"\n" Text
"␤"
                forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text -> Text -> Text
T.replace Text
"\t" Text
"␉"
                forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text -> Text -> Text
T.replace Text
"\012" Text
"␍"
      addnum :: Int -> T.Text -> T.Text
      addnum :: Int -> Text -> Text
addnum Int
n Text
l = let nt :: Text
nt = String -> Text
T.pack (forall a. Show a => a -> String
show Int
n)
                       nl :: Int
nl = Text -> Int
T.length Text
nt
                   in Int -> Text -> Text
T.take (Int
4 forall a. Num a => a -> a -> a
- Int
nl) Text
"    " forall a. Semigroup a => a -> a -> a
<> Text
nt forall a. Semigroup a => a -> a -> a
<> Text
l
      ll :: [a] -> Text
ll = String -> Text
T.pack forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> String
show forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a. Foldable t => t a -> Int
length
      tl :: Text -> Text
tl = String -> Text
T.pack forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> String
show forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Int
T.length
      banner :: Text
banner = Text
"MISMATCH between "
               forall a. Semigroup a => a -> a -> a
<> forall {a}. [a] -> Text
ll [Text]
el forall a. Semigroup a => a -> a -> a
<> Text
"l/" forall a. Semigroup a => a -> a -> a
<> Text -> Text
tl Text
expected forall a. Semigroup a => a -> a -> a
<> Text
"c expected and "
               forall a. Semigroup a => a -> a -> a
<> forall {a}. [a] -> Text
ll [Text]
al forall a. Semigroup a => a -> a -> a
<> Text
"l/" forall a. Semigroup a => a -> a -> a
<> Text -> Text
tl Text
actual forall a. Semigroup a => a -> a -> a
<> Text
"c actual"
      diffReport :: [Text]
diffReport = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Int -> Text -> Text
addnum) forall a b. (a -> b) -> a -> b
$
                   forall a b. [a] -> [b] -> [(a, b)]
zip [Int
1..] forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat forall a b. (a -> b) -> a -> b
$
                   -- Highly simplistic "diff" output assumes
                   -- correlated lines: added or removed lines just
                   -- cause everything to shown as different from that
                   -- point forward.
                   [ forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall {a}. (Eq a, Semigroup a, IsString a) => (a, a) -> a
dl forall a b. (a -> b) -> a -> b
$ forall a b. [a] -> [b] -> [(a, b)]
zip [Text]
el [Text]
al
                   , forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall {a}. (Semigroup a, IsString a) => a -> a -> a
de Text
"∌ ") forall a b. (a -> b) -> a -> b
$ forall a. Int -> [a] -> [a]
drop (forall (t :: * -> *) a. Foldable t => t a -> Int
length [Text]
al) [Text]
el
                   , forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall {a}. (Semigroup a, IsString a) => a -> a -> a
da Text
"∹ ") forall a b. (a -> b) -> a -> b
$ forall a. Int -> [a] -> [a]
drop (forall (t :: * -> *) a. Foldable t => t a -> Int
length [Text]
el) [Text]
al
                   ]
                   -- n.b. T.lines seems to consume trailing whitespace before
                   -- newlines as well.  This will show any of this whitespace
                   -- difference on the last line, but not for other lines with
                   -- whitespace.
                   forall a. Semigroup a => a -> a -> a
<> if [Text]
el forall a. Eq a => a -> a -> Bool
== [Text]
al
                      then let maxlen :: Int
maxlen = forall a. Ord a => a -> a -> a
max (Text -> Int
T.length Text
expected) (Text -> Int
T.length Text
actual)
                               end :: Text -> Text
end Text
x = Int -> Text -> Text
T.drop (Int
maxlen forall a. Num a => a -> a -> a
- Int
5) Text
x
                           in [ [ forall {a}. (Semigroup a, IsString a) => a -> a -> a
de Text
"∌ ending " forall a b. (a -> b) -> a -> b
$ Text -> Text
visible forall a b. (a -> b) -> a -> b
$ Text -> Text
end Text
expected ]
                              , [ forall {a}. (Semigroup a, IsString a) => a -> a -> a
da Text
"∹ ending " forall a b. (a -> b) -> a -> b
$ Text -> Text
visible forall a b. (a -> b) -> a -> b
$ Text -> Text
end Text
actual ]
                              ]
                      else forall a. Monoid a => a
mempty
      details :: [Text]
details = Text
banner forall a. a -> [a] -> [a]
: [Text]
diffReport
  in if Text
expected forall a. Eq a => a -> a -> Bool
== Text
actual then String
"<no difference>" else Text -> String
T.unpack ([Text] -> Text
T.unlines [Text]
details)