macro.rs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #![allow(non_snake_case)]
  2. use dioxus::prelude::*;
  3. use dioxus_router_core::*;
  4. use dioxus_router_macro::*;
  5. use std::str::FromStr;
  6. #[inline_props]
  7. fn Route1(cx: Scope, dynamic: String) -> Element {
  8. render! {
  9. div{
  10. "Route1: {dynamic}"
  11. }
  12. }
  13. }
  14. #[inline_props]
  15. fn Route2(cx: Scope) -> Element {
  16. render! {
  17. div{
  18. "Route2"
  19. }
  20. }
  21. }
  22. #[inline_props]
  23. fn Route3(cx: Scope, dynamic: u32) -> Element {
  24. render! {
  25. div{
  26. "Route3: {dynamic}"
  27. }
  28. }
  29. }
  30. #[inline_props]
  31. fn Route4(cx: Scope, number1: u32, number2: u32) -> Element {
  32. render! {
  33. div{
  34. "Route4: {number1} {number2}"
  35. }
  36. }
  37. }
  38. #[inline_props]
  39. fn Route5(cx: Scope, query: String) -> Element {
  40. render! {
  41. div{
  42. "Route5: {query}"
  43. }
  44. }
  45. }
  46. #[inline_props]
  47. fn Route6(cx: Scope, extra: Vec<String>) -> Element {
  48. render! {
  49. div{
  50. "Route5: {extra:?}"
  51. }
  52. }
  53. }
  54. #[rustfmt::skip]
  55. #[derive(Routable, Clone, Debug, PartialEq)]
  56. enum Route {
  57. #[route("/(dynamic)" Route1)]
  58. Route1 { dynamic: String },
  59. #[nest("/hello_world")]
  60. #[route("/" Route2)]
  61. Route2 {},
  62. // #[redirect("/(dynamic)/hello_world")]
  63. #[route("/(dynamic)" Route3)]
  64. Route3 { dynamic: u32 },
  65. #[end_nest]
  66. #[route("/(number1)/(number2)" Route4)]
  67. Route4 { number1: u32, number2: u32 },
  68. #[route("/?(query)" Route5)]
  69. Route5 { query: String },
  70. #[route("/(...extra)" Route6)]
  71. Route6 { extra: Vec<String> },
  72. }
  73. #[test]
  74. fn display_works() {
  75. let route = Route::Route1 {
  76. dynamic: "hello".to_string(),
  77. };
  78. assert_eq!(route.to_string(), "/hello");
  79. let route = Route::Route3 { dynamic: 1234 };
  80. assert_eq!(route.to_string(), "/hello_world/1234");
  81. let route = Route::Route1 {
  82. dynamic: "hello_world2".to_string(),
  83. };
  84. assert_eq!(route.to_string(), "/hello_world2");
  85. let route = Route::Route4 {
  86. number1: 1234,
  87. number2: 5678,
  88. };
  89. assert_eq!(route.to_string(), "/1234/5678");
  90. let route = Route::Route5 {
  91. query: "hello".to_string(),
  92. };
  93. assert_eq!(route.to_string(), "/?hello");
  94. let route = Route::Route6 {
  95. extra: vec!["hello".to_string(), "world".to_string()],
  96. };
  97. assert_eq!(route.to_string(), "/hello/world");
  98. }
  99. #[test]
  100. fn from_string_works() {
  101. let w = "/hello";
  102. assert_eq!(
  103. Route::from_str(w),
  104. Ok(Route::Route1 {
  105. dynamic: "hello".to_string()
  106. })
  107. );
  108. let w = "/hello/";
  109. assert_eq!(
  110. Route::from_str(w),
  111. Ok(Route::Route1 {
  112. dynamic: "hello".to_string()
  113. })
  114. );
  115. let w = "/hello_world/1234";
  116. assert_eq!(Route::from_str(w), Ok(Route::Route3 { dynamic: 1234 }));
  117. let w = "/hello_world/1234/";
  118. assert_eq!(Route::from_str(w), Ok(Route::Route3 { dynamic: 1234 }));
  119. let w = "/hello_world2";
  120. assert_eq!(
  121. Route::from_str(w),
  122. Ok(Route::Route1 {
  123. dynamic: "hello_world2".to_string()
  124. })
  125. );
  126. let w = "/?x=1234&y=hello";
  127. assert_eq!(
  128. Route::from_str(w),
  129. Ok(Route::Route5 {
  130. query: "x=1234&y=hello".to_string()
  131. })
  132. );
  133. let w = "/hello_world/hello_world/hello_world";
  134. assert_eq!(
  135. Route::from_str(w),
  136. Ok(Route::Route6 {
  137. extra: vec![
  138. "hello_world".to_string(),
  139. "hello_world".to_string(),
  140. "hello_world".to_string()
  141. ]
  142. })
  143. );
  144. }
  145. #[test]
  146. fn round_trip() {
  147. // Route1
  148. let string = "hello_world2";
  149. let route = Route::Route1 {
  150. dynamic: string.to_string(),
  151. };
  152. assert_eq!(Route::from_str(&route.to_string()), Ok(route));
  153. // Route2
  154. for num in 0..100 {
  155. let route = Route::Route3 { dynamic: num };
  156. assert_eq!(Route::from_str(&route.to_string()), Ok(route));
  157. }
  158. // Route3
  159. for num1 in 0..100 {
  160. for num2 in 0..100 {
  161. let route = Route::Route4 {
  162. number1: num1,
  163. number2: num2,
  164. };
  165. assert_eq!(Route::from_str(&route.to_string()), Ok(route));
  166. }
  167. }
  168. // Route4
  169. let string = "x=1234&y=hello";
  170. let route = Route::Route5 {
  171. query: string.to_string(),
  172. };
  173. assert_eq!(Route::from_str(&route.to_string()), Ok(route));
  174. // Route5
  175. let route = Route::Route6 {
  176. extra: vec![
  177. "hello_world".to_string(),
  178. "hello_world".to_string(),
  179. "hello_world".to_string(),
  180. ],
  181. };
  182. assert_eq!(Route::from_str(&route.to_string()), Ok(route));
  183. }
  184. fn main() {}