Debian Codegraph @

tasty-as-hspec/Test/Hspec.hs

raw
1 {-# LANGUAGE ConstraintKinds #-}
2 {-# LANGUAGE DeriveFunctor #-}
3 {-# LANGUAGE FlexibleContexts #-}
4 {-# LANGUAGE GADTs #-}
5 {-# LANGUAGE GeneralizedNewtypeDeriving #-}
6 {-# LANGUAGE TypeOperators #-}
7 -- | A @hspec@ like interface build on top of tasty.
8 module Test.Hspec (
9 -- * Runner
10 Spec,
11 hspec,
12 -- * Test trees
13 describe,
14 context,
15 it,
16 -- * Checks
17 Expectation,
18 expectationFailure,
19 shouldBe,
20 shouldContain,
21 shouldNotContain,
22 compareWith,
23 ) where
24
25 import Control.Applicative (Applicative)
26 import Control.Monad (unless)
27 import Data.List (isInfixOf)
28 import Data.Orphans ()
29 import Prelude
30 (Bool, Eq, Functor, IO, Monad, Show, String, flip, fst, id, not, show,
31 ($), (++))
32 import Test.Tasty (TestName, TestTree, defaultMain, testGroup)
33 import Test.Tasty.HUnit
34 (Assertion, HasCallStack, assertFailure, testCase, (@?=))
35
36 -------------------------------------------------------------------------------
37 -- Runner
38 -------------------------------------------------------------------------------
39
40 type Spec = TestTreeM ()
41
42 hspec :: TestTreeM () -> IO ()
43 hspec t = defaultMain (testGroup "X" (runTestTreeM t))
44
45 -------------------------------------------------------------------------------
46 -- Test trees
47 -------------------------------------------------------------------------------
48
49 newtype TestTreeM a = TestTreeM (Writer [TestTree] a)
50 deriving (Functor, Applicative, Monad)
51
52 runTestTreeM :: TestTreeM () -> [TestTree]
53 runTestTreeM (TestTreeM m) = fst (runWriter m)
54
55 class Describe r where
56 describe :: TestName -> TestTreeM () -> r
57
58 instance a ~ () => Describe (TestTreeM a) where
59 describe n t = TestTreeM $ tell [ describe n t ]
60
61 instance Describe TestTree where
62 describe n t = testGroup n $ runTestTreeM t
63
64 it :: TestName -> Assertion -> TestTreeM ()
65 it n assertion = TestTreeM $ tell [ testCase n assertion ]
66
67 context :: Describe r => TestName -> TestTreeM () -> r
68 context n t = describe n t
69
70 -------------------------------------------------------------------------------
71 -- Checks
72 -------------------------------------------------------------------------------
73
74 type Expectation = Assertion
75
76 expectationFailure :: String -> Expectation
77 expectationFailure = assertFailure
78
79 shouldBe :: (Eq a, Show a, HasCallStack) => a -> a -> Expectation
80 shouldBe = (@?=)
81
82 shouldContain :: (Eq a, Show a, HasCallStack) => [a] -> [a] -> Expectation
83 shouldContain = compareWith (flip isInfixOf) "does not contain"
84
85 shouldNotContain :: (Eq a, Show a, HasCallStack) => [a] -> [a] -> Expectation
86 shouldNotContain = compareWith (\x y -> not (isInfixOf y x)) "contains"
87
88 compareWith :: (Show a, Show b, HasCallStack) => (a -> b -> Bool) -> String -> a -> b -> Expectation
89 compareWith f msg x y = unless (f x y) $ assertFailure $
90 show x ++ " " ++ msg ++ " " ++ show y
91
92 -------------------------------------------------------------------------------
93 -- Writer
94 -------------------------------------------------------------------------------
95
96 type Writer = (,)
97
98 runWriter :: Writer w a -> (w, a)
99 runWriter = id
100
101 tell :: w -> (w, ())
102 tell w = (w, ())