macro.rs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. #[route("/hello_world" Route2)]
  60. Route2 {},
  61. // #[redirect("/(dynamic)/hello_world")]
  62. #[route("/hello_world/(dynamic)" Route3)]
  63. Route3 { dynamic: u32 },
  64. #[route("/(number1)/(number2)" Route4)]
  65. Route4 { number1: u32, number2: u32 },
  66. #[route("/?(query)" Route5)]
  67. Route5 { query: String },
  68. #[route("/(...extra)" Route6)]
  69. Route6 { extra: Vec<String> },
  70. }
  71. #[test]
  72. fn display_works() {
  73. let route = Route::Route1 {
  74. dynamic: "hello".to_string(),
  75. };
  76. assert_eq!(route.to_string(), "/hello");
  77. let route = Route::Route3 { dynamic: 1234 };
  78. assert_eq!(route.to_string(), "/hello_world/1234");
  79. let route = Route::Route1 {
  80. dynamic: "hello_world2".to_string(),
  81. };
  82. assert_eq!(route.to_string(), "/hello_world2");
  83. let route = Route::Route4 {
  84. number1: 1234,
  85. number2: 5678,
  86. };
  87. assert_eq!(route.to_string(), "/1234/5678");
  88. let route = Route::Route5 {
  89. query: "hello".to_string(),
  90. };
  91. assert_eq!(route.to_string(), "/?hello");
  92. let route = Route::Route6 {
  93. extra: vec!["hello".to_string(), "world".to_string()],
  94. };
  95. assert_eq!(route.to_string(), "/hello/world");
  96. }
  97. #[test]
  98. fn from_string_works() {
  99. let w = "/hello";
  100. assert_eq!(
  101. Route::from_str(w),
  102. Ok(Route::Route1 {
  103. dynamic: "hello".to_string()
  104. })
  105. );
  106. let w = "/hello/";
  107. assert_eq!(
  108. Route::from_str(w),
  109. Ok(Route::Route1 {
  110. dynamic: "hello".to_string()
  111. })
  112. );
  113. let w = "/hello_world/1234";
  114. assert_eq!(Route::from_str(w), Ok(Route::Route3 { dynamic: 1234 }));
  115. let w = "/hello_world/1234/";
  116. assert_eq!(Route::from_str(w), Ok(Route::Route3 { dynamic: 1234 }));
  117. let w = "/hello_world2";
  118. assert_eq!(
  119. Route::from_str(w),
  120. Ok(Route::Route1 {
  121. dynamic: "hello_world2".to_string()
  122. })
  123. );
  124. let w = "/?x=1234&y=hello";
  125. assert_eq!(
  126. Route::from_str(w),
  127. Ok(Route::Route5 {
  128. query: "x=1234&y=hello".to_string()
  129. })
  130. );
  131. let w = "/hello_world/hello_world/hello_world";
  132. assert_eq!(
  133. Route::from_str(w),
  134. Ok(Route::Route6 {
  135. extra: vec![
  136. "hello_world".to_string(),
  137. "hello_world".to_string(),
  138. "hello_world".to_string()
  139. ]
  140. })
  141. );
  142. }
  143. #[test]
  144. fn round_trip() {
  145. // Route1
  146. let string = "hello_world2";
  147. let route = Route::Route1 {
  148. dynamic: string.to_string(),
  149. };
  150. assert_eq!(Route::from_str(&route.to_string()), Ok(route));
  151. // Route2
  152. for num in 0..100 {
  153. let route = Route::Route3 { dynamic: num };
  154. assert_eq!(Route::from_str(&route.to_string()), Ok(route));
  155. }
  156. // Route3
  157. for num1 in 0..100 {
  158. for num2 in 0..100 {
  159. let route = Route::Route4 {
  160. number1: num1,
  161. number2: num2,
  162. };
  163. assert_eq!(Route::from_str(&route.to_string()), Ok(route));
  164. }
  165. }
  166. // Route4
  167. let string = "x=1234&y=hello";
  168. let route = Route::Route5 {
  169. query: string.to_string(),
  170. };
  171. assert_eq!(Route::from_str(&route.to_string()), Ok(route));
  172. // Route5
  173. let route = Route::Route6 {
  174. extra: vec![
  175. "hello_world".to_string(),
  176. "hello_world".to_string(),
  177. "hello_world".to_string(),
  178. ],
  179. };
  180. assert_eq!(Route::from_str(&route.to_string()), Ok(route));
  181. }
  182. fn main() {}