-
Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathtest_fn.rs
More file actions
54 lines (48 loc) · 1.64 KB
/
test_fn.rs
File metadata and controls
54 lines (48 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use proc_macro2::TokenStream;
use quote::{ToTokens, TokenStreamExt, quote};
use syn::{AttrStyle, Attribute, Signature, Visibility, parse::Parse};
use crate::{
item_fn::{RawAttr, RawBodyItemFn},
retrieve_runtime_mod,
};
pub(crate) struct CompioTest(pub RawBodyItemFn);
impl CompioTest {
pub fn with_args(mut self, args: RawAttr) -> Self {
self.0.set_args(args.inner_attrs);
self
}
}
impl Parse for CompioTest {
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
let attrs = input.call(Attribute::parse_outer)?;
let vis: Visibility = input.parse()?;
let mut sig: Signature = input.parse()?;
let body: TokenStream = input.parse()?;
if sig.asyncness.is_none() {
return Err(syn::Error::new_spanned(
sig.ident,
"the `async` keyword is missing from the function declaration",
));
};
sig.asyncness.take();
Ok(Self(RawBodyItemFn::new(attrs, vis, sig, body)))
}
}
impl ToTokens for CompioTest {
fn to_tokens(&self, tokens: &mut TokenStream) {
tokens.append_all(quote!(#[test]));
tokens.append_all(
self.0
.attrs
.iter()
.filter(|a| matches!(a.style, AttrStyle::Outer)),
);
self.0.vis.to_tokens(tokens);
self.0.sig.to_tokens(tokens);
let block = &self.0.body;
let runtime_mod = self.0.crate_name().unwrap_or_else(retrieve_runtime_mod);
tokens.append_all(quote!({
#runtime_mod::Runtime::new().expect("cannot create runtime").block_on(async move #block)
}));
}
}