Profpatsch/users/Profpatsch/my-prelude/src/Test.hs
  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
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
{-# LANGUAGE LambdaCase #-}

{- Generate Test suites.

Restricted version of hspec, introduction: http://hspec.github.io/getting-started.html
-}
module Test
  ( Spec,
    runTest,
    testMain,

    -- * Structure
    describe,
    it,

    -- * Hspc types
    SpecWith,
    ActionWith,

    -- * Expectations
    Expectation,
    testOk,
    testErr,
    shouldBe,
    shouldNotBe,
    shouldSatisfy,
    shouldNotSatisfy,

    -- * Setup & Teardown (hooks http://hspec.github.io/writing-specs.html#using-hooks)
    before,
    before_,
    beforeWith,
    beforeAll,
    beforeAll_,
    beforeAllWith,
    after,
    after_,
    afterAll,
    afterAll_,
    around,
    around_,
    aroundWith,
    aroundAll,
    aroundAllWith,

    -- * Common helpful predicates (use with 'shouldSatisfy')
    isRight,
    isLeft,

    -- * Pretty printing of errors
    errColored,
    module Pretty,
  )
where

-- export more expectations if needed

import Data.Either
  ( isLeft,
    isRight,
  )
import Pretty
import Test.Hspec
  ( ActionWith,
    Expectation,
    HasCallStack,
    Spec,
    SpecWith,
    after,
    afterAll,
    afterAll_,
    after_,
    around,
    aroundAll,
    aroundAllWith,
    aroundWith,
    around_,
    before,
    beforeAll,
    beforeAllWith,
    beforeAll_,
    beforeWith,
    before_,
    describe,
    hspec,
    it,
  )
import Test.Hspec.Expectations.Pretty
  ( expectationFailure,
    shouldBe,
    shouldNotBe,
    shouldNotSatisfy,
    shouldSatisfy,
  )

-- | Run a test directly (e.g. from the repl)
runTest :: Spec -> IO ()
runTest = hspec

-- | Run a testsuite
testMain ::
  -- | Name of the test suite
  String ->
  -- | The tests in this test module
  Spec ->
  IO ()
testMain testSuiteName tests = hspec $ describe testSuiteName tests

-- | test successful
testOk :: Expectation
testOk = pure ()

-- | Abort the test with an error message.
-- If you want to display a Haskell type, use `errColored`.
testErr :: (HasCallStack) => String -> Expectation
testErr = expectationFailure

-- | Display a list of 'Err's as a colored error message
-- and abort the test.
errColored :: [Pretty.Err] -> Expectation
errColored = testErr . Pretty.prettyErrs