<commit_before>{-|

This module defines the 'AssertM' monad, which allows you either to run assertions
as ordinary unit tests or to evaluate them as pure functions.

-}
module Test.Framework.AssertM (

    AssertM(..), AssertStackElem(..), AssertBool(..), boolValue, eitherValue, formatStack

) where

import Data.Maybe
import qualified Data.Text as T

import Test.Framework.TestInterface
import Test.Framework.Location
import Test.Framework.Colors

-- | A typeclass for generic assertions.
class Monad m => AssertM m where
    genericAssertFailure__ :: Location -> ColorString -> m a
    genericSubAssert :: Location -> Maybe String -> m a -> m a

instance AssertM IO where
    genericAssertFailure__ loc s = failHTF (FullTestResult (Just loc) [] (Just s) (Just Fail))
    genericSubAssert loc mMsg action = subAssertHTF loc mMsg action

-- | Stack trace element for generic assertions.
data AssertStackElem
    = AssertStackElem
      { ase_message :: Maybe String
      , ase_location :: Maybe Location
      }
      deriving (Eq, Ord, Show, Read)

-- | Type for evaluating a generic assertion as a pure function.
data AssertBool a
    -- | Assertion passes successfully and yields the given value.
    = AssertOk a
    -- | Assertion fails with the given stack trace. In the stack trace, the outermost stackframe comes first.
    | AssertFailed [AssertStackElem]
      deriving (Eq, Ord, Show, Read)

instance Monad AssertBool where
    return = AssertOk
    AssertFailed stack >>= _ = AssertFailed stack
    AssertOk x >>= k = k x
    fail msg = AssertFailed [AssertStackElem (Just msg) Nothing]

instance AssertM AssertBool where
    genericAssertFailure__ loc s =
        AssertFailed [AssertStackElem (Just (T.unpack $ renderColorString s False)) (Just loc)]

    genericSubAssert loc mMsg action =<commit_msg>Functor and Applicative instances for AssertBool<commit_after>{-|

This module defines the 'AssertM' monad, which allows you either to run assertions
as ordinary unit tests or to evaluate them as pure functions.

-}
module Test.Framework.AssertM (

    AssertM(..), AssertStackElem(..), AssertBool(..), boolValue, eitherValue, formatStack

) where

import Data.Maybe
import qualified Data.Text as T
import Control.Applicative (Applicative(..))
import Control.Monad       (liftM, ap)

import Test.Framework.TestInterface
import Test.Framework.Location
import Test.Framework.Colors

-- | A typeclass for generic assertions.
class Monad m => AssertM m where
    genericAssertFailure__ :: Location -> ColorString -> m a
    genericSubAssert :: Location -> Maybe String -> m a -> m a

instance AssertM IO where
    genericAssertFailure__ loc s = failHTF (FullTestResult (Just loc) [] (Just s) (Just Fail))
    genericSubAssert loc mMsg action = subAssertHTF loc mMsg action

-- | Stack trace element for generic assertions.
data AssertStackElem
    = AssertStackElem
      { ase_message :: Maybe String
      , ase_location :: Maybe Location
      }
      deriving (Eq, Ord, Show, Read)

-- | Type for evaluating a generic assertion as a pure function.
data AssertBool a
    -- | Assertion passes successfully and yields the given value.
    = AssertOk a
    -- | Assertion fails with the given stack trace. In the stack trace, the outermost stackframe comes first.
    | AssertFailed [AssertStackElem]
      deriving (Eq, Ord, Show, Read)

instance Functor AssertBool where
    fmap = liftM

instance Applicative AssertBool where
    pure  = return
    (<*>) = ap

instance Monad AssertBool where
    return = AssertOk
    AssertFailed stack >>= _ = AssertFailed stack
    AssertOk x >>= k = k x
    fail msg = AssertFailed [AssertStackElem (Just msg) Nothing]

instance AssertM AssertBool where
    genericAssertFailure__ loc s =
        AssertFailed [AssertStackElem (Just (T.unpack $ renderColorString s False)) (Just loc)]

    genericSubAssert loc mMsg action =
{'fraction_non_alphanumeric': 0.07654639175257733, 'fraction_numerical': 0.0, 'mean_word_length': 3.5498241500586167, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1275680', 'n_tokens_mistral': 1054, 'n_tokens_neox': 1039, 'n_words': 429}