Skip to content

Commit

Permalink
Add macro expansion tests (#39)
Browse files Browse the repository at this point in the history
* Add macro expand tests

* Include cargo-expand in test setup
  • Loading branch information
dcchut committed Mar 17, 2024
1 parent f8b8e21 commit 0b38976
Show file tree
Hide file tree
Showing 19 changed files with 272 additions and 1 deletion.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ jobs:
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@stable
- run: cargo install cargo-expand
- run: cargo test --verbose

fmt:
Expand Down
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ syn = { version = "2.0", features = ["full", "parsing", "printing", "proc-macro"

[dev-dependencies]
futures-executor = "0.3"
trybuild = "1.0"
trybuild = "1.0"
macrotest = "1.0"
5 changes: 5 additions & 0 deletions tests/compiletest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,8 @@ fn ui() {
let t = trybuild::TestCases::new();
t.compile_fail("tests/ui/*.rs");
}

#[test]
fn expand() {
macrotest::expand("tests/expand/*.rs");
}
9 changes: 9 additions & 0 deletions tests/expand/core_module.expanded.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use async_recursion::async_recursion;
#[must_use]
pub fn n(
x: i32,
) -> ::core::pin::Pin<
Box<dyn ::core::future::Future<Output = i32> + ::core::marker::Send>,
> {
Box::pin(async move { x })
}
6 changes: 6 additions & 0 deletions tests/expand/core_module.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use async_recursion::async_recursion;

#[async_recursion]
pub async fn n(x: i32) -> i32 {
x
}
18 changes: 18 additions & 0 deletions tests/expand/fibonacci.expanded.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use async_recursion::async_recursion;
#[must_use]
fn fib(
n: u32,
) -> ::core::pin::Pin<
Box<dyn ::core::future::Future<Output = u64> + ::core::marker::Send>,
> {
Box::pin(async move {
match n {
0 => {
::std::rt::begin_panic("zero is not a valid argument to fib()!");
}
1 | 2 => 1,
3 => 2,
_ => fib(n - 1).await + fib(n - 2).await,
}
})
}
11 changes: 11 additions & 0 deletions tests/expand/fibonacci.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use async_recursion::async_recursion;

#[async_recursion]
async fn fib(n: u32) -> u64 {
match n {
0 => panic!("zero is not a valid argument to fib()!"),
1 | 2 => 1,
3 => 2,
_ => fib(n - 1).await + fib(n - 2).await,
}
}
16 changes: 16 additions & 0 deletions tests/expand/generic_parameter.expanded.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use async_recursion::async_recursion;
#[must_use]
pub fn generic_parameter<'async_recursion, S: Marker + Send>(
mut x: S,
) -> ::core::pin::Pin<
Box<
dyn ::core::future::Future<
Output = u64,
> + 'async_recursion + ::core::marker::Send,
>,
>
where
S: 'async_recursion,
{
Box::pin(async move { if x.descend() { generic_parameter(x).await } else { 0 } })
}
10 changes: 10 additions & 0 deletions tests/expand/generic_parameter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use async_recursion::async_recursion;

#[async_recursion]
pub async fn generic_parameter<S: Marker + Send>(mut x: S) -> u64 {
if x.descend() {
generic_parameter(x).await
} else {
0
}
}
13 changes: 13 additions & 0 deletions tests/expand/generic_parameter_no_send.expanded.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use async_recursion::async_recursion;
#[must_use]
pub fn generic_parameter_no_send<'async_recursion, T>(
x: T,
y: u64,
) -> ::core::pin::Pin<Box<dyn ::core::future::Future<Output = u64> + 'async_recursion>>
where
T: 'async_recursion,
{
Box::pin(async move {
if y > 0 { generic_parameter_no_send(x, y - 1).await } else { 111 }
})
}
10 changes: 10 additions & 0 deletions tests/expand/generic_parameter_no_send.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use async_recursion::async_recursion;

#[async_recursion(?Send)]
pub async fn generic_parameter_no_send<T>(x: T, y: u64) -> u64 {
if y > 0 {
generic_parameter_no_send(x, y - 1).await
} else {
111
}
}
12 changes: 12 additions & 0 deletions tests/expand/lifetimes_multiple.expanded.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use async_recursion::async_recursion;
#[must_use]
fn contains_value_2<'a, 'b, 'async_recursion, T: PartialEq>(
value: &'b T,
node: &'b Node<'a, T>,
) -> ::core::pin::Pin<Box<dyn ::core::future::Future<Output = bool> + 'async_recursion>>
where
T: 'async_recursion,
'b: 'async_recursion,
{
Box::pin(async move { contains_value(value, node).await })
}
6 changes: 6 additions & 0 deletions tests/expand/lifetimes_multiple.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use async_recursion::async_recursion;

#[async_recursion(?Send)]
async fn contains_value_2<'a, 'b, T: PartialEq>(value: &'b T, node: &'b Node<'a, T>) -> bool {
contains_value(value, node).await
}
16 changes: 16 additions & 0 deletions tests/expand/lifetimes_no_send_bound.expanded.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use async_recursion::async_recursion;
struct Node<'a, T> {
ptr: &'a T,
}
#[must_use]
fn contains_value<'a, 'life0, 'life1, 'async_recursion, T: PartialEq>(
value: &'life0 T,
node: &'life1 Node<'a, T>,
) -> ::core::pin::Pin<Box<dyn ::core::future::Future<Output = bool> + 'async_recursion>>
where
T: 'async_recursion,
'life0: 'async_recursion,
'life1: 'async_recursion,
{
Box::pin(async move { if &node.ptr == value { true } else { false } })
}
14 changes: 14 additions & 0 deletions tests/expand/lifetimes_no_send_bound.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use async_recursion::async_recursion;

struct Node<'a, T> {
ptr: &'a T,
}

#[async_recursion(?Send)]
async fn contains_value<'a, T: PartialEq>(value: &T, node: &Node<'a, T>) -> bool {
if &node.ptr == value {
true
} else {
false
}
}
56 changes: 56 additions & 0 deletions tests/expand/macros_nested.expanded.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#[must_use]
fn owned<'life0, 'async_recursion, F>(
param: usize,
f: &'life0 F,
) -> ::core::pin::Pin<
Box<
dyn ::core::future::Future<Output = ()> + 'async_recursion + ::core::marker::Send,
>,
>
where
F: Fn(usize) + Sync + Send,
F: 'async_recursion,
'life0: 'async_recursion,
{
Box::pin(async move {
f(param);
})
}
#[must_use]
fn by_ref<'life0, 'life1, 'async_recursion, F>(
param: &'life0 usize,
f: &'life1 F,
) -> ::core::pin::Pin<
Box<
dyn ::core::future::Future<Output = ()> + 'async_recursion + ::core::marker::Send,
>,
>
where
F: Fn(&usize) + Sync + Send,
F: 'async_recursion,
'life0: 'async_recursion,
'life1: 'async_recursion,
{
Box::pin(async move {
f(param);
})
}
#[must_use]
fn by_ref_mut<'life0, 'life1, 'async_recursion, F>(
param: &'life0 mut usize,
f: &'life1 F,
) -> ::core::pin::Pin<
Box<
dyn ::core::future::Future<Output = ()> + 'async_recursion + ::core::marker::Send,
>,
>
where
F: Fn(&mut usize) + Sync + Send,
F: 'async_recursion,
'life0: 'async_recursion,
'life1: 'async_recursion,
{
Box::pin(async move {
f(param);
})
}
15 changes: 15 additions & 0 deletions tests/expand/macros_nested.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
macro_rules! recurse {
($name:ident, $param:ty) => {
#[::async_recursion::async_recursion]
async fn $name<F>(param: $param, f: &F)
where
F: Fn($param) + Sync + Send,
{
f(param);
}
};
}

recurse!(owned, usize);
recurse!(by_ref, &usize);
recurse!(by_ref_mut, &mut usize);
31 changes: 31 additions & 0 deletions tests/expand/struct_methods.expanded.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use async_recursion::async_recursion;
struct S;
impl S {
#[must_use]
pub fn all_of_the_above<'a, 'b, 'life0, 'life1, 'life_self, 'async_recursion, S, T>(
&'life_self self,
_x: &'life0 S,
_y: &'b T,
_w: S,
_z: T,
_p: &'life1 usize,
_q: &'a u64,
) -> ::core::pin::Pin<
Box<
dyn ::core::future::Future<
Output = (),
> + 'async_recursion + ::core::marker::Send,
>,
>
where
S: 'async_recursion,
T: 'async_recursion,
'life0: 'async_recursion,
'b: 'async_recursion,
'life1: 'async_recursion,
'a: 'async_recursion,
'life_self: 'async_recursion,
{
Box::pin(async move {})
}
}
21 changes: 21 additions & 0 deletions tests/expand/struct_methods.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use async_recursion::async_recursion;

struct S;

impl S {
#[async_recursion]
pub async fn all_of_the_above<'a, 'b, S, T>(
&self,
// Some references with / without lifetimes to generic parameters
_x: &S,
_y: &'b T,
// Some generic parameters passed by value
_w: S,
_z: T,
// A reference to a concrete type without a lifetime
_p: &usize,
// A reference to a concrete type with a lifetime
_q: &'a u64,
) {
}
}

0 comments on commit 0b38976

Please sign in to comment.