Skip to content

Commit 0b38976

Browse files
authored
Add macro expansion tests (#39)
* Add macro expand tests * Include cargo-expand in test setup
1 parent f8b8e21 commit 0b38976

19 files changed

+272
-1
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ jobs:
2020
steps:
2121
- uses: actions/checkout@v3
2222
- uses: dtolnay/rust-toolchain@stable
23+
- run: cargo install cargo-expand
2324
- run: cargo test --verbose
2425

2526
fmt:

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ syn = { version = "2.0", features = ["full", "parsing", "printing", "proc-macro"
1919

2020
[dev-dependencies]
2121
futures-executor = "0.3"
22-
trybuild = "1.0"
22+
trybuild = "1.0"
23+
macrotest = "1.0"

tests/compiletest.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,8 @@ fn ui() {
33
let t = trybuild::TestCases::new();
44
t.compile_fail("tests/ui/*.rs");
55
}
6+
7+
#[test]
8+
fn expand() {
9+
macrotest::expand("tests/expand/*.rs");
10+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
use async_recursion::async_recursion;
2+
#[must_use]
3+
pub fn n(
4+
x: i32,
5+
) -> ::core::pin::Pin<
6+
Box<dyn ::core::future::Future<Output = i32> + ::core::marker::Send>,
7+
> {
8+
Box::pin(async move { x })
9+
}

tests/expand/core_module.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
use async_recursion::async_recursion;
2+
3+
#[async_recursion]
4+
pub async fn n(x: i32) -> i32 {
5+
x
6+
}

tests/expand/fibonacci.expanded.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use async_recursion::async_recursion;
2+
#[must_use]
3+
fn fib(
4+
n: u32,
5+
) -> ::core::pin::Pin<
6+
Box<dyn ::core::future::Future<Output = u64> + ::core::marker::Send>,
7+
> {
8+
Box::pin(async move {
9+
match n {
10+
0 => {
11+
::std::rt::begin_panic("zero is not a valid argument to fib()!");
12+
}
13+
1 | 2 => 1,
14+
3 => 2,
15+
_ => fib(n - 1).await + fib(n - 2).await,
16+
}
17+
})
18+
}

tests/expand/fibonacci.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
use async_recursion::async_recursion;
2+
3+
#[async_recursion]
4+
async fn fib(n: u32) -> u64 {
5+
match n {
6+
0 => panic!("zero is not a valid argument to fib()!"),
7+
1 | 2 => 1,
8+
3 => 2,
9+
_ => fib(n - 1).await + fib(n - 2).await,
10+
}
11+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
use async_recursion::async_recursion;
2+
#[must_use]
3+
pub fn generic_parameter<'async_recursion, S: Marker + Send>(
4+
mut x: S,
5+
) -> ::core::pin::Pin<
6+
Box<
7+
dyn ::core::future::Future<
8+
Output = u64,
9+
> + 'async_recursion + ::core::marker::Send,
10+
>,
11+
>
12+
where
13+
S: 'async_recursion,
14+
{
15+
Box::pin(async move { if x.descend() { generic_parameter(x).await } else { 0 } })
16+
}

tests/expand/generic_parameter.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
use async_recursion::async_recursion;
2+
3+
#[async_recursion]
4+
pub async fn generic_parameter<S: Marker + Send>(mut x: S) -> u64 {
5+
if x.descend() {
6+
generic_parameter(x).await
7+
} else {
8+
0
9+
}
10+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use async_recursion::async_recursion;
2+
#[must_use]
3+
pub fn generic_parameter_no_send<'async_recursion, T>(
4+
x: T,
5+
y: u64,
6+
) -> ::core::pin::Pin<Box<dyn ::core::future::Future<Output = u64> + 'async_recursion>>
7+
where
8+
T: 'async_recursion,
9+
{
10+
Box::pin(async move {
11+
if y > 0 { generic_parameter_no_send(x, y - 1).await } else { 111 }
12+
})
13+
}

0 commit comments

Comments
 (0)