test_with_tokio/lib.rs
1#![doc = include_str!("../README.md")]
2
3/// Run a test possibly using tokio, possibly with extra cases.
4///
5/// Everything before the first `await` or `async` block is run before the tokio
6/// runtime is started, and the remainder of the function is run within the
7/// tokio runtime.
8///
9/// In addition, if there is a statement of the form `let ... = match CASE { ... }`
10/// then the match must match from string literals that are valid identifier suffixes,
11/// and those cases are each used to generate a new test function. For details, see
12/// the example below.
13///
14/// See module-level documentation for more and better examples.
15///
16/// # Examples
17///
18/// The following code will create two tests that safely write to the same file.
19/// ```
20/// static LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());
21/// #[test_with_tokio::please]
22/// fn test_pill() -> std::io::Result<()> {
23/// let contents = match CASE {
24/// "red" => "red pill",
25/// "blue" => "blue pill",
26/// };
27/// let _guard = LOCK.lock().unwrap();
28/// let mut f = tokio::fs::File::create("pill.txt").await?;
29/// use tokio::io::AsyncWriteExt;
30/// f.write_all(contents.as_bytes()).await?;
31/// // do other stuff that needs the file to exist
32/// tokio::fs::remove_file("pill.txt").await
33/// }
34/// ```
35/// this will expand to
36/// ```
37/// static LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());
38///
39/// #[test]
40/// fn test_pill_red() -> std::io::Result<()> {
41/// const CASE: &str = "red";
42/// let contents = "red pill";
43/// let _guard = LOCK.lock().unwrap();
44/// ::tokio::runtime::Builder::new_current_thread()
45/// .enable_all()
46/// .build()
47/// .unwrap()
48/// .block_on(async {
49/// let mut f = tokio::fs::File::create("pill.txt").await?;
50/// use tokio::io::AsyncWriteExt;
51/// f.write_all(contents.as_bytes()).await?;
52/// // do other stuff that needs the file to exist
53/// tokio::fs::remove_file("pill.txt").await
54/// });
55/// }
56///
57/// #[test]
58/// fn test_pill_blue() -> std::io::Result<()> {
59/// const CASE: &str = "blue";
60/// let contents = "blue pill";
61/// let _guard = LOCK.lock().unwrap();
62/// ::tokio::runtime::Builder::new_current_thread()
63/// .enable_all()
64/// .build()
65/// .unwrap()
66/// .block_on(async {
67/// let mut f = tokio::fs::File::create("pill.txt").await?;
68/// use tokio::io::AsyncWriteExt;
69/// f.write_all(contents.as_bytes()).await?;
70/// // do other stuff that needs the file to exist
71/// tokio::fs::remove_file("pill.txt").await
72/// });
73/// }
74/// ```
75#[doc(inline)]
76pub use test_with_tokio_macros::please;