1
0

parsing.rs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. use dioxus::prelude::*;
  2. use std::{
  3. fmt::{self, Display},
  4. str::FromStr,
  5. };
  6. #[component]
  7. fn Root() -> Element {
  8. unimplemented!()
  9. }
  10. #[component]
  11. fn Test() -> Element {
  12. unimplemented!()
  13. }
  14. #[component]
  15. fn Dynamic(id: usize) -> Element {
  16. unimplemented!()
  17. }
  18. // Make sure trailing '/'s work correctly
  19. #[test]
  20. fn trailing_slashes_parse() {
  21. #[derive(Routable, Clone, Copy, PartialEq, Debug)]
  22. enum Route {
  23. #[route("/")]
  24. Root {},
  25. #[route("/test/")]
  26. Test {},
  27. #[route("/:id/test/")]
  28. Dynamic { id: usize },
  29. }
  30. assert_eq!(Route::from_str("/").unwrap(), Route::Root {});
  31. assert_eq!(Route::from_str("/test/").unwrap(), Route::Test {});
  32. assert_eq!(Route::from_str("/test").unwrap(), Route::Test {});
  33. assert_eq!(
  34. Route::from_str("/123/test/").unwrap(),
  35. Route::Dynamic { id: 123 }
  36. );
  37. assert_eq!(
  38. Route::from_str("/123/test").unwrap(),
  39. Route::Dynamic { id: 123 }
  40. );
  41. }
  42. #[test]
  43. fn without_trailing_slashes_parse() {
  44. #[derive(Routable, Clone, Copy, PartialEq, Debug)]
  45. enum RouteWithoutTrailingSlash {
  46. #[route("/")]
  47. Root {},
  48. #[route("/test")]
  49. Test {},
  50. #[route("/:id/test")]
  51. Dynamic { id: usize },
  52. }
  53. assert_eq!(
  54. RouteWithoutTrailingSlash::from_str("/").unwrap(),
  55. RouteWithoutTrailingSlash::Root {}
  56. );
  57. assert_eq!(
  58. RouteWithoutTrailingSlash::from_str("/test/").unwrap(),
  59. RouteWithoutTrailingSlash::Test {}
  60. );
  61. assert_eq!(
  62. RouteWithoutTrailingSlash::from_str("/test").unwrap(),
  63. RouteWithoutTrailingSlash::Test {}
  64. );
  65. assert_eq!(
  66. RouteWithoutTrailingSlash::from_str("/123/test/").unwrap(),
  67. RouteWithoutTrailingSlash::Dynamic { id: 123 }
  68. );
  69. assert_eq!(
  70. RouteWithoutTrailingSlash::from_str("/123/test").unwrap(),
  71. RouteWithoutTrailingSlash::Dynamic { id: 123 }
  72. );
  73. }
  74. // Regression test for https://github.com/DioxusLabs/dioxus/issues/2984
  75. #[test]
  76. fn query_segments_parse() {
  77. #[derive(Debug, Clone, PartialEq)]
  78. enum Query {
  79. Id(u64),
  80. }
  81. impl From<&str> for Query {
  82. fn from(_: &str) -> Self {
  83. // e.g. split query on `&` and split pairs on `=`
  84. Query::Id(10)
  85. }
  86. }
  87. impl Display for Query {
  88. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  89. write!(f, "id=10")
  90. }
  91. }
  92. #[component]
  93. fn Index(query: Query) -> Element {
  94. rsx! {
  95. h1 { "Index" }
  96. }
  97. }
  98. #[derive(Debug, Clone, PartialEq, Routable)]
  99. enum Route {
  100. #[route("/?:..query")]
  101. Index { query: Query },
  102. }
  103. let route = Route::Index {
  104. query: Query::Id(10),
  105. };
  106. assert_eq!(route.to_string(), "/?id=10");
  107. let parsed_route = "/?id=10".parse::<Route>().unwrap();
  108. assert_eq!(parsed_route, route);
  109. }
  110. #[test]
  111. fn optional_query_segments_parse() {
  112. #[derive(Debug, Clone, PartialEq, Routable)]
  113. enum Route {
  114. #[route("/?:query&:other")]
  115. Index { query: Option<u64>, other: u64 },
  116. }
  117. #[component]
  118. fn Index(query: Option<u64>, other: u64) -> Element {
  119. rsx! {
  120. h1 { "Index" }
  121. }
  122. }
  123. let route = Route::Index {
  124. query: Some(10),
  125. other: 20,
  126. };
  127. assert_eq!(route.to_string(), "/?query=10&other=20");
  128. let parsed_route = "/?query=10&other=20".parse::<Route>().unwrap();
  129. assert_eq!(parsed_route, route);
  130. let route_without_query = Route::Index {
  131. query: None,
  132. other: 20,
  133. };
  134. assert_eq!(route_without_query.to_string(), "/?other=20");
  135. let parsed_route_without_query = "/?other=20".parse::<Route>().unwrap();
  136. assert_eq!(parsed_route_without_query, route_without_query);
  137. let route_without_query_and_other = Route::Index {
  138. query: None,
  139. other: 0,
  140. };
  141. assert_eq!(route_without_query_and_other.to_string(), "/?other=0");
  142. let parsed_route_without_query_and_other = "/".parse::<Route>().unwrap();
  143. assert_eq!(
  144. parsed_route_without_query_and_other,
  145. route_without_query_and_other
  146. );
  147. }