rsx_diff.rs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. use dioxus_rsx::{CallBody, HotReloadingContext};
  2. use quote::quote;
  3. struct Mock;
  4. impl HotReloadingContext for Mock {
  5. fn map_attribute(
  6. element_name_rust: &str,
  7. attribute_name_rust: &str,
  8. ) -> Option<(&'static str, Option<&'static str>)> {
  9. match element_name_rust {
  10. "svg" => match attribute_name_rust {
  11. "width" => Some(("width", Some("style"))),
  12. "height" => Some(("height", Some("style"))),
  13. _ => None,
  14. },
  15. _ => None,
  16. }
  17. }
  18. fn map_element(element_name_rust: &str) -> Option<(&'static str, Option<&'static str>)> {
  19. match element_name_rust {
  20. "svg" => Some(("svg", Some("svg"))),
  21. _ => None,
  22. }
  23. }
  24. }
  25. #[test]
  26. fn create_template() {
  27. let input = quote! {
  28. svg {
  29. width: 100,
  30. height: "100px",
  31. "width2": 100,
  32. "height2": "100px",
  33. p { "hello world" }
  34. {(0..10).map(|i| rsx!{"{i}"})}
  35. }
  36. };
  37. let call_body: CallBody = syn::parse2(input).unwrap();
  38. let new_template = call_body.update_template::<Mock>(None, "testing").unwrap();
  39. insta::assert_debug_snapshot!(new_template);
  40. }
  41. #[test]
  42. fn diff_template() {
  43. #[allow(unused, non_snake_case)]
  44. fn Comp() -> dioxus_core::Element {
  45. dioxus_core::VNode::empty()
  46. }
  47. let input = quote! {
  48. svg {
  49. width: 100,
  50. height: "100px",
  51. "width2": 100,
  52. "height2": "100px",
  53. p { "hello world" }
  54. {(0..10).map(|i| rsx!{"{i}"})},
  55. {(0..10).map(|i| rsx!{"{i}"})},
  56. {(0..11).map(|i| rsx!{"{i}"})},
  57. Comp {}
  58. }
  59. };
  60. let call_body1: CallBody = syn::parse2(input).unwrap();
  61. let created_template = call_body1.update_template::<Mock>(None, "testing").unwrap();
  62. insta::assert_debug_snapshot!(created_template);
  63. // scrambling the attributes should not cause a full rebuild
  64. let input = quote! {
  65. div {
  66. "width2": 100,
  67. height: "100px",
  68. "height2": "100px",
  69. width: 100,
  70. Comp {}
  71. {(0..11).map(|i| rsx!{"{i}"})},
  72. {(0..10).map(|i| rsx!{"{i}"})},
  73. {(0..10).map(|i| rsx!{"{i}"})},
  74. p {
  75. "hello world"
  76. }
  77. }
  78. };
  79. let call_body2: CallBody = syn::parse2(input).unwrap();
  80. let new_template = call_body2
  81. .update_template::<Mock>(Some(call_body1), "testing")
  82. .unwrap();
  83. insta::assert_debug_snapshot!(new_template);
  84. }
  85. #[test]
  86. fn changing_forloops_is_okay() {
  87. let input = quote! {
  88. div {
  89. for i in 0..10 {
  90. div { "123" }
  91. "asdasd"
  92. }
  93. }
  94. };
  95. let call_body: CallBody = syn::parse2(input).unwrap();
  96. let new_template = call_body.update_template::<Mock>(None, "testing").unwrap();
  97. dbg!(new_template);
  98. }