diff options
author | Taiki Endo <te316e89@gmail.com> | 2019-09-23 04:09:30 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-09-23 04:09:30 +0900 |
commit | 3a55aba251e445b1ff5e03ee2319d7a2920c1ee9 (patch) | |
tree | 61fef45106b7cf8f6306876afc16ff57eff604e1 | |
parent | ddbb0c38368bdb67d2d1b18e2281da286a540e90 (diff) |
macros: add build tests for #[tokio::main] and #[tokio::test] (#1591)
-rw-r--r-- | azure-pipelines.yml | 1 | ||||
-rw-r--r-- | build-tests/Cargo.toml | 1 | ||||
-rw-r--r-- | build-tests/tests/fail/macros_invalid_input.rs | 28 | ||||
-rw-r--r-- | build-tests/tests/fail/macros_invalid_input.stderr | 47 | ||||
-rw-r--r-- | build-tests/tests/features.rs | 4 | ||||
-rw-r--r-- | tokio-macros/src/lib.rs | 5 |
6 files changed, 84 insertions, 2 deletions
diff --git a/azure-pipelines.yml b/azure-pipelines.yml index e8cbc84f..7386ef97 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -85,6 +85,7 @@ jobs: - tokio-executor - tokio-net - executor-without-current-thread + - macros-invalid-input - net-no-features - net-with-tcp - net-with-udp diff --git a/build-tests/Cargo.toml b/build-tests/Cargo.toml index 6c154605..dfd4f8b4 100644 --- a/build-tests/Cargo.toml +++ b/build-tests/Cargo.toml @@ -7,6 +7,7 @@ publish = false [features] executor-without-current-thread = ["tokio-executor"] +macros-invalid-input = ["tokio/rt-full"] net-no-features = ["tokio-net"] net-with-tcp = ["tokio-net/tcp"] net-with-udp = ["tokio-net/udp"] diff --git a/build-tests/tests/fail/macros_invalid_input.rs b/build-tests/tests/fail/macros_invalid_input.rs new file mode 100644 index 00000000..827507eb --- /dev/null +++ b/build-tests/tests/fail/macros_invalid_input.rs @@ -0,0 +1,28 @@ +use build_tests::tokio; + +#[tokio::main] +fn main_is_not_async() {} + +#[tokio::main] +async fn main_fn_has_args(_x: u8) {} + +#[tokio::main(foo)] +async fn main_attr_has_unknown_args() {} + +#[tokio::main(multi_thread::bar)] +async fn main_attr_has_path_args() {} + +#[tokio::test] +fn test_is_not_async() {} + +#[tokio::test] +async fn test_fn_has_args(_x: u8) {} + +#[tokio::test(foo)] +async fn test_attr_has_args() {} + +#[tokio::test] +#[test] +async fn test_has_second_test_attr() {} + +fn main() {} diff --git a/build-tests/tests/fail/macros_invalid_input.stderr b/build-tests/tests/fail/macros_invalid_input.stderr new file mode 100644 index 00000000..2f136125 --- /dev/null +++ b/build-tests/tests/fail/macros_invalid_input.stderr @@ -0,0 +1,47 @@ +error: the async keyword is missing from the function declaration + --> $DIR/macros_invalid_input.rs:4:1 + | +4 | fn main_is_not_async() {} + | ^^ + +error: the main function cannot accept arguments + --> $DIR/macros_invalid_input.rs:7:27 + | +7 | async fn main_fn_has_args(_x: u8) {} + | ^^^^^^ + +error: Unknown attribute foo is specified + --> $DIR/macros_invalid_input.rs:9:15 + | +9 | #[tokio::main(foo)] + | ^^^ + +error: Must have specified ident + --> $DIR/macros_invalid_input.rs:12:15 + | +12 | #[tokio::main(multi_thread::bar)] + | ^^^^^^^^^^^^^^^^^ + +error: the async keyword is missing from the function declaration + --> $DIR/macros_invalid_input.rs:16:1 + | +16 | fn test_is_not_async() {} + | ^^ + +error: the test function cannot accept arguments + --> $DIR/macros_invalid_input.rs:19:27 + | +19 | async fn test_fn_has_args(_x: u8) {} + | ^^^^^^ + +error: unexpected token + --> $DIR/macros_invalid_input.rs:21:15 + | +21 | #[tokio::test(foo)] + | ^^^ + +error: second test attribute is supplied + --> $DIR/macros_invalid_input.rs:25:1 + | +25 | #[test] + | ^^^^^^^ diff --git a/build-tests/tests/features.rs b/build-tests/tests/features.rs index 5bf26afc..d6d25737 100644 --- a/build-tests/tests/features.rs +++ b/build-tests/tests/features.rs @@ -37,6 +37,7 @@ fn tokio_with_net() { // net is present use build_tests::tokio::net; } + #[test] fn compile_fail() { let t = trybuild::TestCases::new(); @@ -44,6 +45,9 @@ fn compile_fail() { #[cfg(feature = "executor-without-current-thread")] t.compile_fail("tests/fail/executor_without_current_thread.rs"); + #[cfg(feature = "macros-invalid-input")] + t.compile_fail("tests/fail/macros_invalid_input.rs"); + #[cfg(feature = "net-no-features")] { t.compile_fail("tests/fail/net_without_tcp_missing_tcp.rs"); diff --git a/tokio-macros/src/lib.rs b/tokio-macros/src/lib.rs index cabfd8be..9a33c5fd 100644 --- a/tokio-macros/src/lib.rs +++ b/tokio-macros/src/lib.rs @@ -124,8 +124,9 @@ pub fn main(args: TokenStream, item: TokenStream) -> TokenStream { /// } /// ``` #[proc_macro_attribute] -pub fn test(_attr: TokenStream, item: TokenStream) -> TokenStream { +pub fn test(args: TokenStream, item: TokenStream) -> TokenStream { let input = syn::parse_macro_input!(item as syn::ItemFn); + let _ = syn::parse_macro_input!(args as syn::parse::Nothing); let ret = &input.sig.output; let name = &input.sig.ident; @@ -143,7 +144,7 @@ pub fn test(_attr: TokenStream, item: TokenStream) -> TokenStream { if input.sig.asyncness.is_none() { let msg = "the async keyword is missing from the function declaration"; - return syn::Error::new_spanned(&input, msg) + return syn::Error::new_spanned(&input.sig.fn_token, msg) .to_compile_error() .into(); } else if !input.sig.inputs.is_empty() { |