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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
//! [![crates.io version](https://img.shields.io/crates/v/safina-async-test-core.svg)](https://crates.io/crates/safina-async-test-core) //! [![license: Apache 2.0](https://gitlab.com/leonhard-llc/safina-rs/-/raw/main/license-apache-2.0.svg)](http://www.apache.org/licenses/LICENSE-2.0) //! [![unsafe forbidden](https://gitlab.com/leonhard-llc/safina-rs/-/raw/main/unsafe-forbidden-success.svg)](https://github.com/rust-secure-code/safety-dance/) //! [![pipeline status](https://gitlab.com/leonhard-llc/safina-rs/badges/main/pipeline.svg)](https://gitlab.com/leonhard-llc/safina-rs/-/pipelines) //! //! Procedural macro for the [`safina-async-test`](https://crates.io/crates/safina-async-test) //! package //! //! ## Changelog //! - v0.1.1 - Fix paths. //! - v0.1.0 - Initial version //! //! ## Happy Contributors 🙂 //! Fixing bugs and adding features is easy and fast. //! Send us a pull request and we intend to: //! - Always respond within 24 hours //! - Provide clear & concrete feedback //! - Immediately make a new release for your accepted change //! //! # Release Process //! 1. Edit `Cargo.toml` and bump version number. //! 1. Run `./release.sh` #![forbid(unsafe_code)] use proc_macro2::{Ident, TokenStream, TokenTree}; use quote::quote_spanned; #[proc_macro_attribute] pub fn async_test( attr: proc_macro::TokenStream, item: proc_macro::TokenStream, ) -> proc_macro::TokenStream { let mut result = proc_macro::TokenStream::from(impl_async_test( proc_macro2::TokenStream::from(attr), proc_macro2::TokenStream::from(item.clone()), )); result.extend(item); // let mut token_stream_string = String::new(); // for tree in result { // token_stream_string.push_str(format!("tree: {:?}\n", tree).as_str()); // } // panic!("TokenStream:\n{}", token_stream_string); result } fn impl_async_test(attr: TokenStream, item: TokenStream) -> TokenStream { if let Some(tree) = attr.into_iter().next() { return quote_spanned!(tree.span()=>compile_error!("parameters not allowed");); } // Ident { ident: "async", span: #0 bytes(50..55) } // Ident { ident: "fn", span: #0 bytes(56..58) } // Ident { ident: "should_run_async_fn", span: #0 bytes(59..78) } // Group { delimiter: Parenthesis, stream: TokenStream [], span: ... // Group { delimiter: Brace, stream: TokenStream [Ident { ident: "println",... let mut trees = item.into_iter(); let first = trees.next(); if let ( Some(TokenTree::Ident(ref ident_async)), Some(TokenTree::Ident(ref ident_fn)), Some(TokenTree::Ident(ref ident_name)), ) = (&first, trees.next(), trees.next()) { if *ident_async == "async" && *ident_fn == "fn" { let async_name = ident_name.to_string() + "_"; let ident_async_name = Ident::new(&async_name, ident_name.span()); return quote_spanned!(ident_name.span()=> #[test] pub fn #ident_async_name () { safina_async_test::start_timer_thread(); safina_async_test::Executor::new(2, 1).block_on( #ident_name ()) } ); } } if let Some(tree) = first { return quote_spanned!(tree.span()=>compile_error!("expected async fn");); } panic!("expected async fn"); } // The version below has two drawbacks: // 1. IDEs don't know that the stuff inside `async_test! { async fn test1() { ... } }` is a // function, so they don't provide formatting, auto-complete, or other useful editor functions. // This is a fatal flaw. // 2. It puts the test in a module with the same name as the test function, // and a test function named `async_test`. // So for `async_test! { async fn test1() {...} }` below, it makes // `mod test1 { #[test] fn async_test() {...} }`. // This makes IDE test output more verbose. Each test appears as its own category. // // macro_rules! async_test { // (async fn $name:ident () $body:block) => { // async fn $name() $body // // mod $name { // #[test] // fn async_test () { // safina_executor::block_on(super::$name()) // } // } // } // } // // async_test! { // async fn test1() { // println!("test1"); // } // } // The version below requires the user to keep the two names in sync. // Also, the error messages are not great. // // macro_rules! async_test { // ($name:ident) => { // mod $name { // #[test] // fn async_test () { // safina_executor::block_on(super::$name()) // } // } // } // } // // async_test!(test1); // async fn test1() { // println!("test1"); // } // // async_test!(test1); // error[E0428]: the name `test1` is defined multiple times // async fn test2() { // println!("test2"); // } // // async_test!(test3); // error[E0423]: expected function, found module `super::test3` // async fn test4() { // println!("test4"); // }