Debian Codegraph @

Test/Tasty/HUnit/Orig.hs

raw
1 {-# LANGUAGE DeriveDataTypeable, FlexibleInstances, TypeSynonymInstances #-}
2
3 -- required for HasCallStack by different versions of GHC
4 {-# LANGUAGE ConstraintKinds, FlexibleContexts #-}
5
6 -- | This is the code copied from the original hunit package (v. 1.2.5.2).
7 -- with minor modifications
8 module Test.Tasty.HUnit.Orig where
9
10 import qualified Control.Exception as E
11 import Control.Monad
12 import Data.Typeable (Typeable)
13 import Data.CallStack
14
15 -- Interfaces
16 -- ----------
17
18 -- | An assertion is simply an 'IO' action. Assertion failure is indicated
19 -- by throwing an exception, typically 'HUnitFailure'.
20 --
21 -- Instead of throwing the exception directly, you should use
22 -- functions like 'assertFailure' and 'assertBool'.
23 --
24 -- Test cases are composed of a sequence of one or more assertions.
25
26 type Assertion = IO ()
27
28 -- | Unconditionally signals that a failure has occured. All
29 -- other assertions can be expressed with the form:
30 --
31 -- @
32 -- if conditionIsMet
33 -- then return ()
34 -- else assertFailure msg
35 -- @
36
37 assertFailure
38 :: HasCallStack
39 => String -- ^ A message that is displayed with the assertion failure
40 -> IO a
41 assertFailure msg = E.throwIO (HUnitFailure location msg)
42 where
43 location :: Maybe SrcLoc
44 location = case reverse callStack of
45 (_, loc) : _ -> Just loc
46 [] -> Nothing
47
48 -- Conditional Assertion Functions
49 -- -------------------------------
50
51 -- | Asserts that the specified condition holds.
52 assertBool
53 :: HasCallStack
54 => String -- ^ The message that is displayed if the assertion fails
55 -> Bool -- ^ The condition
56 -> Assertion
57 assertBool msg b = unless b (assertFailure msg)
58
59 -- | Asserts that the specified actual value is equal to the expected value.
60 -- The output message will contain the prefix, the expected value, and the
61 -- actual value.
62 --
63 -- If the prefix is the empty string (i.e., @\"\"@), then the prefix is omitted
64 -- and only the expected and actual values are output.
65 assertEqual
66 :: (Eq a, Show a, HasCallStack)
67 => String -- ^ The message prefix
68 -> a -- ^ The expected value
69 -> a -- ^ The actual value
70 -> Assertion
71 assertEqual preface expected actual =
72 unless (actual == expected) (assertFailure msg)
73 where msg = (if null preface then "" else preface ++ "\n") ++
74 "expected: " ++ show expected ++ "\n but got: " ++ show actual
75
76 infix 1 @?, @=?, @?=
77
78 -- | Asserts that the specified actual value is equal to the expected value
79 -- (with the /expected/ value on the left-hand side).
80 (@=?)
81 :: (Eq a, Show a, HasCallStack)
82 => a -- ^ The expected value
83 -> a -- ^ The actual value
84 -> Assertion
85 expected @=? actual = assertEqual "" expected actual
86
87 -- | Asserts that the specified actual value is equal to the expected value
88 -- (with the /actual/ value on the left-hand side).
89 (@?=)
90 :: (Eq a, Show a, HasCallStack)
91 => a -- ^ The actual value
92 -> a -- ^ The expected value
93 -> Assertion
94 actual @?= expected = assertEqual "" expected actual
95
96 -- | An infix and flipped version of 'assertBool'. E.g. instead of
97 --
98 -- >assertBool "Non-empty list" (null [1])
99 --
100 -- you can write
101 --
102 -- >null [1] @? "Non-empty list"
103 --
104 -- '@?' is also overloaded to accept @'IO' 'Bool'@ predicates, so instead
105 -- of
106 --
107 -- > do
108 -- > e <- doesFileExist "test"
109 -- > e @? "File does not exist"
110 --
111 -- you can write
112 --
113 -- > doesFileExist "test" @? "File does not exist"
114 (@?) :: (AssertionPredicable t, HasCallStack)
115 => t -- ^ A value of which the asserted condition is predicated
116 -> String -- ^ A message that is displayed if the assertion fails
117 -> Assertion
118 predi @? msg = assertionPredicate predi >>= assertBool msg
119
120 -- | An ad-hoc class used to overload the '@?' operator.
121 --
122 -- The only intended instances of this class are @'Bool'@ and @'IO' 'Bool'@.
123 --
124 -- You shouldn't need to interact with this class directly.
125 class AssertionPredicable t
126 where assertionPredicate :: t -> IO Bool
127
128 instance AssertionPredicable Bool
129 where assertionPredicate = return
130
131 instance (AssertionPredicable t) => AssertionPredicable (IO t)
132 where assertionPredicate = (>>= assertionPredicate)
133
134
135 -- | Exception thrown by 'assertFailure' etc.
136 data HUnitFailure = HUnitFailure (Maybe SrcLoc) String
137 deriving (Eq, Show, Typeable)
138 instance E.Exception HUnitFailure where
139 displayException (HUnitFailure mbloc s) = prependLocation mbloc s
140
141 prependLocation :: Maybe SrcLoc -> String -> String
142 prependLocation mbloc s =
143 case mbloc of
144 Nothing -> s
145 Just loc -> srcLocFile loc ++ ":" ++ show (srcLocStartLine loc) ++ ":\n" ++ s
146
147 ----------------------------------------------------------------------
148 -- DEPRECATED CODE
149 ----------------------------------------------------------------------
150
151 {-# DEPRECATED assertString "Why not use assertBool instead?" #-}
152 {-# DEPRECATED Assertable, AssertionPredicate
153 "This class or type seems dubious. If you have a good use case for it, please create an issue for tasty. Otherwise, it may be removed in a future version." #-}
154
155 -- | Signals an assertion failure if a non-empty message (i.e., a message
156 -- other than @\"\"@) is passed.
157 assertString
158 :: HasCallStack
159 => String -- ^ The message that is displayed with the assertion failure
160 -> Assertion
161 assertString s = unless (null s) (assertFailure s)
162
163 -- Overloaded `assert` Function
164 -- ----------------------------
165
166 -- | Allows the extension of the assertion mechanism.
167 --
168 -- Since an 'Assertion' can be a sequence of @Assertion@s and @IO@ actions,
169 -- there is a fair amount of flexibility of what can be achieved. As a rule,
170 -- the resulting @Assertion@ should be the body of a @TestCase@ or part of
171 -- a @TestCase@; it should not be used to assert multiple, independent
172 -- conditions.
173 --
174 -- If more complex arrangements of assertions are needed, @Test@ and
175 -- @Testable@ should be used.
176 class Assertable t
177 where assert :: t -> Assertion
178
179 instance Assertable ()
180 where assert = return
181
182 instance Assertable Bool
183 where assert = assertBool ""
184
185 instance (Assertable t) => Assertable (IO t)
186 where assert = (>>= assert)
187
188 instance Assertable String
189 where assert = assertString
190
191
192 -- Overloaded `assertionPredicate` Function
193 -- ----------------------------------------
194
195 -- | The result of an assertion that hasn't been evaluated yet.
196 --
197 -- Most test cases follow the following steps:
198 --
199 -- 1. Do some processing or an action.
200 --
201 -- 2. Assert certain conditions.
202 --
203 -- However, this flow is not always suitable. @AssertionPredicate@ allows for
204 -- additional steps to be inserted without the initial action to be affected
205 -- by side effects. Additionally, clean-up can be done before the test case
206 -- has a chance to end. A potential work flow is:
207 --
208 -- 1. Write data to a file.
209 --
210 -- 2. Read data from a file, evaluate conditions.
211 --
212 -- 3. Clean up the file.
213 --
214 -- 4. Assert that the side effects of the read operation meet certain conditions.
215 --
216 -- 5. Assert that the conditions evaluated in step 2 are met.
217 type AssertionPredicate = IO Bool
218