Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ranile committed Jun 2, 2021
1 parent 45f92ae commit bfcc614
Show file tree
Hide file tree
Showing 14 changed files with 108 additions and 38 deletions.
11 changes: 10 additions & 1 deletion packages/yew-router-macro/src/routable_derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,16 @@ pub fn routable_derive_impl(input: DeriveInput) -> syn::Result<TokenStream> {
let attrs = field
.attrs
.iter()
.filter(|attr| attr.path.is_ident(BIND_ATTR_PATH));
.filter(|attr| attr.path.is_ident(BIND_ATTR_PATH))
.collect::<Vec<_>>();

if attrs.len() > 1 {
return Err(syn::Error::new_spanned(
quote! { #(#attrs)* },
"a field can have only one `#[bind(...)]`"
))
}

for attr in attrs {
// todo maybe don't clone?
let ident = field.ident.clone().unwrap(); // named fields have idents
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#[derive(yew_router::Routable)]
#[derive(Clone, yew_router::Routable)]
enum Routes {
One,
}

#[derive(yew_router::Routable)]
#[derive(Clone, yew_router::Routable)]
enum RoutesTwo {
#[at("/one")]
#[at("/two")]
Expand Down
28 changes: 18 additions & 10 deletions packages/yew-router-macro/tests/routable_derive/bad-ats-fail.stderr
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
error: at attribute must be present on every variant
--> $DIR/bad-ats-fail.rs:3:5
error: at attribute not found
--> $DIR/bad-ats-fail.rs:1:10
|
3 | One,
| ^^^

error: only one at attribute must be present
--> $DIR/bad-ats-fail.rs:8:5
1 | #[derive(yew_router::Routable)]
| ^^^^^^^^^^^^^^^^^^^^
|
8 | / #[at("/one")]
9 | | #[at("/two")]
| |_________________^
= note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `RoutesTwo: Clone` is not satisfied
--> $DIR/bad-ats-fail.rs:6:10
|
6 | #[derive(yew_router::Routable)]
| ^^^^^^^^^^^^^^^^^^^^ the trait `Clone` is not implemented for `RoutesTwo`
|
::: $WORKSPACE/packages/yew-router/src/routable.rs
|
| pub trait Routable: Sized + Clone {
| ----- required by this bound in `Routable`
|
= note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#[derive(Debug, Clone, PartialEq, yew_router::Routable)]
enum Routes {
#[at("/")]
Home {
#[bind(query)]
#[bind(query)]
id: u32
},
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
error: a field can have only one `#[bind(...)]`
--> $DIR/invalid-bind-fail.rs:5:9
|
5 | / #[bind(query)]
6 | | #[bind(query)]
| |______________________^
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
#[derive(Debug, PartialEq, yew_router::Routable)]
#[derive(Clone, Debug, PartialEq, yew_router::Routable)]
enum RoutesOne {
#[at("/")]
#[not_found]
#[bind(not_found)]
Home,
#[at("/404")]
#[not_found]
#[bind(not_found)]
NotFound,
}

#[derive(Debug, PartialEq, yew_router::Routable)]
#[derive(Clone, Debug, PartialEq, yew_router::Routable)]
enum RoutesTwo {
#[at("/404")]
#[not_found]
#[not_found]
#[bind(not_found)]
#[bind(not_found)]
NotFound,
}
fn main() {}
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
error: there can only be one not_found
--> $DIR/invalid-not-found-fail.rs:4:5
|
4 | / #[not_found]
5 | | Home,
6 | | #[at("/404")]
7 | | #[not_found]
| |________________^

error: there can only be one not_found
error: only one `#[bind(not_found)]` can be present
--> $DIR/invalid-not-found-fail.rs:14:5
|
14 | / #[not_found]
15 | | #[not_found]
| |________________^
14 | / #[bind(not_found)]
15 | | #[bind(not_found)]
| |______________________^

error[E0277]: the trait bound `RoutesOne: Clone` is not satisfied
--> $DIR/invalid-not-found-fail.rs:1:28
|
1 | #[derive(Debug, PartialEq, yew_router::Routable)]
| ^^^^^^^^^^^^^^^^^^^^ the trait `Clone` is not implemented for `RoutesOne`
|
::: $WORKSPACE/packages/yew-router/src/routable.rs
|
| pub trait Routable: Sized + Clone {
| ----- required by this bound in `Routable`
|
= note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)
18 changes: 18 additions & 0 deletions packages/yew-router-macro/tests/routable_derive/no-clone-fail.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#![no_implicit_prelude]

#[derive(::yew_router::Routable)]
enum Routes {
#[at("/")]
One,
#[at("/two/:id")]
Two {
id: u32,
#[bind(query)]
query: u32,
},
#[at("/404")]
#[bind(not_found)]
NotFound,
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
error[E0277]: the trait bound `Routes: Clone` is not satisfied
--> $DIR/no-clone-fail.rs:3:10
|
3 | #[derive(::yew_router::Routable)]
| ^^^^^^^^^^^^^^^^^^^^^^ the trait `Clone` is not implemented for `Routes`
|
::: $WORKSPACE/packages/yew-router/src/routable.rs
|
| pub trait Routable: Sized + Clone {
| ----- required by this bound in `Routable`
|
= note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[derive(yew_router::Routable)]
#[derive(Clone, yew_router::Routable)]
enum Routes {
#[at("/one/:two")]
One(u32),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: only named fields are supported
error: only named fields are allowed
--> $DIR/unnamed-fields-fail.rs:4:8
|
4 | One(u32),
Expand Down
8 changes: 6 additions & 2 deletions packages/yew-router-macro/tests/routable_derive/valid-pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ enum Routes {
#[at("/")]
One,
#[at("/two/:id")]
Two { id: u32 },
Two {
id: u32,
#[bind(query)]
query: u32,
},
#[at("/404")]
#[not_found]
#[bind(not_found)]
NotFound,
}

Expand Down
1 change: 0 additions & 1 deletion packages/yew-router/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ features = [
[dev-dependencies]
wasm-bindgen-test = "0.3"
yew-functional = { path = "../yew-functional" }
serde = { version = "1.0", features = ["derive"] }

[dev-dependencies.web-sys]
version = "0.3"
Expand Down
1 change: 0 additions & 1 deletion packages/yew-router/tests/router.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use serde::{Deserialize, Serialize};
use wasm_bindgen_test::{wasm_bindgen_test as test, wasm_bindgen_test_configure};
use yew::prelude::*;
use yew_functional::function_component;
Expand Down

0 comments on commit bfcc614

Please sign in to comment.