From c0574e2c61bff7d19295b6fd4aef07b6c07f6174 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Sun, 13 Sep 2020 12:27:51 +0100 Subject: [PATCH] add trybuild tests to routing codegen --- actix-web-codegen/Cargo.toml | 1 + actix-web-codegen/src/lib.rs | 34 +++++++++---------- actix-web-codegen/tests/trybuild.rs | 7 ++++ .../tests/trybuild/simple-fail.rs | 25 ++++++++++++++ .../tests/trybuild/simple-fail.stderr | 23 +++++++++++++ actix-web-codegen/tests/trybuild/simple.rs | 15 ++++++++ 6 files changed, 88 insertions(+), 17 deletions(-) create mode 100644 actix-web-codegen/tests/trybuild.rs create mode 100644 actix-web-codegen/tests/trybuild/simple-fail.rs create mode 100644 actix-web-codegen/tests/trybuild/simple-fail.stderr create mode 100644 actix-web-codegen/tests/trybuild/simple.rs diff --git a/actix-web-codegen/Cargo.toml b/actix-web-codegen/Cargo.toml index 05b52c9db..1bf78f997 100644 --- a/actix-web-codegen/Cargo.toml +++ b/actix-web-codegen/Cargo.toml @@ -22,3 +22,4 @@ proc-macro2 = "1" actix-rt = "1.0.0" actix-web = "3.0.0" futures-util = { version = "0.3.5", default-features = false } +trybuild = "1" diff --git a/actix-web-codegen/src/lib.rs b/actix-web-codegen/src/lib.rs index b6df3f0dd..445fe924d 100644 --- a/actix-web-codegen/src/lib.rs +++ b/actix-web-codegen/src/lib.rs @@ -49,13 +49,13 @@ use proc_macro::TokenStream; /// Creates route handler with `GET` method guard. /// -/// Syntax: `#[get("path"[, attributes])]` +/// Syntax: `#[get("path" [, attributes])]` /// /// ## Attributes: /// /// - `"path"` - Raw literal string with path for which to register handler. Mandatory. -/// - `guard="function_name"` - Registers function as guard using `actix_web::guard::fn_guard` -/// - `wrap="Middleware"` - Registers a resource middleware. +/// - `guard = "function_name"` - Registers function as guard using `actix_web::guard::fn_guard` +/// - `wrap = "Middleware"` - Registers a resource middleware. #[proc_macro_attribute] pub fn get(args: TokenStream, input: TokenStream) -> TokenStream { route::generate(args, input, route::GuardType::Get) @@ -63,7 +63,7 @@ pub fn get(args: TokenStream, input: TokenStream) -> TokenStream { /// Creates route handler with `POST` method guard. /// -/// Syntax: `#[post("path"[, attributes])]` +/// Syntax: `#[post("path" [, attributes])]` /// /// Attributes are the same as in [get](attr.get.html) #[proc_macro_attribute] @@ -73,7 +73,7 @@ pub fn post(args: TokenStream, input: TokenStream) -> TokenStream { /// Creates route handler with `PUT` method guard. /// -/// Syntax: `#[put("path"[, attributes])]` +/// Syntax: `#[put("path" [, attributes])]` /// /// Attributes are the same as in [get](attr.get.html) #[proc_macro_attribute] @@ -83,9 +83,9 @@ pub fn put(args: TokenStream, input: TokenStream) -> TokenStream { /// Creates route handler with `DELETE` method guard. /// -/// Syntax: `#[delete("path"[, attributes])]` +/// Syntax: `#[delete("path" [, attributes])]` /// -/// Attributes are the same as in [get](attr.get.html) +/// Attributes are the same as in [get](attr.get.html). #[proc_macro_attribute] pub fn delete(args: TokenStream, input: TokenStream) -> TokenStream { route::generate(args, input, route::GuardType::Delete) @@ -93,9 +93,9 @@ pub fn delete(args: TokenStream, input: TokenStream) -> TokenStream { /// Creates route handler with `HEAD` method guard. /// -/// Syntax: `#[head("path"[, attributes])]` +/// Syntax: `#[head("path" [, attributes])]` /// -/// Attributes are the same as in [head](attr.head.html) +/// Attributes are the same as in [get](attr.get.html). #[proc_macro_attribute] pub fn head(args: TokenStream, input: TokenStream) -> TokenStream { route::generate(args, input, route::GuardType::Head) @@ -103,9 +103,9 @@ pub fn head(args: TokenStream, input: TokenStream) -> TokenStream { /// Creates route handler with `CONNECT` method guard. /// -/// Syntax: `#[connect("path"[, attributes])]` +/// Syntax: `#[connect("path" [, attributes])]` /// -/// Attributes are the same as in [connect](attr.connect.html) +/// Attributes are the same as in [get](attr.get.html). #[proc_macro_attribute] pub fn connect(args: TokenStream, input: TokenStream) -> TokenStream { route::generate(args, input, route::GuardType::Connect) @@ -113,9 +113,9 @@ pub fn connect(args: TokenStream, input: TokenStream) -> TokenStream { /// Creates route handler with `OPTIONS` method guard. /// -/// Syntax: `#[options("path"[, attributes])]` +/// Syntax: `#[options("path" [, attributes])]` /// -/// Attributes are the same as in [options](attr.options.html) +/// Attributes are the same as in [get](attr.get.html). #[proc_macro_attribute] pub fn options(args: TokenStream, input: TokenStream) -> TokenStream { route::generate(args, input, route::GuardType::Options) @@ -123,9 +123,9 @@ pub fn options(args: TokenStream, input: TokenStream) -> TokenStream { /// Creates route handler with `TRACE` method guard. /// -/// Syntax: `#[trace("path"[, attributes])]` +/// Syntax: `#[trace("path" [, attributes])]` /// -/// Attributes are the same as in [trace](attr.trace.html) +/// Attributes are the same as in [get](attr.get.html). #[proc_macro_attribute] pub fn trace(args: TokenStream, input: TokenStream) -> TokenStream { route::generate(args, input, route::GuardType::Trace) @@ -133,9 +133,9 @@ pub fn trace(args: TokenStream, input: TokenStream) -> TokenStream { /// Creates route handler with `PATCH` method guard. /// -/// Syntax: `#[patch("path"[, attributes])]` +/// Syntax: `#[patch("path" [, attributes])]` /// -/// Attributes are the same as in [patch](attr.patch.html) +/// Attributes are the same as in [get](attr.get.html). #[proc_macro_attribute] pub fn patch(args: TokenStream, input: TokenStream) -> TokenStream { route::generate(args, input, route::GuardType::Patch) diff --git a/actix-web-codegen/tests/trybuild.rs b/actix-web-codegen/tests/trybuild.rs new file mode 100644 index 000000000..b675947d3 --- /dev/null +++ b/actix-web-codegen/tests/trybuild.rs @@ -0,0 +1,7 @@ +#[test] +fn compile_macros() { + let t = trybuild::TestCases::new(); + + t.pass("tests/trybuild/simple.rs"); + t.compile_fail("tests/trybuild/simple-fail.rs"); +} diff --git a/actix-web-codegen/tests/trybuild/simple-fail.rs b/actix-web-codegen/tests/trybuild/simple-fail.rs new file mode 100644 index 000000000..140497687 --- /dev/null +++ b/actix-web-codegen/tests/trybuild/simple-fail.rs @@ -0,0 +1,25 @@ +use actix_web::*; + +#[get("/one", other)] +async fn one() -> impl Responder { + HttpResponse::Ok() +} + +#[post(/two)] +async fn two() -> impl Responder { + HttpResponse::Ok() +} + +static PATCH_PATH: &str = "/three"; + +#[patch(PATCH_PATH)] +async fn three() -> impl Responder { + HttpResponse::Ok() +} + +#[delete("/four", "/five")] +async fn four() -> impl Responder { + HttpResponse::Ok() +} + +fn main() {} diff --git a/actix-web-codegen/tests/trybuild/simple-fail.stderr b/actix-web-codegen/tests/trybuild/simple-fail.stderr new file mode 100644 index 000000000..12c32c00d --- /dev/null +++ b/actix-web-codegen/tests/trybuild/simple-fail.stderr @@ -0,0 +1,23 @@ +error: Unknown attribute. + --> $DIR/simple-fail.rs:3:15 + | +3 | #[get("/one", other)] + | ^^^^^ + +error: expected identifier or literal + --> $DIR/simple-fail.rs:8:8 + | +8 | #[post(/two)] + | ^ + +error: Unknown attribute. + --> $DIR/simple-fail.rs:15:9 + | +15 | #[patch(PATCH_PATH)] + | ^^^^^^^^^^ + +error: Multiple paths specified! Should be only one! + --> $DIR/simple-fail.rs:20:19 + | +20 | #[delete("/four", "/five")] + | ^^^^^^^ diff --git a/actix-web-codegen/tests/trybuild/simple.rs b/actix-web-codegen/tests/trybuild/simple.rs new file mode 100644 index 000000000..6b1e67442 --- /dev/null +++ b/actix-web-codegen/tests/trybuild/simple.rs @@ -0,0 +1,15 @@ +use actix_web::*; + +#[get("/config")] +async fn config() -> impl Responder { + HttpResponse::Ok() +} + +#[actix_web::main] +async fn main() { + let srv = test::start(|| App::new().service(config)); + + let request = srv.get("/config"); + let response = request.send().await.unwrap(); + assert!(response.status().is_success()); +}