papa-0.7.0: test/Agree.hs
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_GHC -Wall #-}
-- | The agreement of a total function with the partial one it replaces.
module Agree (
agrees,
agreesWhereDefined,
) where
import Control.Applicative ((*>))
import Control.DeepSeq (NFData, force)
import Control.Exception (SomeException, evaluate, try)
import Control.Monad.IO.Class (MonadIO)
import Data.Either (Either, either)
import Data.Eq (Eq)
import Data.Function (const)
import Data.Maybe (Maybe (Just, Nothing))
import Hedgehog (MonadTest, evalIO, label, (===))
import Text.Show (Show)
-- | The total result is 'Nothing' exactly where the partial one throws, and
-- 'Just' the partial one's result everywhere else.
--
-- The partial result is forced in full, so that an exception anywhere in it
-- counts.
agrees :: (Eq a, Show a, NFData a, MonadIO m, MonadTest m) => Maybe a -> a -> m ()
agrees total partial = do
r :: Either SomeException a <- evalIO (try (evaluate (force partial)))
either (const (label "partial throws" *> (total === Nothing))) (\p -> label "partial defined" *> (total === Just p)) r
-- | A total function that returns a plain value equals the partial one
-- wherever the partial one does not throw. Where it throws there is nothing
-- to compare. Each case is labelled, so that the coverage of both shows.
--
-- The partial result is forced in full.
agreesWhereDefined :: (Eq a, Show a, NFData a, MonadIO m, MonadTest m) => a -> a -> m ()
agreesWhereDefined total partial = do
r :: Either SomeException a <- evalIO (try (evaluate (force partial)))
either (const (label "partial throws")) (\p -> label "partial defined" *> (total === p)) r