123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- #![allow(non_snake_case)]
- use dioxus::prelude::*;
- use dioxus_router_core::*;
- use dioxus_router_macro::*;
- use std::str::FromStr;
- #[inline_props]
- fn Route1(cx: Scope, dynamic: String) -> Element {
- render! {
- div{
- "Route1: {dynamic}"
- }
- }
- }
- #[inline_props]
- fn Route2(cx: Scope) -> Element {
- render! {
- div{
- "Route2"
- }
- }
- }
- #[inline_props]
- fn Route3(cx: Scope, dynamic: u32) -> Element {
- render! {
- div{
- "Route3: {dynamic}"
- }
- }
- }
- #[inline_props]
- fn Route4(cx: Scope, number1: u32, number2: u32) -> Element {
- render! {
- div{
- "Route4: {number1} {number2}"
- }
- }
- }
- #[inline_props]
- fn Route5(cx: Scope, query: String) -> Element {
- render! {
- div{
- "Route5: {query}"
- }
- }
- }
- #[inline_props]
- fn Route6(cx: Scope, extra: Vec<String>) -> Element {
- render! {
- div{
- "Route5: {extra:?}"
- }
- }
- }
- #[inline_props]
- fn Nested(cx: Scope, nested: String) -> Element {
- render! {
- div{
- "Nested: {nested:?}"
- }
- }
- }
- #[rustfmt::skip]
- #[routable]
- #[derive(Clone, Debug, PartialEq)]
- enum Route {
- #[route("/(dynamic)" Route1)]
- Route1 { dynamic: String },
- #[nest("/(nested)" nested { nested: String } Nested)]
- #[route("/" Route2)]
- Route2 {},
- // #[redirect("/(dynamic)/hello_world")]
- #[route("/(dynamic)" Route3)]
- Route3 { dynamic: u32 },
- #[end_nest]
- #[route("/(number1)/(number2)" Route4)]
- Route4 { number1: u32, number2: u32 },
- #[route("/?(query)" Route5)]
- Route5 { query: String },
- #[route("/(...extra)" Route6)]
- Route6 { extra: Vec<String> },
- }
- #[test]
- fn display_works() {
- let route = Route::Route1 {
- dynamic: "hello".to_string(),
- };
- assert_eq!(route.to_string(), "/hello");
- let route = Route::Route3 { dynamic: 1234 };
- assert_eq!(route.to_string(), "/hello_world/1234");
- let route = Route::Route1 {
- dynamic: "hello_world2".to_string(),
- };
- assert_eq!(route.to_string(), "/hello_world2");
- let route = Route::Route4 {
- number1: 1234,
- number2: 5678,
- };
- assert_eq!(route.to_string(), "/1234/5678");
- let route = Route::Route5 {
- query: "hello".to_string(),
- };
- assert_eq!(route.to_string(), "/?hello");
- let route = Route::Route6 {
- extra: vec!["hello".to_string(), "world".to_string()],
- };
- assert_eq!(route.to_string(), "/hello/world");
- }
- #[test]
- fn from_string_works() {
- let w = "/hello";
- assert_eq!(
- Route::from_str(w),
- Ok(Route::Route1 {
- dynamic: "hello".to_string()
- })
- );
- let w = "/hello/";
- assert_eq!(
- Route::from_str(w),
- Ok(Route::Route1 {
- dynamic: "hello".to_string()
- })
- );
- let w = "/hello_world/1234";
- assert_eq!(Route::from_str(w), Ok(Route::Route3 { dynamic: 1234 }));
- let w = "/hello_world/1234/";
- assert_eq!(Route::from_str(w), Ok(Route::Route3 { dynamic: 1234 }));
- let w = "/hello_world2";
- assert_eq!(
- Route::from_str(w),
- Ok(Route::Route1 {
- dynamic: "hello_world2".to_string()
- })
- );
- let w = "/?x=1234&y=hello";
- assert_eq!(
- Route::from_str(w),
- Ok(Route::Route5 {
- query: "x=1234&y=hello".to_string()
- })
- );
- let w = "/hello_world/hello_world/hello_world";
- assert_eq!(
- Route::from_str(w),
- Ok(Route::Route6 {
- extra: vec![
- "hello_world".to_string(),
- "hello_world".to_string(),
- "hello_world".to_string()
- ]
- })
- );
- }
- #[test]
- fn round_trip() {
- // Route1
- let string = "hello_world2";
- let route = Route::Route1 {
- dynamic: string.to_string(),
- };
- assert_eq!(Route::from_str(&route.to_string()), Ok(route));
- // Route2
- for num in 0..100 {
- let route = Route::Route3 { dynamic: num };
- assert_eq!(Route::from_str(&route.to_string()), Ok(route));
- }
- // Route3
- for num1 in 0..100 {
- for num2 in 0..100 {
- let route = Route::Route4 {
- number1: num1,
- number2: num2,
- };
- assert_eq!(Route::from_str(&route.to_string()), Ok(route));
- }
- }
- // Route4
- let string = "x=1234&y=hello";
- let route = Route::Route5 {
- query: string.to_string(),
- };
- assert_eq!(Route::from_str(&route.to_string()), Ok(route));
- // Route5
- let route = Route::Route6 {
- extra: vec![
- "hello_world".to_string(),
- "hello_world".to_string(),
- "hello_world".to_string(),
- ],
- };
- assert_eq!(Route::from_str(&route.to_string()), Ok(route));
- }
- fn main() {}
|