add trybuild tests to routing codegen

This commit is contained in:
Rob Ede 2020-09-13 12:27:51 +01:00
parent f861508789
commit c0574e2c61
No known key found for this signature in database
GPG Key ID: C2A3B36E841A91E6
6 changed files with 88 additions and 17 deletions

View File

@ -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"

View File

@ -85,7 +85,7 @@ pub fn put(args: TokenStream, input: TokenStream) -> TokenStream {
///
/// 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)
@ -95,7 +95,7 @@ pub fn delete(args: TokenStream, input: TokenStream) -> TokenStream {
///
/// 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)
@ -105,7 +105,7 @@ pub fn head(args: TokenStream, input: TokenStream) -> TokenStream {
///
/// 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)
@ -115,7 +115,7 @@ pub fn connect(args: TokenStream, input: TokenStream) -> TokenStream {
///
/// 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)
@ -125,7 +125,7 @@ pub fn options(args: TokenStream, input: TokenStream) -> TokenStream {
///
/// 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)
@ -135,7 +135,7 @@ pub fn trace(args: TokenStream, input: TokenStream) -> TokenStream {
///
/// 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)

View File

@ -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");
}

View File

@ -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() {}

View File

@ -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")]
| ^^^^^^^

View File

@ -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());
}