foundry_test_utils/
macros.rs

1/// A macro to generate a new integration test case
2///
3/// The `forgetest!` macro's first argument is the name of the test, the second argument is a
4/// closure to configure and execute the test. The `TestProject` provides utility functions to setup
5/// the project's workspace. The `TestCommand` is a wrapper around the actual `forge` executable
6/// that this then executed with the configured command arguments.
7#[macro_export]
8macro_rules! forgetest {
9    ($(#[$attr:meta])* $test:ident, |$prj:ident, $cmd:ident| $e:expr) => {
10        $crate::forgetest!($(#[$attr])* $test, $crate::foundry_compilers::PathStyle::Dapptools, |$prj, $cmd| $e);
11    };
12    ($(#[$attr:meta])* $test:ident, $style:expr, |$prj:ident, $cmd:ident| $e:expr) => {
13        #[expect(clippy::disallowed_macros)]
14        #[test]
15        $(#[$attr])*
16        fn $test() {
17            let (mut $prj, mut $cmd) = $crate::util::setup_forge(stringify!($test), $style);
18            $e
19        }
20    };
21}
22
23#[macro_export]
24macro_rules! forgetest_async {
25    ($(#[$attr:meta])* $test:ident, |$prj:ident, $cmd:ident| $e:expr) => {
26        $crate::forgetest_async!($(#[$attr])* $test, $crate::foundry_compilers::PathStyle::Dapptools, |$prj, $cmd| $e);
27    };
28    ($(#[$attr:meta])* $test:ident, $style:expr, |$prj:ident, $cmd:ident| $e:expr) => {
29        #[expect(clippy::disallowed_macros)]
30        #[tokio::test(flavor = "multi_thread")]
31        $(#[$attr])*
32        async fn $test() {
33            let (mut $prj, mut $cmd) = $crate::util::setup_forge(stringify!($test), $style);
34            $e;
35            return (); // Works around weird method resolution in `$e` due to `#[tokio::test]`.
36        }
37    };
38}
39
40#[macro_export]
41macro_rules! casttest {
42    ($(#[$attr:meta])* $test:ident, $($async:ident)? |$prj:ident, $cmd:ident| $e:expr) => {
43        $crate::casttest!($(#[$attr])* $test, $crate::foundry_compilers::PathStyle::Dapptools, $($async)? |$prj, $cmd| $e);
44    };
45    ($(#[$attr:meta])* $test:ident, $style:expr, |$prj:ident, $cmd:ident| $e:expr) => {
46        #[expect(clippy::disallowed_macros)]
47        #[test]
48        $(#[$attr])*
49        fn $test() {
50            let (mut $prj, mut $cmd) = $crate::util::setup_cast(stringify!($test), $style);
51            $e
52        }
53    };
54    ($(#[$attr:meta])* $test:ident, $style:expr, async |$prj:ident, $cmd:ident| $e:expr) => {
55        #[expect(clippy::disallowed_macros)]
56        #[tokio::test(flavor = "multi_thread")]
57        $(#[$attr])*
58        async fn $test() {
59            let (mut $prj, mut $cmd) = $crate::util::setup_cast(stringify!($test), $style);
60            $e;
61            return (); // Works around weird method resolution in `$e` due to `#[tokio::test]`.
62        }
63    };
64}
65
66/// Same as `forgetest` but returns an already initialized project workspace (`forge init --empty`).
67#[macro_export]
68macro_rules! forgetest_init {
69    ($(#[$attr:meta])* $test:ident, |$prj:ident, $cmd:ident| $e:expr) => {
70        $crate::forgetest_init!($(#[$attr])* $test, $crate::foundry_compilers::PathStyle::Dapptools, |$prj, $cmd| $e);
71    };
72    ($(#[$attr:meta])* $test:ident, $style:expr, |$prj:ident, $cmd:ident| $e:expr) => {
73        #[test]
74        $(#[$attr])*
75        fn $test() {
76            let (mut $prj, mut $cmd) = $crate::util::setup_forge(stringify!($test), $style);
77            $crate::util::initialize($prj.root());
78            $e
79        }
80    };
81}
82
83/// Setup forge soldeer
84#[macro_export]
85macro_rules! forgesoldeer {
86    ($(#[$attr:meta])* $test:ident, |$prj:ident, $cmd:ident| $e:expr) => {
87        $crate::forgesoldeer!($(#[$attr])* $test, $crate::foundry_compilers::PathStyle::Dapptools, |$prj, $cmd| $e);
88    };
89    ($(#[$attr:meta])* $test:ident, $style:expr, |$prj:ident, $cmd:ident| $e:expr) => {
90        #[expect(clippy::disallowed_macros)]
91        #[test]
92        $(#[$attr])*
93        fn $test() {
94            let (mut $prj, mut $cmd) = $crate::util::setup_forge(stringify!($test), $style);
95            $crate::util::initialize($prj.root());
96            $e
97        }
98    };
99}
100
101#[macro_export]
102macro_rules! test_debug {
103    ($($args:tt)*) => {
104        $crate::test_debug(format_args!($($args)*))
105    }
106}
107
108#[macro_export]
109macro_rules! test_trace {
110    ($($args:tt)*) => {
111        $crate::test_trace(format_args!($($args)*))
112    }
113}