af_core_macros/
test.rs

1// Copyright © 2021 Alexandra Frydl
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7#[macro_export]
8macro_rules! test {
9  ($cx:expr, $name:expr, timeout = immediate, $($rest:tt)+) => {
10    $cx.test(
11      $name,
12      af_core::future::race(
13        async move {
14          $($rest)+;
15
16          #[allow(unreachable_code)]
17          Ok(())
18        },
19        async { fail!("Timed out.") },
20      ),
21    )
22  };
23
24  ($cx:expr, $name:expr, repeat = $times:literal, $($rest:tt)+) => {
25    $cx.test($name, async move {
26      for _ in 0..$times {
27        $($rest)+;
28
29        af_core::task::yield_now().await;
30      }
31
32      #[allow(unreachable_code)]
33      Ok(())
34    })
35  };
36
37  ($cx:expr, $name:expr, timeout = $timeout:literal, $($rest:tt)+) => {
38    $cx.test(
39      $name,
40      af_core::future::race(
41        async move {
42          $($rest)+;
43
44          #[allow(unreachable_code)]
45          Ok(())
46        },
47        async move {
48          af_core::task::sleep($timeout.parse().expect("Failed to parse timeout")).await;
49          fail!("Timed out.")
50        },
51      ),
52    )
53  };
54
55  ($cx:expr, $name:expr, $($rest:tt)+) => {
56    $cx.test($name, async move {
57      $($rest)+;
58
59      #[allow(unreachable_code)]
60      Ok(())
61    })
62  };
63}