From 0743a86f6a092a2d7b94c4fefb60b378dc5f93f1 Mon Sep 17 00:00:00 2001 From: David Pedersen Date: Mon, 14 Feb 2022 09:52:14 +0100 Subject: [PATCH] ui tests --- axum-macros/src/typed_path.rs | 15 +++++++++++ .../tests/typed_path/fail/missing_capture.rs | 10 ++++++++ .../typed_path/fail/missing_capture.stderr | 14 +++++++++++ .../tests/typed_path/fail/missing_field.rs | 9 +++++++ .../typed_path/fail/missing_field.stderr | 5 ++++ .../tests/typed_path/fail/not_deserialize.rs | 9 +++++++ .../typed_path/fail/not_deserialize.stderr | 8 ++++++ .../typed_path/fail/unit_with_capture.rs | 8 ++++++ .../typed_path/fail/unit_with_capture.stderr | 5 ++++ .../typed_path/pass/named_fields_struct.rs | 25 +++++++++++++++++++ .../tests/typed_path/pass/tuple_struct.rs | 13 ++++++++++ .../tests/typed_path/pass/unit_struct.rs | 13 ++++++++++ 12 files changed, 134 insertions(+) create mode 100644 axum-macros/tests/typed_path/fail/missing_capture.rs create mode 100644 axum-macros/tests/typed_path/fail/missing_capture.stderr create mode 100644 axum-macros/tests/typed_path/fail/missing_field.rs create mode 100644 axum-macros/tests/typed_path/fail/missing_field.stderr create mode 100644 axum-macros/tests/typed_path/fail/not_deserialize.rs create mode 100644 axum-macros/tests/typed_path/fail/not_deserialize.stderr create mode 100644 axum-macros/tests/typed_path/fail/unit_with_capture.rs create mode 100644 axum-macros/tests/typed_path/fail/unit_with_capture.stderr create mode 100644 axum-macros/tests/typed_path/pass/named_fields_struct.rs create mode 100644 axum-macros/tests/typed_path/pass/tuple_struct.rs create mode 100644 axum-macros/tests/typed_path/pass/unit_struct.rs diff --git a/axum-macros/src/typed_path.rs b/axum-macros/src/typed_path.rs index ed2264f4..ef6bd999 100644 --- a/axum-macros/src/typed_path.rs +++ b/axum-macros/src/typed_path.rs @@ -262,3 +262,18 @@ enum Segment { Capture(String, Span), Static(String), } + +#[test] +fn ui() { + #[rustversion::stable] + fn go() { + let t = trybuild::TestCases::new(); + t.compile_fail("tests/typed_path/fail/*.rs"); + t.pass("tests/typed_path/pass/*.rs"); + } + + #[rustversion::not(stable)] + fn go() {} + + go(); +} diff --git a/axum-macros/tests/typed_path/fail/missing_capture.rs b/axum-macros/tests/typed_path/fail/missing_capture.rs new file mode 100644 index 00000000..8ecf7d45 --- /dev/null +++ b/axum-macros/tests/typed_path/fail/missing_capture.rs @@ -0,0 +1,10 @@ +use axum_macros::TypedPath; +use serde::Deserialize; + +#[derive(TypedPath, Deserialize)] +#[typed_path("/users")] +struct MyPath { + id: u32, +} + +fn main() {} diff --git a/axum-macros/tests/typed_path/fail/missing_capture.stderr b/axum-macros/tests/typed_path/fail/missing_capture.stderr new file mode 100644 index 00000000..85865a52 --- /dev/null +++ b/axum-macros/tests/typed_path/fail/missing_capture.stderr @@ -0,0 +1,14 @@ +error[E0027]: pattern does not mention field `id` + --> tests/typed_path/fail/missing_capture.rs:5:14 + | +5 | #[typed_path("/users")] + | ^^^^^^^^ missing field `id` + | +help: include the missing field in the pattern + | +5 | #[typed_path("/users" { id })] + | ++++++ +help: if you don't care about this missing field, you can explicitly ignore it + | +5 | #[typed_path("/users" { .. })] + | ++++++ diff --git a/axum-macros/tests/typed_path/fail/missing_field.rs b/axum-macros/tests/typed_path/fail/missing_field.rs new file mode 100644 index 00000000..2e211769 --- /dev/null +++ b/axum-macros/tests/typed_path/fail/missing_field.rs @@ -0,0 +1,9 @@ +use axum_macros::TypedPath; +use serde::Deserialize; + +#[derive(TypedPath, Deserialize)] +#[typed_path("/users/:id")] +struct MyPath {} + +fn main() { +} diff --git a/axum-macros/tests/typed_path/fail/missing_field.stderr b/axum-macros/tests/typed_path/fail/missing_field.stderr new file mode 100644 index 00000000..faf2d4b6 --- /dev/null +++ b/axum-macros/tests/typed_path/fail/missing_field.stderr @@ -0,0 +1,5 @@ +error[E0026]: struct `MyPath` does not have a field named `id` + --> tests/typed_path/fail/missing_field.rs:5:14 + | +5 | #[typed_path("/users/:id")] + | ^^^^^^^^^^^^ struct `MyPath` does not have this field diff --git a/axum-macros/tests/typed_path/fail/not_deserialize.rs b/axum-macros/tests/typed_path/fail/not_deserialize.rs new file mode 100644 index 00000000..b5691866 --- /dev/null +++ b/axum-macros/tests/typed_path/fail/not_deserialize.rs @@ -0,0 +1,9 @@ +use axum_macros::TypedPath; + +#[derive(TypedPath)] +#[typed_path("/users/:id")] +struct MyPath { + id: u32, +} + +fn main() {} diff --git a/axum-macros/tests/typed_path/fail/not_deserialize.stderr b/axum-macros/tests/typed_path/fail/not_deserialize.stderr new file mode 100644 index 00000000..e4a3b734 --- /dev/null +++ b/axum-macros/tests/typed_path/fail/not_deserialize.stderr @@ -0,0 +1,8 @@ +error[E0277]: the trait bound `for<'de> MyPath: serde::de::Deserialize<'de>` is not satisfied + --> tests/typed_path/fail/not_deserialize.rs:4:14 + | +4 | #[typed_path("/users/:id")] + | ^^^^^^^^^^^^ the trait `for<'de> serde::de::Deserialize<'de>` is not implemented for `MyPath` + | + = note: required because of the requirements on the impl of `serde::de::DeserializeOwned` for `MyPath` + = note: required because of the requirements on the impl of `FromRequest` for `axum::extract::Path` diff --git a/axum-macros/tests/typed_path/fail/unit_with_capture.rs b/axum-macros/tests/typed_path/fail/unit_with_capture.rs new file mode 100644 index 00000000..49979cf7 --- /dev/null +++ b/axum-macros/tests/typed_path/fail/unit_with_capture.rs @@ -0,0 +1,8 @@ +use axum_macros::TypedPath; +use serde::Deserialize; + +#[derive(TypedPath, Deserialize)] +#[typed_path("/users/:id")] +struct MyPath; + +fn main() {} diff --git a/axum-macros/tests/typed_path/fail/unit_with_capture.stderr b/axum-macros/tests/typed_path/fail/unit_with_capture.stderr new file mode 100644 index 00000000..d290308c --- /dev/null +++ b/axum-macros/tests/typed_path/fail/unit_with_capture.stderr @@ -0,0 +1,5 @@ +error: Typed paths for unit structs cannot contain captures + --> tests/typed_path/fail/unit_with_capture.rs:5:14 + | +5 | #[typed_path("/users/:id")] + | ^^^^^^^^^^^^ diff --git a/axum-macros/tests/typed_path/pass/named_fields_struct.rs b/axum-macros/tests/typed_path/pass/named_fields_struct.rs new file mode 100644 index 00000000..6942bd33 --- /dev/null +++ b/axum-macros/tests/typed_path/pass/named_fields_struct.rs @@ -0,0 +1,25 @@ +use axum_extra::routing::TypedPath; +use serde::Deserialize; + +#[derive(TypedPath, Deserialize)] +#[typed_path("/users/:user_id/teams/:team_id")] +struct MyPath { + user_id: u32, + team_id: u32, +} + +fn main() { + axum::Router::::new().route("/", axum::routing::get(|_: MyPath| async {})); + + assert_eq!(MyPath::PATH, "/users/:user_id/teams/:team_id"); + assert_eq!( + format!( + "{}", + MyPath { + user_id: 1, + team_id: 2 + } + ), + "/users/1/teams/2" + ); +} diff --git a/axum-macros/tests/typed_path/pass/tuple_struct.rs b/axum-macros/tests/typed_path/pass/tuple_struct.rs new file mode 100644 index 00000000..a0b2e609 --- /dev/null +++ b/axum-macros/tests/typed_path/pass/tuple_struct.rs @@ -0,0 +1,13 @@ +use axum_extra::routing::TypedPath; +use serde::Deserialize; + +#[derive(TypedPath, Deserialize)] +#[typed_path("/users/:user_id/teams/:team_id")] +struct MyPath(u32, u32); + +fn main() { + axum::Router::::new().route("/", axum::routing::get(|_: MyPath| async {})); + + assert_eq!(MyPath::PATH, "/users/:user_id/teams/:team_id"); + assert_eq!(format!("{}", MyPath(1, 2)), "/users/1/teams/2"); +} diff --git a/axum-macros/tests/typed_path/pass/unit_struct.rs b/axum-macros/tests/typed_path/pass/unit_struct.rs new file mode 100644 index 00000000..9b6a0f6e --- /dev/null +++ b/axum-macros/tests/typed_path/pass/unit_struct.rs @@ -0,0 +1,13 @@ +use axum_extra::routing::TypedPath; + +#[derive(TypedPath)] +#[typed_path("/users")] +struct MyPath; + +fn main() { + axum::Router::::new() + .route("/", axum::routing::get(|_: MyPath| async {})); + + assert_eq!(MyPath::PATH, "/users"); + assert_eq!(format!("{}", MyPath), "/users"); +}