0 | module Test.Async.Spec.Asserts
 1 |
 2 | import Data.Linear.Ref1
 3 | import Data.String
 4 | import IO.Async
 5 | import Test.Async.Spec.Report
 6 | import Test.Async.Spec.TestEnv
 7 | import Test.Async.Spec.TestResult
 8 | import Text.Show.Diff
 9 | import Text.Show.Pretty
10 |
11 | %default total
12 |
13 | export %inline
14 | failWith : Maybe Diff -> String -> Test e
15 | failWith diff msg = pure $ Failure diff msg
16 |
17 | ||| Fails with an error that shows the difference between two values.
18 | export
19 | failDiff : Show a => Show b => a -> b -> Test e
20 | failDiff x y =
21 |   case valueDiff <$> reify x <*> reify y of
22 |     Nothing =>
23 |       failWith Nothing $
24 |         unlines
25 |           [ "Failed"
26 |           , "━━ lhs ━━"
27 |           , ppShow x
28 |           , "━━ rhs ━━"
29 |           , ppShow y
30 |           ]
31 |
32 |     Just vdiff@(Same _) =>
33 |       failWith
34 |         (Just $ MkDiff "━━━ Failed ("  "" "no differences" "" ") ━━━" vdiff)
35 |         ""
36 |
37 |     Just vdiff =>
38 |       failWith
39 |         (Just $ MkDiff "━━━ Failed (" "- lhs" ") (" "+ rhs" ") ━━━" vdiff)
40 |         ""
41 |
42 | export %inline
43 | diff : Show a => Show b => a -> (a -> b -> Bool) -> b -> Test e
44 | diff x op y = if x `op` y then pure Success else failDiff x y
45 |
46 | export infix 6 ===, =!=
47 |
48 | ||| Fails the test if the two arguments provided are not equal.
49 | export %inline
50 | (===) : Eq a => Show a => a -> a -> Test e
51 | (===) x y = diff x (==) y
52 |
53 | export
54 | assert :
55 |      {auto all   : All Interpolation es}
56 |   -> {auto showa : Show a}
57 |   -> {auto eqa   : Eq a}
58 |   -> Async e es a
59 |   -> (expected : a)
60 |   -> Test e
61 | assert as exp = do
62 |   f <- start as
63 |   join f >>= \case
64 |     Succeeded res => res === exp
65 |     Error err     =>
66 |      let msg := collapse' $ hzipWith (\_ => interpolate) all err
67 |       in failWith Nothing "Computation failed with error \{msg}"
68 |     Canceled      =>
69 |       failWith Nothing "Computation was canceled unexpectedly"
70 |
71 | ||| Operator version of `assert`
72 | export %inline
73 | (=!=) :
74 |      {auto all   : All Interpolation es}
75 |   -> {auto showa : Show a}
76 |   -> {auto eqa   : Eq a}
77 |   -> Async e es a
78 |   -> (expected : a)
79 |   -> Test e
80 | (=!=) = assert
81 |