Utils.purs 807 Bytes
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
module Test.Utils where

import Prelude

import Control.Monad.Error.Class (class MonadThrow)
import Data.Array as A
import Data.Maybe (Maybe(..))
import Data.Tuple (Tuple(..))
import Effect.Exception (Error)
import Test.Spec.Assertions (fail)

-- | This function can be used to compare arrays, it reports the diff in more
-- | detail which can be useful when debuggnign tests
shouldEqualArray
  :: forall m t
   . MonadThrow Error m
  => Show t
  => Eq t
  => Array t
  -> Array t
  -> m Unit
shouldEqualArray v1 v2 =
  when (v1 /= v2) $
    fail $ show v1 <> " ≠ " <> show v2 <> diff
  where
    diffs = A.filter (\(Tuple a b) -> a /= b) $ A.zip v1 v2
    diff = case A.head diffs of
      Nothing -> ""
      Just (Tuple a1 a2) -> " (first differing element: " <> (show a1 <> " ≠ " <> show a2) <> ")"