/
Assert.daml
61 lines (52 loc) · 1.74 KB
/
Assert.daml
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
-- Copyright (c) 2022 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
-- SPDX-License-Identifier: Apache-2.0
{-# LANGUAGE CPP #-}
module DA.Assert
( module DA.Assert
, CanAssert (assertFail)
, assert
, assertMsg
, assertAfter
, assertBefore
#ifdef DAML_EXCEPTIONS
, AssertionFailed (AssertionFailed)
#endif
) where
infix 4 ===
infix 4 =/=
-- | Check two values for equality. If they're not equal,
-- fail with a message.
assertEq : (CanAssert m, Show a, Eq a) => a -> a -> m ()
-- NOTE(MH): We do not define `assertEq` in terms of `assertMsg` to avoid
-- constructing the error message in the successful case.
assertEq x y
| x == y = return ()
| otherwise = assertFail ("Failure, expected " <> show x <> " == " <> show y)
-- | Infix version of `assertEq`.
(===) : (CanAssert m, Show a, Eq a) => a -> a -> m ()
(===) = assertEq
-- | Check two values for inequality. If they're equal,
-- fail with a message.
assertNotEq : (CanAssert m, Show a, Eq a) => a -> a -> m ()
assertNotEq x y
| x /= y = return ()
| otherwise = assertFail ("Failure, expected " <> show x <> " /= " <> show y)
-- | Infix version of `assertNotEq`.
(=/=) : (CanAssert m, Show a, Eq a) => a -> a -> m ()
(=/=) = assertNotEq
-- | Check whether the given time is in the future. If it's not,
-- abort with a message.
assertAfterMsg : (CanAssert m, HasTime m) => Text -> Time -> m ()
assertAfterMsg msg time = do
now <- getTime
if time > now
then assertFail msg
else return ()
-- | Check whether the given time is in the past. If it's not,
-- abort with a message.
assertBeforeMsg : (CanAssert m, HasTime m) => Text -> Time -> m ()
assertBeforeMsg msg time = do
now <- getTime
if time < now
then assertFail msg
else return ()