1
0

parsing.rs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. use dioxus::prelude::*;
  2. use std::str::FromStr;
  3. #[component]
  4. fn Root() -> Element {
  5. unimplemented!()
  6. }
  7. #[component]
  8. fn Test() -> Element {
  9. unimplemented!()
  10. }
  11. #[component]
  12. fn Dynamic(id: usize) -> Element {
  13. unimplemented!()
  14. }
  15. // Make sure trailing '/'s work correctly
  16. #[test]
  17. fn trailing_slashes_parse() {
  18. #[derive(Routable, Clone, Copy, PartialEq, Debug)]
  19. enum Route {
  20. #[route("/")]
  21. Root {},
  22. #[route("/test/")]
  23. Test {},
  24. #[route("/:id/test/")]
  25. Dynamic { id: usize },
  26. }
  27. assert_eq!(Route::from_str("/").unwrap(), Route::Root {});
  28. assert_eq!(Route::from_str("/test/").unwrap(), Route::Test {});
  29. assert_eq!(Route::from_str("/test").unwrap(), Route::Test {});
  30. assert_eq!(
  31. Route::from_str("/123/test/").unwrap(),
  32. Route::Dynamic { id: 123 }
  33. );
  34. assert_eq!(
  35. Route::from_str("/123/test").unwrap(),
  36. Route::Dynamic { id: 123 }
  37. );
  38. }
  39. #[test]
  40. fn without_trailing_slashes_parse() {
  41. #[derive(Routable, Clone, Copy, PartialEq, Debug)]
  42. enum RouteWithoutTrailingSlash {
  43. #[route("/")]
  44. Root {},
  45. #[route("/test")]
  46. Test {},
  47. #[route("/:id/test")]
  48. Dynamic { id: usize },
  49. }
  50. assert_eq!(
  51. RouteWithoutTrailingSlash::from_str("/").unwrap(),
  52. RouteWithoutTrailingSlash::Root {}
  53. );
  54. assert_eq!(
  55. RouteWithoutTrailingSlash::from_str("/test/").unwrap(),
  56. RouteWithoutTrailingSlash::Test {}
  57. );
  58. assert_eq!(
  59. RouteWithoutTrailingSlash::from_str("/test").unwrap(),
  60. RouteWithoutTrailingSlash::Test {}
  61. );
  62. assert_eq!(
  63. RouteWithoutTrailingSlash::from_str("/123/test/").unwrap(),
  64. RouteWithoutTrailingSlash::Dynamic { id: 123 }
  65. );
  66. assert_eq!(
  67. RouteWithoutTrailingSlash::from_str("/123/test").unwrap(),
  68. RouteWithoutTrailingSlash::Dynamic { id: 123 }
  69. );
  70. }