macro.rs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. use dioxus::prelude::*;
  2. use dioxus_router_macro::*;
  3. use dioxus_router_core::*;
  4. use std::str::FromStr;
  5. #[inline_props]
  6. fn Route1(cx: Scope, dynamic: String) -> Element {
  7. render! {
  8. div{
  9. "Route1: {dynamic}"
  10. }
  11. }
  12. }
  13. #[inline_props]
  14. fn Route2(cx: Scope) -> Element {
  15. render! {
  16. div{
  17. "Route2"
  18. }
  19. }
  20. }
  21. #[inline_props]
  22. fn Route3(cx: Scope, dynamic: u32) -> Element {
  23. render! {
  24. div{
  25. "Route3: {dynamic}"
  26. }
  27. }
  28. }
  29. #[inline_props]
  30. fn Route4(cx: Scope, number1: u32, number2: u32) -> Element {
  31. render! {
  32. div{
  33. "Route4: {number1} {number2}"
  34. }
  35. }
  36. }
  37. #[inline_props]
  38. fn Route5(cx: Scope, query: String) -> Element {
  39. render! {
  40. div{
  41. "Route5"
  42. }
  43. }
  44. }
  45. #[derive(Routable, Clone, Debug, PartialEq)]
  46. enum Route {
  47. #[route("/(dynamic)" Route1)]
  48. Route1 { dynamic: String },
  49. #[route("/hello_world" Route2)]
  50. Route2 {},
  51. // #[redirect("/(dynamic)/hello_world")]
  52. #[route("/hello_world/(dynamic)" Route3)]
  53. Route3 { dynamic: u32 },
  54. #[route("/(number1)/(number2)" Route4)]
  55. Route4 { number1: u32, number2: u32 },
  56. #[route("/?(query)" Route5)]
  57. Route5 {
  58. query: String,
  59. },
  60. }
  61. #[test]
  62. fn display_works() {
  63. let route = Route::Route1 {
  64. dynamic: "hello".to_string(),
  65. };
  66. assert_eq!(route.to_string(), "/hello");
  67. let route = Route::Route3 { dynamic: 1234 };
  68. assert_eq!(route.to_string(), "/hello_world/1234");
  69. let route = Route::Route1 {
  70. dynamic: "hello_world2".to_string(),
  71. };
  72. assert_eq!(route.to_string(), "/hello_world2");
  73. }
  74. #[test]
  75. fn from_string_works() {
  76. let w = "/hello";
  77. assert_eq!(
  78. Route::from_str(w),
  79. Ok(Route::Route1 {
  80. dynamic: "hello".to_string()
  81. })
  82. );
  83. let w = "/hello/";
  84. assert_eq!(
  85. Route::from_str(w),
  86. Ok(Route::Route1 {
  87. dynamic: "hello".to_string()
  88. })
  89. );
  90. let w = "/hello_world/1234";
  91. assert_eq!(Route::from_str(w), Ok(Route::Route3 { dynamic: 1234 }));
  92. let w = "/hello_world/1234/";
  93. assert_eq!(Route::from_str(w), Ok(Route::Route3 { dynamic: 1234 }));
  94. let w = "/hello_world2";
  95. assert_eq!(
  96. Route::from_str(w),
  97. Ok(Route::Route1 {
  98. dynamic: "hello_world2".to_string()
  99. })
  100. );
  101. let w = "/hello_world/-1";
  102. match Route::from_str(w) {
  103. Ok(r) => panic!("should not parse {r:?}"),
  104. Err(err) => println!("{err}"),
  105. }
  106. let w = "/?x=1234&y=hello";
  107. assert_eq!(
  108. Route::from_str(w),
  109. Ok(Route::Route5 {
  110. query: "x=1234&y=hello".to_string()
  111. })
  112. );
  113. }
  114. #[test]
  115. fn round_trip() {
  116. // Route1
  117. let string = "hello_world2";
  118. let route = Route::Route1 {
  119. dynamic: string.to_string(),
  120. };
  121. assert_eq!(Route::from_str(&route.to_string()), Ok(route));
  122. // Route2
  123. for num in 0..100 {
  124. let route = Route::Route3 { dynamic: num };
  125. assert_eq!(Route::from_str(&route.to_string()), Ok(route));
  126. }
  127. // Route3
  128. for num1 in 0..100 {
  129. for num2 in 0..100 {
  130. let route = Route::Route4 {
  131. number1: num1,
  132. number2: num2,
  133. };
  134. assert_eq!(Route::from_str(&route.to_string()), Ok(route));
  135. }
  136. }
  137. // Route4
  138. let string = "x=1234&y=hello";
  139. let route = Route::Route5 {
  140. query: string.to_string(),
  141. };
  142. assert_eq!(Route::from_str(&route.to_string()), Ok(route));
  143. }